674 lines
17 KiB
JavaScript
674 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 Queue_itemsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const queue_items = await db.queue_items.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
position: data.position
|
|
||
|
|
null
|
|
,
|
|
|
|
item_status: data.item_status
|
|
||
|
|
null
|
|
,
|
|
|
|
requested_by_identifier: data.requested_by_identifier
|
|
||
|
|
null
|
|
,
|
|
|
|
queued_at: data.queued_at
|
|
||
|
|
null
|
|
,
|
|
|
|
started_playing_at: data.started_playing_at
|
|
||
|
|
null
|
|
,
|
|
|
|
finished_at: data.finished_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await queue_items.setQueue( data.queue || null, {
|
|
transaction,
|
|
});
|
|
|
|
await queue_items.setTrack( data.track || null, {
|
|
transaction,
|
|
});
|
|
|
|
await queue_items.setRequested_by_user( data.requested_by_user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await queue_items.setDiscord_guilds( data.discord_guilds || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return queue_items;
|
|
}
|
|
|
|
|
|
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 queue_itemsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
position: item.position
|
|
||
|
|
null
|
|
,
|
|
|
|
item_status: item.item_status
|
|
||
|
|
null
|
|
,
|
|
|
|
requested_by_identifier: item.requested_by_identifier
|
|
||
|
|
null
|
|
,
|
|
|
|
queued_at: item.queued_at
|
|
||
|
|
null
|
|
,
|
|
|
|
started_playing_at: item.started_playing_at
|
|
||
|
|
null
|
|
,
|
|
|
|
finished_at: item.finished_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const queue_items = await db.queue_items.bulkCreate(queue_itemsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return queue_items;
|
|
}
|
|
|
|
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 queue_items = await db.queue_items.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.position !== undefined) updatePayload.position = data.position;
|
|
|
|
|
|
if (data.item_status !== undefined) updatePayload.item_status = data.item_status;
|
|
|
|
|
|
if (data.requested_by_identifier !== undefined) updatePayload.requested_by_identifier = data.requested_by_identifier;
|
|
|
|
|
|
if (data.queued_at !== undefined) updatePayload.queued_at = data.queued_at;
|
|
|
|
|
|
if (data.started_playing_at !== undefined) updatePayload.started_playing_at = data.started_playing_at;
|
|
|
|
|
|
if (data.finished_at !== undefined) updatePayload.finished_at = data.finished_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await queue_items.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.queue !== undefined) {
|
|
await queue_items.setQueue(
|
|
|
|
data.queue,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.track !== undefined) {
|
|
await queue_items.setTrack(
|
|
|
|
data.track,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.requested_by_user !== undefined) {
|
|
await queue_items.setRequested_by_user(
|
|
|
|
data.requested_by_user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.discord_guilds !== undefined) {
|
|
await queue_items.setDiscord_guilds(
|
|
|
|
data.discord_guilds,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return queue_items;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const queue_items = await db.queue_items.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of queue_items) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of queue_items) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return queue_items;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const queue_items = await db.queue_items.findByPk(id, options);
|
|
|
|
await queue_items.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await queue_items.destroy({
|
|
transaction
|
|
});
|
|
|
|
return queue_items;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const queue_items = await db.queue_items.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!queue_items) {
|
|
return queue_items;
|
|
}
|
|
|
|
const output = queue_items.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.queue = await queue_items.getQueue({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.track = await queue_items.getTrack({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.requested_by_user = await queue_items.getRequested_by_user({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.discord_guilds = await queue_items.getDiscord_guilds({
|
|
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.play_queues,
|
|
as: 'queue',
|
|
|
|
where: filter.queue ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.queue.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
now_playing_title: {
|
|
[Op.or]: filter.queue.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.tracks,
|
|
as: 'track',
|
|
|
|
where: filter.track ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.track.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.track.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'requested_by_user',
|
|
|
|
where: filter.requested_by_user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.requested_by_user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.requested_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.discord_guilds,
|
|
as: 'discord_guilds',
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.requested_by_identifier) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'queue_items',
|
|
'requested_by_identifier',
|
|
filter.requested_by_identifier,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.positionRange) {
|
|
const [start, end] = filter.positionRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
position: {
|
|
...where.position,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
position: {
|
|
...where.position,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.queued_atRange) {
|
|
const [start, end] = filter.queued_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
queued_at: {
|
|
...where.queued_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
queued_at: {
|
|
...where.queued_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.started_playing_atRange) {
|
|
const [start, end] = filter.started_playing_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
started_playing_at: {
|
|
...where.started_playing_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
started_playing_at: {
|
|
...where.started_playing_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.finished_atRange) {
|
|
const [start, end] = filter.finished_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
finished_at: {
|
|
...where.finished_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
finished_at: {
|
|
...where.finished_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.item_status) {
|
|
where = {
|
|
...where,
|
|
item_status: filter.item_status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.discord_guilds) {
|
|
const listItems = filter.discord_guilds.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
discord_guildsId: {[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.queue_items.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(
|
|
'queue_items',
|
|
'requested_by_identifier',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.queue_items.findAll({
|
|
attributes: [ 'id', 'requested_by_identifier' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['requested_by_identifier', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.requested_by_identifier,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|