2026-02-22 10:13:56 +00:00

177 lines
2.3 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 budgets = sequelize.define(
'budgets',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
period: {
type: DataTypes.ENUM,
values: [
"monthly",
"quarterly",
"yearly"
],
},
valid_from: {
type: DataTypes.DATE,
},
valid_to: {
type: DataTypes.DATE,
},
amount_limit: {
type: DataTypes.DECIMAL,
},
currency: {
type: DataTypes.TEXT,
},
alert_threshold_percent: {
type: DataTypes.DECIMAL,
},
active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
budgets.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.budgets.hasMany(db.departments, {
as: 'departments_budget',
foreignKey: {
name: 'budgetId',
},
constraints: false,
});
db.budgets.hasMany(db.expense_reports, {
as: 'expense_reports_budget',
foreignKey: {
name: 'budgetId',
},
constraints: false,
});
db.budgets.hasMany(db.budget_checks, {
as: 'budget_checks_budget',
foreignKey: {
name: 'budgetId',
},
constraints: false,
});
db.budgets.hasMany(db.alerts, {
as: 'alerts_budget',
foreignKey: {
name: 'budgetId',
},
constraints: false,
});
//end loop
db.budgets.belongsTo(db.users, {
as: 'createdBy',
});
db.budgets.belongsTo(db.users, {
as: 'updatedBy',
});
};
return budgets;
};