575 lines
14 KiB
JavaScript
575 lines
14 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 Curl_templatesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const curl_templates = await db.curl_templates.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
template_name: data.template_name
|
|
||
|
|
null
|
|
,
|
|
|
|
api_style: data.api_style
|
|
||
|
|
null
|
|
,
|
|
|
|
http_method: data.http_method
|
|
||
|
|
null
|
|
,
|
|
|
|
path: data.path
|
|
||
|
|
null
|
|
,
|
|
|
|
header_auth_name: data.header_auth_name
|
|
||
|
|
null
|
|
,
|
|
|
|
header_auth_prefix: data.header_auth_prefix
|
|
||
|
|
null
|
|
,
|
|
|
|
body_template: data.body_template
|
|
||
|
|
null
|
|
,
|
|
|
|
is_default: data.is_default
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await curl_templates.setEndpoint( data.endpoint || null, {
|
|
transaction,
|
|
});
|
|
|
|
await curl_templates.setModel( data.model || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return curl_templates;
|
|
}
|
|
|
|
|
|
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 curl_templatesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
template_name: item.template_name
|
|
||
|
|
null
|
|
,
|
|
|
|
api_style: item.api_style
|
|
||
|
|
null
|
|
,
|
|
|
|
http_method: item.http_method
|
|
||
|
|
null
|
|
,
|
|
|
|
path: item.path
|
|
||
|
|
null
|
|
,
|
|
|
|
header_auth_name: item.header_auth_name
|
|
||
|
|
null
|
|
,
|
|
|
|
header_auth_prefix: item.header_auth_prefix
|
|
||
|
|
null
|
|
,
|
|
|
|
body_template: item.body_template
|
|
||
|
|
null
|
|
,
|
|
|
|
is_default: item.is_default
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const curl_templates = await db.curl_templates.bulkCreate(curl_templatesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return curl_templates;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const curl_templates = await db.curl_templates.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.template_name !== undefined) updatePayload.template_name = data.template_name;
|
|
|
|
|
|
if (data.api_style !== undefined) updatePayload.api_style = data.api_style;
|
|
|
|
|
|
if (data.http_method !== undefined) updatePayload.http_method = data.http_method;
|
|
|
|
|
|
if (data.path !== undefined) updatePayload.path = data.path;
|
|
|
|
|
|
if (data.header_auth_name !== undefined) updatePayload.header_auth_name = data.header_auth_name;
|
|
|
|
|
|
if (data.header_auth_prefix !== undefined) updatePayload.header_auth_prefix = data.header_auth_prefix;
|
|
|
|
|
|
if (data.body_template !== undefined) updatePayload.body_template = data.body_template;
|
|
|
|
|
|
if (data.is_default !== undefined) updatePayload.is_default = data.is_default;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await curl_templates.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.endpoint !== undefined) {
|
|
await curl_templates.setEndpoint(
|
|
|
|
data.endpoint,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.model !== undefined) {
|
|
await curl_templates.setModel(
|
|
|
|
data.model,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return curl_templates;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const curl_templates = await db.curl_templates.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of curl_templates) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of curl_templates) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return curl_templates;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const curl_templates = await db.curl_templates.findByPk(id, options);
|
|
|
|
await curl_templates.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await curl_templates.destroy({
|
|
transaction
|
|
});
|
|
|
|
return curl_templates;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const curl_templates = await db.curl_templates.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!curl_templates) {
|
|
return curl_templates;
|
|
}
|
|
|
|
const output = curl_templates.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.endpoint = await curl_templates.getEndpoint({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.model = await curl_templates.getModel({
|
|
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.llm_endpoints,
|
|
as: 'endpoint',
|
|
|
|
where: filter.endpoint ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.endpoint.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
endpoint_name: {
|
|
[Op.or]: filter.endpoint.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.model_catalog,
|
|
as: 'model',
|
|
|
|
where: filter.model ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.model.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
model_name: {
|
|
[Op.or]: filter.model.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.template_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'curl_templates',
|
|
'template_name',
|
|
filter.template_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.http_method) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'curl_templates',
|
|
'http_method',
|
|
filter.http_method,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.path) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'curl_templates',
|
|
'path',
|
|
filter.path,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.header_auth_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'curl_templates',
|
|
'header_auth_name',
|
|
filter.header_auth_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.header_auth_prefix) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'curl_templates',
|
|
'header_auth_prefix',
|
|
filter.header_auth_prefix,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.body_template) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'curl_templates',
|
|
'body_template',
|
|
filter.body_template,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.api_style) {
|
|
where = {
|
|
...where,
|
|
api_style: filter.api_style,
|
|
};
|
|
}
|
|
|
|
if (filter.is_default) {
|
|
where = {
|
|
...where,
|
|
is_default: filter.is_default,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.curl_templates.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(
|
|
'curl_templates',
|
|
'template_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.curl_templates.findAll({
|
|
attributes: [ 'id', 'template_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['template_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.template_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|