38296-vm/backend/src/db/api/task_exports.js
2026-02-08 20:23:46 +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 Task_exportsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const task_exports = await db.task_exports.create(
{
id: data.id || undefined,
format: data.format
||
null
,
status: data.status
||
null
,
requested_at: data.requested_at
||
null
,
completed_at: data.completed_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await task_exports.setRequested_by( data.requested_by || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.task_exports.getTableName(),
belongsToColumn: 'file',
belongsToId: task_exports.id,
},
data.file,
options,
);
return task_exports;
}
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 task_exportsData = data.map((item, index) => ({
id: item.id || undefined,
format: item.format
||
null
,
status: item.status
||
null
,
requested_at: item.requested_at
||
null
,
completed_at: item.completed_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const task_exports = await db.task_exports.bulkCreate(task_exportsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < task_exports.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.task_exports.getTableName(),
belongsToColumn: 'file',
belongsToId: task_exports[i].id,
},
data[i].file,
options,
);
}
return task_exports;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const task_exports = await db.task_exports.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.format !== undefined) updatePayload.format = data.format;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at;
updatePayload.updatedById = currentUser.id;
await task_exports.update(updatePayload, {transaction});
if (data.requested_by !== undefined) {
await task_exports.setRequested_by(
data.requested_by,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.task_exports.getTableName(),
belongsToColumn: 'file',
belongsToId: task_exports.id,
},
data.file,
options,
);
return task_exports;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const task_exports = await db.task_exports.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of task_exports) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of task_exports) {
await record.destroy({transaction});
}
});
return task_exports;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const task_exports = await db.task_exports.findByPk(id, options);
await task_exports.update({
deletedBy: currentUser.id
}, {
transaction,
});
await task_exports.destroy({
transaction
});
return task_exports;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const task_exports = await db.task_exports.findOne(
{ where },
{ transaction },
);
if (!task_exports) {
return task_exports;
}
const output = task_exports.get({plain: true});
output.requested_by = await task_exports.getRequested_by({
transaction
});
output.file = await task_exports.getFile({
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: 'requested_by',
where: filter.requested_by ? {
[Op.or]: [
{ id: { [Op.in]: filter.requested_by.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.requested_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'file',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.requested_atRange) {
const [start, end] = filter.requested_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
requested_at: {
...where.requested_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
requested_at: {
...where.requested_at,
[Op.lte]: end,
},
};
}
}
if (filter.completed_atRange) {
const [start, end] = filter.completed_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.format) {
where = {
...where,
format: filter.format,
};
}
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.task_exports.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(
'task_exports',
'format',
query,
),
],
};
}
const records = await db.task_exports.findAll({
attributes: [ 'id', 'format' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['format', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.format,
}));
}
};