32265/backend/src/db/models/stores.js
2025-06-16 07:29:23 +00:00

96 lines
1.9 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 stores = sequelize.define(
'stores',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
location: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
stores.associate = (db) => {
db.stores.belongsToMany(db.users, {
as: 'employees',
foreignKey: {
name: 'stores_employeesId',
},
constraints: false,
through: 'storesEmployeesUsers',
});
db.stores.belongsToMany(db.users, {
as: 'employees_filter',
foreignKey: {
name: 'stores_employeesId',
},
constraints: false,
through: 'storesEmployeesUsers',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.stores.hasMany(db.financial_reports, {
as: 'financial_reports_store',
foreignKey: {
name: 'storeId',
},
constraints: false,
});
db.stores.hasMany(db.inventory_adjustments, {
as: 'inventory_adjustments_store',
foreignKey: {
name: 'storeId',
},
constraints: false,
});
db.stores.hasMany(db.sales, {
as: 'sales_store',
foreignKey: {
name: 'storeId',
},
constraints: false,
});
//end loop
db.stores.belongsTo(db.users, {
as: 'createdBy',
});
db.stores.belongsTo(db.users, {
as: 'updatedBy',
});
};
return stores;
};