112 lines
2.2 KiB
JavaScript
112 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,
|
|
},
|
|
|
|
account_name: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
account_currency: {
|
|
type: DataTypes.ENUM,
|
|
|
|
values: ['H£', 'R$'],
|
|
},
|
|
|
|
account_type: {
|
|
type: DataTypes.ENUM,
|
|
|
|
values: ['Checking', 'Savings'],
|
|
},
|
|
|
|
ownership_type: {
|
|
type: DataTypes.ENUM,
|
|
|
|
values: ['Personal', 'Corporate'],
|
|
},
|
|
|
|
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.invoices, {
|
|
as: 'invoices_sender_account',
|
|
foreignKey: {
|
|
name: 'sender_accountId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.accounts.hasMany(db.invoices, {
|
|
as: 'invoices_client_account',
|
|
foreignKey: {
|
|
name: 'client_accountId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.accounts.hasMany(db.transactions, {
|
|
as: 'transactions_account',
|
|
foreignKey: {
|
|
name: 'accountId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
//end loop
|
|
|
|
db.accounts.belongsTo(db.users, {
|
|
as: 'owner',
|
|
foreignKey: {
|
|
name: 'ownerId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.accounts.belongsTo(db.organizations, {
|
|
as: 'organizations',
|
|
foreignKey: {
|
|
name: 'organizationsId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.accounts.belongsTo(db.users, {
|
|
as: 'createdBy',
|
|
});
|
|
|
|
db.accounts.belongsTo(db.users, {
|
|
as: 'updatedBy',
|
|
});
|
|
};
|
|
|
|
return accounts;
|
|
};
|