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 Price_change_logsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const price_change_logs = await db.price_change_logs.create( { id: data.id || undefined, old_price: data.old_price || null , new_price: data.new_price || null , currency: data.currency || null , changed_at: data.changed_at || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await price_change_logs.setListing( data.listing || null, { transaction, }); await price_change_logs.setChanged_by_user( data.changed_by_user || null, { transaction, }); return price_change_logs; } 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 price_change_logsData = data.map((item, index) => ({ id: item.id || undefined, old_price: item.old_price || null , new_price: item.new_price || null , currency: item.currency || null , changed_at: item.changed_at || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const price_change_logs = await db.price_change_logs.bulkCreate(price_change_logsData, { transaction }); // For each item created, replace relation files return price_change_logs; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const price_change_logs = await db.price_change_logs.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.old_price !== undefined) updatePayload.old_price = data.old_price; if (data.new_price !== undefined) updatePayload.new_price = data.new_price; if (data.currency !== undefined) updatePayload.currency = data.currency; if (data.changed_at !== undefined) updatePayload.changed_at = data.changed_at; updatePayload.updatedById = currentUser.id; await price_change_logs.update(updatePayload, {transaction}); if (data.listing !== undefined) { await price_change_logs.setListing( data.listing, { transaction } ); } if (data.changed_by_user !== undefined) { await price_change_logs.setChanged_by_user( data.changed_by_user, { transaction } ); } return price_change_logs; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const price_change_logs = await db.price_change_logs.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of price_change_logs) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of price_change_logs) { await record.destroy({transaction}); } }); return price_change_logs; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const price_change_logs = await db.price_change_logs.findByPk(id, options); await price_change_logs.update({ deletedBy: currentUser.id }, { transaction, }); await price_change_logs.destroy({ transaction }); return price_change_logs; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const price_change_logs = await db.price_change_logs.findOne( { where }, { transaction }, ); if (!price_change_logs) { return price_change_logs; } const output = price_change_logs.get({plain: true}); output.listing = await price_change_logs.getListing({ transaction }); output.changed_by_user = await price_change_logs.getChanged_by_user({ 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.account_listings, as: 'listing', where: filter.listing ? { [Op.or]: [ { id: { [Op.in]: filter.listing.split('|').map(term => Utils.uuid(term)) } }, { title: { [Op.or]: filter.listing.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.users, as: 'changed_by_user', where: filter.changed_by_user ? { [Op.or]: [ { id: { [Op.in]: filter.changed_by_user.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.changed_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.old_priceRange) { const [start, end] = filter.old_priceRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, old_price: { ...where.old_price, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, old_price: { ...where.old_price, [Op.lte]: end, }, }; } } if (filter.new_priceRange) { const [start, end] = filter.new_priceRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, new_price: { ...where.new_price, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, new_price: { ...where.new_price, [Op.lte]: end, }, }; } } if (filter.changed_atRange) { const [start, end] = filter.changed_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, changed_at: { ...where.changed_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, changed_at: { ...where.changed_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.currency) { where = { ...where, currency: filter.currency, }; } 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.price_change_logs.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( 'price_change_logs', 'listing', query, ), ], }; } const records = await db.price_change_logs.findAll({ attributes: [ 'id', 'listing' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['listing', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.listing, })); } };