628 lines
15 KiB
JavaScript
628 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 Ocr_jobsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const ocr_jobs = await db.ocr_jobs.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
title: data.title
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
created: data.created
|
|
||
|
|
null
|
|
,
|
|
|
|
completed: data.completed
|
|
||
|
|
null
|
|
,
|
|
|
|
pages_processed: data.pages_processed
|
|
||
|
|
null
|
|
,
|
|
|
|
error_message: data.error_message
|
|
||
|
|
null
|
|
,
|
|
|
|
extracted_text: data.extracted_text
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await ocr_jobs.setDocument( data.document || null, {
|
|
transaction,
|
|
});
|
|
|
|
await ocr_jobs.setRequested_by( data.requested_by || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.ocr_jobs.getTableName(),
|
|
belongsToColumn: 'result_file',
|
|
belongsToId: ocr_jobs.id,
|
|
},
|
|
data.result_file,
|
|
options,
|
|
);
|
|
|
|
|
|
return ocr_jobs;
|
|
}
|
|
|
|
|
|
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 ocr_jobsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
title: item.title
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
created: item.created
|
|
||
|
|
null
|
|
,
|
|
|
|
completed: item.completed
|
|
||
|
|
null
|
|
,
|
|
|
|
pages_processed: item.pages_processed
|
|
||
|
|
null
|
|
,
|
|
|
|
error_message: item.error_message
|
|
||
|
|
null
|
|
,
|
|
|
|
extracted_text: item.extracted_text
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const ocr_jobs = await db.ocr_jobs.bulkCreate(ocr_jobsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < ocr_jobs.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.ocr_jobs.getTableName(),
|
|
belongsToColumn: 'result_file',
|
|
belongsToId: ocr_jobs[i].id,
|
|
},
|
|
data[i].result_file,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return ocr_jobs;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const ocr_jobs = await db.ocr_jobs.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.title !== undefined) updatePayload.title = data.title;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.created !== undefined) updatePayload.created = data.created;
|
|
|
|
|
|
if (data.completed !== undefined) updatePayload.completed = data.completed;
|
|
|
|
|
|
if (data.pages_processed !== undefined) updatePayload.pages_processed = data.pages_processed;
|
|
|
|
|
|
if (data.error_message !== undefined) updatePayload.error_message = data.error_message;
|
|
|
|
|
|
if (data.extracted_text !== undefined) updatePayload.extracted_text = data.extracted_text;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await ocr_jobs.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.document !== undefined) {
|
|
await ocr_jobs.setDocument(
|
|
|
|
data.document,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.requested_by !== undefined) {
|
|
await ocr_jobs.setRequested_by(
|
|
|
|
data.requested_by,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.ocr_jobs.getTableName(),
|
|
belongsToColumn: 'result_file',
|
|
belongsToId: ocr_jobs.id,
|
|
},
|
|
data.result_file,
|
|
options,
|
|
);
|
|
|
|
|
|
return ocr_jobs;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const ocr_jobs = await db.ocr_jobs.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of ocr_jobs) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of ocr_jobs) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return ocr_jobs;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const ocr_jobs = await db.ocr_jobs.findByPk(id, options);
|
|
|
|
await ocr_jobs.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await ocr_jobs.destroy({
|
|
transaction
|
|
});
|
|
|
|
return ocr_jobs;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const ocr_jobs = await db.ocr_jobs.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!ocr_jobs) {
|
|
return ocr_jobs;
|
|
}
|
|
|
|
const output = ocr_jobs.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.document = await ocr_jobs.getDocument({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.requested_by = await ocr_jobs.getRequested_by({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.result_file = await ocr_jobs.getResult_file({
|
|
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.documents,
|
|
as: 'document',
|
|
|
|
where: filter.document ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.document.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.document.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'requested_by',
|
|
|
|
where: filter.requested_by ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.requested_by.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.requested_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'result_file',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'ocr_jobs',
|
|
'title',
|
|
filter.title,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.error_message) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'ocr_jobs',
|
|
'error_message',
|
|
filter.error_message,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.extracted_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'ocr_jobs',
|
|
'extracted_text',
|
|
filter.extracted_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.createdRange) {
|
|
const [start, end] = filter.createdRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
created: {
|
|
...where.created,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
created: {
|
|
...where.created,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.completedRange) {
|
|
const [start, end] = filter.completedRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
completed: {
|
|
...where.completed,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
completed: {
|
|
...where.completed,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.pages_processedRange) {
|
|
const [start, end] = filter.pages_processedRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
pages_processed: {
|
|
...where.pages_processed,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
pages_processed: {
|
|
...where.pages_processed,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.ocr_jobs.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(
|
|
'ocr_jobs',
|
|
'title',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.ocr_jobs.findAll({
|
|
attributes: [ 'id', 'title' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['title', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.title,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|