38326-vm/backend/src/db/models/products.js
2026-02-09 17:42:55 +00:00

204 lines
2.6 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,
},
sku: {
type: DataTypes.TEXT,
},
name: {
type: DataTypes.TEXT,
},
product_type: {
type: DataTypes.ENUM,
values: [
"material",
"service",
"equipment"
],
},
unit: {
type: DataTypes.TEXT,
},
unit_price: {
type: DataTypes.DECIMAL,
},
tax_rate: {
type: DataTypes.DECIMAL,
},
supplier_name: {
type: DataTypes.TEXT,
},
supplier_product_code: {
type: DataTypes.TEXT,
},
active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
description: {
type: DataTypes.TEXT,
},
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.quote_items, {
as: 'quote_items_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.material_list_items, {
as: 'material_list_items_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
db.products.hasMany(db.purchase_order_items, {
as: 'purchase_order_items_product',
foreignKey: {
name: 'productId',
},
constraints: false,
});
//end loop
db.products.belongsTo(db.companies, {
as: 'company',
foreignKey: {
name: 'companyId',
},
constraints: false,
});
db.products.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.products.belongsTo(db.users, {
as: 'createdBy',
});
db.products.belongsTo(db.users, {
as: 'updatedBy',
});
};
return products;
};