691 lines
18 KiB
JavaScript
691 lines
18 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_requestsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const api_requests = await db.api_requests.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
requested_at: data.requested_at
|
|
||
|
|
null
|
|
,
|
|
|
|
responded_at: data.responded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
endpoint: data.endpoint
|
|
||
|
|
null
|
|
,
|
|
|
|
method: data.method
|
|
||
|
|
null
|
|
,
|
|
|
|
status_code: data.status_code
|
|
||
|
|
null
|
|
,
|
|
|
|
latency_ms: data.latency_ms
|
|
||
|
|
null
|
|
,
|
|
|
|
request_payload_json: data.request_payload_json
|
|
||
|
|
null
|
|
,
|
|
|
|
response_payload_json: data.response_payload_json
|
|
||
|
|
null
|
|
,
|
|
|
|
client_ip: data.client_ip
|
|
||
|
|
null
|
|
,
|
|
|
|
user_agent: data.user_agent
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await api_requests.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await api_requests.setRisk_assessment( data.risk_assessment || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return api_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 api_requestsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
requested_at: item.requested_at
|
|
||
|
|
null
|
|
,
|
|
|
|
responded_at: item.responded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
endpoint: item.endpoint
|
|
||
|
|
null
|
|
,
|
|
|
|
method: item.method
|
|
||
|
|
null
|
|
,
|
|
|
|
status_code: item.status_code
|
|
||
|
|
null
|
|
,
|
|
|
|
latency_ms: item.latency_ms
|
|
||
|
|
null
|
|
,
|
|
|
|
request_payload_json: item.request_payload_json
|
|
||
|
|
null
|
|
,
|
|
|
|
response_payload_json: item.response_payload_json
|
|
||
|
|
null
|
|
,
|
|
|
|
client_ip: item.client_ip
|
|
||
|
|
null
|
|
,
|
|
|
|
user_agent: item.user_agent
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const api_requests = await db.api_requests.bulkCreate(api_requestsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return api_requests;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const api_requests = await db.api_requests.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
|
|
|
|
|
|
if (data.responded_at !== undefined) updatePayload.responded_at = data.responded_at;
|
|
|
|
|
|
if (data.endpoint !== undefined) updatePayload.endpoint = data.endpoint;
|
|
|
|
|
|
if (data.method !== undefined) updatePayload.method = data.method;
|
|
|
|
|
|
if (data.status_code !== undefined) updatePayload.status_code = data.status_code;
|
|
|
|
|
|
if (data.latency_ms !== undefined) updatePayload.latency_ms = data.latency_ms;
|
|
|
|
|
|
if (data.request_payload_json !== undefined) updatePayload.request_payload_json = data.request_payload_json;
|
|
|
|
|
|
if (data.response_payload_json !== undefined) updatePayload.response_payload_json = data.response_payload_json;
|
|
|
|
|
|
if (data.client_ip !== undefined) updatePayload.client_ip = data.client_ip;
|
|
|
|
|
|
if (data.user_agent !== undefined) updatePayload.user_agent = data.user_agent;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await api_requests.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.user !== undefined) {
|
|
await api_requests.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.risk_assessment !== undefined) {
|
|
await api_requests.setRisk_assessment(
|
|
|
|
data.risk_assessment,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return api_requests;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const api_requests = await db.api_requests.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of api_requests) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of api_requests) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return api_requests;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const api_requests = await db.api_requests.findByPk(id, options);
|
|
|
|
await api_requests.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await api_requests.destroy({
|
|
transaction
|
|
});
|
|
|
|
return api_requests;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const api_requests = await db.api_requests.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!api_requests) {
|
|
return api_requests;
|
|
}
|
|
|
|
const output = api_requests.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.user = await api_requests.getUser({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.risk_assessment = await api_requests.getRisk_assessment({
|
|
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.risk_assessments,
|
|
as: 'risk_assessment',
|
|
|
|
where: filter.risk_assessment ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.risk_assessment.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
recommendation_text: {
|
|
[Op.or]: filter.risk_assessment.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.endpoint) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'api_requests',
|
|
'endpoint',
|
|
filter.endpoint,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.request_payload_json) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'api_requests',
|
|
'request_payload_json',
|
|
filter.request_payload_json,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.response_payload_json) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'api_requests',
|
|
'response_payload_json',
|
|
filter.response_payload_json,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.client_ip) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'api_requests',
|
|
'client_ip',
|
|
filter.client_ip,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.user_agent) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'api_requests',
|
|
'user_agent',
|
|
filter.user_agent,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
requested_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
responded_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
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.responded_atRange) {
|
|
const [start, end] = filter.responded_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
responded_at: {
|
|
...where.responded_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
responded_at: {
|
|
...where.responded_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.status_codeRange) {
|
|
const [start, end] = filter.status_codeRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
status_code: {
|
|
...where.status_code,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
status_code: {
|
|
...where.status_code,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.latency_msRange) {
|
|
const [start, end] = filter.latency_msRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
latency_ms: {
|
|
...where.latency_ms,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
latency_ms: {
|
|
...where.latency_ms,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.method) {
|
|
where = {
|
|
...where,
|
|
method: filter.method,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_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, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'api_requests',
|
|
'endpoint',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.api_requests.findAll({
|
|
attributes: [ 'id', 'endpoint' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['endpoint', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.endpoint,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|