39213-vm/backend/src/db/api/events.js
2026-03-16 11:14:54 +00:00

702 lines
17 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,
title: data.title
||
null
,
event_kind: data.event_kind
||
null
,
start_at: data.start_at
||
null
,
end_at: data.end_at
||
null
,
location_name: data.location_name
||
null
,
location_address: data.location_address
||
null
,
status: data.status
||
null
,
notes: data.notes
||
null
,
event_qr_value: data.event_qr_value
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await events.setBand( data.band || null, {
transaction,
});
await events.setEvent_type( data.event_type || null, {
transaction,
});
await events.setCreated_by_user( data.created_by_user || null, {
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,
title: item.title
||
null
,
event_kind: item.event_kind
||
null
,
start_at: item.start_at
||
null
,
end_at: item.end_at
||
null
,
location_name: item.location_name
||
null
,
location_address: item.location_address
||
null
,
status: item.status
||
null
,
notes: item.notes
||
null
,
event_qr_value: item.event_qr_value
||
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 globalAccess = currentUser.app_role?.globalAccess;
const events = await db.events.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.title !== undefined) updatePayload.title = data.title;
if (data.event_kind !== undefined) updatePayload.event_kind = data.event_kind;
if (data.start_at !== undefined) updatePayload.start_at = data.start_at;
if (data.end_at !== undefined) updatePayload.end_at = data.end_at;
if (data.location_name !== undefined) updatePayload.location_name = data.location_name;
if (data.location_address !== undefined) updatePayload.location_address = data.location_address;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.notes !== undefined) updatePayload.notes = data.notes;
if (data.event_qr_value !== undefined) updatePayload.event_qr_value = data.event_qr_value;
updatePayload.updatedById = currentUser.id;
await events.update(updatePayload, {transaction});
if (data.band !== undefined) {
await events.setBand(
data.band,
{ transaction }
);
}
if (data.event_type !== undefined) {
await events.setEvent_type(
data.event_type,
{ transaction }
);
}
if (data.created_by_user !== undefined) {
await events.setCreated_by_user(
data.created_by_user,
{ 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.attendance_records_event = await events.getAttendance_records_event({
transaction
});
output.checkin_sessions_event = await events.getCheckin_sessions_event({
transaction
});
output.band = await events.getBand({
transaction
});
output.event_type = await events.getEvent_type({
transaction
});
output.created_by_user = await events.getCreated_by_user({
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 userBands = (user && user.bands?.id) || null;
if (userBands) {
if (options?.currentUser?.bandsId) {
where.bandsId = options.currentUser.bandsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.bands,
as: 'band',
},
{
model: db.event_types,
as: 'event_type',
where: filter.event_type ? {
[Op.or]: [
{ id: { [Op.in]: filter.event_type.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.event_type.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'created_by_user',
where: filter.created_by_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.created_by_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.created_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'events',
'title',
filter.title,
),
};
}
if (filter.location_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'events',
'location_name',
filter.location_name,
),
};
}
if (filter.location_address) {
where = {
...where,
[Op.and]: Utils.ilike(
'events',
'location_address',
filter.location_address,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'events',
'notes',
filter.notes,
),
};
}
if (filter.event_qr_value) {
where = {
...where,
[Op.and]: Utils.ilike(
'events',
'event_qr_value',
filter.event_qr_value,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
start_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
end_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.start_atRange) {
const [start, end] = filter.start_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
start_at: {
...where.start_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
start_at: {
...where.start_at,
[Op.lte]: end,
},
};
}
}
if (filter.end_atRange) {
const [start, end] = filter.end_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
end_at: {
...where.end_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
end_at: {
...where.end_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.event_kind) {
where = {
...where,
event_kind: filter.event_kind,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.band) {
const listItems = filter.band.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
bandId: {[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.bandsId;
}
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, globalAccess, organizationId,) {
let where = {};
if (!globalAccess && organizationId) {
where.organizationId = organizationId;
}
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'events',
'title',
query,
),
],
};
}
const records = await db.events.findAll({
attributes: [ 'id', 'title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.title,
}));
}
};