124 lines
2.5 KiB
JavaScript
124 lines
2.5 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 companies = sequelize.define(
|
|
'companies',
|
|
{
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true,
|
|
},
|
|
|
|
name: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
address: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
vat_number: {
|
|
type: DataTypes.TEXT,
|
|
},
|
|
|
|
importHash: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
paranoid: true,
|
|
freezeTableName: true,
|
|
},
|
|
);
|
|
|
|
companies.associate = (db) => {
|
|
db.companies.belongsToMany(db.users, {
|
|
as: 'users',
|
|
foreignKey: {
|
|
name: 'companies_usersId',
|
|
},
|
|
constraints: false,
|
|
through: 'companiesUsersUsers',
|
|
});
|
|
|
|
db.companies.belongsToMany(db.users, {
|
|
as: 'users_filter',
|
|
foreignKey: {
|
|
name: 'companies_usersId',
|
|
},
|
|
constraints: false,
|
|
through: 'companiesUsersUsers',
|
|
});
|
|
|
|
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
|
|
|
db.companies.hasMany(db.bank_reconciliations, {
|
|
as: 'bank_reconciliations_company',
|
|
foreignKey: {
|
|
name: 'companyId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.companies.hasMany(db.invoices, {
|
|
as: 'invoices_company',
|
|
foreignKey: {
|
|
name: 'companyId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.companies.hasMany(db.quotations, {
|
|
as: 'quotations_company',
|
|
foreignKey: {
|
|
name: 'companyId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.companies.hasMany(db.tax_reports, {
|
|
as: 'tax_reports_company',
|
|
foreignKey: {
|
|
name: 'companyId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.companies.hasMany(db.transactions, {
|
|
as: 'transactions_company',
|
|
foreignKey: {
|
|
name: 'companyId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
//end loop
|
|
|
|
db.companies.belongsTo(db.company, {
|
|
as: 'company',
|
|
foreignKey: {
|
|
name: 'companyId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.companies.belongsTo(db.users, {
|
|
as: 'createdBy',
|
|
});
|
|
|
|
db.companies.belongsTo(db.users, {
|
|
as: 'updatedBy',
|
|
});
|
|
};
|
|
|
|
return companies;
|
|
};
|