31563/backend/src/db/models/shifts.js
2025-05-15 17:36:13 +00:00

98 lines
2.0 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 shifts = sequelize.define(
'shifts',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
start_time: {
type: DataTypes.DATE,
},
end_time: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
shifts.associate = (db) => {
db.shifts.belongsToMany(db.users, {
as: 'employees',
foreignKey: {
name: 'shifts_employeesId',
},
constraints: false,
through: 'shiftsEmployeesUsers',
});
db.shifts.belongsToMany(db.users, {
as: 'employees_filter',
foreignKey: {
name: 'shifts_employeesId',
},
constraints: false,
through: 'shiftsEmployeesUsers',
});
db.shifts.belongsToMany(db.nozzles, {
as: 'meter_readings',
foreignKey: {
name: 'shifts_meter_readingsId',
},
constraints: false,
through: 'shiftsMeter_readingsNozzles',
});
db.shifts.belongsToMany(db.nozzles, {
as: 'meter_readings_filter',
foreignKey: {
name: 'shifts_meter_readingsId',
},
constraints: false,
through: 'shiftsMeter_readingsNozzles',
});
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
//end loop
db.shifts.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.shifts.belongsTo(db.users, {
as: 'createdBy',
});
db.shifts.belongsTo(db.users, {
as: 'updatedBy',
});
};
return shifts;
};