38492-vm/backend/src/db/models/customers.js
2026-02-16 17:22:10 +00:00

174 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 customers = sequelize.define(
'customers',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
customer_code: {
type: DataTypes.TEXT,
},
company_name: {
type: DataTypes.TEXT,
},
registration_number: {
type: DataTypes.TEXT,
},
industry: {
type: DataTypes.TEXT,
},
country: {
type: DataTypes.TEXT,
},
account_manager: {
type: DataTypes.TEXT,
},
onboarded_at: {
type: DataTypes.DATE,
},
status: {
type: DataTypes.ENUM,
values: [
"prospect",
"active",
"inactive",
"blacklisted"
],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
customers.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.customers.hasMany(db.ctos_reports, {
as: 'ctos_reports_customer',
foreignKey: {
name: 'customerId',
},
constraints: false,
});
db.customers.hasMany(db.credit_decisions, {
as: 'credit_decisions_customer',
foreignKey: {
name: 'customerId',
},
constraints: false,
});
db.customers.hasMany(db.audit_events, {
as: 'audit_events_customer',
foreignKey: {
name: 'customerId',
},
constraints: false,
});
db.customers.hasMany(db.repayment_outcomes, {
as: 'repayment_outcomes_customer',
foreignKey: {
name: 'customerId',
},
constraints: false,
});
//end loop
db.customers.belongsTo(db.users, {
as: 'createdBy',
});
db.customers.belongsTo(db.users, {
as: 'updatedBy',
});
};
return customers;
};