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 Project_modulesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const project_modules = await db.project_modules.create( { id: data.id || undefined, module_type: data.module_type || null , status: data.status || null , notes: data.notes || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await project_modules.setProject( data.project || null, { transaction, }); return project_modules; } 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 project_modulesData = data.map((item, index) => ({ id: item.id || undefined, module_type: item.module_type || null , status: item.status || null , notes: item.notes || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const project_modules = await db.project_modules.bulkCreate(project_modulesData, { transaction }); // For each item created, replace relation files return project_modules; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const project_modules = await db.project_modules.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.module_type !== undefined) updatePayload.module_type = data.module_type; if (data.status !== undefined) updatePayload.status = data.status; if (data.notes !== undefined) updatePayload.notes = data.notes; updatePayload.updatedById = currentUser.id; await project_modules.update(updatePayload, {transaction}); if (data.project !== undefined) { await project_modules.setProject( data.project, { transaction } ); } return project_modules; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const project_modules = await db.project_modules.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of project_modules) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of project_modules) { await record.destroy({transaction}); } }); return project_modules; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const project_modules = await db.project_modules.findByPk(id, options); await project_modules.update({ deletedBy: currentUser.id }, { transaction, }); await project_modules.destroy({ transaction }); return project_modules; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const project_modules = await db.project_modules.findOne( { where }, { transaction }, ); if (!project_modules) { return project_modules; } const output = project_modules.get({plain: true}); output.project = await project_modules.getProject({ 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.projects, as: 'project', where: filter.project ? { [Op.or]: [ { id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.notes) { where = { ...where, [Op.and]: Utils.ilike( 'project_modules', 'notes', filter.notes, ), }; } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.module_type) { where = { ...where, module_type: filter.module_type, }; } 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.project_modules.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( 'project_modules', 'module_type', query, ), ], }; } const records = await db.project_modules.findAll({ attributes: [ 'id', 'module_type' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['module_type', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.module_type, })); } };