39883-vm/backend/src/db/models/attachments.js
2026-05-04 09:38:49 +00:00

132 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 attachments = sequelize.define(
'attachments',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
context_type: {
type: DataTypes.ENUM,
values: [
"loan_request",
"loan",
"penalty",
"item"
],
},
context_key: {
type: DataTypes.TEXT,
},
description: {
type: DataTypes.TEXT,
},
uploaded_at: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
attachments.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.attachments.belongsTo(db.users, {
as: 'uploaded_by',
foreignKey: {
name: 'uploaded_byId',
},
constraints: false,
});
db.attachments.hasMany(db.file, {
as: 'documents',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.attachments.getTableName(),
belongsToColumn: 'documents',
},
});
db.attachments.belongsTo(db.users, {
as: 'createdBy',
});
db.attachments.belongsTo(db.users, {
as: 'updatedBy',
});
};
return attachments;
};