457 lines
11 KiB
JavaScript
457 lines
11 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 HydrantsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const hydrants = await db.hydrants.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
hydrant_id: data.hydrant_id || null,
|
|
location: data.location || null,
|
|
type: data.type || null,
|
|
initial_condition: data.initial_condition || null,
|
|
installation_date: data.installation_date || null,
|
|
notes: data.notes || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await hydrants.setProject(data.project || null, {
|
|
transaction,
|
|
});
|
|
|
|
await hydrants.setProjects(data.projects || null, {
|
|
transaction,
|
|
});
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.hydrants.getTableName(),
|
|
belongsToColumn: 'photos',
|
|
belongsToId: hydrants.id,
|
|
},
|
|
data.photos,
|
|
options,
|
|
);
|
|
|
|
return hydrants;
|
|
}
|
|
|
|
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 hydrantsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
hydrant_id: item.hydrant_id || null,
|
|
location: item.location || null,
|
|
type: item.type || null,
|
|
initial_condition: item.initial_condition || null,
|
|
installation_date: item.installation_date || 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 hydrants = await db.hydrants.bulkCreate(hydrantsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < hydrants.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.hydrants.getTableName(),
|
|
belongsToColumn: 'photos',
|
|
belongsToId: hydrants[i].id,
|
|
},
|
|
data[i].photos,
|
|
options,
|
|
);
|
|
}
|
|
|
|
return hydrants;
|
|
}
|
|
|
|
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 hydrants = await db.hydrants.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.hydrant_id !== undefined)
|
|
updatePayload.hydrant_id = data.hydrant_id;
|
|
|
|
if (data.location !== undefined) updatePayload.location = data.location;
|
|
|
|
if (data.type !== undefined) updatePayload.type = data.type;
|
|
|
|
if (data.initial_condition !== undefined)
|
|
updatePayload.initial_condition = data.initial_condition;
|
|
|
|
if (data.installation_date !== undefined)
|
|
updatePayload.installation_date = data.installation_date;
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await hydrants.update(updatePayload, { transaction });
|
|
|
|
if (data.project !== undefined) {
|
|
await hydrants.setProject(
|
|
data.project,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.projects !== undefined) {
|
|
await hydrants.setProjects(
|
|
data.projects,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.hydrants.getTableName(),
|
|
belongsToColumn: 'photos',
|
|
belongsToId: hydrants.id,
|
|
},
|
|
data.photos,
|
|
options,
|
|
);
|
|
|
|
return hydrants;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const hydrants = await db.hydrants.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of hydrants) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of hydrants) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return hydrants;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const hydrants = await db.hydrants.findByPk(id, options);
|
|
|
|
await hydrants.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await hydrants.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return hydrants;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const hydrants = await db.hydrants.findOne({ where }, { transaction });
|
|
|
|
if (!hydrants) {
|
|
return hydrants;
|
|
}
|
|
|
|
const output = hydrants.get({ plain: true });
|
|
|
|
output.inspections_hydrant = await hydrants.getInspections_hydrant({
|
|
transaction,
|
|
});
|
|
|
|
output.photos = await hydrants.getPhotos({
|
|
transaction,
|
|
});
|
|
|
|
output.project = await hydrants.getProject({
|
|
transaction,
|
|
});
|
|
|
|
output.projects = await hydrants.getProjects({
|
|
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 userProjects = (user && user.projects?.id) || null;
|
|
|
|
if (userProjects) {
|
|
if (options?.currentUser?.projectsId) {
|
|
where.projectsId = options.currentUser.projectsId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.projects,
|
|
as: 'project',
|
|
},
|
|
|
|
{
|
|
model: db.projects,
|
|
as: 'projects',
|
|
},
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'photos',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.hydrant_id) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('hydrants', 'hydrant_id', filter.hydrant_id),
|
|
};
|
|
}
|
|
|
|
if (filter.location) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('hydrants', 'location', filter.location),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('hydrants', 'notes', filter.notes),
|
|
};
|
|
}
|
|
|
|
if (filter.installation_dateRange) {
|
|
const [start, end] = filter.installation_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
installation_date: {
|
|
...where.installation_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
installation_date: {
|
|
...where.installation_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.type) {
|
|
where = {
|
|
...where,
|
|
type: filter.type,
|
|
};
|
|
}
|
|
|
|
if (filter.initial_condition) {
|
|
where = {
|
|
...where,
|
|
initial_condition: filter.initial_condition,
|
|
};
|
|
}
|
|
|
|
if (filter.project) {
|
|
const listItems = filter.project.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
projectId: { [Op.or]: listItems },
|
|
};
|
|
}
|
|
|
|
if (filter.projects) {
|
|
const listItems = filter.projects.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
projectsId: { [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.projectsId;
|
|
}
|
|
|
|
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.hydrants.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('hydrants', 'hydrant_id', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.hydrants.findAll({
|
|
attributes: ['id', 'hydrant_id'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['hydrant_id', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.hydrant_id,
|
|
}));
|
|
}
|
|
};
|