31023/backend/src/db/models/tenants.js
2025-04-26 22:20:13 +00:00

106 lines
2.1 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,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
tenants.associate = (db) => {
/// 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.users, {
as: 'users_tenants',
foreignKey: {
name: 'tenantsId',
},
constraints: false,
});
db.tenants.hasMany(db.events, {
as: 'events_tenant',
foreignKey: {
name: 'tenantId',
},
constraints: false,
});
db.tenants.hasMany(db.events, {
as: 'events_tenants',
foreignKey: {
name: 'tenantsId',
},
constraints: false,
});
db.tenants.hasMany(db.financial_reports, {
as: 'financial_reports_tenant',
foreignKey: {
name: 'tenantId',
},
constraints: false,
});
db.tenants.hasMany(db.financial_reports, {
as: 'financial_reports_tenants',
foreignKey: {
name: 'tenantsId',
},
constraints: false,
});
db.tenants.hasMany(db.messages, {
as: 'messages_tenant',
foreignKey: {
name: 'tenantId',
},
constraints: false,
});
db.tenants.hasMany(db.messages, {
as: 'messages_tenants',
foreignKey: {
name: 'tenantsId',
},
constraints: false,
});
//end loop
db.tenants.belongsTo(db.users, {
as: 'createdBy',
});
db.tenants.belongsTo(db.users, {
as: 'updatedBy',
});
};
return tenants;
};