2026-04-05 14:23:32 +00:00

652 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 ShowsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const shows = await db.shows.create(
{
id: data.id || undefined,
title: data.title
||
null
,
slug: data.slug
||
null
,
summary: data.summary
||
null
,
show_type: data.show_type
||
null
,
status: data.status
||
null
,
is_featured: data.is_featured
||
false
,
release_year: data.release_year
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await shows.setCategory( data.category || null, {
transaction,
});
await shows.setOwner( data.owner || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.shows.getTableName(),
belongsToColumn: 'poster_image',
belongsToId: shows.id,
},
data.poster_image,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.shows.getTableName(),
belongsToColumn: 'banner_image',
belongsToId: shows.id,
},
data.banner_image,
options,
);
return shows;
}
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 showsData = data.map((item, index) => ({
id: item.id || undefined,
title: item.title
||
null
,
slug: item.slug
||
null
,
summary: item.summary
||
null
,
show_type: item.show_type
||
null
,
status: item.status
||
null
,
is_featured: item.is_featured
||
false
,
release_year: item.release_year
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const shows = await db.shows.bulkCreate(showsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < shows.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.shows.getTableName(),
belongsToColumn: 'poster_image',
belongsToId: shows[i].id,
},
data[i].poster_image,
options,
);
}
for (let i = 0; i < shows.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.shows.getTableName(),
belongsToColumn: 'banner_image',
belongsToId: shows[i].id,
},
data[i].banner_image,
options,
);
}
return shows;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const shows = await db.shows.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.title !== undefined) updatePayload.title = data.title;
if (data.slug !== undefined) updatePayload.slug = data.slug;
if (data.summary !== undefined) updatePayload.summary = data.summary;
if (data.show_type !== undefined) updatePayload.show_type = data.show_type;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.is_featured !== undefined) updatePayload.is_featured = data.is_featured;
if (data.release_year !== undefined) updatePayload.release_year = data.release_year;
updatePayload.updatedById = currentUser.id;
await shows.update(updatePayload, {transaction});
if (data.category !== undefined) {
await shows.setCategory(
data.category,
{ transaction }
);
}
if (data.owner !== undefined) {
await shows.setOwner(
data.owner,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.shows.getTableName(),
belongsToColumn: 'poster_image',
belongsToId: shows.id,
},
data.poster_image,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.shows.getTableName(),
belongsToColumn: 'banner_image',
belongsToId: shows.id,
},
data.banner_image,
options,
);
return shows;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const shows = await db.shows.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of shows) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of shows) {
await record.destroy({transaction});
}
});
return shows;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const shows = await db.shows.findByPk(id, options);
await shows.update({
deletedBy: currentUser.id
}, {
transaction,
});
await shows.destroy({
transaction
});
return shows;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const shows = await db.shows.findOne(
{ where },
{ transaction },
);
if (!shows) {
return shows;
}
const output = shows.get({plain: true});
output.episodes_show = await shows.getEpisodes_show({
transaction
});
output.favorites_show = await shows.getFavorites_show({
transaction
});
output.poster_image = await shows.getPoster_image({
transaction
});
output.banner_image = await shows.getBanner_image({
transaction
});
output.category = await shows.getCategory({
transaction
});
output.owner = await shows.getOwner({
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.categories,
as: 'category',
where: filter.category ? {
[Op.or]: [
{ id: { [Op.in]: filter.category.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.category.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'owner',
where: filter.owner ? {
[Op.or]: [
{ id: { [Op.in]: filter.owner.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.owner.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'poster_image',
},
{
model: db.file,
as: 'banner_image',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'shows',
'title',
filter.title,
),
};
}
if (filter.slug) {
where = {
...where,
[Op.and]: Utils.ilike(
'shows',
'slug',
filter.slug,
),
};
}
if (filter.summary) {
where = {
...where,
[Op.and]: Utils.ilike(
'shows',
'summary',
filter.summary,
),
};
}
if (filter.release_yearRange) {
const [start, end] = filter.release_yearRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
release_year: {
...where.release_year,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
release_year: {
...where.release_year,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.show_type) {
where = {
...where,
show_type: filter.show_type,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.is_featured) {
where = {
...where,
is_featured: filter.is_featured,
};
}
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.shows.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(
'shows',
'title',
query,
),
],
};
}
const records = await db.shows.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,
}));
}
};