31607/backend/src/db/models/shifts.js
2025-05-17 15:15:47 +00:00

84 lines
1.7 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,
},
name: {
type: DataTypes.TEXT,
},
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.employees, {
as: 'employees',
foreignKey: {
name: 'shifts_employeesId',
},
constraints: false,
through: 'shiftsEmployeesEmployees',
});
db.shifts.belongsToMany(db.employees, {
as: 'employees_filter',
foreignKey: {
name: 'shifts_employeesId',
},
constraints: false,
through: 'shiftsEmployeesEmployees',
});
/// 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.companies, {
as: 'companies',
foreignKey: {
name: 'companiesId',
},
constraints: false,
});
db.shifts.belongsTo(db.users, {
as: 'createdBy',
});
db.shifts.belongsTo(db.users, {
as: 'updatedBy',
});
};
return shifts;
};