39355-vm/backend/src/db/api/access_grants.js
2026-03-28 00:06:46 +00:00

506 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 Access_grantsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const access_grants = await db.access_grants.create(
{
id: data.id || undefined,
access_level: data.access_level
||
null
,
is_enabled: data.is_enabled
||
false
,
granted_at: data.granted_at
||
null
,
expires_at: data.expires_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await access_grants.setUser( data.user || null, {
transaction,
});
await access_grants.setWeb_app( data.web_app || null, {
transaction,
});
return access_grants;
}
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 access_grantsData = data.map((item, index) => ({
id: item.id || undefined,
access_level: item.access_level
||
null
,
is_enabled: item.is_enabled
||
false
,
granted_at: item.granted_at
||
null
,
expires_at: item.expires_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const access_grants = await db.access_grants.bulkCreate(access_grantsData, { transaction });
// For each item created, replace relation files
return access_grants;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const access_grants = await db.access_grants.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.access_level !== undefined) updatePayload.access_level = data.access_level;
if (data.is_enabled !== undefined) updatePayload.is_enabled = data.is_enabled;
if (data.granted_at !== undefined) updatePayload.granted_at = data.granted_at;
if (data.expires_at !== undefined) updatePayload.expires_at = data.expires_at;
updatePayload.updatedById = currentUser.id;
await access_grants.update(updatePayload, {transaction});
if (data.user !== undefined) {
await access_grants.setUser(
data.user,
{ transaction }
);
}
if (data.web_app !== undefined) {
await access_grants.setWeb_app(
data.web_app,
{ transaction }
);
}
return access_grants;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const access_grants = await db.access_grants.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of access_grants) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of access_grants) {
await record.destroy({transaction});
}
});
return access_grants;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const access_grants = await db.access_grants.findByPk(id, options);
await access_grants.update({
deletedBy: currentUser.id
}, {
transaction,
});
await access_grants.destroy({
transaction
});
return access_grants;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const access_grants = await db.access_grants.findOne(
{ where },
{ transaction },
);
if (!access_grants) {
return access_grants;
}
const output = access_grants.get({plain: true});
output.user = await access_grants.getUser({
transaction
});
output.web_app = await access_grants.getWeb_app({
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: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.web_apps,
as: 'web_app',
where: filter.web_app ? {
[Op.or]: [
{ id: { [Op.in]: filter.web_app.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.web_app.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.granted_atRange) {
const [start, end] = filter.granted_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
granted_at: {
...where.granted_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
granted_at: {
...where.granted_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.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.access_level) {
where = {
...where,
access_level: filter.access_level,
};
}
if (filter.is_enabled) {
where = {
...where,
is_enabled: filter.is_enabled,
};
}
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.access_grants.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(
'access_grants',
'access_level',
query,
),
],
};
}
const records = await db.access_grants.findAll({
attributes: [ 'id', 'access_level' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['access_level', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.access_level,
}));
}
};