376 lines
9.1 KiB
JavaScript
376 lines
9.1 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 Emergency_alertsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const emergency_alerts = await db.emergency_alerts.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
alert_time: data.alert_time || null,
|
|
location: data.location || null,
|
|
notified_cab: data.notified_cab || false,
|
|
|
|
notified_hospital: data.notified_hospital || false,
|
|
|
|
notified_police: data.notified_police || false,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await emergency_alerts.setPatient(data.patient || null, {
|
|
transaction,
|
|
});
|
|
|
|
return emergency_alerts;
|
|
}
|
|
|
|
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 emergency_alertsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
alert_time: item.alert_time || null,
|
|
location: item.location || null,
|
|
notified_cab: item.notified_cab || false,
|
|
|
|
notified_hospital: item.notified_hospital || false,
|
|
|
|
notified_police: item.notified_police || false,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const emergency_alerts = await db.emergency_alerts.bulkCreate(
|
|
emergency_alertsData,
|
|
{ transaction },
|
|
);
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return emergency_alerts;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const emergency_alerts = await db.emergency_alerts.findByPk(
|
|
id,
|
|
{},
|
|
{ transaction },
|
|
);
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.alert_time !== undefined)
|
|
updatePayload.alert_time = data.alert_time;
|
|
|
|
if (data.location !== undefined) updatePayload.location = data.location;
|
|
|
|
if (data.notified_cab !== undefined)
|
|
updatePayload.notified_cab = data.notified_cab;
|
|
|
|
if (data.notified_hospital !== undefined)
|
|
updatePayload.notified_hospital = data.notified_hospital;
|
|
|
|
if (data.notified_police !== undefined)
|
|
updatePayload.notified_police = data.notified_police;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await emergency_alerts.update(updatePayload, { transaction });
|
|
|
|
if (data.patient !== undefined) {
|
|
await emergency_alerts.setPatient(
|
|
data.patient,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return emergency_alerts;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const emergency_alerts = await db.emergency_alerts.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of emergency_alerts) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of emergency_alerts) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return emergency_alerts;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const emergency_alerts = await db.emergency_alerts.findByPk(id, options);
|
|
|
|
await emergency_alerts.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await emergency_alerts.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return emergency_alerts;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const emergency_alerts = await db.emergency_alerts.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!emergency_alerts) {
|
|
return emergency_alerts;
|
|
}
|
|
|
|
const output = emergency_alerts.get({ plain: true });
|
|
|
|
output.patient = await emergency_alerts.getPatient({
|
|
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.patients,
|
|
as: 'patient',
|
|
|
|
where: filter.patient
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.patient
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.patient
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.location) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'emergency_alerts',
|
|
'location',
|
|
filter.location,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.alert_timeRange) {
|
|
const [start, end] = filter.alert_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
alert_time: {
|
|
...where.alert_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
alert_time: {
|
|
...where.alert_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.notified_cab) {
|
|
where = {
|
|
...where,
|
|
notified_cab: filter.notified_cab,
|
|
};
|
|
}
|
|
|
|
if (filter.notified_hospital) {
|
|
where = {
|
|
...where,
|
|
notified_hospital: filter.notified_hospital,
|
|
};
|
|
}
|
|
|
|
if (filter.notified_police) {
|
|
where = {
|
|
...where,
|
|
notified_police: filter.notified_police,
|
|
};
|
|
}
|
|
|
|
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.emergency_alerts.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('emergency_alerts', 'location', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.emergency_alerts.findAll({
|
|
attributes: ['id', 'location'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['location', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.location,
|
|
}));
|
|
}
|
|
};
|