38737-vm/backend/src/db/api/end_of_summer_trip_selections.js
2026-02-24 13:48:31 +00:00

459 lines
11 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 End_of_summer_trip_selectionsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const end_of_summer_trip_selections = await db.end_of_summer_trip_selections.create(
{
id: data.id || undefined,
status: data.status
||
null
,
selected_datetime: data.selected_datetime
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await end_of_summer_trip_selections.setEnrollment( data.enrollment || null, {
transaction,
});
await end_of_summer_trip_selections.setTrip_option( data.trip_option || null, {
transaction,
});
return end_of_summer_trip_selections;
}
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 end_of_summer_trip_selectionsData = data.map((item, index) => ({
id: item.id || undefined,
status: item.status
||
null
,
selected_datetime: item.selected_datetime
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const end_of_summer_trip_selections = await db.end_of_summer_trip_selections.bulkCreate(end_of_summer_trip_selectionsData, { transaction });
// For each item created, replace relation files
return end_of_summer_trip_selections;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const end_of_summer_trip_selections = await db.end_of_summer_trip_selections.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.status !== undefined) updatePayload.status = data.status;
if (data.selected_datetime !== undefined) updatePayload.selected_datetime = data.selected_datetime;
updatePayload.updatedById = currentUser.id;
await end_of_summer_trip_selections.update(updatePayload, {transaction});
if (data.enrollment !== undefined) {
await end_of_summer_trip_selections.setEnrollment(
data.enrollment,
{ transaction }
);
}
if (data.trip_option !== undefined) {
await end_of_summer_trip_selections.setTrip_option(
data.trip_option,
{ transaction }
);
}
return end_of_summer_trip_selections;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const end_of_summer_trip_selections = await db.end_of_summer_trip_selections.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of end_of_summer_trip_selections) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of end_of_summer_trip_selections) {
await record.destroy({transaction});
}
});
return end_of_summer_trip_selections;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const end_of_summer_trip_selections = await db.end_of_summer_trip_selections.findByPk(id, options);
await end_of_summer_trip_selections.update({
deletedBy: currentUser.id
}, {
transaction,
});
await end_of_summer_trip_selections.destroy({
transaction
});
return end_of_summer_trip_selections;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const end_of_summer_trip_selections = await db.end_of_summer_trip_selections.findOne(
{ where },
{ transaction },
);
if (!end_of_summer_trip_selections) {
return end_of_summer_trip_selections;
}
const output = end_of_summer_trip_selections.get({plain: true});
output.enrollment = await end_of_summer_trip_selections.getEnrollment({
transaction
});
output.trip_option = await end_of_summer_trip_selections.getTrip_option({
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.enrollments,
as: 'enrollment',
where: filter.enrollment ? {
[Op.or]: [
{ id: { [Op.in]: filter.enrollment.split('|').map(term => Utils.uuid(term)) } },
{
notes: {
[Op.or]: filter.enrollment.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.end_of_summer_trip_options,
as: 'trip_option',
where: filter.trip_option ? {
[Op.or]: [
{ id: { [Op.in]: filter.trip_option.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.trip_option.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.selected_datetimeRange) {
const [start, end] = filter.selected_datetimeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
selected_datetime: {
...where.selected_datetime,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
selected_datetime: {
...where.selected_datetime,
[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.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.end_of_summer_trip_selections.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(
'end_of_summer_trip_selections',
'status',
query,
),
],
};
}
const records = await db.end_of_summer_trip_selections.findAll({
attributes: [ 'id', 'status' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['status', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.status,
}));
}
};