30653/backend/src/db/models/estimates.js
2025-04-11 17:39:25 +00:00

120 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 estimates = sequelize.define(
'estimates',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
estimates.associate = (db) => {
db.estimates.belongsToMany(db.materials, {
as: 'materials',
foreignKey: {
name: 'estimates_materialsId',
},
constraints: false,
through: 'estimatesMaterialsMaterials',
});
db.estimates.belongsToMany(db.materials, {
as: 'materials_filter',
foreignKey: {
name: 'estimates_materialsId',
},
constraints: false,
through: 'estimatesMaterialsMaterials',
});
db.estimates.belongsToMany(db.labour, {
as: 'labour',
foreignKey: {
name: 'estimates_labourId',
},
constraints: false,
through: 'estimatesLabourLabour',
});
db.estimates.belongsToMany(db.labour, {
as: 'labour_filter',
foreignKey: {
name: 'estimates_labourId',
},
constraints: false,
through: 'estimatesLabourLabour',
});
db.estimates.belongsToMany(db.plants, {
as: 'plants',
foreignKey: {
name: 'estimates_plantsId',
},
constraints: false,
through: 'estimatesPlantsPlants',
});
db.estimates.belongsToMany(db.plants, {
as: 'plants_filter',
foreignKey: {
name: 'estimates_plantsId',
},
constraints: false,
through: 'estimatesPlantsPlants',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.estimates.belongsTo(db.companies, {
as: 'company',
foreignKey: {
name: 'companyId',
},
constraints: false,
});
db.estimates.belongsTo(db.companies, {
as: 'companies',
foreignKey: {
name: 'companiesId',
},
constraints: false,
});
db.estimates.belongsTo(db.users, {
as: 'createdBy',
});
db.estimates.belongsTo(db.users, {
as: 'updatedBy',
});
};
return estimates;
};