774 lines
19 KiB
JavaScript
774 lines
19 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 VisitsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const visits = await db.visits.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
reference_code: data.reference_code
|
|
||
|
|
null
|
|
,
|
|
|
|
created_ts: data.created_ts
|
|
||
|
|
null
|
|
,
|
|
|
|
checkin_time: data.checkin_time
|
|
||
|
|
null
|
|
,
|
|
|
|
estimated_call_time: data.estimated_call_time
|
|
||
|
|
null
|
|
,
|
|
|
|
symptom_text: data.symptom_text
|
|
||
|
|
null
|
|
,
|
|
|
|
triage_level: data.triage_level
|
|
||
|
|
null
|
|
,
|
|
|
|
priority_score: data.priority_score
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
ai_fallback: data.ai_fallback
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await visits.setPatient( data.patient || null, {
|
|
transaction,
|
|
});
|
|
|
|
await visits.setAssigned_staff( data.assigned_staff || null, {
|
|
transaction,
|
|
});
|
|
|
|
await visits.setRegions( data.regions || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
await visits.setReported_symptoms(data.reported_symptoms || [], {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
return visits;
|
|
}
|
|
|
|
|
|
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 visitsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
reference_code: item.reference_code
|
|
||
|
|
null
|
|
,
|
|
|
|
created_ts: item.created_ts
|
|
||
|
|
null
|
|
,
|
|
|
|
checkin_time: item.checkin_time
|
|
||
|
|
null
|
|
,
|
|
|
|
estimated_call_time: item.estimated_call_time
|
|
||
|
|
null
|
|
,
|
|
|
|
symptom_text: item.symptom_text
|
|
||
|
|
null
|
|
,
|
|
|
|
triage_level: item.triage_level
|
|
||
|
|
null
|
|
,
|
|
|
|
priority_score: item.priority_score
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
ai_fallback: item.ai_fallback
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const visits = await db.visits.bulkCreate(visitsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return visits;
|
|
}
|
|
|
|
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 visits = await db.visits.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.reference_code !== undefined) updatePayload.reference_code = data.reference_code;
|
|
|
|
|
|
if (data.created_ts !== undefined) updatePayload.created_ts = data.created_ts;
|
|
|
|
|
|
if (data.checkin_time !== undefined) updatePayload.checkin_time = data.checkin_time;
|
|
|
|
|
|
if (data.estimated_call_time !== undefined) updatePayload.estimated_call_time = data.estimated_call_time;
|
|
|
|
|
|
if (data.symptom_text !== undefined) updatePayload.symptom_text = data.symptom_text;
|
|
|
|
|
|
if (data.triage_level !== undefined) updatePayload.triage_level = data.triage_level;
|
|
|
|
|
|
if (data.priority_score !== undefined) updatePayload.priority_score = data.priority_score;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.ai_fallback !== undefined) updatePayload.ai_fallback = data.ai_fallback;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await visits.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.patient !== undefined) {
|
|
await visits.setPatient(
|
|
|
|
data.patient,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.assigned_staff !== undefined) {
|
|
await visits.setAssigned_staff(
|
|
|
|
data.assigned_staff,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.regions !== undefined) {
|
|
await visits.setRegions(
|
|
|
|
data.regions,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.reported_symptoms !== undefined) {
|
|
await visits.setReported_symptoms(data.reported_symptoms, { transaction });
|
|
}
|
|
|
|
|
|
|
|
|
|
return visits;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const visits = await db.visits.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of visits) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of visits) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return visits;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const visits = await db.visits.findByPk(id, options);
|
|
|
|
await visits.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await visits.destroy({
|
|
transaction
|
|
});
|
|
|
|
return visits;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const visits = await db.visits.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!visits) {
|
|
return visits;
|
|
}
|
|
|
|
const output = visits.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.queue_visit = await visits.getQueue_visit({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
output.ai_analyses_visit = await visits.getAi_analyses_visit({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.notifications_visit = await visits.getNotifications_visit({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
output.patient = await visits.getPatient({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.reported_symptoms = await visits.getReported_symptoms({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.assigned_staff = await visits.getAssigned_staff({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.regions = await visits.getRegions({
|
|
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 userRegions = (user && user.regions?.id) || null;
|
|
|
|
|
|
|
|
if (userRegions) {
|
|
if (options?.currentUser?.regionsId) {
|
|
where.regionsId = options.currentUser.regionsId;
|
|
}
|
|
}
|
|
|
|
|
|
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)) } },
|
|
{
|
|
display_name: {
|
|
[Op.or]: filter.patient.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.staff,
|
|
as: 'assigned_staff',
|
|
|
|
where: filter.assigned_staff ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.assigned_staff.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
staff_code: {
|
|
[Op.or]: filter.assigned_staff.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.regions,
|
|
as: 'regions',
|
|
|
|
},
|
|
|
|
|
|
{
|
|
model: db.symptoms,
|
|
as: 'reported_symptoms',
|
|
required: false,
|
|
},
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.reference_code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'visits',
|
|
'reference_code',
|
|
filter.reference_code,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.symptom_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'visits',
|
|
'symptom_text',
|
|
filter.symptom_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
checkin_time: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
estimated_call_time: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.created_tsRange) {
|
|
const [start, end] = filter.created_tsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
created_ts: {
|
|
...where.created_ts,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
created_ts: {
|
|
...where.created_ts,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.checkin_timeRange) {
|
|
const [start, end] = filter.checkin_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
checkin_time: {
|
|
...where.checkin_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
checkin_time: {
|
|
...where.checkin_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.estimated_call_timeRange) {
|
|
const [start, end] = filter.estimated_call_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
estimated_call_time: {
|
|
...where.estimated_call_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
estimated_call_time: {
|
|
...where.estimated_call_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.priority_scoreRange) {
|
|
const [start, end] = filter.priority_scoreRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
priority_score: {
|
|
...where.priority_score,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
priority_score: {
|
|
...where.priority_score,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.triage_level) {
|
|
where = {
|
|
...where,
|
|
triage_level: filter.triage_level,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
if (filter.ai_fallback) {
|
|
where = {
|
|
...where,
|
|
ai_fallback: filter.ai_fallback,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.regions) {
|
|
const listItems = filter.regions.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
regionsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.reported_symptoms) {
|
|
const searchTerms = filter.reported_symptoms.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.symptoms,
|
|
as: 'reported_symptoms_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
|
|
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.regionsId;
|
|
}
|
|
|
|
|
|
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.visits.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(
|
|
'visits',
|
|
'reference_code',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.visits.findAll({
|
|
attributes: [ 'id', 'reference_code' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['reference_code', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.reference_code,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|