422 lines
10 KiB
JavaScript
422 lines
10 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 Wrapped_highlightsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const wrapped_highlights = await db.wrapped_highlights.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
position: data.position
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await wrapped_highlights.setWrapped_run( data.wrapped_run || null, {
|
|
transaction,
|
|
});
|
|
|
|
await wrapped_highlights.setPhoto( data.photo || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return wrapped_highlights;
|
|
}
|
|
|
|
|
|
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 wrapped_highlightsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
position: item.position
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const wrapped_highlights = await db.wrapped_highlights.bulkCreate(wrapped_highlightsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return wrapped_highlights;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const wrapped_highlights = await db.wrapped_highlights.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.position !== undefined) updatePayload.position = data.position;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await wrapped_highlights.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.wrapped_run !== undefined) {
|
|
await wrapped_highlights.setWrapped_run(
|
|
|
|
data.wrapped_run,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.photo !== undefined) {
|
|
await wrapped_highlights.setPhoto(
|
|
|
|
data.photo,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return wrapped_highlights;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const wrapped_highlights = await db.wrapped_highlights.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of wrapped_highlights) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of wrapped_highlights) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return wrapped_highlights;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const wrapped_highlights = await db.wrapped_highlights.findByPk(id, options);
|
|
|
|
await wrapped_highlights.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await wrapped_highlights.destroy({
|
|
transaction
|
|
});
|
|
|
|
return wrapped_highlights;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const wrapped_highlights = await db.wrapped_highlights.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!wrapped_highlights) {
|
|
return wrapped_highlights;
|
|
}
|
|
|
|
const output = wrapped_highlights.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.wrapped_run = await wrapped_highlights.getWrapped_run({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.photo = await wrapped_highlights.getPhoto({
|
|
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.wrapped_runs,
|
|
as: 'wrapped_run',
|
|
|
|
where: filter.wrapped_run ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.wrapped_run.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
tagline: {
|
|
[Op.or]: filter.wrapped_run.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.photos,
|
|
as: 'photo',
|
|
|
|
where: filter.photo ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.photo.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
storage_path: {
|
|
[Op.or]: filter.photo.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.positionRange) {
|
|
const [start, end] = filter.positionRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
position: {
|
|
...where.position,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
position: {
|
|
...where.position,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.wrapped_highlights.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(
|
|
'wrapped_highlights',
|
|
'position',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.wrapped_highlights.findAll({
|
|
attributes: [ 'id', 'position' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['position', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.position,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|