628 lines
16 KiB
JavaScript
628 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 ParticipationsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const participations = await db.participations.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
registered_at: data.registered_at
|
|
||
|
|
null
|
|
,
|
|
|
|
participation_status: data.participation_status
|
|
||
|
|
null
|
|
,
|
|
|
|
materials_json: data.materials_json
|
|
||
|
|
null
|
|
,
|
|
|
|
organizer_comment: data.organizer_comment
|
|
||
|
|
null
|
|
,
|
|
|
|
awarded_points: data.awarded_points
|
|
||
|
|
null
|
|
,
|
|
|
|
reviewed_at: data.reviewed_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await participations.setActivity( data.activity || null, {
|
|
transaction,
|
|
});
|
|
|
|
await participations.setStudent( data.student || null, {
|
|
transaction,
|
|
});
|
|
|
|
await participations.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return participations;
|
|
}
|
|
|
|
|
|
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 participationsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
registered_at: item.registered_at
|
|
||
|
|
null
|
|
,
|
|
|
|
participation_status: item.participation_status
|
|
||
|
|
null
|
|
,
|
|
|
|
materials_json: item.materials_json
|
|
||
|
|
null
|
|
,
|
|
|
|
organizer_comment: item.organizer_comment
|
|
||
|
|
null
|
|
,
|
|
|
|
awarded_points: item.awarded_points
|
|
||
|
|
null
|
|
,
|
|
|
|
reviewed_at: item.reviewed_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const participations = await db.participations.bulkCreate(participationsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return participations;
|
|
}
|
|
|
|
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 participations = await db.participations.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.registered_at !== undefined) updatePayload.registered_at = data.registered_at;
|
|
|
|
|
|
if (data.participation_status !== undefined) updatePayload.participation_status = data.participation_status;
|
|
|
|
|
|
if (data.materials_json !== undefined) updatePayload.materials_json = data.materials_json;
|
|
|
|
|
|
if (data.organizer_comment !== undefined) updatePayload.organizer_comment = data.organizer_comment;
|
|
|
|
|
|
if (data.awarded_points !== undefined) updatePayload.awarded_points = data.awarded_points;
|
|
|
|
|
|
if (data.reviewed_at !== undefined) updatePayload.reviewed_at = data.reviewed_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await participations.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.activity !== undefined) {
|
|
await participations.setActivity(
|
|
|
|
data.activity,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.student !== undefined) {
|
|
await participations.setStudent(
|
|
|
|
data.student,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await participations.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return participations;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const participations = await db.participations.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of participations) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of participations) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return participations;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const participations = await db.participations.findByPk(id, options);
|
|
|
|
await participations.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await participations.destroy({
|
|
transaction
|
|
});
|
|
|
|
return participations;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const participations = await db.participations.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!participations) {
|
|
return participations;
|
|
}
|
|
|
|
const output = participations.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.uploads_participation = await participations.getUploads_participation({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.activity = await participations.getActivity({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.student = await participations.getStudent({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await participations.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.activities,
|
|
as: 'activity',
|
|
|
|
where: filter.activity ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.activity.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.activity.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'student',
|
|
|
|
where: filter.student ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.student.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.student.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.materials_json) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'participations',
|
|
'materials_json',
|
|
filter.materials_json,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.organizer_comment) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'participations',
|
|
'organizer_comment',
|
|
filter.organizer_comment,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.registered_atRange) {
|
|
const [start, end] = filter.registered_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
registered_at: {
|
|
...where.registered_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
registered_at: {
|
|
...where.registered_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.awarded_pointsRange) {
|
|
const [start, end] = filter.awarded_pointsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
awarded_points: {
|
|
...where.awarded_points,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
awarded_points: {
|
|
...where.awarded_points,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.reviewed_atRange) {
|
|
const [start, end] = filter.reviewed_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
reviewed_at: {
|
|
...where.reviewed_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
reviewed_at: {
|
|
...where.reviewed_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.participation_status) {
|
|
where = {
|
|
...where,
|
|
participation_status: filter.participation_status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.participations.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(
|
|
'participations',
|
|
'materials_json',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.participations.findAll({
|
|
attributes: [ 'id', 'materials_json' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['materials_json', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.materials_json,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|