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 Field_site_payment_requisitionsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const field_site_payment_requisitions = await db.field_site_payment_requisitions.create( { id: data.id || undefined, department: data.department || null, employee_name: data.employee_name || null, departure_place: data.departure_place || null, departure_date: data.departure_date || null, arrival_place: data.arrival_place || null, return_date: data.return_date || null, requisition_date: data.requisition_date || null, requested_amount: data.requested_amount || null, payment_type: data.payment_type || null, status: data.status || null, requester: data.requester || null, approver: data.approver || null, due_date: data.due_date || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await field_site_payment_requisitions.setOrganization( currentUser.organization.id || null, { transaction, }, ); await field_site_payment_requisitions.setProject(data.project || null, { transaction, }); await field_site_payment_requisitions.setOrganizations( data.organizations || null, { transaction, }, ); return field_site_payment_requisitions; } 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 field_site_payment_requisitionsData = data.map((item, index) => ({ id: item.id || undefined, department: item.department || null, employee_name: item.employee_name || null, departure_place: item.departure_place || null, departure_date: item.departure_date || null, arrival_place: item.arrival_place || null, return_date: item.return_date || null, requisition_date: item.requisition_date || null, requested_amount: item.requested_amount || null, payment_type: item.payment_type || null, status: item.status || null, requester: item.requester || null, approver: item.approver || null, due_date: item.due_date || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const field_site_payment_requisitions = await db.field_site_payment_requisitions.bulkCreate( field_site_payment_requisitionsData, { transaction }, ); // For each item created, replace relation files return field_site_payment_requisitions; } 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 field_site_payment_requisitions = await db.field_site_payment_requisitions.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.department !== undefined) updatePayload.department = data.department; if (data.employee_name !== undefined) updatePayload.employee_name = data.employee_name; if (data.departure_place !== undefined) updatePayload.departure_place = data.departure_place; if (data.departure_date !== undefined) updatePayload.departure_date = data.departure_date; if (data.arrival_place !== undefined) updatePayload.arrival_place = data.arrival_place; if (data.return_date !== undefined) updatePayload.return_date = data.return_date; if (data.requisition_date !== undefined) updatePayload.requisition_date = data.requisition_date; if (data.requested_amount !== undefined) updatePayload.requested_amount = data.requested_amount; if (data.payment_type !== undefined) updatePayload.payment_type = data.payment_type; if (data.status !== undefined) updatePayload.status = data.status; if (data.requester !== undefined) updatePayload.requester = data.requester; if (data.approver !== undefined) updatePayload.approver = data.approver; if (data.due_date !== undefined) updatePayload.due_date = data.due_date; updatePayload.updatedById = currentUser.id; await field_site_payment_requisitions.update(updatePayload, { transaction, }); if (data.organization !== undefined) { await field_site_payment_requisitions.setOrganization( globalAccess ? data.organization : currentUser.organization.id, { transaction }, ); } if (data.project !== undefined) { await field_site_payment_requisitions.setProject( data.project, { transaction }, ); } if (data.organizations !== undefined) { await field_site_payment_requisitions.setOrganizations( data.organizations, { transaction }, ); } return field_site_payment_requisitions; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const field_site_payment_requisitions = await db.field_site_payment_requisitions.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of field_site_payment_requisitions) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of field_site_payment_requisitions) { await record.destroy({ transaction }); } }); return field_site_payment_requisitions; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const field_site_payment_requisitions = await db.field_site_payment_requisitions.findByPk(id, options); await field_site_payment_requisitions.update( { deletedBy: currentUser.id, }, { transaction, }, ); await field_site_payment_requisitions.destroy({ transaction, }); return field_site_payment_requisitions; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const field_site_payment_requisitions = await db.field_site_payment_requisitions.findOne( { where }, { transaction }, ); if (!field_site_payment_requisitions) { return field_site_payment_requisitions; } const output = field_site_payment_requisitions.get({ plain: true }); output.organization = await field_site_payment_requisitions.getOrganization( { transaction, }, ); output.project = await field_site_payment_requisitions.getProject({ transaction, }); output.organizations = await field_site_payment_requisitions.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.organizations, as: 'organization', }, { model: db.projects, as: 'project', where: filter.project ? { [Op.or]: [ { id: { [Op.in]: filter.project .split('|') .map((term) => Utils.uuid(term)), }, }, { name: { [Op.or]: filter.project .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.organizations, as: 'organizations', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.department) { where = { ...where, [Op.and]: Utils.ilike( 'field_site_payment_requisitions', 'department', filter.department, ), }; } if (filter.employee_name) { where = { ...where, [Op.and]: Utils.ilike( 'field_site_payment_requisitions', 'employee_name', filter.employee_name, ), }; } if (filter.departure_place) { where = { ...where, [Op.and]: Utils.ilike( 'field_site_payment_requisitions', 'departure_place', filter.departure_place, ), }; } if (filter.arrival_place) { where = { ...where, [Op.and]: Utils.ilike( 'field_site_payment_requisitions', 'arrival_place', filter.arrival_place, ), }; } if (filter.requester) { where = { ...where, [Op.and]: Utils.ilike( 'field_site_payment_requisitions', 'requester', filter.requester, ), }; } if (filter.approver) { where = { ...where, [Op.and]: Utils.ilike( 'field_site_payment_requisitions', 'approver', filter.approver, ), }; } if (filter.departure_dateRange) { const [start, end] = filter.departure_dateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, departure_date: { ...where.departure_date, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, departure_date: { ...where.departure_date, [Op.lte]: end, }, }; } } if (filter.return_dateRange) { const [start, end] = filter.return_dateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, return_date: { ...where.return_date, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, return_date: { ...where.return_date, [Op.lte]: end, }, }; } } if (filter.requisition_dateRange) { const [start, end] = filter.requisition_dateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, requisition_date: { ...where.requisition_date, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, requisition_date: { ...where.requisition_date, [Op.lte]: end, }, }; } } if (filter.requested_amountRange) { const [start, end] = filter.requested_amountRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, requested_amount: { ...where.requested_amount, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, requested_amount: { ...where.requested_amount, [Op.lte]: end, }, }; } } if (filter.due_dateRange) { const [start, end] = filter.due_dateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, due_date: { ...where.due_date, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, due_date: { ...where.due_date, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.payment_type) { where = { ...where, payment_type: filter.payment_type, }; } if (filter.status) { where = { ...where, status: filter.status, }; } if (filter.organization) { const listItems = filter.organization.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, organizationId: { [Op.or]: listItems }, }; } if (filter.organizations) { const listItems = filter.organizations.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, organizationsId: { [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.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.field_site_payment_requisitions.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( 'field_site_payment_requisitions', 'employee_name', query, ), ], }; } const records = await db.field_site_payment_requisitions.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, })); } };