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 RedemptionDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const redemption = await db.redemption.create( { id: data.id || undefined, pointsused: data.pointsused || null, redemptiontype: data.redemptiontype || null, discountvalue: data.discountvalue || null, status: data.status || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await redemption.setUser(data.user || null, { transaction, }); await redemption.setBooking(data.booking || null, { transaction, }); return redemption; } 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 redemptionData = data.map((item, index) => ({ id: item.id || undefined, pointsused: item.pointsused || null, redemptiontype: item.redemptiontype || null, discountvalue: item.discountvalue || null, status: item.status || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const redemption = await db.redemption.bulkCreate(redemptionData, { transaction, }); // For each item created, replace relation files return redemption; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const redemption = await db.redemption.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.pointsused !== undefined) updatePayload.pointsused = data.pointsused; if (data.redemptiontype !== undefined) updatePayload.redemptiontype = data.redemptiontype; if (data.discountvalue !== undefined) updatePayload.discountvalue = data.discountvalue; if (data.status !== undefined) updatePayload.status = data.status; updatePayload.updatedById = currentUser.id; await redemption.update(updatePayload, { transaction }); if (data.user !== undefined) { await redemption.setUser( data.user, { transaction }, ); } if (data.booking !== undefined) { await redemption.setBooking( data.booking, { transaction }, ); } return redemption; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const redemption = await db.redemption.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of redemption) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of redemption) { await record.destroy({ transaction }); } }); return redemption; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const redemption = await db.redemption.findByPk(id, options); await redemption.update( { deletedBy: currentUser.id, }, { transaction, }, ); await redemption.destroy({ transaction, }); return redemption; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const redemption = await db.redemption.findOne({ where }, { transaction }); if (!redemption) { return redemption; } const output = redemption.get({ plain: true }); output.user = await redemption.getUser({ transaction, }); output.booking = await redemption.getBooking({ 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.users, as: 'user', where: filter.user ? { [Op.or]: [ { id: { [Op.in]: filter.user .split('|') .map((term) => Utils.uuid(term)), }, }, { firstName: { [Op.or]: filter.user .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.bookings, as: 'booking', where: filter.booking ? { [Op.or]: [ { id: { [Op.in]: filter.booking .split('|') .map((term) => Utils.uuid(term)), }, }, { service_type: { [Op.or]: filter.booking .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.pointsusedRange) { const [start, end] = filter.pointsusedRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, pointsused: { ...where.pointsused, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, pointsused: { ...where.pointsused, [Op.lte]: end, }, }; } } if (filter.discountvalueRange) { const [start, end] = filter.discountvalueRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, discountvalue: { ...where.discountvalue, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, discountvalue: { ...where.discountvalue, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.redemptiontype) { where = { ...where, redemptiontype: filter.redemptiontype, }; } 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.redemption.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('redemption', 'id', query), ], }; } const records = await db.redemption.findAll({ attributes: ['id', 'id'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['id', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.id, })); } };