39271-vm/backend/src/db/api/system_backups.js
2026-03-23 04:48:34 +00:00

536 lines
13 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 System_backupsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const system_backups = await db.system_backups.create(
{
id: data.id || undefined,
backup_type: data.backup_type
||
null
,
backup_at: data.backup_at
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await system_backups.setSchool( data.school || null, {
transaction,
});
await system_backups.setCreated_by_user( data.created_by_user || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.system_backups.getTableName(),
belongsToColumn: 'backup_file',
belongsToId: system_backups.id,
},
data.backup_file,
options,
);
return system_backups;
}
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 system_backupsData = data.map((item, index) => ({
id: item.id || undefined,
backup_type: item.backup_type
||
null
,
backup_at: item.backup_at
||
null
,
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 system_backups = await db.system_backups.bulkCreate(system_backupsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < system_backups.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.system_backups.getTableName(),
belongsToColumn: 'backup_file',
belongsToId: system_backups[i].id,
},
data[i].backup_file,
options,
);
}
return system_backups;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const globalAccess = currentUser.app_role?.globalAccess;
const system_backups = await db.system_backups.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.backup_type !== undefined) updatePayload.backup_type = data.backup_type;
if (data.backup_at !== undefined) updatePayload.backup_at = data.backup_at;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await system_backups.update(updatePayload, {transaction});
if (data.school !== undefined) {
await system_backups.setSchool(
data.school,
{ transaction }
);
}
if (data.created_by_user !== undefined) {
await system_backups.setCreated_by_user(
data.created_by_user,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.system_backups.getTableName(),
belongsToColumn: 'backup_file',
belongsToId: system_backups.id,
},
data.backup_file,
options,
);
return system_backups;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const system_backups = await db.system_backups.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of system_backups) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of system_backups) {
await record.destroy({transaction});
}
});
return system_backups;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const system_backups = await db.system_backups.findByPk(id, options);
await system_backups.update({
deletedBy: currentUser.id
}, {
transaction,
});
await system_backups.destroy({
transaction
});
return system_backups;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const system_backups = await db.system_backups.findOne(
{ where },
{ transaction },
);
if (!system_backups) {
return system_backups;
}
const output = system_backups.get({plain: true});
output.school = await system_backups.getSchool({
transaction
});
output.backup_file = await system_backups.getBackup_file({
transaction
});
output.created_by_user = await system_backups.getCreated_by_user({
transaction
});
return output;
}
static async findAll(
filter,
globalAccess, options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
const user = (options && options.currentUser) || null;
const userSchools = (user && user.schools?.id) || null;
if (userSchools) {
if (options?.currentUser?.schoolsId) {
where.schoolsId = options.currentUser.schoolsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.schools,
as: 'school',
},
{
model: db.users,
as: 'created_by_user',
where: filter.created_by_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.created_by_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.created_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'backup_file',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'system_backups',
'notes',
filter.notes,
),
};
}
if (filter.backup_atRange) {
const [start, end] = filter.backup_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
backup_at: {
...where.backup_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
backup_at: {
...where.backup_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.backup_type) {
where = {
...where,
backup_type: filter.backup_type,
};
}
if (filter.school) {
const listItems = filter.school.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
schoolId: {[Op.or]: listItems}
};
}
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,
},
};
}
}
}
if (globalAccess) {
delete where.schoolsId;
}
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.system_backups.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, globalAccess, organizationId,) {
let where = {};
if (!globalAccess && organizationId) {
where.organizationId = organizationId;
}
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'system_backups',
'notes',
query,
),
],
};
}
const records = await db.system_backups.findAll({
attributes: [ 'id', 'notes' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['notes', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.notes,
}));
}
};