38499-vm/backend/src/db/api/office_calendar_events.js
2026-02-16 21:56:53 +00:00

606 lines
15 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 Office_calendar_eventsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const office_calendar_events = await db.office_calendar_events.create(
{
id: data.id || undefined,
event_type: data.event_type
||
null
,
title: data.title
||
null
,
starts_at: data.starts_at
||
null
,
ends_at: data.ends_at
||
null
,
is_all_day: data.is_all_day
||
false
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await office_calendar_events.setUser( data.user || null, {
transaction,
});
await office_calendar_events.setTime_off_request( data.time_off_request || null, {
transaction,
});
await office_calendar_events.setHoliday( data.holiday || null, {
transaction,
});
return office_calendar_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 office_calendar_eventsData = data.map((item, index) => ({
id: item.id || undefined,
event_type: item.event_type
||
null
,
title: item.title
||
null
,
starts_at: item.starts_at
||
null
,
ends_at: item.ends_at
||
null
,
is_all_day: item.is_all_day
||
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 office_calendar_events = await db.office_calendar_events.bulkCreate(office_calendar_eventsData, { transaction });
// For each item created, replace relation files
return office_calendar_events;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const office_calendar_events = await db.office_calendar_events.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.event_type !== undefined) updatePayload.event_type = data.event_type;
if (data.title !== undefined) updatePayload.title = data.title;
if (data.starts_at !== undefined) updatePayload.starts_at = data.starts_at;
if (data.ends_at !== undefined) updatePayload.ends_at = data.ends_at;
if (data.is_all_day !== undefined) updatePayload.is_all_day = data.is_all_day;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await office_calendar_events.update(updatePayload, {transaction});
if (data.user !== undefined) {
await office_calendar_events.setUser(
data.user,
{ transaction }
);
}
if (data.time_off_request !== undefined) {
await office_calendar_events.setTime_off_request(
data.time_off_request,
{ transaction }
);
}
if (data.holiday !== undefined) {
await office_calendar_events.setHoliday(
data.holiday,
{ transaction }
);
}
return office_calendar_events;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const office_calendar_events = await db.office_calendar_events.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of office_calendar_events) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of office_calendar_events) {
await record.destroy({transaction});
}
});
return office_calendar_events;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const office_calendar_events = await db.office_calendar_events.findByPk(id, options);
await office_calendar_events.update({
deletedBy: currentUser.id
}, {
transaction,
});
await office_calendar_events.destroy({
transaction
});
return office_calendar_events;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const office_calendar_events = await db.office_calendar_events.findOne(
{ where },
{ transaction },
);
if (!office_calendar_events) {
return office_calendar_events;
}
const output = office_calendar_events.get({plain: true});
output.user = await office_calendar_events.getUser({
transaction
});
output.time_off_request = await office_calendar_events.getTime_off_request({
transaction
});
output.holiday = await office_calendar_events.getHoliday({
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.users,
as: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.time_off_requests,
as: 'time_off_request',
where: filter.time_off_request ? {
[Op.or]: [
{ id: { [Op.in]: filter.time_off_request.split('|').map(term => Utils.uuid(term)) } },
{
reason: {
[Op.or]: filter.time_off_request.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.holidays,
as: 'holiday',
where: filter.holiday ? {
[Op.or]: [
{ id: { [Op.in]: filter.holiday.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.holiday.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(
'office_calendar_events',
'title',
filter.title,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'office_calendar_events',
'notes',
filter.notes,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
starts_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
ends_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.starts_atRange) {
const [start, end] = filter.starts_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
starts_at: {
...where.starts_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
starts_at: {
...where.starts_at,
[Op.lte]: end,
},
};
}
}
if (filter.ends_atRange) {
const [start, end] = filter.ends_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ends_at: {
...where.ends_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ends_at: {
...where.ends_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.event_type) {
where = {
...where,
event_type: filter.event_type,
};
}
if (filter.is_all_day) {
where = {
...where,
is_all_day: filter.is_all_day,
};
}
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.office_calendar_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(
'office_calendar_events',
'title',
query,
),
],
};
}
const records = await db.office_calendar_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,
}));
}
};