30464/backend/src/db/models/orders.js
2025-04-04 00:43:08 +00:00

106 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 orders = sequelize.define(
'orders',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
material: {
type: DataTypes.ENUM,
values: ['plastic', 'metal', 'resin'],
},
quantity: {
type: DataTypes.INTEGER,
},
price: {
type: DataTypes.DECIMAL,
},
status: {
type: DataTypes.ENUM,
values: ['pending', 'in_progress', 'completed'],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
orders.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.orders.hasMany(db.invoices, {
as: 'invoices_order',
foreignKey: {
name: 'orderId',
},
constraints: false,
});
db.orders.hasMany(db.production_schedules, {
as: 'production_schedules_order',
foreignKey: {
name: 'orderId',
},
constraints: false,
});
db.orders.hasMany(db.shipments, {
as: 'shipments_order',
foreignKey: {
name: 'orderId',
},
constraints: false,
});
//end loop
db.orders.belongsTo(db.molds, {
as: 'mold',
foreignKey: {
name: 'moldId',
},
constraints: false,
});
db.orders.belongsTo(db.users, {
as: 'customer',
foreignKey: {
name: 'customerId',
},
constraints: false,
});
db.orders.belongsTo(db.users, {
as: 'createdBy',
});
db.orders.belongsTo(db.users, {
as: 'updatedBy',
});
};
return orders;
};