38691-vm/backend/src/db/api/tts_voices.js
2026-02-22 17:34:06 +00:00

496 lines
12 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 Tts_voicesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tts_voices = await db.tts_voices.create(
{
id: data.id || undefined,
provider: data.provider
||
null
,
voice_name: data.voice_name
||
null
,
voice_code: data.voice_code
||
null
,
language: data.language
||
null
,
gender: data.gender
||
null
,
is_premium: data.is_premium
||
false
,
cost_per_million_chars: data.cost_per_million_chars
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return tts_voices;
}
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 tts_voicesData = data.map((item, index) => ({
id: item.id || undefined,
provider: item.provider
||
null
,
voice_name: item.voice_name
||
null
,
voice_code: item.voice_code
||
null
,
language: item.language
||
null
,
gender: item.gender
||
null
,
is_premium: item.is_premium
||
false
,
cost_per_million_chars: item.cost_per_million_chars
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const tts_voices = await db.tts_voices.bulkCreate(tts_voicesData, { transaction });
// For each item created, replace relation files
return tts_voices;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tts_voices = await db.tts_voices.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.provider !== undefined) updatePayload.provider = data.provider;
if (data.voice_name !== undefined) updatePayload.voice_name = data.voice_name;
if (data.voice_code !== undefined) updatePayload.voice_code = data.voice_code;
if (data.language !== undefined) updatePayload.language = data.language;
if (data.gender !== undefined) updatePayload.gender = data.gender;
if (data.is_premium !== undefined) updatePayload.is_premium = data.is_premium;
if (data.cost_per_million_chars !== undefined) updatePayload.cost_per_million_chars = data.cost_per_million_chars;
updatePayload.updatedById = currentUser.id;
await tts_voices.update(updatePayload, {transaction});
return tts_voices;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tts_voices = await db.tts_voices.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of tts_voices) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of tts_voices) {
await record.destroy({transaction});
}
});
return tts_voices;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tts_voices = await db.tts_voices.findByPk(id, options);
await tts_voices.update({
deletedBy: currentUser.id
}, {
transaction,
});
await tts_voices.destroy({
transaction
});
return tts_voices;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const tts_voices = await db.tts_voices.findOne(
{ where },
{ transaction },
);
if (!tts_voices) {
return tts_voices;
}
const output = tts_voices.get({plain: true});
output.tts_profiles_voice = await tts_voices.getTts_profiles_voice({
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 = [
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.provider) {
where = {
...where,
[Op.and]: Utils.ilike(
'tts_voices',
'provider',
filter.provider,
),
};
}
if (filter.voice_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'tts_voices',
'voice_name',
filter.voice_name,
),
};
}
if (filter.voice_code) {
where = {
...where,
[Op.and]: Utils.ilike(
'tts_voices',
'voice_code',
filter.voice_code,
),
};
}
if (filter.language) {
where = {
...where,
[Op.and]: Utils.ilike(
'tts_voices',
'language',
filter.language,
),
};
}
if (filter.cost_per_million_charsRange) {
const [start, end] = filter.cost_per_million_charsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
cost_per_million_chars: {
...where.cost_per_million_chars,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
cost_per_million_chars: {
...where.cost_per_million_chars,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.gender) {
where = {
...where,
gender: filter.gender,
};
}
if (filter.is_premium) {
where = {
...where,
is_premium: filter.is_premium,
};
}
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.tts_voices.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(
'tts_voices',
'voice_name',
query,
),
],
};
}
const records = await db.tts_voices.findAll({
attributes: [ 'id', 'voice_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['voice_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.voice_name,
}));
}
};