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_plansDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const project_plans = await db.project_plans.create( { id: data.id || undefined, recommended_use: data.recommended_use || null, floors: data.floors || null, units_per_floor: data.units_per_floor || null, unit_sizes: data.unit_sizes || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await project_plans.setLand_project(data.land_project || null, { transaction, }); await project_plans.setOrganization(currentUser.organization.id || null, { transaction, }); await project_plans.setOrganizations(data.organizations || null, { transaction, }); return project_plans; } 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_plansData = data.map((item, index) => ({ id: item.id || undefined, recommended_use: item.recommended_use || null, floors: item.floors || null, units_per_floor: item.units_per_floor || null, unit_sizes: item.unit_sizes || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const project_plans = await db.project_plans.bulkCreate(project_plansData, { transaction, }); // For each item created, replace relation files return project_plans; } 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 project_plans = await db.project_plans.findByPk( id, {}, { transaction }, ); const updatePayload = {}; if (data.recommended_use !== undefined) updatePayload.recommended_use = data.recommended_use; if (data.floors !== undefined) updatePayload.floors = data.floors; if (data.units_per_floor !== undefined) updatePayload.units_per_floor = data.units_per_floor; if (data.unit_sizes !== undefined) updatePayload.unit_sizes = data.unit_sizes; updatePayload.updatedById = currentUser.id; await project_plans.update(updatePayload, { transaction }); if (data.land_project !== undefined) { await project_plans.setLand_project( data.land_project, { transaction }, ); } if (data.organization !== undefined) { await project_plans.setOrganization( globalAccess ? data.organization : currentUser.organization.id, { transaction }, ); } if (data.organizations !== undefined) { await project_plans.setOrganizations( data.organizations, { transaction }, ); } return project_plans; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const project_plans = await db.project_plans.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of project_plans) { await record.update({ deletedBy: currentUser.id }, { transaction }); } for (const record of project_plans) { await record.destroy({ transaction }); } }); return project_plans; } static async remove(id, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const project_plans = await db.project_plans.findByPk(id, options); await project_plans.update( { deletedBy: currentUser.id, }, { transaction, }, ); await project_plans.destroy({ transaction, }); return project_plans; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const project_plans = await db.project_plans.findOne( { where }, { transaction }, ); if (!project_plans) { return project_plans; } const output = project_plans.get({ plain: true }); output.land_project = await project_plans.getLand_project({ transaction, }); output.organization = await project_plans.getOrganization({ transaction, }); output.organizations = await project_plans.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.land_projects, as: 'land_project', where: filter.land_project ? { [Op.or]: [ { id: { [Op.in]: filter.land_project .split('|') .map((term) => Utils.uuid(term)), }, }, { location: { [Op.or]: filter.land_project .split('|') .map((term) => ({ [Op.iLike]: `%${term}%` })), }, }, ], } : {}, }, { model: db.organizations, as: 'organization', }, { model: db.organizations, as: 'organizations', }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.recommended_use) { where = { ...where, [Op.and]: Utils.ilike( 'project_plans', 'recommended_use', filter.recommended_use, ), }; } if (filter.unit_sizes) { where = { ...where, [Op.and]: Utils.ilike( 'project_plans', 'unit_sizes', filter.unit_sizes, ), }; } if (filter.floorsRange) { const [start, end] = filter.floorsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, floors: { ...where.floors, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, floors: { ...where.floors, [Op.lte]: end, }, }; } } if (filter.units_per_floorRange) { const [start, end] = filter.units_per_floorRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, units_per_floor: { ...where.units_per_floor, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, units_per_floor: { ...where.units_per_floor, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true', }; } if (filter.organization) { const listItems = filter.organization.split('|').map((item) => { return Utils.uuid(item); }); where = { ...where, organizationId: { [Op.or]: listItems }, }; } 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.project_plans.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('project_plans', 'recommended_use', query), ], }; } const records = await db.project_plans.findAll({ attributes: ['id', 'recommended_use'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['recommended_use', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.recommended_use, })); } };