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 Route_cardsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const route_cards = await db.route_cards.create( { id: data.id || undefined, route_card_number: data.route_card_number || null, status: data.status || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await route_cards.setSales_order(data.sales_order || null, { transaction, }); await route_cards.setOrganizations(data.organizations || null, { transaction, }); return route_cards; } 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 route_cardsData = data.map((item, index) => ({ id: item.id || undefined, route_card_number: item.route_card_number || null, status: item.status || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const route_cards = await db.route_cards.bulkCreate(route_cardsData, { transaction, }); // For each item created, replace relation files return route_cards; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const globalAccess = currentUser.app_role?.globalAccess; const route_cards = await db.route_cards.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.route_card_number !== undefined) updatePayload.route_card_number = data.route_card_number; if (data.status !== undefined) updatePayload.status = data.status; updatePayload.updatedById = currentUser.id; await route_cards.update(updatePayload, { transaction }); if (data.sales_order !== undefined) { await route_cards.setSales_order( data.sales_order, { transaction }, ); } if (data.organizations !== undefined) { await route_cards.setOrganizations( data.organizations, { transaction }, ); } return route_cards; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const route_cards = await db.route_cards.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of route_cards) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of route_cards) { await record.destroy({ transaction }); } }); return route_cards; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const route_cards = await db.route_cards.findByPk(id, options); await route_cards.update( { deletedBy: currentUser.id, }, { transaction, }, ); await route_cards.destroy({ transaction, }); return route_cards; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const route_cards = await db.route_cards.findOne( { where }, { transaction }, ); if (!route_cards) { return route_cards; } const output = route_cards.get({ plain: true }); output.quality_controls_route_card = await route_cards.getQuality_controls_route_card({ transaction, }); output.sales_order = await route_cards.getSales_order({ transaction, }); output.organizations = await route_cards.getOrganizations({ transaction, }); return output; } static async findAll(filter, globalAccess, options) { const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; const user = (options && options.currentUser) || null; const userOrganizations = (user && user.organizations?.id) || null; if (userOrganizations) { if (options?.currentUser?.organizationsId) { where.organizationsId = options.currentUser.organizationsId; } } offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.sales_orders, as: 'sales_order', where: filter.sales_order ? { [Op.or]: [ { id: { [Op.in]: filter.sales_order .split('|') .map((term) => Utils.uuid(term)), }, }, { order_number: { [Op.or]: filter.sales_order .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.organizations, as: 'organizations', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.route_card_number) { where = { ...where, [Op.and]: Utils.ilike( 'route_cards', 'route_card_number', filter.route_card_number, ), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.status) { where = { ...where, status: filter.status, }; } if (filter.organizations) { const listItems = filter.organizations.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, organizationsId: { [Op.or]: listItems }, }; } 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, }, }; } } } if (globalAccess) { delete where.organizationsId; } 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.route_cards.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, globalAccess, organizationId, ) { let where = {}; if (!globalAccess && organizationId) { where.organizationId = organizationId; } if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike('route_cards', 'route_card_number', query), ], }; } const records = await db.route_cards.findAll({ attributes: ['id', 'route_card_number'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['route_card_number', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.route_card_number, })); } };