598 lines
15 KiB
JavaScript
598 lines
15 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 Session_inputsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const session_inputs = await db.session_inputs.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
input_type: data.input_type
|
|
||
|
|
null
|
|
,
|
|
|
|
content: data.content
|
|
||
|
|
null
|
|
,
|
|
|
|
source_url: data.source_url
|
|
||
|
|
null
|
|
,
|
|
|
|
captured_at: data.captured_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await session_inputs.setSession( data.session || null, {
|
|
transaction,
|
|
});
|
|
|
|
await session_inputs.setWorkspaces( data.workspaces || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.session_inputs.getTableName(),
|
|
belongsToColumn: 'files',
|
|
belongsToId: session_inputs.id,
|
|
},
|
|
data.files,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.session_inputs.getTableName(),
|
|
belongsToColumn: 'images',
|
|
belongsToId: session_inputs.id,
|
|
},
|
|
data.images,
|
|
options,
|
|
);
|
|
|
|
|
|
return session_inputs;
|
|
}
|
|
|
|
|
|
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 session_inputsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
input_type: item.input_type
|
|
||
|
|
null
|
|
,
|
|
|
|
content: item.content
|
|
||
|
|
null
|
|
,
|
|
|
|
source_url: item.source_url
|
|
||
|
|
null
|
|
,
|
|
|
|
captured_at: item.captured_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const session_inputs = await db.session_inputs.bulkCreate(session_inputsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < session_inputs.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.session_inputs.getTableName(),
|
|
belongsToColumn: 'files',
|
|
belongsToId: session_inputs[i].id,
|
|
},
|
|
data[i].files,
|
|
options,
|
|
);
|
|
}
|
|
|
|
for (let i = 0; i < session_inputs.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.session_inputs.getTableName(),
|
|
belongsToColumn: 'images',
|
|
belongsToId: session_inputs[i].id,
|
|
},
|
|
data[i].images,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return session_inputs;
|
|
}
|
|
|
|
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 session_inputs = await db.session_inputs.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.input_type !== undefined) updatePayload.input_type = data.input_type;
|
|
|
|
|
|
if (data.content !== undefined) updatePayload.content = data.content;
|
|
|
|
|
|
if (data.source_url !== undefined) updatePayload.source_url = data.source_url;
|
|
|
|
|
|
if (data.captured_at !== undefined) updatePayload.captured_at = data.captured_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await session_inputs.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.session !== undefined) {
|
|
await session_inputs.setSession(
|
|
|
|
data.session,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.workspaces !== undefined) {
|
|
await session_inputs.setWorkspaces(
|
|
|
|
data.workspaces,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.session_inputs.getTableName(),
|
|
belongsToColumn: 'files',
|
|
belongsToId: session_inputs.id,
|
|
},
|
|
data.files,
|
|
options,
|
|
);
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.session_inputs.getTableName(),
|
|
belongsToColumn: 'images',
|
|
belongsToId: session_inputs.id,
|
|
},
|
|
data.images,
|
|
options,
|
|
);
|
|
|
|
|
|
return session_inputs;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const session_inputs = await db.session_inputs.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of session_inputs) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of session_inputs) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return session_inputs;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const session_inputs = await db.session_inputs.findByPk(id, options);
|
|
|
|
await session_inputs.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await session_inputs.destroy({
|
|
transaction
|
|
});
|
|
|
|
return session_inputs;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const session_inputs = await db.session_inputs.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!session_inputs) {
|
|
return session_inputs;
|
|
}
|
|
|
|
const output = session_inputs.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.session = await session_inputs.getSession({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.files = await session_inputs.getFiles({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.images = await session_inputs.getImages({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.workspaces = await session_inputs.getWorkspaces({
|
|
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 userWorkspaces = (user && user.workspaces?.id) || null;
|
|
|
|
|
|
|
|
if (userWorkspaces) {
|
|
if (options?.currentUser?.workspacesId) {
|
|
where.workspacesId = options.currentUser.workspacesId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.strategy_sessions,
|
|
as: 'session',
|
|
|
|
where: filter.session ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.session.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
session_title: {
|
|
[Op.or]: filter.session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.workspaces,
|
|
as: 'workspaces',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'files',
|
|
},
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'images',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.content) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'session_inputs',
|
|
'content',
|
|
filter.content,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.source_url) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'session_inputs',
|
|
'source_url',
|
|
filter.source_url,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.captured_atRange) {
|
|
const [start, end] = filter.captured_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
captured_at: {
|
|
...where.captured_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
captured_at: {
|
|
...where.captured_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.input_type) {
|
|
where = {
|
|
...where,
|
|
input_type: filter.input_type,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.workspaces) {
|
|
const listItems = filter.workspaces.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
workspacesId: {[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.workspacesId;
|
|
}
|
|
|
|
|
|
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.session_inputs.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(
|
|
'session_inputs',
|
|
'input_type',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.session_inputs.findAll({
|
|
attributes: [ 'id', 'input_type' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['input_type', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.input_type,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|