38667-vm/backend/src/db/models/products.js
2026-02-21 13:56:02 +00:00

198 lines
2.8 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,
},
tiktok_product_key: {
type: DataTypes.TEXT,
},
title: {
type: DataTypes.TEXT,
},
price: {
type: DataTypes.DECIMAL,
},
currency: {
type: DataTypes.TEXT,
},
shop_name: {
type: DataTypes.TEXT,
},
shop_key: {
type: DataTypes.TEXT,
},
category: {
type: DataTypes.TEXT,
},
primary_image_url: {
type: DataTypes.TEXT,
},
product_url: {
type: DataTypes.TEXT,
},
is_active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
first_seen_at: {
type: DataTypes.DATE,
},
last_seen_at: {
type: DataTypes.DATE,
},
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_metrics, {
as: 'product_metrics_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.viral_videos, {
as: 'viral_videos_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.supplier_matches, {
as: 'supplier_matches_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.user_tracked_products, {
as: 'user_tracked_products_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
//end loop
db.products.hasMany(db.file, {
as: 'product_images',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.products.getTableName(),
belongsToColumn: 'product_images',
},
});
db.products.belongsTo(db.users, {
as: 'createdBy',
});
db.products.belongsTo(db.users, {
as: 'updatedBy',
});
};
return products;
};