const db = require('../models'); const crypto = require('crypto'); const Utils = require('../utils'); const Sequelize = db.Sequelize; const Op = Sequelize.Op; module.exports = class QuotesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const quotes = await db.quotes.create( { id: data.id || undefined, accessories_cost: data.accessories_cost || null , ew_cost: data.ew_cost || null , bnp_cost: data.bnp_cost || null , smc_cost: data.smc_cost || null , final_on_road_price: data.final_on_road_price || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); return quotes; } 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 quotesData = data.map((item, index) => ({ id: item.id || undefined, accessories_cost: item.accessories_cost || null , ew_cost: item.ew_cost || null , bnp_cost: item.bnp_cost || null , smc_cost: item.smc_cost || null , final_on_road_price: item.final_on_road_price || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const quotes = await db.quotes.bulkCreate(quotesData, { transaction }); return quotes; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const quotes = await db.quotes.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.accessories_cost !== undefined) updatePayload.accessories_cost = data.accessories_cost; if (data.ew_cost !== undefined) updatePayload.ew_cost = data.ew_cost; if (data.bnp_cost !== undefined) updatePayload.bnp_cost = data.bnp_cost; if (data.smc_cost !== undefined) updatePayload.smc_cost = data.smc_cost; if (data.final_on_road_price !== undefined) updatePayload.final_on_road_price = data.final_on_road_price; updatePayload.updatedById = currentUser.id; await quotes.update(updatePayload, {transaction}); return quotes; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const quotes = await db.quotes.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of quotes) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of quotes) { await record.destroy({transaction}); } }); return quotes; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const quotes = await db.quotes.findByPk(id, options); await quotes.update({ deletedBy: currentUser.id }, { transaction, }); await quotes.destroy({ transaction }); return quotes; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const quotes = await db.quotes.findOne( { where }, { transaction }, ); if (!quotes) { return quotes; } const output = quotes.get({plain: true}); return output; } static async findAll(filter, options) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; const user = (options && options.currentUser) || null; offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = []; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.accessories_costRange) { const [start, end] = filter.accessories_costRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, accessories_cost: { ...where.accessories_cost, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, accessories_cost: { ...where.accessories_cost, [Op.lte]: end, }, }; } } if (filter.ew_costRange) { const [start, end] = filter.ew_costRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, ew_cost: { ...where.ew_cost, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, ew_cost: { ...where.ew_cost, [Op.lte]: end, }, }; } } if (filter.bnp_costRange) { const [start, end] = filter.bnp_costRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, bnp_cost: { ...where.bnp_cost, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, bnp_cost: { ...where.bnp_cost, [Op.lte]: end, }, }; } } if (filter.smc_costRange) { const [start, end] = filter.smc_costRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, smc_cost: { ...where.smc_cost, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, smc_cost: { ...where.smc_cost, [Op.lte]: end, }, }; } } if (filter.final_on_road_priceRange) { const [start, end] = filter.final_on_road_priceRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, final_on_road_price: { ...where.final_on_road_price, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, final_on_road_price: { ...where.final_on_road_price, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } 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.quotes.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( 'quotes', 'vehicle', query, ), ], }; } const records = await db.quotes.findAll({ attributes: [ 'id', 'vehicle' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['vehicle', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.vehicle, })); } };