827 lines
21 KiB
JavaScript
827 lines
21 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 Counseling_sessionsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const counseling_sessions = await db.counseling_sessions.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
scheduled_start_at: data.scheduled_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
scheduled_end_at: data.scheduled_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_start_at: data.actual_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_end_at: data.actual_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
session_type: data.session_type
|
|
||
|
|
null
|
|
,
|
|
|
|
mode: data.mode
|
|
||
|
|
null
|
|
,
|
|
|
|
location: data.location
|
|
||
|
|
null
|
|
,
|
|
|
|
outcome: data.outcome
|
|
||
|
|
null
|
|
,
|
|
|
|
summary: data.summary
|
|
||
|
|
null
|
|
,
|
|
|
|
action_items: data.action_items
|
|
||
|
|
null
|
|
,
|
|
|
|
follow_up_at: data.follow_up_at
|
|
||
|
|
null
|
|
,
|
|
|
|
is_confidential: data.is_confidential
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await counseling_sessions.setCase_record( data.case_record || null, {
|
|
transaction,
|
|
});
|
|
|
|
await counseling_sessions.setStudent( data.student || null, {
|
|
transaction,
|
|
});
|
|
|
|
await counseling_sessions.setCounselor( data.counselor || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.counseling_sessions.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: counseling_sessions.id,
|
|
},
|
|
data.attachments,
|
|
options,
|
|
);
|
|
|
|
|
|
return counseling_sessions;
|
|
}
|
|
|
|
|
|
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 counseling_sessionsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
scheduled_start_at: item.scheduled_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
scheduled_end_at: item.scheduled_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_start_at: item.actual_start_at
|
|
||
|
|
null
|
|
,
|
|
|
|
actual_end_at: item.actual_end_at
|
|
||
|
|
null
|
|
,
|
|
|
|
session_type: item.session_type
|
|
||
|
|
null
|
|
,
|
|
|
|
mode: item.mode
|
|
||
|
|
null
|
|
,
|
|
|
|
location: item.location
|
|
||
|
|
null
|
|
,
|
|
|
|
outcome: item.outcome
|
|
||
|
|
null
|
|
,
|
|
|
|
summary: item.summary
|
|
||
|
|
null
|
|
,
|
|
|
|
action_items: item.action_items
|
|
||
|
|
null
|
|
,
|
|
|
|
follow_up_at: item.follow_up_at
|
|
||
|
|
null
|
|
,
|
|
|
|
is_confidential: item.is_confidential
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const counseling_sessions = await db.counseling_sessions.bulkCreate(counseling_sessionsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < counseling_sessions.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.counseling_sessions.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: counseling_sessions[i].id,
|
|
},
|
|
data[i].attachments,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return counseling_sessions;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const counseling_sessions = await db.counseling_sessions.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.scheduled_start_at !== undefined) updatePayload.scheduled_start_at = data.scheduled_start_at;
|
|
|
|
|
|
if (data.scheduled_end_at !== undefined) updatePayload.scheduled_end_at = data.scheduled_end_at;
|
|
|
|
|
|
if (data.actual_start_at !== undefined) updatePayload.actual_start_at = data.actual_start_at;
|
|
|
|
|
|
if (data.actual_end_at !== undefined) updatePayload.actual_end_at = data.actual_end_at;
|
|
|
|
|
|
if (data.session_type !== undefined) updatePayload.session_type = data.session_type;
|
|
|
|
|
|
if (data.mode !== undefined) updatePayload.mode = data.mode;
|
|
|
|
|
|
if (data.location !== undefined) updatePayload.location = data.location;
|
|
|
|
|
|
if (data.outcome !== undefined) updatePayload.outcome = data.outcome;
|
|
|
|
|
|
if (data.summary !== undefined) updatePayload.summary = data.summary;
|
|
|
|
|
|
if (data.action_items !== undefined) updatePayload.action_items = data.action_items;
|
|
|
|
|
|
if (data.follow_up_at !== undefined) updatePayload.follow_up_at = data.follow_up_at;
|
|
|
|
|
|
if (data.is_confidential !== undefined) updatePayload.is_confidential = data.is_confidential;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await counseling_sessions.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.case_record !== undefined) {
|
|
await counseling_sessions.setCase_record(
|
|
|
|
data.case_record,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.student !== undefined) {
|
|
await counseling_sessions.setStudent(
|
|
|
|
data.student,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.counselor !== undefined) {
|
|
await counseling_sessions.setCounselor(
|
|
|
|
data.counselor,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.counseling_sessions.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: counseling_sessions.id,
|
|
},
|
|
data.attachments,
|
|
options,
|
|
);
|
|
|
|
|
|
return counseling_sessions;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const counseling_sessions = await db.counseling_sessions.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of counseling_sessions) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of counseling_sessions) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return counseling_sessions;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const counseling_sessions = await db.counseling_sessions.findByPk(id, options);
|
|
|
|
await counseling_sessions.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await counseling_sessions.destroy({
|
|
transaction
|
|
});
|
|
|
|
return counseling_sessions;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const counseling_sessions = await db.counseling_sessions.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!counseling_sessions) {
|
|
return counseling_sessions;
|
|
}
|
|
|
|
const output = counseling_sessions.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.session_notes_counseling_session = await counseling_sessions.getSession_notes_counseling_session({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.case_record = await counseling_sessions.getCase_record({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.student = await counseling_sessions.getStudent({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.counselor = await counseling_sessions.getCounselor({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.attachments = await counseling_sessions.getAttachments({
|
|
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.case_records,
|
|
as: 'case_record',
|
|
|
|
where: filter.case_record ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.case_record.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
case_code: {
|
|
[Op.or]: filter.case_record.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.students,
|
|
as: 'student',
|
|
|
|
where: filter.student ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.student.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
student_number: {
|
|
[Op.or]: filter.student.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'counselor',
|
|
|
|
where: filter.counselor ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.counselor.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.counselor.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'attachments',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.location) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'counseling_sessions',
|
|
'location',
|
|
filter.location,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.summary) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'counseling_sessions',
|
|
'summary',
|
|
filter.summary,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.action_items) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'counseling_sessions',
|
|
'action_items',
|
|
filter.action_items,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
scheduled_start_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
scheduled_end_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.scheduled_start_atRange) {
|
|
const [start, end] = filter.scheduled_start_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
scheduled_start_at: {
|
|
...where.scheduled_start_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
scheduled_start_at: {
|
|
...where.scheduled_start_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.scheduled_end_atRange) {
|
|
const [start, end] = filter.scheduled_end_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
scheduled_end_at: {
|
|
...where.scheduled_end_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
scheduled_end_at: {
|
|
...where.scheduled_end_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.actual_start_atRange) {
|
|
const [start, end] = filter.actual_start_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
actual_start_at: {
|
|
...where.actual_start_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
actual_start_at: {
|
|
...where.actual_start_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.actual_end_atRange) {
|
|
const [start, end] = filter.actual_end_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
actual_end_at: {
|
|
...where.actual_end_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
actual_end_at: {
|
|
...where.actual_end_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.follow_up_atRange) {
|
|
const [start, end] = filter.follow_up_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
follow_up_at: {
|
|
...where.follow_up_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
follow_up_at: {
|
|
...where.follow_up_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.session_type) {
|
|
where = {
|
|
...where,
|
|
session_type: filter.session_type,
|
|
};
|
|
}
|
|
|
|
if (filter.mode) {
|
|
where = {
|
|
...where,
|
|
mode: filter.mode,
|
|
};
|
|
}
|
|
|
|
if (filter.outcome) {
|
|
where = {
|
|
...where,
|
|
outcome: filter.outcome,
|
|
};
|
|
}
|
|
|
|
if (filter.is_confidential) {
|
|
where = {
|
|
...where,
|
|
is_confidential: filter.is_confidential,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.counseling_sessions.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(
|
|
'counseling_sessions',
|
|
'location',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.counseling_sessions.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,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|