33874/backend/src/db/models/tenants.js
2025-09-04 06:10:27 +00:00

114 lines
2.3 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 tenants = sequelize.define(
'tenants',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
address: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
tenants.associate = (db) => {
db.tenants.belongsToMany(db.cameras, {
as: 'cameras',
foreignKey: {
name: 'tenants_camerasId',
},
constraints: false,
through: 'tenantsCamerasCameras',
});
db.tenants.belongsToMany(db.cameras, {
as: 'cameras_filter',
foreignKey: {
name: 'tenants_camerasId',
},
constraints: false,
through: 'tenantsCamerasCameras',
});
db.tenants.belongsToMany(db.vehicles, {
as: 'vehicles',
foreignKey: {
name: 'tenants_vehiclesId',
},
constraints: false,
through: 'tenantsVehiclesVehicles',
});
db.tenants.belongsToMany(db.vehicles, {
as: 'vehicles_filter',
foreignKey: {
name: 'tenants_vehiclesId',
},
constraints: false,
through: 'tenantsVehiclesVehicles',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.tenants.hasMany(db.cameras, {
as: 'cameras_tenant',
foreignKey: {
name: 'tenantId',
},
constraints: false,
});
db.tenants.hasMany(db.vehicles, {
as: 'vehicles_tenant',
foreignKey: {
name: 'tenantId',
},
constraints: false,
});
//end loop
db.tenants.belongsTo(db.apartments, {
as: 'apartments',
foreignKey: {
name: 'apartmentsId',
},
constraints: false,
});
db.tenants.belongsTo(db.users, {
as: 'createdBy',
});
db.tenants.belongsTo(db.users, {
as: 'updatedBy',
});
};
return tenants;
};