38591-vm/backend/src/db/api/api_endpoints.js
2026-02-19 03:08:38 +00:00

477 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 Api_endpointsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const api_endpoints = await db.api_endpoints.create(
{
id: data.id || undefined,
endpoint_name: data.endpoint_name
||
null
,
path: data.path
||
null
,
method: data.method
||
null
,
is_enabled: data.is_enabled
||
false
,
auth_scheme: data.auth_scheme
||
null
,
allowed_origins: data.allowed_origins
||
null
,
description: data.description
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return api_endpoints;
}
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 api_endpointsData = data.map((item, index) => ({
id: item.id || undefined,
endpoint_name: item.endpoint_name
||
null
,
path: item.path
||
null
,
method: item.method
||
null
,
is_enabled: item.is_enabled
||
false
,
auth_scheme: item.auth_scheme
||
null
,
allowed_origins: item.allowed_origins
||
null
,
description: item.description
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const api_endpoints = await db.api_endpoints.bulkCreate(api_endpointsData, { transaction });
// For each item created, replace relation files
return api_endpoints;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const api_endpoints = await db.api_endpoints.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.endpoint_name !== undefined) updatePayload.endpoint_name = data.endpoint_name;
if (data.path !== undefined) updatePayload.path = data.path;
if (data.method !== undefined) updatePayload.method = data.method;
if (data.is_enabled !== undefined) updatePayload.is_enabled = data.is_enabled;
if (data.auth_scheme !== undefined) updatePayload.auth_scheme = data.auth_scheme;
if (data.allowed_origins !== undefined) updatePayload.allowed_origins = data.allowed_origins;
if (data.description !== undefined) updatePayload.description = data.description;
updatePayload.updatedById = currentUser.id;
await api_endpoints.update(updatePayload, {transaction});
return api_endpoints;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const api_endpoints = await db.api_endpoints.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of api_endpoints) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of api_endpoints) {
await record.destroy({transaction});
}
});
return api_endpoints;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const api_endpoints = await db.api_endpoints.findByPk(id, options);
await api_endpoints.update({
deletedBy: currentUser.id
}, {
transaction,
});
await api_endpoints.destroy({
transaction
});
return api_endpoints;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const api_endpoints = await db.api_endpoints.findOne(
{ where },
{ transaction },
);
if (!api_endpoints) {
return api_endpoints;
}
const output = api_endpoints.get({plain: true});
output.roblox_scripts_endpoint = await api_endpoints.getRoblox_scripts_endpoint({
transaction
});
output.key_access_logs_endpoint = await api_endpoints.getKey_access_logs_endpoint({
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 = [
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.endpoint_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'api_endpoints',
'endpoint_name',
filter.endpoint_name,
),
};
}
if (filter.path) {
where = {
...where,
[Op.and]: Utils.ilike(
'api_endpoints',
'path',
filter.path,
),
};
}
if (filter.allowed_origins) {
where = {
...where,
[Op.and]: Utils.ilike(
'api_endpoints',
'allowed_origins',
filter.allowed_origins,
),
};
}
if (filter.description) {
where = {
...where,
[Op.and]: Utils.ilike(
'api_endpoints',
'description',
filter.description,
),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.method) {
where = {
...where,
method: filter.method,
};
}
if (filter.is_enabled) {
where = {
...where,
is_enabled: filter.is_enabled,
};
}
if (filter.auth_scheme) {
where = {
...where,
auth_scheme: filter.auth_scheme,
};
}
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.api_endpoints.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(
'api_endpoints',
'endpoint_name',
query,
),
],
};
}
const records = await db.api_endpoints.findAll({
attributes: [ 'id', 'endpoint_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['endpoint_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.endpoint_name,
}));
}
};