40191-vm/backend/src/db/api/project_transfer_requests.js
2026-06-04 13:33:42 +00:00

642 lines
16 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 Project_transfer_requestsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const project_transfer_requests = await db.project_transfer_requests.create(
{
id: data.id || undefined,
status: data.status
||
null
,
claim_token: data.claim_token
||
null
,
requested_at: data.requested_at
||
null
,
expires_at: data.expires_at
||
null
,
claimed_at: data.claimed_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await project_transfer_requests.setProject( data.project || null, {
transaction,
});
await project_transfer_requests.setAnonymous_session( data.anonymous_session || null, {
transaction,
});
await project_transfer_requests.setClaimed_by( data.claimed_by || null, {
transaction,
});
await project_transfer_requests.setOrganizations( data.organizations || null, {
transaction,
});
return project_transfer_requests;
}
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 project_transfer_requestsData = data.map((item, index) => ({
id: item.id || undefined,
status: item.status
||
null
,
claim_token: item.claim_token
||
null
,
requested_at: item.requested_at
||
null
,
expires_at: item.expires_at
||
null
,
claimed_at: item.claimed_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const project_transfer_requests = await db.project_transfer_requests.bulkCreate(project_transfer_requestsData, { transaction });
// For each item created, replace relation files
return project_transfer_requests;
}
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 project_transfer_requests = await db.project_transfer_requests.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.status !== undefined) updatePayload.status = data.status;
if (data.claim_token !== undefined) updatePayload.claim_token = data.claim_token;
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
if (data.expires_at !== undefined) updatePayload.expires_at = data.expires_at;
if (data.claimed_at !== undefined) updatePayload.claimed_at = data.claimed_at;
updatePayload.updatedById = currentUser.id;
await project_transfer_requests.update(updatePayload, {transaction});
if (data.project !== undefined) {
await project_transfer_requests.setProject(
data.project,
{ transaction }
);
}
if (data.anonymous_session !== undefined) {
await project_transfer_requests.setAnonymous_session(
data.anonymous_session,
{ transaction }
);
}
if (data.claimed_by !== undefined) {
await project_transfer_requests.setClaimed_by(
data.claimed_by,
{ transaction }
);
}
if (data.organizations !== undefined) {
await project_transfer_requests.setOrganizations(
data.organizations,
{ transaction }
);
}
return project_transfer_requests;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const project_transfer_requests = await db.project_transfer_requests.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of project_transfer_requests) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of project_transfer_requests) {
await record.destroy({transaction});
}
});
return project_transfer_requests;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const project_transfer_requests = await db.project_transfer_requests.findByPk(id, options);
await project_transfer_requests.update({
deletedBy: currentUser.id
}, {
transaction,
});
await project_transfer_requests.destroy({
transaction
});
return project_transfer_requests;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const project_transfer_requests = await db.project_transfer_requests.findOne(
{ where },
{ transaction },
);
if (!project_transfer_requests) {
return project_transfer_requests;
}
const output = project_transfer_requests.get({plain: true});
output.project = await project_transfer_requests.getProject({
transaction
});
output.anonymous_session = await project_transfer_requests.getAnonymous_session({
transaction
});
output.claimed_by = await project_transfer_requests.getClaimed_by({
transaction
});
output.organizations = await project_transfer_requests.getOrganizations({
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 userOrganizations = (user && user.organizations?.id) || null;
if (userOrganizations) {
if (options?.currentUser?.organizationsId) {
where.organizationsId = options.currentUser.organizationsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.projects,
as: 'project',
where: filter.project ? {
[Op.or]: [
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.anonymous_sessions,
as: 'anonymous_session',
where: filter.anonymous_session ? {
[Op.or]: [
{ id: { [Op.in]: filter.anonymous_session.split('|').map(term => Utils.uuid(term)) } },
{
session_token: {
[Op.or]: filter.anonymous_session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'claimed_by',
where: filter.claimed_by ? {
[Op.or]: [
{ id: { [Op.in]: filter.claimed_by.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.claimed_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.organizations,
as: 'organizations',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.claim_token) {
where = {
...where,
[Op.and]: Utils.ilike(
'project_transfer_requests',
'claim_token',
filter.claim_token,
),
};
}
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.expires_atRange) {
const [start, end] = filter.expires_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
expires_at: {
...where.expires_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
expires_at: {
...where.expires_at,
[Op.lte]: end,
},
};
}
}
if (filter.claimed_atRange) {
const [start, end] = filter.claimed_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
claimed_at: {
...where.claimed_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
claimed_at: {
...where.claimed_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.organizations) {
const listItems = filter.organizations.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
organizationsId: {[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.organizationsId;
}
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.project_transfer_requests.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(
'project_transfer_requests',
'claim_token',
query,
),
],
};
}
const records = await db.project_transfer_requests.findAll({
attributes: [ 'id', 'claim_token' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['claim_token', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.claim_token,
}));
}
};