33142/backend/src/db/models/reports.js
2025-07-31 15:16:26 +00:00

102 lines
2.0 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 reports = sequelize.define(
'reports',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.TEXT,
},
created_date: {
type: DataTypes.DATE,
},
views: {
type: DataTypes.INTEGER,
},
address: {
type: DataTypes.TEXT,
},
phone: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
reports.associate = (db) => {
db.reports.belongsToMany(db.annotations, {
as: 'Annotation',
foreignKey: {
name: 'reports_AnnotationId',
},
constraints: false,
through: 'reportsAnnotationAnnotations',
});
db.reports.belongsToMany(db.annotations, {
as: 'Annotation_filter',
foreignKey: {
name: 'reports_AnnotationId',
},
constraints: false,
through: 'reportsAnnotationAnnotations',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.reports.belongsTo(db.clients, {
as: 'client',
foreignKey: {
name: 'clientId',
},
constraints: false,
});
db.reports.hasMany(db.file, {
as: 'image',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.reports.getTableName(),
belongsToColumn: 'image',
},
});
db.reports.belongsTo(db.users, {
as: 'createdBy',
});
db.reports.belongsTo(db.users, {
as: 'updatedBy',
});
};
return reports;
};