39136-vm/backend/src/db/api/ppdb_waves.js
2026-03-11 16:01:32 +00:00

580 lines
14 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_wavesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ppdb_waves = await db.ppdb_waves.create(
{
id: data.id || undefined,
name: data.name
||
null
,
open_at: data.open_at
||
null
,
close_at: data.close_at
||
null
,
quota: data.quota
||
null
,
registration_fee: data.registration_fee
||
null
,
is_active: data.is_active
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await ppdb_waves.setAcademic_year( data.academic_year || null, {
transaction,
});
return ppdb_waves;
}
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_wavesData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
open_at: item.open_at
||
null
,
close_at: item.close_at
||
null
,
quota: item.quota
||
null
,
registration_fee: item.registration_fee
||
null
,
is_active: item.is_active
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const ppdb_waves = await db.ppdb_waves.bulkCreate(ppdb_wavesData, { transaction });
// For each item created, replace relation files
return ppdb_waves;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ppdb_waves = await db.ppdb_waves.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.open_at !== undefined) updatePayload.open_at = data.open_at;
if (data.close_at !== undefined) updatePayload.close_at = data.close_at;
if (data.quota !== undefined) updatePayload.quota = data.quota;
if (data.registration_fee !== undefined) updatePayload.registration_fee = data.registration_fee;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
updatePayload.updatedById = currentUser.id;
await ppdb_waves.update(updatePayload, {transaction});
if (data.academic_year !== undefined) {
await ppdb_waves.setAcademic_year(
data.academic_year,
{ transaction }
);
}
return ppdb_waves;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ppdb_waves = await db.ppdb_waves.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of ppdb_waves) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of ppdb_waves) {
await record.destroy({transaction});
}
});
return ppdb_waves;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ppdb_waves = await db.ppdb_waves.findByPk(id, options);
await ppdb_waves.update({
deletedBy: currentUser.id
}, {
transaction,
});
await ppdb_waves.destroy({
transaction
});
return ppdb_waves;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const ppdb_waves = await db.ppdb_waves.findOne(
{ where },
{ transaction },
);
if (!ppdb_waves) {
return ppdb_waves;
}
const output = ppdb_waves.get({plain: true});
output.registration_forms_ppdb_wave = await ppdb_waves.getRegistration_forms_ppdb_wave({
transaction
});
output.test_schedules_ppdb_wave = await ppdb_waves.getTest_schedules_ppdb_wave({
transaction
});
output.announcements_ppdb_wave = await ppdb_waves.getAnnouncements_ppdb_wave({
transaction
});
output.academic_year = await ppdb_waves.getAcademic_year({
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.academic_years,
as: 'academic_year',
where: filter.academic_year ? {
[Op.or]: [
{ id: { [Op.in]: filter.academic_year.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.academic_year.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'ppdb_waves',
'name',
filter.name,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
open_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
close_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.open_atRange) {
const [start, end] = filter.open_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
open_at: {
...where.open_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
open_at: {
...where.open_at,
[Op.lte]: end,
},
};
}
}
if (filter.close_atRange) {
const [start, end] = filter.close_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
close_at: {
...where.close_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
close_at: {
...where.close_at,
[Op.lte]: end,
},
};
}
}
if (filter.quotaRange) {
const [start, end] = filter.quotaRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
quota: {
...where.quota,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
quota: {
...where.quota,
[Op.lte]: end,
},
};
}
}
if (filter.registration_feeRange) {
const [start, end] = filter.registration_feeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
registration_fee: {
...where.registration_fee,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
registration_fee: {
...where.registration_fee,
[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_waves.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_waves',
'name',
query,
),
],
};
}
const records = await db.ppdb_waves.findAll({
attributes: [ 'id', 'name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};