39135-vm/backend/src/db/api/ppdb_periods.js
2026-03-11 15:59:29 +00:00

629 lines
16 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 Ppdb_periodsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ppdb_periods = await db.ppdb_periods.create(
{
id: data.id || undefined,
period_name: data.period_name
||
null
,
academic_year: data.academic_year
||
null
,
registration_open_at: data.registration_open_at
||
null
,
registration_close_at: data.registration_close_at
||
null
,
selection_start_at: data.selection_start_at
||
null
,
selection_end_at: data.selection_end_at
||
null
,
announcement_at: data.announcement_at
||
null
,
is_active: data.is_active
||
false
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return ppdb_periods;
}
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 ppdb_periodsData = data.map((item, index) => ({
id: item.id || undefined,
period_name: item.period_name
||
null
,
academic_year: item.academic_year
||
null
,
registration_open_at: item.registration_open_at
||
null
,
registration_close_at: item.registration_close_at
||
null
,
selection_start_at: item.selection_start_at
||
null
,
selection_end_at: item.selection_end_at
||
null
,
announcement_at: item.announcement_at
||
null
,
is_active: item.is_active
||
false
,
notes: item.notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const ppdb_periods = await db.ppdb_periods.bulkCreate(ppdb_periodsData, { transaction });
// For each item created, replace relation files
return ppdb_periods;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ppdb_periods = await db.ppdb_periods.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.period_name !== undefined) updatePayload.period_name = data.period_name;
if (data.academic_year !== undefined) updatePayload.academic_year = data.academic_year;
if (data.registration_open_at !== undefined) updatePayload.registration_open_at = data.registration_open_at;
if (data.registration_close_at !== undefined) updatePayload.registration_close_at = data.registration_close_at;
if (data.selection_start_at !== undefined) updatePayload.selection_start_at = data.selection_start_at;
if (data.selection_end_at !== undefined) updatePayload.selection_end_at = data.selection_end_at;
if (data.announcement_at !== undefined) updatePayload.announcement_at = data.announcement_at;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await ppdb_periods.update(updatePayload, {transaction});
return ppdb_periods;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ppdb_periods = await db.ppdb_periods.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of ppdb_periods) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of ppdb_periods) {
await record.destroy({transaction});
}
});
return ppdb_periods;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ppdb_periods = await db.ppdb_periods.findByPk(id, options);
await ppdb_periods.update({
deletedBy: currentUser.id
}, {
transaction,
});
await ppdb_periods.destroy({
transaction
});
return ppdb_periods;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const ppdb_periods = await db.ppdb_periods.findOne(
{ where },
{ transaction },
);
if (!ppdb_periods) {
return ppdb_periods;
}
const output = ppdb_periods.get({plain: true});
output.registrations_period = await ppdb_periods.getRegistrations_period({
transaction
});
output.selection_events_period = await ppdb_periods.getSelection_events_period({
transaction
});
output.announcements_period = await ppdb_periods.getAnnouncements_period({
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 = [
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.period_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'ppdb_periods',
'period_name',
filter.period_name,
),
};
}
if (filter.academic_year) {
where = {
...where,
[Op.and]: Utils.ilike(
'ppdb_periods',
'academic_year',
filter.academic_year,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'ppdb_periods',
'notes',
filter.notes,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
registration_open_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
registration_close_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.registration_open_atRange) {
const [start, end] = filter.registration_open_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
registration_open_at: {
...where.registration_open_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
registration_open_at: {
...where.registration_open_at,
[Op.lte]: end,
},
};
}
}
if (filter.registration_close_atRange) {
const [start, end] = filter.registration_close_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
registration_close_at: {
...where.registration_close_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
registration_close_at: {
...where.registration_close_at,
[Op.lte]: end,
},
};
}
}
if (filter.selection_start_atRange) {
const [start, end] = filter.selection_start_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
selection_start_at: {
...where.selection_start_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
selection_start_at: {
...where.selection_start_at,
[Op.lte]: end,
},
};
}
}
if (filter.selection_end_atRange) {
const [start, end] = filter.selection_end_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
selection_end_at: {
...where.selection_end_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
selection_end_at: {
...where.selection_end_at,
[Op.lte]: end,
},
};
}
}
if (filter.announcement_atRange) {
const [start, end] = filter.announcement_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
announcement_at: {
...where.announcement_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
announcement_at: {
...where.announcement_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.is_active) {
where = {
...where,
is_active: filter.is_active,
};
}
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.ppdb_periods.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(
'ppdb_periods',
'period_name',
query,
),
],
};
}
const records = await db.ppdb_periods.findAll({
attributes: [ 'id', 'period_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['period_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.period_name,
}));
}
};