30945/backend/src/db/models/accounts.js
2025-04-24 19:39:21 +00:00

88 lines
1.7 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,
},
account_name: {
type: DataTypes.TEXT,
},
bank_institution: {
type: DataTypes.TEXT,
},
account_type: {
type: DataTypes.ENUM,
values: ['Checking', 'Savings', 'DigitalWallet'],
},
initial_balance: {
type: DataTypes.DECIMAL,
},
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.goals, {
as: 'goals_associated_account',
foreignKey: {
name: 'associated_accountId',
},
constraints: false,
});
db.accounts.hasMany(db.transactions, {
as: 'transactions_account',
foreignKey: {
name: 'accountId',
},
constraints: false,
});
//end loop
db.accounts.belongsTo(db.users, {
as: 'user',
foreignKey: {
name: 'userId',
},
constraints: false,
});
db.accounts.belongsTo(db.users, {
as: 'createdBy',
});
db.accounts.belongsTo(db.users, {
as: 'updatedBy',
});
};
return accounts;
};