32242/backend/src/db/models/quotes.js
2025-06-14 08:00:21 +00:00

84 lines
1.7 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 quotes = sequelize.define(
'quotes',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
total_price: {
type: DataTypes.DECIMAL,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
quotes.associate = (db) => {
db.quotes.belongsToMany(db.billboards, {
as: 'billboards',
foreignKey: {
name: 'quotes_billboardsId',
},
constraints: false,
through: 'quotesBillboardsBillboards',
});
db.quotes.belongsToMany(db.billboards, {
as: 'billboards_filter',
foreignKey: {
name: 'quotes_billboardsId',
},
constraints: false,
through: 'quotesBillboardsBillboards',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.quotes.hasMany(db.contracts, {
as: 'contracts_quote',
foreignKey: {
name: 'quoteId',
},
constraints: false,
});
//end loop
db.quotes.belongsTo(db.clients, {
as: 'client',
foreignKey: {
name: 'clientId',
},
constraints: false,
});
db.quotes.belongsTo(db.users, {
as: 'createdBy',
});
db.quotes.belongsTo(db.users, {
as: 'updatedBy',
});
};
return quotes;
};