Flatlogic Bot 70b32b34db 1
2026-03-02 03:48:38 +00:00

698 lines
17 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 SongsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const songs = await db.songs.create(
{
id: data.id || undefined,
song_title: data.song_title
||
null
,
generation_mode: data.generation_mode
||
null
,
lyrics_text: data.lyrics_text
||
null
,
tempo_bpm: data.tempo_bpm
||
null
,
key_signature: data.key_signature
||
null
,
status: data.status
||
null
,
requested_at: data.requested_at
||
null
,
completed_at: data.completed_at
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await songs.setLanguage( data.language || null, {
transaction,
});
await songs.setStyle( data.style || null, {
transaction,
});
await songs.setEra( data.era || null, {
transaction,
});
return songs;
}
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 songsData = data.map((item, index) => ({
id: item.id || undefined,
song_title: item.song_title
||
null
,
generation_mode: item.generation_mode
||
null
,
lyrics_text: item.lyrics_text
||
null
,
tempo_bpm: item.tempo_bpm
||
null
,
key_signature: item.key_signature
||
null
,
status: item.status
||
null
,
requested_at: item.requested_at
||
null
,
completed_at: item.completed_at
||
null
,
notes: item.notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const songs = await db.songs.bulkCreate(songsData, { transaction });
// For each item created, replace relation files
return songs;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const songs = await db.songs.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.song_title !== undefined) updatePayload.song_title = data.song_title;
if (data.generation_mode !== undefined) updatePayload.generation_mode = data.generation_mode;
if (data.lyrics_text !== undefined) updatePayload.lyrics_text = data.lyrics_text;
if (data.tempo_bpm !== undefined) updatePayload.tempo_bpm = data.tempo_bpm;
if (data.key_signature !== undefined) updatePayload.key_signature = data.key_signature;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await songs.update(updatePayload, {transaction});
if (data.language !== undefined) {
await songs.setLanguage(
data.language,
{ transaction }
);
}
if (data.style !== undefined) {
await songs.setStyle(
data.style,
{ transaction }
);
}
if (data.era !== undefined) {
await songs.setEra(
data.era,
{ transaction }
);
}
return songs;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const songs = await db.songs.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of songs) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of songs) {
await record.destroy({transaction});
}
});
return songs;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const songs = await db.songs.findByPk(id, options);
await songs.update({
deletedBy: currentUser.id
}, {
transaction,
});
await songs.destroy({
transaction
});
return songs;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const songs = await db.songs.findOne(
{ where },
{ transaction },
);
if (!songs) {
return songs;
}
const output = songs.get({plain: true});
output.song_voice_tracks_song = await songs.getSong_voice_tracks_song({
transaction
});
output.generation_jobs_song = await songs.getGeneration_jobs_song({
transaction
});
output.media_assets_song = await songs.getMedia_assets_song({
transaction
});
output.playback_sessions_song = await songs.getPlayback_sessions_song({
transaction
});
output.language = await songs.getLanguage({
transaction
});
output.style = await songs.getStyle({
transaction
});
output.era = await songs.getEra({
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.languages,
as: 'language',
where: filter.language ? {
[Op.or]: [
{ id: { [Op.in]: filter.language.split('|').map(term => Utils.uuid(term)) } },
{
language_name: {
[Op.or]: filter.language.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.music_styles,
as: 'style',
where: filter.style ? {
[Op.or]: [
{ id: { [Op.in]: filter.style.split('|').map(term => Utils.uuid(term)) } },
{
style_name: {
[Op.or]: filter.style.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.eras,
as: 'era',
where: filter.era ? {
[Op.or]: [
{ id: { [Op.in]: filter.era.split('|').map(term => Utils.uuid(term)) } },
{
era_name: {
[Op.or]: filter.era.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.media_assets,
as: 'media_assets_song',
include: [{
model: db.file,
as: 'file_blob',
}]
}
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.song_title) {
where = {
...where,
[Op.and]: Utils.ilike(
'songs',
'song_title',
filter.song_title,
),
};
}
if (filter.lyrics_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'songs',
'lyrics_text',
filter.lyrics_text,
),
};
}
if (filter.key_signature) {
where = {
...where,
[Op.and]: Utils.ilike(
'songs',
'key_signature',
filter.key_signature,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'songs',
'notes',
filter.notes,
),
};
}
if (filter.tempo_bpmRange) {
const [start, end] = filter.tempo_bpmRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
tempo_bpm: {
...where.tempo_bpm,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
tempo_bpm: {
...where.tempo_bpm,
[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.generation_mode) {
where = {
...where,
generation_mode: filter.generation_mode,
};
}
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.songs.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(
'songs',
'song_title',
query,
),
],
};
}
const records = await db.songs.findAll({
attributes: [ 'id', 'song_title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['song_title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.song_title,
}));
}
};