472 lines
12 KiB
JavaScript
472 lines
12 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 EventsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const events = await db.events.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name || null,
|
|
start_time: data.start_time || null,
|
|
end_time: data.end_time || null,
|
|
server_name: data.server_name || null,
|
|
server_password: data.server_password || null,
|
|
briefing_link: data.briefing_link || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await events.setSquads(data.squads || [], {
|
|
transaction,
|
|
});
|
|
|
|
await events.setAttendances(data.attendances || [], {
|
|
transaction,
|
|
});
|
|
|
|
return events;
|
|
}
|
|
|
|
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 eventsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name || null,
|
|
start_time: item.start_time || null,
|
|
end_time: item.end_time || null,
|
|
server_name: item.server_name || null,
|
|
server_password: item.server_password || null,
|
|
briefing_link: item.briefing_link || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const events = await db.events.bulkCreate(eventsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return events;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const events = await db.events.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
if (data.start_time !== undefined)
|
|
updatePayload.start_time = data.start_time;
|
|
|
|
if (data.end_time !== undefined) updatePayload.end_time = data.end_time;
|
|
|
|
if (data.server_name !== undefined)
|
|
updatePayload.server_name = data.server_name;
|
|
|
|
if (data.server_password !== undefined)
|
|
updatePayload.server_password = data.server_password;
|
|
|
|
if (data.briefing_link !== undefined)
|
|
updatePayload.briefing_link = data.briefing_link;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await events.update(updatePayload, { transaction });
|
|
|
|
if (data.squads !== undefined) {
|
|
await events.setSquads(data.squads, { transaction });
|
|
}
|
|
|
|
if (data.attendances !== undefined) {
|
|
await events.setAttendances(data.attendances, { transaction });
|
|
}
|
|
|
|
return events;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const events = await db.events.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of events) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of events) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return events;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const events = await db.events.findByPk(id, options);
|
|
|
|
await events.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await events.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return events;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const events = await db.events.findOne({ where }, { transaction });
|
|
|
|
if (!events) {
|
|
return events;
|
|
}
|
|
|
|
const output = events.get({ plain: true });
|
|
|
|
output.attendances_event = await events.getAttendances_event({
|
|
transaction,
|
|
});
|
|
|
|
output.reports_event = await events.getReports_event({
|
|
transaction,
|
|
});
|
|
|
|
output.squads = await events.getSquads({
|
|
transaction,
|
|
});
|
|
|
|
output.attendances = await events.getAttendances({
|
|
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.squads,
|
|
as: 'squads',
|
|
},
|
|
|
|
{
|
|
model: db.attendances,
|
|
as: 'attendances',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('events', 'name', filter.name),
|
|
};
|
|
}
|
|
|
|
if (filter.server_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('events', 'server_name', filter.server_name),
|
|
};
|
|
}
|
|
|
|
if (filter.server_password) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'events',
|
|
'server_password',
|
|
filter.server_password,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.briefing_link) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'events',
|
|
'briefing_link',
|
|
filter.briefing_link,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
start_time: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
end_time: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
if (filter.start_timeRange) {
|
|
const [start, end] = filter.start_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
start_time: {
|
|
...where.start_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
start_time: {
|
|
...where.start_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.end_timeRange) {
|
|
const [start, end] = filter.end_timeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
end_time: {
|
|
...where.end_time,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
end_time: {
|
|
...where.end_time,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.squads) {
|
|
const searchTerms = filter.squads.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.squads,
|
|
as: 'squads_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.attendances) {
|
|
const searchTerms = filter.attendances.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.attendances,
|
|
as: 'attendances_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
justification: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
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.events.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('events', 'name', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.events.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,
|
|
}));
|
|
}
|
|
};
|