37795-vm/backend/src/db/api/rename_jobs.js
2026-01-25 07:35:49 +00:00

582 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 Rename_jobsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const rename_jobs = await db.rename_jobs.create(
{
id: data.id || undefined,
original_filename: data.original_filename
||
null
,
new_filename: data.new_filename
||
null
,
epoch_millis: data.epoch_millis
||
null
,
scheduled_at: data.scheduled_at
||
null
,
completed_at: data.completed_at
||
null
,
status: data.status
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await rename_jobs.setTarget_file( data.target_file || null, {
transaction,
});
await rename_jobs.setPerformed_by( data.performed_by || null, {
transaction,
});
return rename_jobs;
}
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 rename_jobsData = data.map((item, index) => ({
id: item.id || undefined,
original_filename: item.original_filename
||
null
,
new_filename: item.new_filename
||
null
,
epoch_millis: item.epoch_millis
||
null
,
scheduled_at: item.scheduled_at
||
null
,
completed_at: item.completed_at
||
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 rename_jobs = await db.rename_jobs.bulkCreate(rename_jobsData, { transaction });
// For each item created, replace relation files
return rename_jobs;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const rename_jobs = await db.rename_jobs.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.original_filename !== undefined) updatePayload.original_filename = data.original_filename;
if (data.new_filename !== undefined) updatePayload.new_filename = data.new_filename;
if (data.epoch_millis !== undefined) updatePayload.epoch_millis = data.epoch_millis;
if (data.scheduled_at !== undefined) updatePayload.scheduled_at = data.scheduled_at;
if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at;
if (data.status !== undefined) updatePayload.status = data.status;
updatePayload.updatedById = currentUser.id;
await rename_jobs.update(updatePayload, {transaction});
if (data.target_file !== undefined) {
await rename_jobs.setTarget_file(
data.target_file,
{ transaction }
);
}
if (data.performed_by !== undefined) {
await rename_jobs.setPerformed_by(
data.performed_by,
{ transaction }
);
}
return rename_jobs;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const rename_jobs = await db.rename_jobs.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of rename_jobs) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of rename_jobs) {
await record.destroy({transaction});
}
});
return rename_jobs;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const rename_jobs = await db.rename_jobs.findByPk(id, options);
await rename_jobs.update({
deletedBy: currentUser.id
}, {
transaction,
});
await rename_jobs.destroy({
transaction
});
return rename_jobs;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const rename_jobs = await db.rename_jobs.findOne(
{ where },
{ transaction },
);
if (!rename_jobs) {
return rename_jobs;
}
const output = rename_jobs.get({plain: true});
output.target_file = await rename_jobs.getTarget_file({
transaction
});
output.performed_by = await rename_jobs.getPerformed_by({
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.text_files,
as: 'target_file',
where: filter.target_file ? {
[Op.or]: [
{ id: { [Op.in]: filter.target_file.split('|').map(term => Utils.uuid(term)) } },
{
filename: {
[Op.or]: filter.target_file.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'performed_by',
where: filter.performed_by ? {
[Op.or]: [
{ id: { [Op.in]: filter.performed_by.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.performed_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.original_filename) {
where = {
...where,
[Op.and]: Utils.ilike(
'rename_jobs',
'original_filename',
filter.original_filename,
),
};
}
if (filter.new_filename) {
where = {
...where,
[Op.and]: Utils.ilike(
'rename_jobs',
'new_filename',
filter.new_filename,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
scheduled_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
completed_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.epoch_millisRange) {
const [start, end] = filter.epoch_millisRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
epoch_millis: {
...where.epoch_millis,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
epoch_millis: {
...where.epoch_millis,
[Op.lte]: end,
},
};
}
}
if (filter.scheduled_atRange) {
const [start, end] = filter.scheduled_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
scheduled_at: {
...where.scheduled_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
scheduled_at: {
...where.scheduled_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.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.rename_jobs.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(
'rename_jobs',
'original_filename',
query,
),
],
};
}
const records = await db.rename_jobs.findAll({
attributes: [ 'id', 'original_filename' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['original_filename', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.original_filename,
}));
}
};