38085-vm/backend/src/db/api/episode_scenes.js
2026-02-02 05:41:28 +00:00

696 lines
18 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 Episode_scenesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const episode_scenes = await db.episode_scenes.create(
{
id: data.id || undefined,
scene_number: data.scene_number
||
null
,
scene_title: data.scene_title
||
null
,
scene_prompt: data.scene_prompt
||
null
,
camera_style: data.camera_style
||
null
,
version: data.version
||
null
,
credit_cost_seconds: data.credit_cost_seconds
||
null
,
status: data.status
||
null
,
created_on: data.created_on
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await episode_scenes.setEpisode( data.episode || null, {
transaction,
});
await episode_scenes.setOrganizations( data.organizations || null, {
transaction,
});
await episode_scenes.setScene_characters(data.scene_characters || [], {
transaction,
});
return episode_scenes;
}
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 episode_scenesData = data.map((item, index) => ({
id: item.id || undefined,
scene_number: item.scene_number
||
null
,
scene_title: item.scene_title
||
null
,
scene_prompt: item.scene_prompt
||
null
,
camera_style: item.camera_style
||
null
,
version: item.version
||
null
,
credit_cost_seconds: item.credit_cost_seconds
||
null
,
status: item.status
||
null
,
created_on: item.created_on
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const episode_scenes = await db.episode_scenes.bulkCreate(episode_scenesData, { transaction });
// For each item created, replace relation files
return episode_scenes;
}
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 episode_scenes = await db.episode_scenes.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.scene_number !== undefined) updatePayload.scene_number = data.scene_number;
if (data.scene_title !== undefined) updatePayload.scene_title = data.scene_title;
if (data.scene_prompt !== undefined) updatePayload.scene_prompt = data.scene_prompt;
if (data.camera_style !== undefined) updatePayload.camera_style = data.camera_style;
if (data.version !== undefined) updatePayload.version = data.version;
if (data.credit_cost_seconds !== undefined) updatePayload.credit_cost_seconds = data.credit_cost_seconds;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.created_on !== undefined) updatePayload.created_on = data.created_on;
updatePayload.updatedById = currentUser.id;
await episode_scenes.update(updatePayload, {transaction});
if (data.episode !== undefined) {
await episode_scenes.setEpisode(
data.episode,
{ transaction }
);
}
if (data.organizations !== undefined) {
await episode_scenes.setOrganizations(
data.organizations,
{ transaction }
);
}
if (data.scene_characters !== undefined) {
await episode_scenes.setScene_characters(data.scene_characters, { transaction });
}
return episode_scenes;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const episode_scenes = await db.episode_scenes.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of episode_scenes) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of episode_scenes) {
await record.destroy({transaction});
}
});
return episode_scenes;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const episode_scenes = await db.episode_scenes.findByPk(id, options);
await episode_scenes.update({
deletedBy: currentUser.id
}, {
transaction,
});
await episode_scenes.destroy({
transaction
});
return episode_scenes;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const episode_scenes = await db.episode_scenes.findOne(
{ where },
{ transaction },
);
if (!episode_scenes) {
return episode_scenes;
}
const output = episode_scenes.get({plain: true});
output.renders_episode_scene = await episode_scenes.getRenders_episode_scene({
transaction
});
output.episode = await episode_scenes.getEpisode({
transaction
});
output.scene_characters = await episode_scenes.getScene_characters({
transaction
});
output.organizations = await episode_scenes.getOrganizations({
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 userOrganizations = (user && user.organizations?.id) || null;
if (userOrganizations) {
if (options?.currentUser?.organizationsId) {
where.organizationsId = options.currentUser.organizationsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.episodes,
as: 'episode',
where: filter.episode ? {
[Op.or]: [
{ id: { [Op.in]: filter.episode.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.episode.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.organizations,
as: 'organizations',
},
{
model: db.characters,
as: 'scene_characters',
required: false,
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.scene_title) {
where = {
...where,
[Op.and]: Utils.ilike(
'episode_scenes',
'scene_title',
filter.scene_title,
),
};
}
if (filter.scene_prompt) {
where = {
...where,
[Op.and]: Utils.ilike(
'episode_scenes',
'scene_prompt',
filter.scene_prompt,
),
};
}
if (filter.scene_numberRange) {
const [start, end] = filter.scene_numberRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
scene_number: {
...where.scene_number,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
scene_number: {
...where.scene_number,
[Op.lte]: end,
},
};
}
}
if (filter.versionRange) {
const [start, end] = filter.versionRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
version: {
...where.version,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
version: {
...where.version,
[Op.lte]: end,
},
};
}
}
if (filter.credit_cost_secondsRange) {
const [start, end] = filter.credit_cost_secondsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
credit_cost_seconds: {
...where.credit_cost_seconds,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
credit_cost_seconds: {
...where.credit_cost_seconds,
[Op.lte]: end,
},
};
}
}
if (filter.created_onRange) {
const [start, end] = filter.created_onRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
created_on: {
...where.created_on,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
created_on: {
...where.created_on,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.camera_style) {
where = {
...where,
camera_style: filter.camera_style,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.organizations) {
const listItems = filter.organizations.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
organizationsId: {[Op.or]: listItems}
};
}
if (filter.scene_characters) {
const searchTerms = filter.scene_characters.split('|');
include = [
{
model: db.characters,
as: 'scene_characters_filter',
required: searchTerms.length > 0,
where: searchTerms.length > 0 ? {
[Op.or]: [
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
}
}
]
} : undefined
},
...include,
]
}
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.organizationsId;
}
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.episode_scenes.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(
'episode_scenes',
'scene_title',
query,
),
],
};
}
const records = await db.episode_scenes.findAll({
attributes: [ 'id', 'scene_title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['scene_title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.scene_title,
}));
}
};