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 Site_tasksDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const site_tasks = await db.site_tasks.create( { id: data.id || undefined, task_name: data.task_name || null , notes: data.notes || null , planned_start_at: data.planned_start_at || null , planned_end_at: data.planned_end_at || null , actual_start_at: data.actual_start_at || null , actual_end_at: data.actual_end_at || null , planned_quantity: data.planned_quantity || null , actual_quantity: data.actual_quantity || null , planned_cost: data.planned_cost || null , actual_cost: data.actual_cost || null , status: data.status || null , progress_percent: data.progress_percent || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await site_tasks.setSite( data.site || null, { transaction, }); await site_tasks.setWork_package( data.work_package || null, { transaction, }); await site_tasks.setAssigned_to( data.assigned_to || null, { transaction, }); return site_tasks; } 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 site_tasksData = data.map((item, index) => ({ id: item.id || undefined, task_name: item.task_name || null , notes: item.notes || null , planned_start_at: item.planned_start_at || null , planned_end_at: item.planned_end_at || null , actual_start_at: item.actual_start_at || null , actual_end_at: item.actual_end_at || null , planned_quantity: item.planned_quantity || null , actual_quantity: item.actual_quantity || null , planned_cost: item.planned_cost || null , actual_cost: item.actual_cost || null , status: item.status || null , progress_percent: item.progress_percent || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const site_tasks = await db.site_tasks.bulkCreate(site_tasksData, { transaction }); // For each item created, replace relation files return site_tasks; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const site_tasks = await db.site_tasks.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.task_name !== undefined) updatePayload.task_name = data.task_name; if (data.notes !== undefined) updatePayload.notes = data.notes; if (data.planned_start_at !== undefined) updatePayload.planned_start_at = data.planned_start_at; if (data.planned_end_at !== undefined) updatePayload.planned_end_at = data.planned_end_at; if (data.actual_start_at !== undefined) updatePayload.actual_start_at = data.actual_start_at; if (data.actual_end_at !== undefined) updatePayload.actual_end_at = data.actual_end_at; if (data.planned_quantity !== undefined) updatePayload.planned_quantity = data.planned_quantity; if (data.actual_quantity !== undefined) updatePayload.actual_quantity = data.actual_quantity; if (data.planned_cost !== undefined) updatePayload.planned_cost = data.planned_cost; if (data.actual_cost !== undefined) updatePayload.actual_cost = data.actual_cost; if (data.status !== undefined) updatePayload.status = data.status; if (data.progress_percent !== undefined) updatePayload.progress_percent = data.progress_percent; updatePayload.updatedById = currentUser.id; await site_tasks.update(updatePayload, {transaction}); if (data.site !== undefined) { await site_tasks.setSite( data.site, { transaction } ); } if (data.work_package !== undefined) { await site_tasks.setWork_package( data.work_package, { transaction } ); } if (data.assigned_to !== undefined) { await site_tasks.setAssigned_to( data.assigned_to, { transaction } ); } return site_tasks; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const site_tasks = await db.site_tasks.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of site_tasks) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of site_tasks) { await record.destroy({transaction}); } }); return site_tasks; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const site_tasks = await db.site_tasks.findByPk(id, options); await site_tasks.update({ deletedBy: currentUser.id }, { transaction, }); await site_tasks.destroy({ transaction }); return site_tasks; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const site_tasks = await db.site_tasks.findOne( { where }, { transaction }, ); if (!site_tasks) { return site_tasks; } const output = site_tasks.get({plain: true}); output.progress_updates_site_task = await site_tasks.getProgress_updates_site_task({ transaction }); output.qc_inspections_site_task = await site_tasks.getQc_inspections_site_task({ transaction }); output.issues_site_task = await site_tasks.getIssues_site_task({ transaction }); output.site = await site_tasks.getSite({ transaction }); output.work_package = await site_tasks.getWork_package({ transaction }); output.assigned_to = await site_tasks.getAssigned_to({ 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.sites, as: 'site', where: filter.site ? { [Op.or]: [ { id: { [Op.in]: filter.site.split('|').map(term => Utils.uuid(term)) } }, { site_name: { [Op.or]: filter.site.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.work_packages, as: 'work_package', where: filter.work_package ? { [Op.or]: [ { id: { [Op.in]: filter.work_package.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.work_package.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.users, as: 'assigned_to', where: filter.assigned_to ? { [Op.or]: [ { id: { [Op.in]: filter.assigned_to.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.assigned_to.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.task_name) { where = { ...where, [Op.and]: Utils.ilike( 'site_tasks', 'task_name', filter.task_name, ), }; } if (filter.notes) { where = { ...where, [Op.and]: Utils.ilike( 'site_tasks', 'notes', filter.notes, ), }; } if (filter.planned_start_atRange) { const [start, end] = filter.planned_start_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, planned_start_at: { ...where.planned_start_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, planned_start_at: { ...where.planned_start_at, [Op.lte]: end, }, }; } } if (filter.planned_end_atRange) { const [start, end] = filter.planned_end_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, planned_end_at: { ...where.planned_end_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, planned_end_at: { ...where.planned_end_at, [Op.lte]: end, }, }; } } if (filter.actual_start_atRange) { const [start, end] = filter.actual_start_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, actual_start_at: { ...where.actual_start_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, actual_start_at: { ...where.actual_start_at, [Op.lte]: end, }, }; } } if (filter.actual_end_atRange) { const [start, end] = filter.actual_end_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, actual_end_at: { ...where.actual_end_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, actual_end_at: { ...where.actual_end_at, [Op.lte]: end, }, }; } } if (filter.planned_quantityRange) { const [start, end] = filter.planned_quantityRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, planned_quantity: { ...where.planned_quantity, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, planned_quantity: { ...where.planned_quantity, [Op.lte]: end, }, }; } } if (filter.actual_quantityRange) { const [start, end] = filter.actual_quantityRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, actual_quantity: { ...where.actual_quantity, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, actual_quantity: { ...where.actual_quantity, [Op.lte]: end, }, }; } } if (filter.planned_costRange) { const [start, end] = filter.planned_costRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, planned_cost: { ...where.planned_cost, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, planned_cost: { ...where.planned_cost, [Op.lte]: end, }, }; } } if (filter.actual_costRange) { const [start, end] = filter.actual_costRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, actual_cost: { ...where.actual_cost, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, actual_cost: { ...where.actual_cost, [Op.lte]: end, }, }; } } if (filter.progress_percentRange) { const [start, end] = filter.progress_percentRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, progress_percent: { ...where.progress_percent, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, progress_percent: { ...where.progress_percent, [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.site_tasks.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( 'site_tasks', 'task_name', query, ), ], }; } const records = await db.site_tasks.findAll({ attributes: [ 'id', 'task_name' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['task_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.task_name, })); } };