38635-vm/backend/src/db/api/remix_requests.js
2026-02-20 08:24:10 +00:00

661 lines
16 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 Remix_requestsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const remix_requests = await db.remix_requests.create(
{
id: data.id || undefined,
remix_type: data.remix_type
||
null
,
remix_prompt_text: data.remix_prompt_text
||
null
,
seed: data.seed
||
null
,
status: data.status
||
null
,
failure_reason: data.failure_reason
||
null
,
requested_at: data.requested_at
||
null
,
completed_at: data.completed_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await remix_requests.setSource_image( data.source_image || null, {
transaction,
});
await remix_requests.setUser( data.user || null, {
transaction,
});
await remix_requests.setStyle_preset( data.style_preset || null, {
transaction,
});
await remix_requests.setResult_job( data.result_job || null, {
transaction,
});
return remix_requests;
}
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 remix_requestsData = data.map((item, index) => ({
id: item.id || undefined,
remix_type: item.remix_type
||
null
,
remix_prompt_text: item.remix_prompt_text
||
null
,
seed: item.seed
||
null
,
status: item.status
||
null
,
failure_reason: item.failure_reason
||
null
,
requested_at: item.requested_at
||
null
,
completed_at: item.completed_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const remix_requests = await db.remix_requests.bulkCreate(remix_requestsData, { transaction });
// For each item created, replace relation files
return remix_requests;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const remix_requests = await db.remix_requests.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.remix_type !== undefined) updatePayload.remix_type = data.remix_type;
if (data.remix_prompt_text !== undefined) updatePayload.remix_prompt_text = data.remix_prompt_text;
if (data.seed !== undefined) updatePayload.seed = data.seed;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.failure_reason !== undefined) updatePayload.failure_reason = data.failure_reason;
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at;
updatePayload.updatedById = currentUser.id;
await remix_requests.update(updatePayload, {transaction});
if (data.source_image !== undefined) {
await remix_requests.setSource_image(
data.source_image,
{ transaction }
);
}
if (data.user !== undefined) {
await remix_requests.setUser(
data.user,
{ transaction }
);
}
if (data.style_preset !== undefined) {
await remix_requests.setStyle_preset(
data.style_preset,
{ transaction }
);
}
if (data.result_job !== undefined) {
await remix_requests.setResult_job(
data.result_job,
{ transaction }
);
}
return remix_requests;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const remix_requests = await db.remix_requests.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of remix_requests) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of remix_requests) {
await record.destroy({transaction});
}
});
return remix_requests;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const remix_requests = await db.remix_requests.findByPk(id, options);
await remix_requests.update({
deletedBy: currentUser.id
}, {
transaction,
});
await remix_requests.destroy({
transaction
});
return remix_requests;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const remix_requests = await db.remix_requests.findOne(
{ where },
{ transaction },
);
if (!remix_requests) {
return remix_requests;
}
const output = remix_requests.get({plain: true});
output.source_image = await remix_requests.getSource_image({
transaction
});
output.user = await remix_requests.getUser({
transaction
});
output.style_preset = await remix_requests.getStyle_preset({
transaction
});
output.result_job = await remix_requests.getResult_job({
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.generated_images,
as: 'source_image',
where: filter.source_image ? {
[Op.or]: [
{ id: { [Op.in]: filter.source_image.split('|').map(term => Utils.uuid(term)) } },
{
prompt_used: {
[Op.or]: filter.source_image.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.style_presets,
as: 'style_preset',
where: filter.style_preset ? {
[Op.or]: [
{ id: { [Op.in]: filter.style_preset.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.style_preset.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.generation_jobs,
as: 'result_job',
where: filter.result_job ? {
[Op.or]: [
{ id: { [Op.in]: filter.result_job.split('|').map(term => Utils.uuid(term)) } },
{
prompt_text: {
[Op.or]: filter.result_job.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.remix_prompt_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'remix_requests',
'remix_prompt_text',
filter.remix_prompt_text,
),
};
}
if (filter.failure_reason) {
where = {
...where,
[Op.and]: Utils.ilike(
'remix_requests',
'failure_reason',
filter.failure_reason,
),
};
}
if (filter.seedRange) {
const [start, end] = filter.seedRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
seed: {
...where.seed,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
seed: {
...where.seed,
[Op.lte]: end,
},
};
}
}
if (filter.requested_atRange) {
const [start, end] = filter.requested_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
requested_at: {
...where.requested_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
requested_at: {
...where.requested_at,
[Op.lte]: end,
},
};
}
}
if (filter.completed_atRange) {
const [start, end] = filter.completed_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.remix_type) {
where = {
...where,
remix_type: filter.remix_type,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.remix_requests.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(
'remix_requests',
'remix_prompt_text',
query,
),
],
};
}
const records = await db.remix_requests.findAll({
attributes: [ 'id', 'remix_prompt_text' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['remix_prompt_text', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.remix_prompt_text,
}));
}
};