32768/backend/src/db/models/raw_materials.js
2025-07-12 09:30:47 +00:00

92 lines
2.0 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 raw_materials = sequelize.define(
'raw_materials',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
quantity: {
type: DataTypes.DECIMAL,
},
reorder_level: {
type: DataTypes.DECIMAL,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
raw_materials.associate = (db) => {
db.raw_materials.belongsToMany(db.suppliers, {
as: 'suppliers',
foreignKey: {
name: 'raw_materials_suppliersId',
},
constraints: false,
through: 'raw_materialsSuppliersSuppliers',
});
db.raw_materials.belongsToMany(db.suppliers, {
as: 'suppliers_filter',
foreignKey: {
name: 'raw_materials_suppliersId',
},
constraints: false,
through: 'raw_materialsSuppliersSuppliers',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.raw_materials.hasMany(db.work_orders, {
as: 'work_orders_raw_material',
foreignKey: {
name: 'raw_materialId',
},
constraints: false,
});
//end loop
db.raw_materials.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.raw_materials.belongsTo(db.users, {
as: 'createdBy',
});
db.raw_materials.belongsTo(db.users, {
as: 'updatedBy',
});
};
return raw_materials;
};