898 lines
23 KiB
JavaScript
898 lines
23 KiB
JavaScript
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class EmployeesDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const employees = await db.employees.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name || null,
|
|
employee_id: data.employee_id || null,
|
|
contact_details: data.contact_details || null,
|
|
address: data.address || null,
|
|
emergency_contact: data.emergency_contact || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await employees.setJob_details(data.job_details || null, {
|
|
transaction,
|
|
});
|
|
|
|
await employees.setCompensation(data.compensation || null, {
|
|
transaction,
|
|
});
|
|
|
|
await employees.setExit_information(data.exit_information || null, {
|
|
transaction,
|
|
});
|
|
|
|
await employees.setOrganizations(data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
await employees.setPerformance_reviews(data.performance_reviews || [], {
|
|
transaction,
|
|
});
|
|
|
|
await employees.setAttendance_records(data.attendance_records || [], {
|
|
transaction,
|
|
});
|
|
|
|
await employees.setProfessional_development(
|
|
data.professional_development || [],
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await employees.setCompliance_documents(data.compliance_documents || [], {
|
|
transaction,
|
|
});
|
|
|
|
await employees.setAssigned_assets(data.assigned_assets || [], {
|
|
transaction,
|
|
});
|
|
|
|
await employees.setHealth_records(data.health_records || [], {
|
|
transaction,
|
|
});
|
|
|
|
await employees.setCommunications(data.communications || [], {
|
|
transaction,
|
|
});
|
|
|
|
return employees;
|
|
}
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const employeesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name || null,
|
|
employee_id: item.employee_id || null,
|
|
contact_details: item.contact_details || null,
|
|
address: item.address || null,
|
|
emergency_contact: item.emergency_contact || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const employees = await db.employees.bulkCreate(employeesData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return employees;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const employees = await db.employees.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
if (data.employee_id !== undefined)
|
|
updatePayload.employee_id = data.employee_id;
|
|
|
|
if (data.contact_details !== undefined)
|
|
updatePayload.contact_details = data.contact_details;
|
|
|
|
if (data.address !== undefined) updatePayload.address = data.address;
|
|
|
|
if (data.emergency_contact !== undefined)
|
|
updatePayload.emergency_contact = data.emergency_contact;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await employees.update(updatePayload, { transaction });
|
|
|
|
if (data.job_details !== undefined) {
|
|
await employees.setJob_details(
|
|
data.job_details,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.compensation !== undefined) {
|
|
await employees.setCompensation(
|
|
data.compensation,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.exit_information !== undefined) {
|
|
await employees.setExit_information(
|
|
data.exit_information,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await employees.setOrganizations(
|
|
data.organizations,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.performance_reviews !== undefined) {
|
|
await employees.setPerformance_reviews(data.performance_reviews, {
|
|
transaction,
|
|
});
|
|
}
|
|
|
|
if (data.attendance_records !== undefined) {
|
|
await employees.setAttendance_records(data.attendance_records, {
|
|
transaction,
|
|
});
|
|
}
|
|
|
|
if (data.professional_development !== undefined) {
|
|
await employees.setProfessional_development(
|
|
data.professional_development,
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.compliance_documents !== undefined) {
|
|
await employees.setCompliance_documents(data.compliance_documents, {
|
|
transaction,
|
|
});
|
|
}
|
|
|
|
if (data.assigned_assets !== undefined) {
|
|
await employees.setAssigned_assets(data.assigned_assets, { transaction });
|
|
}
|
|
|
|
if (data.health_records !== undefined) {
|
|
await employees.setHealth_records(data.health_records, { transaction });
|
|
}
|
|
|
|
if (data.communications !== undefined) {
|
|
await employees.setCommunications(data.communications, { transaction });
|
|
}
|
|
|
|
return employees;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const employees = await db.employees.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of employees) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of employees) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return employees;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const employees = await db.employees.findByPk(id, options);
|
|
|
|
await employees.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await employees.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return employees;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const employees = await db.employees.findOne({ where }, { transaction });
|
|
|
|
if (!employees) {
|
|
return employees;
|
|
}
|
|
|
|
const output = employees.get({ plain: true });
|
|
|
|
output.assets_employee = await employees.getAssets_employee({
|
|
transaction,
|
|
});
|
|
|
|
output.attendance_records_employee =
|
|
await employees.getAttendance_records_employee({
|
|
transaction,
|
|
});
|
|
|
|
output.communications_employee = await employees.getCommunications_employee(
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
output.compensations_employee = await employees.getCompensations_employee({
|
|
transaction,
|
|
});
|
|
|
|
output.compliance_documents_employee =
|
|
await employees.getCompliance_documents_employee({
|
|
transaction,
|
|
});
|
|
|
|
output.exit_information_employee =
|
|
await employees.getExit_information_employee({
|
|
transaction,
|
|
});
|
|
|
|
output.health_records_employee = await employees.getHealth_records_employee(
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
output.job_details_manager = await employees.getJob_details_manager({
|
|
transaction,
|
|
});
|
|
|
|
output.performance_reviews_employee =
|
|
await employees.getPerformance_reviews_employee({
|
|
transaction,
|
|
});
|
|
|
|
output.professional_development_employee =
|
|
await employees.getProfessional_development_employee({
|
|
transaction,
|
|
});
|
|
|
|
output.job_details = await employees.getJob_details({
|
|
transaction,
|
|
});
|
|
|
|
output.performance_reviews = await employees.getPerformance_reviews({
|
|
transaction,
|
|
});
|
|
|
|
output.attendance_records = await employees.getAttendance_records({
|
|
transaction,
|
|
});
|
|
|
|
output.compensation = await employees.getCompensation({
|
|
transaction,
|
|
});
|
|
|
|
output.professional_development =
|
|
await employees.getProfessional_development({
|
|
transaction,
|
|
});
|
|
|
|
output.compliance_documents = await employees.getCompliance_documents({
|
|
transaction,
|
|
});
|
|
|
|
output.assigned_assets = await employees.getAssigned_assets({
|
|
transaction,
|
|
});
|
|
|
|
output.health_records = await employees.getHealth_records({
|
|
transaction,
|
|
});
|
|
|
|
output.communications = await employees.getCommunications({
|
|
transaction,
|
|
});
|
|
|
|
output.exit_information = await employees.getExit_information({
|
|
transaction,
|
|
});
|
|
|
|
output.organizations = await employees.getOrganizations({
|
|
transaction,
|
|
});
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(filter, globalAccess, options) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.job_details,
|
|
as: 'job_details',
|
|
|
|
where: filter.job_details
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.job_details
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
position: {
|
|
[Op.or]: filter.job_details
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.compensations,
|
|
as: 'compensation',
|
|
|
|
where: filter.compensation
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.compensation
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
salary: {
|
|
[Op.or]: filter.compensation
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.exit_information,
|
|
as: 'exit_information',
|
|
|
|
where: filter.exit_information
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.exit_information
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
resignation_date: {
|
|
[Op.or]: filter.exit_information
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
},
|
|
|
|
{
|
|
model: db.performance_reviews,
|
|
as: 'performance_reviews',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.attendance_records,
|
|
as: 'attendance_records',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.professional_development,
|
|
as: 'professional_development',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.compliance_documents,
|
|
as: 'compliance_documents',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.assets,
|
|
as: 'assigned_assets',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.health_records,
|
|
as: 'health_records',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.communications,
|
|
as: 'communications',
|
|
required: false,
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('employees', 'name', filter.name),
|
|
};
|
|
}
|
|
|
|
if (filter.employee_id) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('employees', 'employee_id', filter.employee_id),
|
|
};
|
|
}
|
|
|
|
if (filter.contact_details) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'employees',
|
|
'contact_details',
|
|
filter.contact_details,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.address) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('employees', 'address', filter.address),
|
|
};
|
|
}
|
|
|
|
if (filter.emergency_contact) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'employees',
|
|
'emergency_contact',
|
|
filter.emergency_contact,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: { [Op.or]: listItems },
|
|
};
|
|
}
|
|
|
|
if (filter.performance_reviews) {
|
|
const searchTerms = filter.performance_reviews.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.performance_reviews,
|
|
as: 'performance_reviews_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
goals: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.attendance_records) {
|
|
const searchTerms = filter.attendance_records.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.attendance_records,
|
|
as: 'attendance_records_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
work_date: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.professional_development) {
|
|
const searchTerms = filter.professional_development.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.professional_development,
|
|
as: 'professional_development_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
training_attended: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.compliance_documents) {
|
|
const searchTerms = filter.compliance_documents.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.compliance_documents,
|
|
as: 'compliance_documents_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
contracts: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.assigned_assets) {
|
|
const searchTerms = filter.assigned_assets.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.assets,
|
|
as: 'assigned_assets_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
equipment_name: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.health_records) {
|
|
const searchTerms = filter.health_records.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.health_records,
|
|
as: 'health_records_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
medical_records: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.communications) {
|
|
const searchTerms = filter.communications.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.communications,
|
|
as: 'communications_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
internal_communications: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
if (globalAccess) {
|
|
delete where.organizationsId;
|
|
}
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order:
|
|
filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log,
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.employees.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count,
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(
|
|
query,
|
|
limit,
|
|
offset,
|
|
globalAccess,
|
|
organizationId,
|
|
) {
|
|
let where = {};
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike('employees', 'name', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.employees.findAll({
|
|
attributes: ['id', 'name'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.name,
|
|
}));
|
|
}
|
|
};
|