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 Account_price_listsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const account_price_lists = await db.account_price_lists.create( { id: data.id || undefined, effective_start: data.effective_start || null , effective_end: data.effective_end || null , is_primary: data.is_primary || false , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await account_price_lists.setAccount( data.account || null, { transaction, }); await account_price_lists.setPrice_list( data.price_list || null, { transaction, }); return account_price_lists; } 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 account_price_listsData = data.map((item, index) => ({ id: item.id || undefined, effective_start: item.effective_start || null , effective_end: item.effective_end || null , is_primary: item.is_primary || false , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const account_price_lists = await db.account_price_lists.bulkCreate(account_price_listsData, { transaction }); // For each item created, replace relation files return account_price_lists; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const account_price_lists = await db.account_price_lists.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.effective_start !== undefined) updatePayload.effective_start = data.effective_start; if (data.effective_end !== undefined) updatePayload.effective_end = data.effective_end; if (data.is_primary !== undefined) updatePayload.is_primary = data.is_primary; updatePayload.updatedById = currentUser.id; await account_price_lists.update(updatePayload, {transaction}); if (data.account !== undefined) { await account_price_lists.setAccount( data.account, { transaction } ); } if (data.price_list !== undefined) { await account_price_lists.setPrice_list( data.price_list, { transaction } ); } return account_price_lists; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const account_price_lists = await db.account_price_lists.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of account_price_lists) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of account_price_lists) { await record.destroy({transaction}); } }); return account_price_lists; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const account_price_lists = await db.account_price_lists.findByPk(id, options); await account_price_lists.update({ deletedBy: currentUser.id }, { transaction, }); await account_price_lists.destroy({ transaction }); return account_price_lists; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const account_price_lists = await db.account_price_lists.findOne( { where }, { transaction }, ); if (!account_price_lists) { return account_price_lists; } const output = account_price_lists.get({plain: true}); output.account = await account_price_lists.getAccount({ transaction }); output.price_list = await account_price_lists.getPrice_list({ 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.accounts, as: 'account', where: filter.account ? { [Op.or]: [ { id: { [Op.in]: filter.account.split('|').map(term => Utils.uuid(term)) } }, { account_name: { [Op.or]: filter.account.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.price_lists, as: 'price_list', where: filter.price_list ? { [Op.or]: [ { id: { [Op.in]: filter.price_list.split('|').map(term => Utils.uuid(term)) } }, { price_list_name: { [Op.or]: filter.price_list.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.effective_startRange) { const [start, end] = filter.effective_startRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, effective_start: { ...where.effective_start, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, effective_start: { ...where.effective_start, [Op.lte]: end, }, }; } } if (filter.effective_endRange) { const [start, end] = filter.effective_endRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, effective_end: { ...where.effective_end, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, effective_end: { ...where.effective_end, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.is_primary) { where = { ...where, is_primary: filter.is_primary, }; } 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.account_price_lists.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( 'account_price_lists', 'is_primary', query, ), ], }; } const records = await db.account_price_lists.findAll({ attributes: [ 'id', 'is_primary' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['is_primary', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.is_primary, })); } };