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 New_tr_requestsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const new_tr_requests = await db.new_tr_requests.create( { id: data.id || undefined, date: data.date || null, route: data.route || null, vehicle_type: data.vehicle_type || null, company: data.company || null, contact_person: data.contact_person || null, contact_number: data.contact_number || null, special_note: data.special_note || null, status: data.status || null, tr_no: data.tr_no || null, timestamp: data.timestamp || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await new_tr_requests.setKam(data.kam || null, { transaction, }); return new_tr_requests; } 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 new_tr_requestsData = data.map((item, index) => ({ id: item.id || undefined, date: item.date || null, route: item.route || null, vehicle_type: item.vehicle_type || null, company: item.company || null, contact_person: item.contact_person || null, contact_number: item.contact_number || null, special_note: item.special_note || null, status: item.status || null, tr_no: item.tr_no || null, timestamp: item.timestamp || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const new_tr_requests = await db.new_tr_requests.bulkCreate( new_tr_requestsData, { transaction }, ); // For each item created, replace relation files return new_tr_requests; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const new_tr_requests = await db.new_tr_requests.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.date !== undefined) updatePayload.date = data.date; if (data.route !== undefined) updatePayload.route = data.route; if (data.vehicle_type !== undefined) updatePayload.vehicle_type = data.vehicle_type; if (data.company !== undefined) updatePayload.company = data.company; if (data.contact_person !== undefined) updatePayload.contact_person = data.contact_person; if (data.contact_number !== undefined) updatePayload.contact_number = data.contact_number; if (data.special_note !== undefined) updatePayload.special_note = data.special_note; if (data.status !== undefined) updatePayload.status = data.status; if (data.tr_no !== undefined) updatePayload.tr_no = data.tr_no; if (data.timestamp !== undefined) updatePayload.timestamp = data.timestamp; updatePayload.updatedById = currentUser.id; await new_tr_requests.update(updatePayload, { transaction }); if (data.kam !== undefined) { await new_tr_requests.setKam( data.kam, { transaction }, ); } return new_tr_requests; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const new_tr_requests = await db.new_tr_requests.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of new_tr_requests) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of new_tr_requests) { await record.destroy({ transaction }); } }); return new_tr_requests; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const new_tr_requests = await db.new_tr_requests.findByPk(id, options); await new_tr_requests.update( { deletedBy: currentUser.id, }, { transaction, }, ); await new_tr_requests.destroy({ transaction, }); return new_tr_requests; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const new_tr_requests = await db.new_tr_requests.findOne( { where }, { transaction }, ); if (!new_tr_requests) { return new_tr_requests; } const output = new_tr_requests.get({ plain: true }); output.kam = await new_tr_requests.getKam({ 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: 'kam', where: filter.kam ? { [Op.or]: [ { id: { [Op.in]: filter.kam .split('|') .map((term) => Utils.uuid(term)), }, }, { firstName: { [Op.or]: filter.kam .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.route) { where = { ...where, [Op.and]: Utils.ilike('new_tr_requests', 'route', filter.route), }; } if (filter.vehicle_type) { where = { ...where, [Op.and]: Utils.ilike( 'new_tr_requests', 'vehicle_type', filter.vehicle_type, ), }; } if (filter.company) { where = { ...where, [Op.and]: Utils.ilike('new_tr_requests', 'company', filter.company), }; } if (filter.contact_person) { where = { ...where, [Op.and]: Utils.ilike( 'new_tr_requests', 'contact_person', filter.contact_person, ), }; } if (filter.contact_number) { where = { ...where, [Op.and]: Utils.ilike( 'new_tr_requests', 'contact_number', filter.contact_number, ), }; } if (filter.special_note) { where = { ...where, [Op.and]: Utils.ilike( 'new_tr_requests', 'special_note', filter.special_note, ), }; } if (filter.tr_no) { where = { ...where, [Op.and]: Utils.ilike('new_tr_requests', 'tr_no', filter.tr_no), }; } if (filter.dateRange) { const [start, end] = filter.dateRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, date: { ...where.date, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, date: { ...where.date, [Op.lte]: end, }, }; } } if (filter.timestampRange) { const [start, end] = filter.timestampRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, timestamp: { ...where.timestamp, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, timestamp: { ...where.timestamp, [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.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.new_tr_requests.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('new_tr_requests', 'tr_no', query), ], }; } const records = await db.new_tr_requests.findAll({ attributes: ['id', 'tr_no'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['tr_no', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.tr_no, })); } };