39549-vm/backend/src/db/models/sales_targets.js
2026-04-11 11:10:37 +00:00

191 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 sales_targets = sequelize.define(
'sales_targets',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
period: {
type: DataTypes.ENUM,
values: [
"MONTH",
"QUARTER",
"YEAR"
],
},
start_date: {
type: DataTypes.DATE,
},
end_date: {
type: DataTypes.DATE,
},
target_amount: {
type: DataTypes.DECIMAL,
},
achieved_amount: {
type: DataTypes.DECIMAL,
},
target_deals: {
type: DataTypes.DECIMAL,
},
achieved_deals: {
type: DataTypes.DECIMAL,
},
status: {
type: DataTypes.ENUM,
values: [
"PLANNED",
"ACTIVE",
"CLOSED"
],
},
notes: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
sales_targets.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.sales_targets.belongsTo(db.companies, {
as: 'company',
foreignKey: {
name: 'companyId',
},
constraints: false,
});
db.sales_targets.belongsTo(db.departments, {
as: 'department',
foreignKey: {
name: 'departmentId',
},
constraints: false,
});
db.sales_targets.belongsTo(db.employees, {
as: 'owner_employee',
foreignKey: {
name: 'owner_employeeId',
},
constraints: false,
});
db.sales_targets.belongsTo(db.users, {
as: 'createdBy',
});
db.sales_targets.belongsTo(db.users, {
as: 'updatedBy',
});
};
return sales_targets;
};