40003-vm/backend/src/db/api/training_courses.js
2026-05-14 13:24:57 +00:00

561 lines
14 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 Training_coursesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const training_courses = await db.training_courses.create(
{
id: data.id || undefined,
name: data.name
||
null
,
description: data.description
||
null
,
delivery_type: data.delivery_type
||
null
,
link: data.link
||
null
,
duration_minutes: data.duration_minutes
||
null
,
validity_days: data.validity_days
||
null
,
status: data.status
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.training_courses.getTableName(),
belongsToColumn: 'attachments',
belongsToId: training_courses.id,
},
data.attachments,
options,
);
return training_courses;
}
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 training_coursesData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
description: item.description
||
null
,
delivery_type: item.delivery_type
||
null
,
link: item.link
||
null
,
duration_minutes: item.duration_minutes
||
null
,
validity_days: item.validity_days
||
null
,
status: item.status
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const training_courses = await db.training_courses.bulkCreate(training_coursesData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < training_courses.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.training_courses.getTableName(),
belongsToColumn: 'attachments',
belongsToId: training_courses[i].id,
},
data[i].attachments,
options,
);
}
return training_courses;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const training_courses = await db.training_courses.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.description !== undefined) updatePayload.description = data.description;
if (data.delivery_type !== undefined) updatePayload.delivery_type = data.delivery_type;
if (data.link !== undefined) updatePayload.link = data.link;
if (data.duration_minutes !== undefined) updatePayload.duration_minutes = data.duration_minutes;
if (data.validity_days !== undefined) updatePayload.validity_days = data.validity_days;
if (data.status !== undefined) updatePayload.status = data.status;
updatePayload.updatedById = currentUser.id;
await training_courses.update(updatePayload, {transaction});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.training_courses.getTableName(),
belongsToColumn: 'attachments',
belongsToId: training_courses.id,
},
data.attachments,
options,
);
return training_courses;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const training_courses = await db.training_courses.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of training_courses) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of training_courses) {
await record.destroy({transaction});
}
});
return training_courses;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const training_courses = await db.training_courses.findByPk(id, options);
await training_courses.update({
deletedBy: currentUser.id
}, {
transaction,
});
await training_courses.destroy({
transaction
});
return training_courses;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const training_courses = await db.training_courses.findOne(
{ where },
{ transaction },
);
if (!training_courses) {
return training_courses;
}
const output = training_courses.get({plain: true});
output.training_requirements_course = await training_courses.getTraining_requirements_course({
transaction
});
output.user_training_records_course = await training_courses.getUser_training_records_course({
transaction
});
output.attachments = await training_courses.getAttachments({
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.file,
as: 'attachments',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'training_courses',
'name',
filter.name,
),
};
}
if (filter.description) {
where = {
...where,
[Op.and]: Utils.ilike(
'training_courses',
'description',
filter.description,
),
};
}
if (filter.link) {
where = {
...where,
[Op.and]: Utils.ilike(
'training_courses',
'link',
filter.link,
),
};
}
if (filter.duration_minutesRange) {
const [start, end] = filter.duration_minutesRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
duration_minutes: {
...where.duration_minutes,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
duration_minutes: {
...where.duration_minutes,
[Op.lte]: end,
},
};
}
}
if (filter.validity_daysRange) {
const [start, end] = filter.validity_daysRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
validity_days: {
...where.validity_days,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
validity_days: {
...where.validity_days,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.delivery_type) {
where = {
...where,
delivery_type: filter.delivery_type,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.training_courses.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(
'training_courses',
'name',
query,
),
],
};
}
const records = await db.training_courses.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,
}));
}
};