40319-vm/backend/src/db/api/generations.js
2026-06-23 10:22:17 +00:00

797 lines
20 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 GenerationsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const generations = await db.generations.create(
{
id: data.id || undefined,
generated_at: data.generated_at
||
null
,
language: data.language
||
null
,
narration_person: data.narration_person
||
null
,
situation_type: data.situation_type
||
null
,
size_preset: data.size_preset
||
null
,
layout_width: data.layout_width
||
null
,
decision_count: data.decision_count
||
null
,
decision_quality: data.decision_quality
||
null
,
dialogue_length: data.dialogue_length
||
null
,
require_non_repetition: data.require_non_repetition
||
false
,
font_family: data.font_family
||
null
,
primary_color: data.primary_color
||
null
,
secondary_color: data.secondary_color
||
null
,
protagonist_name_color: data.protagonist_name_color
||
null
,
secondary_names_color: data.secondary_names_color
||
null
,
visual_theme: data.visual_theme
||
null
,
free_text_instructions: data.free_text_instructions
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await generations.setScenario_template( data.scenario_template || null, {
transaction,
});
await generations.setProtagonist_profile( data.protagonist_profile || null, {
transaction,
});
return generations;
}
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 generationsData = data.map((item, index) => ({
id: item.id || undefined,
generated_at: item.generated_at
||
null
,
language: item.language
||
null
,
narration_person: item.narration_person
||
null
,
situation_type: item.situation_type
||
null
,
size_preset: item.size_preset
||
null
,
layout_width: item.layout_width
||
null
,
decision_count: item.decision_count
||
null
,
decision_quality: item.decision_quality
||
null
,
dialogue_length: item.dialogue_length
||
null
,
require_non_repetition: item.require_non_repetition
||
false
,
font_family: item.font_family
||
null
,
primary_color: item.primary_color
||
null
,
secondary_color: item.secondary_color
||
null
,
protagonist_name_color: item.protagonist_name_color
||
null
,
secondary_names_color: item.secondary_names_color
||
null
,
visual_theme: item.visual_theme
||
null
,
free_text_instructions: item.free_text_instructions
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const generations = await db.generations.bulkCreate(generationsData, { transaction });
// For each item created, replace relation files
return generations;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const generations = await db.generations.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.generated_at !== undefined) updatePayload.generated_at = data.generated_at;
if (data.language !== undefined) updatePayload.language = data.language;
if (data.narration_person !== undefined) updatePayload.narration_person = data.narration_person;
if (data.situation_type !== undefined) updatePayload.situation_type = data.situation_type;
if (data.size_preset !== undefined) updatePayload.size_preset = data.size_preset;
if (data.layout_width !== undefined) updatePayload.layout_width = data.layout_width;
if (data.decision_count !== undefined) updatePayload.decision_count = data.decision_count;
if (data.decision_quality !== undefined) updatePayload.decision_quality = data.decision_quality;
if (data.dialogue_length !== undefined) updatePayload.dialogue_length = data.dialogue_length;
if (data.require_non_repetition !== undefined) updatePayload.require_non_repetition = data.require_non_repetition;
if (data.font_family !== undefined) updatePayload.font_family = data.font_family;
if (data.primary_color !== undefined) updatePayload.primary_color = data.primary_color;
if (data.secondary_color !== undefined) updatePayload.secondary_color = data.secondary_color;
if (data.protagonist_name_color !== undefined) updatePayload.protagonist_name_color = data.protagonist_name_color;
if (data.secondary_names_color !== undefined) updatePayload.secondary_names_color = data.secondary_names_color;
if (data.visual_theme !== undefined) updatePayload.visual_theme = data.visual_theme;
if (data.free_text_instructions !== undefined) updatePayload.free_text_instructions = data.free_text_instructions;
updatePayload.updatedById = currentUser.id;
await generations.update(updatePayload, {transaction});
if (data.scenario_template !== undefined) {
await generations.setScenario_template(
data.scenario_template,
{ transaction }
);
}
if (data.protagonist_profile !== undefined) {
await generations.setProtagonist_profile(
data.protagonist_profile,
{ transaction }
);
}
return generations;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const generations = await db.generations.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of generations) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of generations) {
await record.destroy({transaction});
}
});
return generations;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const generations = await db.generations.findByPk(id, options);
await generations.update({
deletedBy: currentUser.id
}, {
transaction,
});
await generations.destroy({
transaction
});
return generations;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const generations = await db.generations.findOne(
{ where },
{ transaction },
);
if (!generations) {
return generations;
}
const output = generations.get({plain: true});
output.situations_generation = await generations.getSituations_generation({
transaction
});
output.scenario_template = await generations.getScenario_template({
transaction
});
output.protagonist_profile = await generations.getProtagonist_profile({
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.scenario_templates,
as: 'scenario_template',
where: filter.scenario_template ? {
[Op.or]: [
{ id: { [Op.in]: filter.scenario_template.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.scenario_template.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.protagonist_profiles,
as: 'protagonist_profile',
where: filter.protagonist_profile ? {
[Op.or]: [
{ id: { [Op.in]: filter.protagonist_profile.split('|').map(term => Utils.uuid(term)) } },
{
display_name: {
[Op.or]: filter.protagonist_profile.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.font_family) {
where = {
...where,
[Op.and]: Utils.ilike(
'generations',
'font_family',
filter.font_family,
),
};
}
if (filter.primary_color) {
where = {
...where,
[Op.and]: Utils.ilike(
'generations',
'primary_color',
filter.primary_color,
),
};
}
if (filter.secondary_color) {
where = {
...where,
[Op.and]: Utils.ilike(
'generations',
'secondary_color',
filter.secondary_color,
),
};
}
if (filter.protagonist_name_color) {
where = {
...where,
[Op.and]: Utils.ilike(
'generations',
'protagonist_name_color',
filter.protagonist_name_color,
),
};
}
if (filter.secondary_names_color) {
where = {
...where,
[Op.and]: Utils.ilike(
'generations',
'secondary_names_color',
filter.secondary_names_color,
),
};
}
if (filter.free_text_instructions) {
where = {
...where,
[Op.and]: Utils.ilike(
'generations',
'free_text_instructions',
filter.free_text_instructions,
),
};
}
if (filter.generated_atRange) {
const [start, end] = filter.generated_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
generated_at: {
...where.generated_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
generated_at: {
...where.generated_at,
[Op.lte]: end,
},
};
}
}
if (filter.decision_countRange) {
const [start, end] = filter.decision_countRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
decision_count: {
...where.decision_count,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
decision_count: {
...where.decision_count,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.language) {
where = {
...where,
language: filter.language,
};
}
if (filter.narration_person) {
where = {
...where,
narration_person: filter.narration_person,
};
}
if (filter.situation_type) {
where = {
...where,
situation_type: filter.situation_type,
};
}
if (filter.size_preset) {
where = {
...where,
size_preset: filter.size_preset,
};
}
if (filter.layout_width) {
where = {
...where,
layout_width: filter.layout_width,
};
}
if (filter.decision_quality) {
where = {
...where,
decision_quality: filter.decision_quality,
};
}
if (filter.dialogue_length) {
where = {
...where,
dialogue_length: filter.dialogue_length,
};
}
if (filter.require_non_repetition) {
where = {
...where,
require_non_repetition: filter.require_non_repetition,
};
}
if (filter.visual_theme) {
where = {
...where,
visual_theme: filter.visual_theme,
};
}
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.generations.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(
'generations',
'free_text_instructions',
query,
),
],
};
}
const records = await db.generations.findAll({
attributes: [ 'id', 'free_text_instructions' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['free_text_instructions', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.free_text_instructions,
}));
}
};