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 organization = sequelize.define( 'organization', { 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, }, ); organization.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.organization.hasMany(db.users, { as: 'users_organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.organization.hasMany(db.categories, { as: 'categories_organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.organization.hasMany(db.companies, { as: 'companies_organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.organization.hasMany(db.departments, { as: 'departments_organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.organization.hasMany(db.employees, { as: 'employees_organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.organization.hasMany(db.ingredients, { as: 'ingredients_organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.organization.hasMany(db.manufacturers, { as: 'manufacturers_organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.organization.hasMany(db.products, { as: 'products_organization', foreignKey: { name: 'organizationId', }, constraints: false, }); db.organization.hasMany(db.suppliers, { as: 'suppliers_organization', foreignKey: { name: 'organizationId', }, constraints: false, }); //end loop db.organization.belongsTo(db.users, { as: 'createdBy', }); db.organization.belongsTo(db.users, { as: 'updatedBy', }); }; return organization; };