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 BodyDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const body = await db.body.create( { id: data.id || undefined, doors: data.doors || null, seats: data.seats || null, length_mm: data.length_mm || null, width_mm: data.width_mm || null, height_mm: data.height_mm || null, wheelbase_mm: data.wheelbase_mm || null, trunk_volume_l: data.trunk_volume_l || null, fuel_tank_l: data.fuel_tank_l || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); return body; } 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 bodyData = data.map((item, index) => ({ id: item.id || undefined, doors: item.doors || null, seats: item.seats || null, length_mm: item.length_mm || null, width_mm: item.width_mm || null, height_mm: item.height_mm || null, wheelbase_mm: item.wheelbase_mm || null, trunk_volume_l: item.trunk_volume_l || null, fuel_tank_l: item.fuel_tank_l || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const body = await db.body.bulkCreate(bodyData, { transaction }); // For each item created, replace relation files return body; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const body = await db.body.findByPk(id, {}, { transaction }); const updatePayload = {}; if (data.doors !== undefined) updatePayload.doors = data.doors; if (data.seats !== undefined) updatePayload.seats = data.seats; if (data.length_mm !== undefined) updatePayload.length_mm = data.length_mm; if (data.width_mm !== undefined) updatePayload.width_mm = data.width_mm; if (data.height_mm !== undefined) updatePayload.height_mm = data.height_mm; if (data.wheelbase_mm !== undefined) updatePayload.wheelbase_mm = data.wheelbase_mm; if (data.trunk_volume_l !== undefined) updatePayload.trunk_volume_l = data.trunk_volume_l; if (data.fuel_tank_l !== undefined) updatePayload.fuel_tank_l = data.fuel_tank_l; updatePayload.updatedById = currentUser.id; await body.update(updatePayload, { transaction }); return body; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const body = await db.body.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of body) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of body) { await record.destroy({ transaction }); } }); return body; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const body = await db.body.findByPk(id, options); await body.update( { deletedBy: currentUser.id, }, { transaction, }, ); await body.destroy({ transaction, }); return body; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const body = await db.body.findOne({ where }, { transaction }); if (!body) { return body; } const output = body.get({ plain: true }); 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 = []; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.doorsRange) { const [start, end] = filter.doorsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, doors: { ...where.doors, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, doors: { ...where.doors, [Op.lte]: end, }, }; } } if (filter.seatsRange) { const [start, end] = filter.seatsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, seats: { ...where.seats, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, seats: { ...where.seats, [Op.lte]: end, }, }; } } if (filter.length_mmRange) { const [start, end] = filter.length_mmRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, length_mm: { ...where.length_mm, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, length_mm: { ...where.length_mm, [Op.lte]: end, }, }; } } if (filter.width_mmRange) { const [start, end] = filter.width_mmRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, width_mm: { ...where.width_mm, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, width_mm: { ...where.width_mm, [Op.lte]: end, }, }; } } if (filter.height_mmRange) { const [start, end] = filter.height_mmRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, height_mm: { ...where.height_mm, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, height_mm: { ...where.height_mm, [Op.lte]: end, }, }; } } if (filter.wheelbase_mmRange) { const [start, end] = filter.wheelbase_mmRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, wheelbase_mm: { ...where.wheelbase_mm, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, wheelbase_mm: { ...where.wheelbase_mm, [Op.lte]: end, }, }; } } if (filter.trunk_volume_lRange) { const [start, end] = filter.trunk_volume_lRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, trunk_volume_l: { ...where.trunk_volume_l, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, trunk_volume_l: { ...where.trunk_volume_l, [Op.lte]: end, }, }; } } if (filter.fuel_tank_lRange) { const [start, end] = filter.fuel_tank_lRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, fuel_tank_l: { ...where.fuel_tank_l, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, fuel_tank_l: { ...where.fuel_tank_l, [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.body.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('body', 'doors', query), ], }; } const records = await db.body.findAll({ attributes: ['id', 'doors'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['doors', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.doors, })); } };