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 Maintenance_reportsDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const maintenance_reports = await db.maintenance_reports.create( { id: data.id || undefined, reported_at: data.reported_at || null, description: data.description || null, severity: data.severity || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await maintenance_reports.setDrone(data.drone || null, { transaction, }); await maintenance_reports.setReported_by(data.reported_by || null, { transaction, }); return maintenance_reports; } 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 maintenance_reportsData = data.map((item, index) => ({ id: item.id || undefined, reported_at: item.reported_at || null, description: item.description || null, severity: item.severity || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const maintenance_reports = await db.maintenance_reports.bulkCreate( maintenance_reportsData, { transaction }, ); // For each item created, replace relation files return maintenance_reports; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const maintenance_reports = await db.maintenance_reports.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.reported_at !== undefined) updatePayload.reported_at = data.reported_at; if (data.description !== undefined) updatePayload.description = data.description; if (data.severity !== undefined) updatePayload.severity = data.severity; updatePayload.updatedById = currentUser.id; await maintenance_reports.update(updatePayload, { transaction }); if (data.drone !== undefined) { await maintenance_reports.setDrone( data.drone, { transaction }, ); } if (data.reported_by !== undefined) { await maintenance_reports.setReported_by( data.reported_by, { transaction }, ); } return maintenance_reports; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const maintenance_reports = await db.maintenance_reports.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of maintenance_reports) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of maintenance_reports) { await record.destroy({ transaction }); } }); return maintenance_reports; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const maintenance_reports = await db.maintenance_reports.findByPk( id, options, ); await maintenance_reports.update( { deletedBy: currentUser.id, }, { transaction, }, ); await maintenance_reports.destroy({ transaction, }); return maintenance_reports; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const maintenance_reports = await db.maintenance_reports.findOne( { where }, { transaction }, ); if (!maintenance_reports) { return maintenance_reports; } const output = maintenance_reports.get({ plain: true }); output.drone = await maintenance_reports.getDrone({ transaction, }); output.reported_by = await maintenance_reports.getReported_by({ 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.drones, as: 'drone', where: filter.drone ? { [Op.or]: [ { id: { [Op.in]: filter.drone .split('|') .map((term) => Utils.uuid(term)), }, }, { serial_number: { [Op.or]: filter.drone .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.users, as: 'reported_by', where: filter.reported_by ? { [Op.or]: [ { id: { [Op.in]: filter.reported_by .split('|') .map((term) => Utils.uuid(term)), }, }, { firstName: { [Op.or]: filter.reported_by .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.description) { where = { ...where, [Op.and]: Utils.ilike( 'maintenance_reports', 'description', filter.description, ), }; } if (filter.reported_atRange) { const [start, end] = filter.reported_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, reported_at: { ...where.reported_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, reported_at: { ...where.reported_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.severity) { where = { ...where, severity: filter.severity, }; } 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.maintenance_reports.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('maintenance_reports', 'description', query), ], }; } const records = await db.maintenance_reports.findAll({ attributes: ['id', 'description'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['description', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.description, })); } };