39850-vm/backend/src/db/models/documents.js
2026-05-01 06:47:18 +00:00

193 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 documents = sequelize.define(
'documents',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
document_type: {
type: DataTypes.ENUM,
values: [
"business_plan",
"pitch_deck",
"budget_model",
"operating_agreement",
"bylaws",
"lease",
"purchase_agreement",
"permit_application",
"policy",
"training_material",
"marketing_asset",
"other"
],
},
document_title: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
"draft",
"in_review",
"approved",
"superseded"
],
},
content: {
type: DataTypes.TEXT,
},
published_at: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
documents.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.documents.belongsTo(db.projects, {
as: 'project',
foreignKey: {
name: 'projectId',
},
constraints: false,
});
db.documents.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.documents.hasMany(db.file, {
as: 'file_assets',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.documents.getTableName(),
belongsToColumn: 'file_assets',
},
});
db.documents.belongsTo(db.users, {
as: 'createdBy',
});
db.documents.belongsTo(db.users, {
as: 'updatedBy',
});
};
return documents;
};