40170-vm/backend/src/db/api/roster_entries.js
2026-05-31 03:52:41 +00:00

507 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 Roster_entriesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const roster_entries = await db.roster_entries.create(
{
id: data.id || undefined,
shift_date: data.shift_date
||
null
,
shift_code: data.shift_code
||
null
,
source: data.source
||
null
,
comment: data.comment
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await roster_entries.setRoster( data.roster || null, {
transaction,
});
await roster_entries.setGuard( data.guard || null, {
transaction,
});
return roster_entries;
}
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 roster_entriesData = data.map((item, index) => ({
id: item.id || undefined,
shift_date: item.shift_date
||
null
,
shift_code: item.shift_code
||
null
,
source: item.source
||
null
,
comment: item.comment
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const roster_entries = await db.roster_entries.bulkCreate(roster_entriesData, { transaction });
// For each item created, replace relation files
return roster_entries;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const roster_entries = await db.roster_entries.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.shift_date !== undefined) updatePayload.shift_date = data.shift_date;
if (data.shift_code !== undefined) updatePayload.shift_code = data.shift_code;
if (data.source !== undefined) updatePayload.source = data.source;
if (data.comment !== undefined) updatePayload.comment = data.comment;
updatePayload.updatedById = currentUser.id;
await roster_entries.update(updatePayload, {transaction});
if (data.roster !== undefined) {
await roster_entries.setRoster(
data.roster,
{ transaction }
);
}
if (data.guard !== undefined) {
await roster_entries.setGuard(
data.guard,
{ transaction }
);
}
return roster_entries;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const roster_entries = await db.roster_entries.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of roster_entries) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of roster_entries) {
await record.destroy({transaction});
}
});
return roster_entries;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const roster_entries = await db.roster_entries.findByPk(id, options);
await roster_entries.update({
deletedBy: currentUser.id
}, {
transaction,
});
await roster_entries.destroy({
transaction
});
return roster_entries;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const roster_entries = await db.roster_entries.findOne(
{ where },
{ transaction },
);
if (!roster_entries) {
return roster_entries;
}
const output = roster_entries.get({plain: true});
output.roster = await roster_entries.getRoster({
transaction
});
output.guard = await roster_entries.getGuard({
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.rosters,
as: 'roster',
where: filter.roster ? {
[Op.or]: [
{ id: { [Op.in]: filter.roster.split('|').map(term => Utils.uuid(term)) } },
{
notes: {
[Op.or]: filter.roster.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.guards,
as: 'guard',
where: filter.guard ? {
[Op.or]: [
{ id: { [Op.in]: filter.guard.split('|').map(term => Utils.uuid(term)) } },
{
full_name: {
[Op.or]: filter.guard.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.comment) {
where = {
...where,
[Op.and]: Utils.ilike(
'roster_entries',
'comment',
filter.comment,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
shift_date: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
shift_date: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.shift_dateRange) {
const [start, end] = filter.shift_dateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
shift_date: {
...where.shift_date,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
shift_date: {
...where.shift_date,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.shift_code) {
where = {
...where,
shift_code: filter.shift_code,
};
}
if (filter.source) {
where = {
...where,
source: filter.source,
};
}
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.roster_entries.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(
'roster_entries',
'comment',
query,
),
],
};
}
const records = await db.roster_entries.findAll({
attributes: [ 'id', 'comment' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['comment', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.comment,
}));
}
};