499 lines
12 KiB
JavaScript
499 lines
12 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 Content_overlaysDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const content_overlays = await db.content_overlays.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
overlay_name: data.overlay_name
|
|
||
|
|
null
|
|
,
|
|
|
|
tone: data.tone
|
|
||
|
|
null
|
|
,
|
|
|
|
headline_template: data.headline_template
|
|
||
|
|
null
|
|
,
|
|
|
|
body_template: data.body_template
|
|
||
|
|
null
|
|
,
|
|
|
|
proceed_label: data.proceed_label
|
|
||
|
|
null
|
|
,
|
|
|
|
protect_label: data.protect_label
|
|
||
|
|
null
|
|
,
|
|
|
|
is_default: data.is_default
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
applies_to: data.applies_to
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return content_overlays;
|
|
}
|
|
|
|
|
|
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 content_overlaysData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
overlay_name: item.overlay_name
|
|
||
|
|
null
|
|
,
|
|
|
|
tone: item.tone
|
|
||
|
|
null
|
|
,
|
|
|
|
headline_template: item.headline_template
|
|
||
|
|
null
|
|
,
|
|
|
|
body_template: item.body_template
|
|
||
|
|
null
|
|
,
|
|
|
|
proceed_label: item.proceed_label
|
|
||
|
|
null
|
|
,
|
|
|
|
protect_label: item.protect_label
|
|
||
|
|
null
|
|
,
|
|
|
|
is_default: item.is_default
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
applies_to: item.applies_to
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const content_overlays = await db.content_overlays.bulkCreate(content_overlaysData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return content_overlays;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const content_overlays = await db.content_overlays.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.overlay_name !== undefined) updatePayload.overlay_name = data.overlay_name;
|
|
|
|
|
|
if (data.tone !== undefined) updatePayload.tone = data.tone;
|
|
|
|
|
|
if (data.headline_template !== undefined) updatePayload.headline_template = data.headline_template;
|
|
|
|
|
|
if (data.body_template !== undefined) updatePayload.body_template = data.body_template;
|
|
|
|
|
|
if (data.proceed_label !== undefined) updatePayload.proceed_label = data.proceed_label;
|
|
|
|
|
|
if (data.protect_label !== undefined) updatePayload.protect_label = data.protect_label;
|
|
|
|
|
|
if (data.is_default !== undefined) updatePayload.is_default = data.is_default;
|
|
|
|
|
|
if (data.applies_to !== undefined) updatePayload.applies_to = data.applies_to;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await content_overlays.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return content_overlays;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const content_overlays = await db.content_overlays.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of content_overlays) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of content_overlays) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return content_overlays;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const content_overlays = await db.content_overlays.findByPk(id, options);
|
|
|
|
await content_overlays.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await content_overlays.destroy({
|
|
transaction
|
|
});
|
|
|
|
return content_overlays;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const content_overlays = await db.content_overlays.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!content_overlays) {
|
|
return content_overlays;
|
|
}
|
|
|
|
const output = content_overlays.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = [
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.overlay_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'content_overlays',
|
|
'overlay_name',
|
|
filter.overlay_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.headline_template) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'content_overlays',
|
|
'headline_template',
|
|
filter.headline_template,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.body_template) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'content_overlays',
|
|
'body_template',
|
|
filter.body_template,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.proceed_label) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'content_overlays',
|
|
'proceed_label',
|
|
filter.proceed_label,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.protect_label) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'content_overlays',
|
|
'protect_label',
|
|
filter.protect_label,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.tone) {
|
|
where = {
|
|
...where,
|
|
tone: filter.tone,
|
|
};
|
|
}
|
|
|
|
if (filter.is_default) {
|
|
where = {
|
|
...where,
|
|
is_default: filter.is_default,
|
|
};
|
|
}
|
|
|
|
if (filter.applies_to) {
|
|
where = {
|
|
...where,
|
|
applies_to: filter.applies_to,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.content_overlays.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(
|
|
'content_overlays',
|
|
'overlay_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.content_overlays.findAll({
|
|
attributes: [ 'id', 'overlay_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['overlay_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.overlay_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|