39903-vm/backend/src/db/api/accreditation_cases.js
2026-05-05 09:38:04 +00:00

813 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 Accreditation_casesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const accreditation_cases = await db.accreditation_cases.create(
{
id: data.id || undefined,
case_number: data.case_number
||
null
,
status: data.status
||
null
,
smart_score: data.smart_score
||
null
,
score_grade: data.score_grade
||
null
,
needs_attention: data.needs_attention
||
false
,
submitted_at: data.submitted_at
||
null
,
decision_at: data.decision_at
||
null
,
due_at: data.due_at
||
null
,
public_notes: data.public_notes
||
null
,
internal_notes: data.internal_notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await accreditation_cases.setProgram( data.program || null, {
transaction,
});
await accreditation_cases.setApplicant( data.applicant || null, {
transaction,
});
await accreditation_cases.setCurrent_stage( data.current_stage || null, {
transaction,
});
await accreditation_cases.setAssigned_reviewer( data.assigned_reviewer || null, {
transaction,
});
await accreditation_cases.setOrganizations( data.organizations || null, {
transaction,
});
return accreditation_cases;
}
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 accreditation_casesData = data.map((item, index) => ({
id: item.id || undefined,
case_number: item.case_number
||
null
,
status: item.status
||
null
,
smart_score: item.smart_score
||
null
,
score_grade: item.score_grade
||
null
,
needs_attention: item.needs_attention
||
false
,
submitted_at: item.submitted_at
||
null
,
decision_at: item.decision_at
||
null
,
due_at: item.due_at
||
null
,
public_notes: item.public_notes
||
null
,
internal_notes: item.internal_notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const accreditation_cases = await db.accreditation_cases.bulkCreate(accreditation_casesData, { transaction });
// For each item created, replace relation files
return accreditation_cases;
}
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 accreditation_cases = await db.accreditation_cases.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.case_number !== undefined) updatePayload.case_number = data.case_number;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.smart_score !== undefined) updatePayload.smart_score = data.smart_score;
if (data.score_grade !== undefined) updatePayload.score_grade = data.score_grade;
if (data.needs_attention !== undefined) updatePayload.needs_attention = data.needs_attention;
if (data.submitted_at !== undefined) updatePayload.submitted_at = data.submitted_at;
if (data.decision_at !== undefined) updatePayload.decision_at = data.decision_at;
if (data.due_at !== undefined) updatePayload.due_at = data.due_at;
if (data.public_notes !== undefined) updatePayload.public_notes = data.public_notes;
if (data.internal_notes !== undefined) updatePayload.internal_notes = data.internal_notes;
updatePayload.updatedById = currentUser.id;
await accreditation_cases.update(updatePayload, {transaction});
if (data.program !== undefined) {
await accreditation_cases.setProgram(
data.program,
{ transaction }
);
}
if (data.applicant !== undefined) {
await accreditation_cases.setApplicant(
data.applicant,
{ transaction }
);
}
if (data.current_stage !== undefined) {
await accreditation_cases.setCurrent_stage(
data.current_stage,
{ transaction }
);
}
if (data.assigned_reviewer !== undefined) {
await accreditation_cases.setAssigned_reviewer(
data.assigned_reviewer,
{ transaction }
);
}
if (data.organizations !== undefined) {
await accreditation_cases.setOrganizations(
data.organizations,
{ transaction }
);
}
return accreditation_cases;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const accreditation_cases = await db.accreditation_cases.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of accreditation_cases) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of accreditation_cases) {
await record.destroy({transaction});
}
});
return accreditation_cases;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const accreditation_cases = await db.accreditation_cases.findByPk(id, options);
await accreditation_cases.update({
deletedBy: currentUser.id
}, {
transaction,
});
await accreditation_cases.destroy({
transaction
});
return accreditation_cases;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const accreditation_cases = await db.accreditation_cases.findOne(
{ where },
{ transaction },
);
if (!accreditation_cases) {
return accreditation_cases;
}
const output = accreditation_cases.get({plain: true});
output.case_documents_accreditation_case = await accreditation_cases.getCase_documents_accreditation_case({
transaction
});
output.reviews_accreditation_case = await accreditation_cases.getReviews_accreditation_case({
transaction
});
output.case_timeline_events_accreditation_case = await accreditation_cases.getCase_timeline_events_accreditation_case({
transaction
});
output.program = await accreditation_cases.getProgram({
transaction
});
output.applicant = await accreditation_cases.getApplicant({
transaction
});
output.current_stage = await accreditation_cases.getCurrent_stage({
transaction
});
output.assigned_reviewer = await accreditation_cases.getAssigned_reviewer({
transaction
});
output.organizations = await accreditation_cases.getOrganizations({
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.accreditation_programs,
as: 'program',
where: filter.program ? {
[Op.or]: [
{ id: { [Op.in]: filter.program.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.program.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.applicants,
as: 'applicant',
where: filter.applicant ? {
[Op.or]: [
{ id: { [Op.in]: filter.applicant.split('|').map(term => Utils.uuid(term)) } },
{
full_name: {
[Op.or]: filter.applicant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.accreditation_stages,
as: 'current_stage',
where: filter.current_stage ? {
[Op.or]: [
{ id: { [Op.in]: filter.current_stage.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.current_stage.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'assigned_reviewer',
where: filter.assigned_reviewer ? {
[Op.or]: [
{ id: { [Op.in]: filter.assigned_reviewer.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.assigned_reviewer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.organizations,
as: 'organizations',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.case_number) {
where = {
...where,
[Op.and]: Utils.ilike(
'accreditation_cases',
'case_number',
filter.case_number,
),
};
}
if (filter.public_notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'accreditation_cases',
'public_notes',
filter.public_notes,
),
};
}
if (filter.internal_notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'accreditation_cases',
'internal_notes',
filter.internal_notes,
),
};
}
if (filter.smart_scoreRange) {
const [start, end] = filter.smart_scoreRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
smart_score: {
...where.smart_score,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
smart_score: {
...where.smart_score,
[Op.lte]: end,
},
};
}
}
if (filter.submitted_atRange) {
const [start, end] = filter.submitted_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
submitted_at: {
...where.submitted_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
submitted_at: {
...where.submitted_at,
[Op.lte]: end,
},
};
}
}
if (filter.decision_atRange) {
const [start, end] = filter.decision_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
decision_at: {
...where.decision_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
decision_at: {
...where.decision_at,
[Op.lte]: end,
},
};
}
}
if (filter.due_atRange) {
const [start, end] = filter.due_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
due_at: {
...where.due_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
due_at: {
...where.due_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.score_grade) {
where = {
...where,
score_grade: filter.score_grade,
};
}
if (filter.needs_attention) {
where = {
...where,
needs_attention: filter.needs_attention,
};
}
if (filter.organizations) {
const listItems = filter.organizations.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
organizationsId: {[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.accreditation_cases.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(
'accreditation_cases',
'case_number',
query,
),
],
};
}
const records = await db.accreditation_cases.findAll({
attributes: [ 'id', 'case_number' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['case_number', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.case_number,
}));
}
};