39472-vm/backend/src/db/models/agreements.js
2026-04-04 15:26:36 +00:00

194 lines
2.4 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 agreements = sequelize.define(
'agreements',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
agreement_reference: {
type: DataTypes.TEXT,
},
title: {
type: DataTypes.TEXT,
},
period_type: {
type: DataTypes.ENUM,
values: [
"quarterly",
"half_year",
"yearly"
],
},
period_start: {
type: DataTypes.DATE,
},
period_end: {
type: DataTypes.DATE,
},
status: {
type: DataTypes.ENUM,
values: [
"draft",
"submitted",
"active",
"expired",
"cancelled"
],
},
submitted_at: {
type: DataTypes.DATE,
},
approved_at: {
type: DataTypes.DATE,
},
external_portal_reference: {
type: DataTypes.TEXT,
},
notes: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
agreements.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.agreements.hasMany(db.agreement_vehicles, {
as: 'agreement_vehicles_agreement',
foreignKey: {
name: 'agreementId',
},
constraints: false,
});
//end loop
db.agreements.belongsTo(db.companies, {
as: 'company',
foreignKey: {
name: 'companyId',
},
constraints: false,
});
db.agreements.hasMany(db.file, {
as: 'documents',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.agreements.getTableName(),
belongsToColumn: 'documents',
},
});
db.agreements.belongsTo(db.users, {
as: 'createdBy',
});
db.agreements.belongsTo(db.users, {
as: 'updatedBy',
});
};
return agreements;
};