38910-vm/backend/src/db/models/money_transactions.js
2026-03-01 11:20:04 +00:00

176 lines
2.4 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 money_transactions = sequelize.define(
'money_transactions',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
transaction_at: {
type: DataTypes.DATE,
},
direction: {
type: DataTypes.ENUM,
values: [
"expense",
"income"
],
},
amount: {
type: DataTypes.DECIMAL,
},
merchant: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
is_recurring: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
location: {
type: DataTypes.TEXT,
},
payment_method: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
money_transactions.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.money_transactions.belongsTo(db.users, {
as: 'user',
foreignKey: {
name: 'userId',
},
constraints: false,
});
db.money_transactions.belongsTo(db.money_accounts, {
as: 'account',
foreignKey: {
name: 'accountId',
},
constraints: false,
});
db.money_transactions.belongsTo(db.expense_categories, {
as: 'category',
foreignKey: {
name: 'categoryId',
},
constraints: false,
});
db.money_transactions.hasMany(db.file, {
as: 'receipts',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.money_transactions.getTableName(),
belongsToColumn: 'receipts',
},
});
db.money_transactions.belongsTo(db.users, {
as: 'createdBy',
});
db.money_transactions.belongsTo(db.users, {
as: 'updatedBy',
});
};
return money_transactions;
};