33862/backend/src/db/models/inspections.js
2025-09-03 16:44:01 +00:00

100 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 inspections = sequelize.define(
'inspections',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
type: {
type: DataTypes.ENUM,
values: ['move_in', 'move_out', 'periodic'],
},
date: {
type: DataTypes.DATE,
},
checklist: {
type: DataTypes.TEXT,
},
notes: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
inspections.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.inspections.belongsTo(db.units, {
as: 'unit',
foreignKey: {
name: 'unitId',
},
constraints: false,
});
db.inspections.belongsTo(db.company, {
as: 'company',
foreignKey: {
name: 'companyId',
},
constraints: false,
});
db.inspections.hasMany(db.file, {
as: 'photos_before',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.inspections.getTableName(),
belongsToColumn: 'photos_before',
},
});
db.inspections.hasMany(db.file, {
as: 'photos_after',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.inspections.getTableName(),
belongsToColumn: 'photos_after',
},
});
db.inspections.belongsTo(db.users, {
as: 'createdBy',
});
db.inspections.belongsTo(db.users, {
as: 'updatedBy',
});
};
return inspections;
};