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 OrdersDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const orders = await db.orders.create( { id: data.id || undefined, customer_mobile_number: data.customer_mobile_number || null , status: data.status || null , current_zone: data.current_zone || null , base_prep_minutes: data.base_prep_minutes || null , packing_buffer_minutes: data.packing_buffer_minutes || null , transit_minutes: data.transit_minutes || null , total_eta_minutes: data.total_eta_minutes || null , placed_at: data.placed_at || null , eta_due_at: data.eta_due_at || null , delivered_at: data.delivered_at || null , subtotal_aud: data.subtotal_aud || null , delivery_fee_aud: data.delivery_fee_aud || null , gst_aud: data.gst_aud || null , total_aud: data.total_aud || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await orders.setCustomer( data.customer || null, { transaction, }); await orders.setHub( data.hub || null, { transaction, }); await orders.setDelivery_address( data.delivery_address || null, { transaction, }); return orders; } 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 ordersData = data.map((item, index) => ({ id: item.id || undefined, customer_mobile_number: item.customer_mobile_number || null , status: item.status || null , current_zone: item.current_zone || null , base_prep_minutes: item.base_prep_minutes || null , packing_buffer_minutes: item.packing_buffer_minutes || null , transit_minutes: item.transit_minutes || null , total_eta_minutes: item.total_eta_minutes || null , placed_at: item.placed_at || null , eta_due_at: item.eta_due_at || null , delivered_at: item.delivered_at || null , subtotal_aud: item.subtotal_aud || null , delivery_fee_aud: item.delivery_fee_aud || null , gst_aud: item.gst_aud || null , total_aud: item.total_aud || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const orders = await db.orders.bulkCreate(ordersData, { transaction }); // For each item created, replace relation files return orders; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const orders = await db.orders.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.customer_mobile_number !== undefined) updatePayload.customer_mobile_number = data.customer_mobile_number; if (data.status !== undefined) updatePayload.status = data.status; if (data.current_zone !== undefined) updatePayload.current_zone = data.current_zone; if (data.base_prep_minutes !== undefined) updatePayload.base_prep_minutes = data.base_prep_minutes; if (data.packing_buffer_minutes !== undefined) updatePayload.packing_buffer_minutes = data.packing_buffer_minutes; if (data.transit_minutes !== undefined) updatePayload.transit_minutes = data.transit_minutes; if (data.total_eta_minutes !== undefined) updatePayload.total_eta_minutes = data.total_eta_minutes; if (data.placed_at !== undefined) updatePayload.placed_at = data.placed_at; if (data.eta_due_at !== undefined) updatePayload.eta_due_at = data.eta_due_at; if (data.delivered_at !== undefined) updatePayload.delivered_at = data.delivered_at; if (data.subtotal_aud !== undefined) updatePayload.subtotal_aud = data.subtotal_aud; if (data.delivery_fee_aud !== undefined) updatePayload.delivery_fee_aud = data.delivery_fee_aud; if (data.gst_aud !== undefined) updatePayload.gst_aud = data.gst_aud; if (data.total_aud !== undefined) updatePayload.total_aud = data.total_aud; updatePayload.updatedById = currentUser.id; await orders.update(updatePayload, {transaction}); if (data.customer !== undefined) { await orders.setCustomer( data.customer, { transaction } ); } if (data.hub !== undefined) { await orders.setHub( data.hub, { transaction } ); } if (data.delivery_address !== undefined) { await orders.setDelivery_address( data.delivery_address, { transaction } ); } return orders; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const orders = await db.orders.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of orders) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of orders) { await record.destroy({transaction}); } }); return orders; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const orders = await db.orders.findByPk(id, options); await orders.update({ deletedBy: currentUser.id }, { transaction, }); await orders.destroy({ transaction }); return orders; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const orders = await db.orders.findOne( { where }, { transaction }, ); if (!orders) { return orders; } const output = orders.get({plain: true}); output.order_items_order = await orders.getOrder_items_order({ transaction }); output.payments_order = await orders.getPayments_order({ transaction }); output.dispatch_jobs_order = await orders.getDispatch_jobs_order({ transaction }); output.customer = await orders.getCustomer({ transaction }); output.hub = await orders.getHub({ transaction }); output.delivery_address = await orders.getDelivery_address({ 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: 'customer', where: filter.customer ? { [Op.or]: [ { id: { [Op.in]: filter.customer.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.customer.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.hubs, as: 'hub', where: filter.hub ? { [Op.or]: [ { id: { [Op.in]: filter.hub.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.hub.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.customer_addresses, as: 'delivery_address', where: filter.delivery_address ? { [Op.or]: [ { id: { [Op.in]: filter.delivery_address.split('|').map(term => Utils.uuid(term)) } }, { address_text: { [Op.or]: filter.delivery_address.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.customer_mobile_number) { where = { ...where, [Op.and]: Utils.ilike( 'orders', 'customer_mobile_number', filter.customer_mobile_number, ), }; } if (filter.base_prep_minutesRange) { const [start, end] = filter.base_prep_minutesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, base_prep_minutes: { ...where.base_prep_minutes, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, base_prep_minutes: { ...where.base_prep_minutes, [Op.lte]: end, }, }; } } if (filter.packing_buffer_minutesRange) { const [start, end] = filter.packing_buffer_minutesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, packing_buffer_minutes: { ...where.packing_buffer_minutes, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, packing_buffer_minutes: { ...where.packing_buffer_minutes, [Op.lte]: end, }, }; } } if (filter.transit_minutesRange) { const [start, end] = filter.transit_minutesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, transit_minutes: { ...where.transit_minutes, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, transit_minutes: { ...where.transit_minutes, [Op.lte]: end, }, }; } } if (filter.total_eta_minutesRange) { const [start, end] = filter.total_eta_minutesRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, total_eta_minutes: { ...where.total_eta_minutes, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, total_eta_minutes: { ...where.total_eta_minutes, [Op.lte]: end, }, }; } } if (filter.placed_atRange) { const [start, end] = filter.placed_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, placed_at: { ...where.placed_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, placed_at: { ...where.placed_at, [Op.lte]: end, }, }; } } if (filter.eta_due_atRange) { const [start, end] = filter.eta_due_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, eta_due_at: { ...where.eta_due_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, eta_due_at: { ...where.eta_due_at, [Op.lte]: end, }, }; } } if (filter.delivered_atRange) { const [start, end] = filter.delivered_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, delivered_at: { ...where.delivered_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, delivered_at: { ...where.delivered_at, [Op.lte]: end, }, }; } } if (filter.subtotal_audRange) { const [start, end] = filter.subtotal_audRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, subtotal_aud: { ...where.subtotal_aud, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, subtotal_aud: { ...where.subtotal_aud, [Op.lte]: end, }, }; } } if (filter.delivery_fee_audRange) { const [start, end] = filter.delivery_fee_audRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, delivery_fee_aud: { ...where.delivery_fee_aud, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, delivery_fee_aud: { ...where.delivery_fee_aud, [Op.lte]: end, }, }; } } if (filter.gst_audRange) { const [start, end] = filter.gst_audRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, gst_aud: { ...where.gst_aud, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, gst_aud: { ...where.gst_aud, [Op.lte]: end, }, }; } } if (filter.total_audRange) { const [start, end] = filter.total_audRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, total_aud: { ...where.total_aud, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, total_aud: { ...where.total_aud, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.status) { where = { ...where, status: filter.status, }; } if (filter.current_zone) { where = { ...where, current_zone: filter.current_zone, }; } 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.orders.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( 'orders', 'status', query, ), ], }; } const records = await db.orders.findAll({ attributes: [ 'id', 'status' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['status', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.status, })); } };