38529-vm/backend/src/db/api/play_queues.js
2026-02-17 16:34:20 +00:00

572 lines
14 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 Play_queuesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const play_queues = await db.play_queues.create(
{
id: data.id || undefined,
queue_status: data.queue_status
||
null
,
repeat_mode: data.repeat_mode
||
null
,
autoplay: data.autoplay
||
false
,
volume: data.volume
||
null
,
now_playing_title: data.now_playing_title
||
null
,
started_at: data.started_at
||
null
,
last_updated_at: data.last_updated_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await play_queues.setGuild( data.guild || null, {
transaction,
});
return play_queues;
}
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 play_queuesData = data.map((item, index) => ({
id: item.id || undefined,
queue_status: item.queue_status
||
null
,
repeat_mode: item.repeat_mode
||
null
,
autoplay: item.autoplay
||
false
,
volume: item.volume
||
null
,
now_playing_title: item.now_playing_title
||
null
,
started_at: item.started_at
||
null
,
last_updated_at: item.last_updated_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const play_queues = await db.play_queues.bulkCreate(play_queuesData, { transaction });
// For each item created, replace relation files
return play_queues;
}
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 play_queues = await db.play_queues.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.queue_status !== undefined) updatePayload.queue_status = data.queue_status;
if (data.repeat_mode !== undefined) updatePayload.repeat_mode = data.repeat_mode;
if (data.autoplay !== undefined) updatePayload.autoplay = data.autoplay;
if (data.volume !== undefined) updatePayload.volume = data.volume;
if (data.now_playing_title !== undefined) updatePayload.now_playing_title = data.now_playing_title;
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
if (data.last_updated_at !== undefined) updatePayload.last_updated_at = data.last_updated_at;
updatePayload.updatedById = currentUser.id;
await play_queues.update(updatePayload, {transaction});
if (data.guild !== undefined) {
await play_queues.setGuild(
data.guild,
{ transaction }
);
}
return play_queues;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const play_queues = await db.play_queues.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of play_queues) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of play_queues) {
await record.destroy({transaction});
}
});
return play_queues;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const play_queues = await db.play_queues.findByPk(id, options);
await play_queues.update({
deletedBy: currentUser.id
}, {
transaction,
});
await play_queues.destroy({
transaction
});
return play_queues;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const play_queues = await db.play_queues.findOne(
{ where },
{ transaction },
);
if (!play_queues) {
return play_queues;
}
const output = play_queues.get({plain: true});
output.queue_items_queue = await play_queues.getQueue_items_queue({
transaction
});
output.guild = await play_queues.getGuild({
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 userDiscord_guilds = (user && user.discord_guilds?.id) || null;
if (userDiscord_guilds) {
if (options?.currentUser?.discord_guildsId) {
where.discord_guildsId = options.currentUser.discord_guildsId;
}
}
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.discord_guilds,
as: 'guild',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.now_playing_title) {
where = {
...where,
[Op.and]: Utils.ilike(
'play_queues',
'now_playing_title',
filter.now_playing_title,
),
};
}
if (filter.volumeRange) {
const [start, end] = filter.volumeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
volume: {
...where.volume,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
volume: {
...where.volume,
[Op.lte]: end,
},
};
}
}
if (filter.started_atRange) {
const [start, end] = filter.started_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.lte]: end,
},
};
}
}
if (filter.last_updated_atRange) {
const [start, end] = filter.last_updated_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
last_updated_at: {
...where.last_updated_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
last_updated_at: {
...where.last_updated_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.queue_status) {
where = {
...where,
queue_status: filter.queue_status,
};
}
if (filter.repeat_mode) {
where = {
...where,
repeat_mode: filter.repeat_mode,
};
}
if (filter.autoplay) {
where = {
...where,
autoplay: filter.autoplay,
};
}
if (filter.guild) {
const listItems = filter.guild.split('|').map(item => {
return Utils.uuid(item)
});
where = {
...where,
guildId: {[Op.or]: listItems}
};
}
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.discord_guildsId;
}
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.play_queues.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(
'play_queues',
'now_playing_title',
query,
),
],
};
}
const records = await db.play_queues.findAll({
attributes: [ 'id', 'now_playing_title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['now_playing_title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.now_playing_title,
}));
}
};