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 Rent_paymentsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const rent_payments = await db.rent_payments.create( { id: data.id || undefined, period_month: data.period_month || null, amount: data.amount || null, currency: data.currency || null, paid_at: data.paid_at || null, method: data.method || null, status: data.status || null, note: data.note || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await rent_payments.setLease(data.lease || null, { transaction, }); await rent_payments.setCompany(data.company || null, { transaction, }); return rent_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 rent_paymentsData = data.map((item, index) => ({ id: item.id || undefined, period_month: item.period_month || null, amount: item.amount || null, currency: item.currency || null, paid_at: item.paid_at || null, method: item.method || null, status: item.status || null, note: item.note || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const rent_payments = await db.rent_payments.bulkCreate(rent_paymentsData, { transaction, }); // For each item created, replace relation files return rent_payments; } 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 rent_payments = await db.rent_payments.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.period_month !== undefined) updatePayload.period_month = data.period_month; if (data.amount !== undefined) updatePayload.amount = data.amount; if (data.currency !== undefined) updatePayload.currency = data.currency; if (data.paid_at !== undefined) updatePayload.paid_at = data.paid_at; if (data.method !== undefined) updatePayload.method = data.method; if (data.status !== undefined) updatePayload.status = data.status; if (data.note !== undefined) updatePayload.note = data.note; updatePayload.updatedById = currentUser.id; await rent_payments.update(updatePayload, { transaction }); if (data.lease !== undefined) { await rent_payments.setLease( data.lease, { transaction }, ); } if (data.company !== undefined) { await rent_payments.setCompany( data.company, { transaction }, ); } return rent_payments; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const rent_payments = await db.rent_payments.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of rent_payments) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of rent_payments) { await record.destroy({ transaction }); } }); return rent_payments; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const rent_payments = await db.rent_payments.findByPk(id, options); await rent_payments.update( { deletedBy: currentUser.id, }, { transaction, }, ); await rent_payments.destroy({ transaction, }); return rent_payments; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const rent_payments = await db.rent_payments.findOne( { where }, { transaction }, ); if (!rent_payments) { return rent_payments; } const output = rent_payments.get({ plain: true }); output.lease = await rent_payments.getLease({ transaction, }); output.company = await rent_payments.getCompany({ 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 userCompany = (user && user.company?.id) || null; if (userCompany) { if (options?.currentUser?.companyId) { where.companyId = options.currentUser.companyId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.leases, as: 'lease', where: filter.lease ? { [Op.or]: [ { id: { [Op.in]: filter.lease .split('|') .map((term) => Utils.uuid(term)), }, }, { status: { [Op.or]: filter.lease .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.company, as: 'company', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.period_month) { where = { ...where, [Op.and]: Utils.ilike( 'rent_payments', 'period_month', filter.period_month, ), }; } if (filter.currency) { where = { ...where, [Op.and]: Utils.ilike('rent_payments', 'currency', filter.currency), }; } if (filter.note) { where = { ...where, [Op.and]: Utils.ilike('rent_payments', 'note', filter.note), }; } 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.paid_atRange) { const [start, end] = filter.paid_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, paid_at: { ...where.paid_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, paid_at: { ...where.paid_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.method) { where = { ...where, method: filter.method, }; } if (filter.status) { where = { ...where, status: filter.status, }; } if (filter.company) { const listItems = filter.company.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, companyId: { [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.companyId; } 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.rent_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, globalAccess, organizationId, ) { let where = {}; if (!globalAccess && organizationId) { where.organizationId = organizationId; } if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike('rent_payments', 'period_month', query), ], }; } const records = await db.rent_payments.findAll({ attributes: ['id', 'period_month'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['period_month', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.period_month, })); } };