39548-vm/backend/src/db/api/assets.js
2026-04-11 03:52:51 +00:00

1330 lines
35 KiB
JavaScript

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 AssetsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const assets = await db.assets.create(
{
id: data.id || undefined,
asset_name: data.asset_name
||
null
,
municipality_uf: data.municipality_uf
||
null
,
holder: data.holder
||
null
,
registration_number: data.registration_number
||
null
,
appraisal_source: data.appraisal_source
||
null
,
total_area_m2: data.total_area_m2
||
null
,
total_area_ha: data.total_area_ha
||
null
,
topography: data.topography
||
null
,
shape: data.shape
||
null
,
vegetation: data.vegetation
||
null
,
current_occupation: data.current_occupation
||
null
,
urban_distance_band: data.urban_distance_band
||
null
,
logistics_distance_band: data.logistics_distance_band
||
null
,
biome_region: data.biome_region
||
null
,
zoning: data.zoning
||
null
,
main_access: data.main_access
||
null
,
ca_basic: data.ca_basic
||
null
,
ca_max: data.ca_max
||
null
,
occupancy_rate_pct: data.occupancy_rate_pct
||
null
,
permeability_rate_pct: data.permeability_rate_pct
||
null
,
height_limit_floors: data.height_limit_floors
||
null
,
height_limit_meters: data.height_limit_meters
||
null
,
setback_front_m: data.setback_front_m
||
null
,
setback_side_m: data.setback_side_m
||
null
,
setback_rear_m: data.setback_rear_m
||
null
,
setback_between_blocks_m: data.setback_between_blocks_m
||
null
,
infra_water: data.infra_water
||
false
,
infra_sewage: data.infra_sewage
||
false
,
infra_energy: data.infra_energy
||
false
,
infra_pavement: data.infra_pavement
||
false
,
infra_waste_collection: data.infra_waste_collection
||
false
,
infra_local_commerce: data.infra_local_commerce
||
false
,
infra_schools: data.infra_schools
||
false
,
infra_healthcare: data.infra_healthcare
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await assets.setClient( data.client || null, {
transaction,
});
await assets.setOrganizations( data.organizations || null, {
transaction,
});
return assets;
}
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 assetsData = data.map((item, index) => ({
id: item.id || undefined,
asset_name: item.asset_name
||
null
,
municipality_uf: item.municipality_uf
||
null
,
holder: item.holder
||
null
,
registration_number: item.registration_number
||
null
,
appraisal_source: item.appraisal_source
||
null
,
total_area_m2: item.total_area_m2
||
null
,
total_area_ha: item.total_area_ha
||
null
,
topography: item.topography
||
null
,
shape: item.shape
||
null
,
vegetation: item.vegetation
||
null
,
current_occupation: item.current_occupation
||
null
,
urban_distance_band: item.urban_distance_band
||
null
,
logistics_distance_band: item.logistics_distance_band
||
null
,
biome_region: item.biome_region
||
null
,
zoning: item.zoning
||
null
,
main_access: item.main_access
||
null
,
ca_basic: item.ca_basic
||
null
,
ca_max: item.ca_max
||
null
,
occupancy_rate_pct: item.occupancy_rate_pct
||
null
,
permeability_rate_pct: item.permeability_rate_pct
||
null
,
height_limit_floors: item.height_limit_floors
||
null
,
height_limit_meters: item.height_limit_meters
||
null
,
setback_front_m: item.setback_front_m
||
null
,
setback_side_m: item.setback_side_m
||
null
,
setback_rear_m: item.setback_rear_m
||
null
,
setback_between_blocks_m: item.setback_between_blocks_m
||
null
,
infra_water: item.infra_water
||
false
,
infra_sewage: item.infra_sewage
||
false
,
infra_energy: item.infra_energy
||
false
,
infra_pavement: item.infra_pavement
||
false
,
infra_waste_collection: item.infra_waste_collection
||
false
,
infra_local_commerce: item.infra_local_commerce
||
false
,
infra_schools: item.infra_schools
||
false
,
infra_healthcare: item.infra_healthcare
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const assets = await db.assets.bulkCreate(assetsData, { transaction });
// For each item created, replace relation files
return assets;
}
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 assets = await db.assets.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.asset_name !== undefined) updatePayload.asset_name = data.asset_name;
if (data.municipality_uf !== undefined) updatePayload.municipality_uf = data.municipality_uf;
if (data.holder !== undefined) updatePayload.holder = data.holder;
if (data.registration_number !== undefined) updatePayload.registration_number = data.registration_number;
if (data.appraisal_source !== undefined) updatePayload.appraisal_source = data.appraisal_source;
if (data.total_area_m2 !== undefined) updatePayload.total_area_m2 = data.total_area_m2;
if (data.total_area_ha !== undefined) updatePayload.total_area_ha = data.total_area_ha;
if (data.topography !== undefined) updatePayload.topography = data.topography;
if (data.shape !== undefined) updatePayload.shape = data.shape;
if (data.vegetation !== undefined) updatePayload.vegetation = data.vegetation;
if (data.current_occupation !== undefined) updatePayload.current_occupation = data.current_occupation;
if (data.urban_distance_band !== undefined) updatePayload.urban_distance_band = data.urban_distance_band;
if (data.logistics_distance_band !== undefined) updatePayload.logistics_distance_band = data.logistics_distance_band;
if (data.biome_region !== undefined) updatePayload.biome_region = data.biome_region;
if (data.zoning !== undefined) updatePayload.zoning = data.zoning;
if (data.main_access !== undefined) updatePayload.main_access = data.main_access;
if (data.ca_basic !== undefined) updatePayload.ca_basic = data.ca_basic;
if (data.ca_max !== undefined) updatePayload.ca_max = data.ca_max;
if (data.occupancy_rate_pct !== undefined) updatePayload.occupancy_rate_pct = data.occupancy_rate_pct;
if (data.permeability_rate_pct !== undefined) updatePayload.permeability_rate_pct = data.permeability_rate_pct;
if (data.height_limit_floors !== undefined) updatePayload.height_limit_floors = data.height_limit_floors;
if (data.height_limit_meters !== undefined) updatePayload.height_limit_meters = data.height_limit_meters;
if (data.setback_front_m !== undefined) updatePayload.setback_front_m = data.setback_front_m;
if (data.setback_side_m !== undefined) updatePayload.setback_side_m = data.setback_side_m;
if (data.setback_rear_m !== undefined) updatePayload.setback_rear_m = data.setback_rear_m;
if (data.setback_between_blocks_m !== undefined) updatePayload.setback_between_blocks_m = data.setback_between_blocks_m;
if (data.infra_water !== undefined) updatePayload.infra_water = data.infra_water;
if (data.infra_sewage !== undefined) updatePayload.infra_sewage = data.infra_sewage;
if (data.infra_energy !== undefined) updatePayload.infra_energy = data.infra_energy;
if (data.infra_pavement !== undefined) updatePayload.infra_pavement = data.infra_pavement;
if (data.infra_waste_collection !== undefined) updatePayload.infra_waste_collection = data.infra_waste_collection;
if (data.infra_local_commerce !== undefined) updatePayload.infra_local_commerce = data.infra_local_commerce;
if (data.infra_schools !== undefined) updatePayload.infra_schools = data.infra_schools;
if (data.infra_healthcare !== undefined) updatePayload.infra_healthcare = data.infra_healthcare;
updatePayload.updatedById = currentUser.id;
await assets.update(updatePayload, {transaction});
if (data.client !== undefined) {
await assets.setClient(
data.client,
{ transaction }
);
}
if (data.organizations !== undefined) {
await assets.setOrganizations(
data.organizations,
{ transaction }
);
}
return assets;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const assets = await db.assets.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of assets) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of assets) {
await record.destroy({transaction});
}
});
return assets;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const assets = await db.assets.findByPk(id, options);
await assets.update({
deletedBy: currentUser.id
}, {
transaction,
});
await assets.destroy({
transaction
});
return assets;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const assets = await db.assets.findOne(
{ where },
{ transaction },
);
if (!assets) {
return assets;
}
const output = assets.get({plain: true});
output.analyses_asset = await assets.getAnalyses_asset({
transaction
});
output.client = await assets.getClient({
transaction
});
output.organizations = await assets.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.clients,
as: 'client',
where: filter.client ? {
[Op.or]: [
{ id: { [Op.in]: filter.client.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.client.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.organizations,
as: 'organizations',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.asset_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'assets',
'asset_name',
filter.asset_name,
),
};
}
if (filter.municipality_uf) {
where = {
...where,
[Op.and]: Utils.ilike(
'assets',
'municipality_uf',
filter.municipality_uf,
),
};
}
if (filter.holder) {
where = {
...where,
[Op.and]: Utils.ilike(
'assets',
'holder',
filter.holder,
),
};
}
if (filter.registration_number) {
where = {
...where,
[Op.and]: Utils.ilike(
'assets',
'registration_number',
filter.registration_number,
),
};
}
if (filter.appraisal_source) {
where = {
...where,
[Op.and]: Utils.ilike(
'assets',
'appraisal_source',
filter.appraisal_source,
),
};
}
if (filter.total_area_m2Range) {
const [start, end] = filter.total_area_m2Range;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_area_m2: {
...where.total_area_m2,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_area_m2: {
...where.total_area_m2,
[Op.lte]: end,
},
};
}
}
if (filter.total_area_haRange) {
const [start, end] = filter.total_area_haRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_area_ha: {
...where.total_area_ha,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_area_ha: {
...where.total_area_ha,
[Op.lte]: end,
},
};
}
}
if (filter.ca_basicRange) {
const [start, end] = filter.ca_basicRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ca_basic: {
...where.ca_basic,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ca_basic: {
...where.ca_basic,
[Op.lte]: end,
},
};
}
}
if (filter.ca_maxRange) {
const [start, end] = filter.ca_maxRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ca_max: {
...where.ca_max,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ca_max: {
...where.ca_max,
[Op.lte]: end,
},
};
}
}
if (filter.occupancy_rate_pctRange) {
const [start, end] = filter.occupancy_rate_pctRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
occupancy_rate_pct: {
...where.occupancy_rate_pct,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
occupancy_rate_pct: {
...where.occupancy_rate_pct,
[Op.lte]: end,
},
};
}
}
if (filter.permeability_rate_pctRange) {
const [start, end] = filter.permeability_rate_pctRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
permeability_rate_pct: {
...where.permeability_rate_pct,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
permeability_rate_pct: {
...where.permeability_rate_pct,
[Op.lte]: end,
},
};
}
}
if (filter.height_limit_floorsRange) {
const [start, end] = filter.height_limit_floorsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
height_limit_floors: {
...where.height_limit_floors,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
height_limit_floors: {
...where.height_limit_floors,
[Op.lte]: end,
},
};
}
}
if (filter.height_limit_metersRange) {
const [start, end] = filter.height_limit_metersRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
height_limit_meters: {
...where.height_limit_meters,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
height_limit_meters: {
...where.height_limit_meters,
[Op.lte]: end,
},
};
}
}
if (filter.setback_front_mRange) {
const [start, end] = filter.setback_front_mRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
setback_front_m: {
...where.setback_front_m,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
setback_front_m: {
...where.setback_front_m,
[Op.lte]: end,
},
};
}
}
if (filter.setback_side_mRange) {
const [start, end] = filter.setback_side_mRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
setback_side_m: {
...where.setback_side_m,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
setback_side_m: {
...where.setback_side_m,
[Op.lte]: end,
},
};
}
}
if (filter.setback_rear_mRange) {
const [start, end] = filter.setback_rear_mRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
setback_rear_m: {
...where.setback_rear_m,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
setback_rear_m: {
...where.setback_rear_m,
[Op.lte]: end,
},
};
}
}
if (filter.setback_between_blocks_mRange) {
const [start, end] = filter.setback_between_blocks_mRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
setback_between_blocks_m: {
...where.setback_between_blocks_m,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
setback_between_blocks_m: {
...where.setback_between_blocks_m,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.topography) {
where = {
...where,
topography: filter.topography,
};
}
if (filter.shape) {
where = {
...where,
shape: filter.shape,
};
}
if (filter.vegetation) {
where = {
...where,
vegetation: filter.vegetation,
};
}
if (filter.current_occupation) {
where = {
...where,
current_occupation: filter.current_occupation,
};
}
if (filter.urban_distance_band) {
where = {
...where,
urban_distance_band: filter.urban_distance_band,
};
}
if (filter.logistics_distance_band) {
where = {
...where,
logistics_distance_band: filter.logistics_distance_band,
};
}
if (filter.biome_region) {
where = {
...where,
biome_region: filter.biome_region,
};
}
if (filter.zoning) {
where = {
...where,
zoning: filter.zoning,
};
}
if (filter.main_access) {
where = {
...where,
main_access: filter.main_access,
};
}
if (filter.infra_water) {
where = {
...where,
infra_water: filter.infra_water,
};
}
if (filter.infra_sewage) {
where = {
...where,
infra_sewage: filter.infra_sewage,
};
}
if (filter.infra_energy) {
where = {
...where,
infra_energy: filter.infra_energy,
};
}
if (filter.infra_pavement) {
where = {
...where,
infra_pavement: filter.infra_pavement,
};
}
if (filter.infra_waste_collection) {
where = {
...where,
infra_waste_collection: filter.infra_waste_collection,
};
}
if (filter.infra_local_commerce) {
where = {
...where,
infra_local_commerce: filter.infra_local_commerce,
};
}
if (filter.infra_schools) {
where = {
...where,
infra_schools: filter.infra_schools,
};
}
if (filter.infra_healthcare) {
where = {
...where,
infra_healthcare: filter.infra_healthcare,
};
}
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.assets.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(
'assets',
'asset_name',
query,
),
],
};
}
const records = await db.assets.findAll({
attributes: [ 'id', 'asset_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['asset_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.asset_name,
}));
}
};