39076-vm/backend/src/db/api/recovery_centers.js
2026-03-10 07:34:42 +00:00

630 lines
16 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 Recovery_centersDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const recovery_centers = await db.recovery_centers.create(
{
id: data.id || undefined,
center_name: data.center_name
||
null
,
opening_hours: data.opening_hours
||
null
,
contact_phone: data.contact_phone
||
null
,
contact_email: data.contact_email
||
null
,
center_status: data.center_status
||
null
,
accepts_documents: data.accepts_documents
||
false
,
accepts_devices: data.accepts_devices
||
false
,
accepts_vehicles: data.accepts_vehicles
||
false
,
instructions: data.instructions
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await recovery_centers.setOrganization(currentUser.organization.id || null, {
transaction,
});
await recovery_centers.setLocation( data.location || null, {
transaction,
});
return recovery_centers;
}
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 recovery_centersData = data.map((item, index) => ({
id: item.id || undefined,
center_name: item.center_name
||
null
,
opening_hours: item.opening_hours
||
null
,
contact_phone: item.contact_phone
||
null
,
contact_email: item.contact_email
||
null
,
center_status: item.center_status
||
null
,
accepts_documents: item.accepts_documents
||
false
,
accepts_devices: item.accepts_devices
||
false
,
accepts_vehicles: item.accepts_vehicles
||
false
,
instructions: item.instructions
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const recovery_centers = await db.recovery_centers.bulkCreate(recovery_centersData, { transaction });
// For each item created, replace relation files
return recovery_centers;
}
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 recovery_centers = await db.recovery_centers.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.center_name !== undefined) updatePayload.center_name = data.center_name;
if (data.opening_hours !== undefined) updatePayload.opening_hours = data.opening_hours;
if (data.contact_phone !== undefined) updatePayload.contact_phone = data.contact_phone;
if (data.contact_email !== undefined) updatePayload.contact_email = data.contact_email;
if (data.center_status !== undefined) updatePayload.center_status = data.center_status;
if (data.accepts_documents !== undefined) updatePayload.accepts_documents = data.accepts_documents;
if (data.accepts_devices !== undefined) updatePayload.accepts_devices = data.accepts_devices;
if (data.accepts_vehicles !== undefined) updatePayload.accepts_vehicles = data.accepts_vehicles;
if (data.instructions !== undefined) updatePayload.instructions = data.instructions;
updatePayload.updatedById = currentUser.id;
await recovery_centers.update(updatePayload, {transaction});
if (data.organization !== undefined) {
await recovery_centers.setOrganization(
(globalAccess ? data.organization : currentUser.organization.id),
{ transaction }
);
}
if (data.location !== undefined) {
await recovery_centers.setLocation(
data.location,
{ transaction }
);
}
return recovery_centers;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const recovery_centers = await db.recovery_centers.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of recovery_centers) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of recovery_centers) {
await record.destroy({transaction});
}
});
return recovery_centers;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const recovery_centers = await db.recovery_centers.findByPk(id, options);
await recovery_centers.update({
deletedBy: currentUser.id
}, {
transaction,
});
await recovery_centers.destroy({
transaction
});
return recovery_centers;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const recovery_centers = await db.recovery_centers.findOne(
{ where },
{ transaction },
);
if (!recovery_centers) {
return recovery_centers;
}
const output = recovery_centers.get({plain: true});
output.reports_assigned_recovery_center = await recovery_centers.getReports_assigned_recovery_center({
transaction
});
output.recoveries_center = await recovery_centers.getRecoveries_center({
transaction
});
output.organization = await recovery_centers.getOrganization({
transaction
});
output.location = await recovery_centers.getLocation({
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.organizations,
as: 'organization',
},
{
model: db.locations,
as: 'location',
where: filter.location ? {
[Op.or]: [
{ id: { [Op.in]: filter.location.split('|').map(term => Utils.uuid(term)) } },
{
label: {
[Op.or]: filter.location.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.center_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'recovery_centers',
'center_name',
filter.center_name,
),
};
}
if (filter.opening_hours) {
where = {
...where,
[Op.and]: Utils.ilike(
'recovery_centers',
'opening_hours',
filter.opening_hours,
),
};
}
if (filter.contact_phone) {
where = {
...where,
[Op.and]: Utils.ilike(
'recovery_centers',
'contact_phone',
filter.contact_phone,
),
};
}
if (filter.contact_email) {
where = {
...where,
[Op.and]: Utils.ilike(
'recovery_centers',
'contact_email',
filter.contact_email,
),
};
}
if (filter.instructions) {
where = {
...where,
[Op.and]: Utils.ilike(
'recovery_centers',
'instructions',
filter.instructions,
),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.center_status) {
where = {
...where,
center_status: filter.center_status,
};
}
if (filter.accepts_documents) {
where = {
...where,
accepts_documents: filter.accepts_documents,
};
}
if (filter.accepts_devices) {
where = {
...where,
accepts_devices: filter.accepts_devices,
};
}
if (filter.accepts_vehicles) {
where = {
...where,
accepts_vehicles: filter.accepts_vehicles,
};
}
if (filter.organization) {
const listItems = filter.organization.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
organizationId: {[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.recovery_centers.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(
'recovery_centers',
'center_name',
query,
),
],
};
}
const records = await db.recovery_centers.findAll({
attributes: [ 'id', 'center_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['center_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.center_name,
}));
}
};