39895-vm/backend/src/db/models/accounts.js
2026-05-04 20:37:17 +00:00

183 lines
2.2 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 accounts = sequelize.define(
'accounts',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
account_type: {
type: DataTypes.ENUM,
values: [
"bank",
"cash"
],
},
currency: {
type: DataTypes.ENUM,
values: [
"VES",
"USD"
],
},
institution: {
type: DataTypes.TEXT,
},
account_reference: {
type: DataTypes.TEXT,
},
opening_balance: {
type: DataTypes.DECIMAL,
},
opening_date: {
type: DataTypes.DATE,
},
is_active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
notes: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
accounts.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.accounts.hasMany(db.payments, {
as: 'payments_account',
foreignKey: {
name: 'accountId',
},
constraints: false,
});
db.accounts.hasMany(db.expenses, {
as: 'expenses_account',
foreignKey: {
name: 'accountId',
},
constraints: false,
});
//end loop
db.accounts.belongsTo(db.buildings, {
as: 'building',
foreignKey: {
name: 'buildingId',
},
constraints: false,
});
db.accounts.belongsTo(db.users, {
as: 'createdBy',
});
db.accounts.belongsTo(db.users, {
as: 'updatedBy',
});
};
return accounts;
};