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 HrDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const hr = await db.hr.create( { id: data.id || undefined, employee_name: data.employee_name || null, position: data.position || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await hr.setBranch(data.branch || null, { transaction, }); await hr.setBranches(data.branches || null, { transaction, }); return hr; } 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 hrData = data.map((item, index) => ({ id: item.id || undefined, employee_name: item.employee_name || null, position: item.position || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const hr = await db.hr.bulkCreate(hrData, { transaction }); // For each item created, replace relation files return hr; } 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 hr = await db.hr.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.employee_name !== undefined) updatePayload.employee_name = data.employee_name; if (data.position !== undefined) updatePayload.position = data.position; updatePayload.updatedById = currentUser.id; await hr.update(updatePayload, { transaction }); if (data.branch !== undefined) { await hr.setBranch( data.branch, { transaction }, ); } if (data.branches !== undefined) { await hr.setBranches( data.branches, { transaction }, ); } return hr; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const hr = await db.hr.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of hr) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of hr) { await record.destroy({ transaction }); } }); return hr; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const hr = await db.hr.findByPk(id, options); await hr.update( { deletedBy: currentUser.id, }, { transaction, }, ); await hr.destroy({ transaction, }); return hr; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const hr = await db.hr.findOne({ where }, { transaction }); if (!hr) { return hr; } const output = hr.get({ plain: true }); output.branch = await hr.getBranch({ transaction, }); output.branches = await hr.getBranches({ 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 userBranches = (user && user.branches?.id) || null; if (userBranches) { if (options?.currentUser?.branchesId) { where.branchesId = options.currentUser.branchesId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.branches, as: 'branch', }, { model: db.branches, as: 'branches', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.employee_name) { where = { ...where, [Op.and]: Utils.ilike('hr', 'employee_name', filter.employee_name), }; } if (filter.position) { where = { ...where, [Op.and]: Utils.ilike('hr', 'position', filter.position), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.branch) { const listItems = filter.branch.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, branchId: { [Op.or]: listItems }, }; } if (filter.branches) { const listItems = filter.branches.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, branchesId: { [Op.or]: listItems }, }; } 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.branchesId; } 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.hr.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('hr', 'employee_name', query), ], }; } const records = await db.hr.findAll({ attributes: ['id', 'employee_name'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['employee_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.employee_name, })); } };