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 organizations = sequelize.define( 'organizations', { 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, }, ); organizations.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.organizations.hasMany(db.users, { as: 'users_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.hotels, { as: 'hotels_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.departments, { as: 'departments_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.staff_profiles, { as: 'staff_profiles_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.shifts, { as: 'shifts_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.room_types, { as: 'room_types_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.rooms, { as: 'rooms_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.guests, { as: 'guests_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.guest_documents, { as: 'guest_documents_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.rate_plans, { as: 'rate_plans_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.seasonal_rates, { as: 'seasonal_rates_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.dynamic_pricing_rules, { as: 'dynamic_pricing_rules_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.sales_channels, { as: 'sales_channels_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.ota_connections, { as: 'ota_connections_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.reservations, { as: 'reservations_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.reservation_guests, { as: 'reservation_guests_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.stay_folios, { as: 'stay_folios_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.folio_items, { as: 'folio_items_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.payments, { as: 'payments_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.refunds, { as: 'refunds_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.invoices, { as: 'invoices_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.housekeeping_tasks, { as: 'housekeeping_tasks_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.maintenance_tickets, { as: 'maintenance_tickets_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.notifications, { as: 'notifications_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.booking_widgets, { as: 'booking_widgets_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.subscriptions, { as: 'subscriptions_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.audit_logs, { as: 'audit_logs_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.organizations.hasMany(db.daily_snapshots, { as: 'daily_snapshots_organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); //end loop db.organizations.belongsTo(db.users, { as: 'createdBy', }); db.organizations.belongsTo(db.users, { as: 'updatedBy', }); }; return organizations; };