39091-vm/backend/src/db/models/products.js
2026-03-10 14:20:13 +00:00

341 lines
4.2 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const products = sequelize.define(
'products',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
slug: {
type: DataTypes.TEXT,
},
short_description: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
"active",
"inactive"
],
},
visibility: {
type: DataTypes.ENUM,
values: [
"public",
"hidden"
],
},
target_audience: {
type: DataTypes.TEXT,
},
featured: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
free_shipping: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
price: {
type: DataTypes.DECIMAL,
},
promo_price: {
type: DataTypes.DECIMAL,
},
cost: {
type: DataTypes.DECIMAL,
},
sku: {
type: DataTypes.TEXT,
},
barcode: {
type: DataTypes.TEXT,
},
weight: {
type: DataTypes.DECIMAL,
},
height: {
type: DataTypes.DECIMAL,
},
width: {
type: DataTypes.DECIMAL,
},
length: {
type: DataTypes.DECIMAL,
},
views_count: {
type: DataTypes.INTEGER,
},
sales_count: {
type: DataTypes.INTEGER,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
products.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.products.hasMany(db.product_images, {
as: 'product_images_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.product_videos, {
as: 'product_videos_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.product_variations, {
as: 'product_variations_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.inventory, {
as: 'inventory_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.inventory_movements, {
as: 'inventory_movements_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.order_items, {
as: 'order_items_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.cart_items, {
as: 'cart_items_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.analytics_events, {
as: 'analytics_events_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
//end loop
db.products.belongsTo(db.categories, {
as: 'category',
foreignKey: {
name: 'categoryId',
},
constraints: false,
});
db.products.belongsTo(db.brands, {
as: 'brand',
foreignKey: {
name: 'brandId',
},
constraints: false,
});
db.products.belongsTo(db.users, {
as: 'createdBy',
});
db.products.belongsTo(db.users, {
as: 'updatedBy',
});
};
return products;
};