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 PaymentsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const payments = await db.payments.create( { id: data.id || undefined, payee: data.payee || null, payment_date: data.payment_date || null, purpose: data.purpose || null, amount: data.amount || null, document_type: data.document_type || null, auto_generated_document_no: data.auto_generated_document_no || null, status: data.status || null, shelf_no: data.shelf_no || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await payments.setProject(data.project || null, { transaction, }); return payments; } 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 paymentsData = data.map((item, index) => ({ id: item.id || undefined, payee: item.payee || null, payment_date: item.payment_date || null, purpose: item.purpose || null, amount: item.amount || null, document_type: item.document_type || null, auto_generated_document_no: item.auto_generated_document_no || null, status: item.status || null, shelf_no: item.shelf_no || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const payments = await db.payments.bulkCreate(paymentsData, { transaction, }); // For each item created, replace relation files return payments; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const payments = await db.payments.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.payee !== undefined) updatePayload.payee = data.payee; if (data.payment_date !== undefined) updatePayload.payment_date = data.payment_date; if (data.purpose !== undefined) updatePayload.purpose = data.purpose; if (data.amount !== undefined) updatePayload.amount = data.amount; if (data.document_type !== undefined) updatePayload.document_type = data.document_type; if (data.auto_generated_document_no !== undefined) updatePayload.auto_generated_document_no = data.auto_generated_document_no; if (data.status !== undefined) updatePayload.status = data.status; if (data.shelf_no !== undefined) updatePayload.shelf_no = data.shelf_no; updatePayload.updatedById = currentUser.id; await payments.update(updatePayload, { transaction }); if (data.project !== undefined) { await payments.setProject( data.project, { transaction }, ); } return payments; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const payments = await db.payments.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of payments) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of payments) { await record.destroy({ transaction }); } }); return payments; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const payments = await db.payments.findByPk(id, options); await payments.update( { deletedBy: currentUser.id, }, { transaction, }, ); await payments.destroy({ transaction, }); return payments; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const payments = await db.payments.findOne({ where }, { transaction }); if (!payments) { return payments; } const output = payments.get({ plain: true }); output.project = await payments.getProject({ transaction, }); return output; } static async findAll(filter, options) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { 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}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.payee) { where = { ...where, [Op.and]: Utils.ilike('payments', 'payee', filter.payee), }; } if (filter.purpose) { where = { ...where, [Op.and]: Utils.ilike('payments', 'purpose', filter.purpose), }; } if (filter.auto_generated_document_no) { where = { ...where, [Op.and]: Utils.ilike( 'payments', 'auto_generated_document_no', filter.auto_generated_document_no, ), }; } if (filter.shelf_no) { where = { ...where, [Op.and]: Utils.ilike('payments', 'shelf_no', filter.shelf_no), }; } if (filter.payment_dateRange) { const [start, end] = filter.payment_dateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, payment_date: { ...where.payment_date, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, payment_date: { ...where.payment_date, [Op.lte]: end, }, }; } } if (filter.amountRange) { const [start, end] = filter.amountRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, amount: { ...where.amount, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, amount: { ...where.amount, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.document_type) { where = { ...where, document_type: filter.document_type, }; } if (filter.status) { where = { ...where, status: filter.status, }; } 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, }, }; } } } 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.payments.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) { let where = {}; if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike('payments', 'payee', query), ], }; } const records = await db.payments.findAll({ attributes: ['id', 'payee'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['payee', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.payee, })); } };