637 lines
16 KiB
JavaScript
637 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 Playback_logsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const playback_logs = await db.playback_logs.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
trigger_type: data.trigger_type
|
|
||
|
|
null
|
|
,
|
|
|
|
result: data.result
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at: data.started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
ended_at: data.ended_at
|
|
||
|
|
null
|
|
,
|
|
|
|
played_seconds: data.played_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
details: data.details
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await playback_logs.setDiscord_server( data.discord_server || null, {
|
|
transaction,
|
|
});
|
|
|
|
await playback_logs.setVoice_channel( data.voice_channel || null, {
|
|
transaction,
|
|
});
|
|
|
|
await playback_logs.setMedia_source( data.media_source || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return playback_logs;
|
|
}
|
|
|
|
|
|
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 playback_logsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
trigger_type: item.trigger_type
|
|
||
|
|
null
|
|
,
|
|
|
|
result: item.result
|
|
||
|
|
null
|
|
,
|
|
|
|
started_at: item.started_at
|
|
||
|
|
null
|
|
,
|
|
|
|
ended_at: item.ended_at
|
|
||
|
|
null
|
|
,
|
|
|
|
played_seconds: item.played_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
details: item.details
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const playback_logs = await db.playback_logs.bulkCreate(playback_logsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return playback_logs;
|
|
}
|
|
|
|
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 playback_logs = await db.playback_logs.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.trigger_type !== undefined) updatePayload.trigger_type = data.trigger_type;
|
|
|
|
|
|
if (data.result !== undefined) updatePayload.result = data.result;
|
|
|
|
|
|
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
|
|
|
|
|
|
if (data.ended_at !== undefined) updatePayload.ended_at = data.ended_at;
|
|
|
|
|
|
if (data.played_seconds !== undefined) updatePayload.played_seconds = data.played_seconds;
|
|
|
|
|
|
if (data.details !== undefined) updatePayload.details = data.details;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await playback_logs.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.discord_server !== undefined) {
|
|
await playback_logs.setDiscord_server(
|
|
|
|
data.discord_server,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.voice_channel !== undefined) {
|
|
await playback_logs.setVoice_channel(
|
|
|
|
data.voice_channel,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.media_source !== undefined) {
|
|
await playback_logs.setMedia_source(
|
|
|
|
data.media_source,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return playback_logs;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const playback_logs = await db.playback_logs.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of playback_logs) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of playback_logs) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return playback_logs;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const playback_logs = await db.playback_logs.findByPk(id, options);
|
|
|
|
await playback_logs.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await playback_logs.destroy({
|
|
transaction
|
|
});
|
|
|
|
return playback_logs;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const playback_logs = await db.playback_logs.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!playback_logs) {
|
|
return playback_logs;
|
|
}
|
|
|
|
const output = playback_logs.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.discord_server = await playback_logs.getDiscord_server({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.voice_channel = await playback_logs.getVoice_channel({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.media_source = await playback_logs.getMedia_source({
|
|
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_servers = (user && user.discord_servers?.id) || null;
|
|
|
|
|
|
|
|
if (userDiscord_servers) {
|
|
if (options?.currentUser?.discord_serversId) {
|
|
where.discord_serversId = options.currentUser.discord_serversId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.discord_servers,
|
|
as: 'discord_server',
|
|
|
|
},
|
|
|
|
{
|
|
model: db.voice_channels,
|
|
as: 'voice_channel',
|
|
|
|
where: filter.voice_channel ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.voice_channel.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
channel_name: {
|
|
[Op.or]: filter.voice_channel.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.media_sources,
|
|
as: 'media_source',
|
|
|
|
where: filter.media_source ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.media_source.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
source_name: {
|
|
[Op.or]: filter.media_source.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.details) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'playback_logs',
|
|
'details',
|
|
filter.details,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
started_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
ended_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
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.ended_atRange) {
|
|
const [start, end] = filter.ended_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
ended_at: {
|
|
...where.ended_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
ended_at: {
|
|
...where.ended_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.played_secondsRange) {
|
|
const [start, end] = filter.played_secondsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
played_seconds: {
|
|
...where.played_seconds,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
played_seconds: {
|
|
...where.played_seconds,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.trigger_type) {
|
|
where = {
|
|
...where,
|
|
trigger_type: filter.trigger_type,
|
|
};
|
|
}
|
|
|
|
if (filter.result) {
|
|
where = {
|
|
...where,
|
|
result: filter.result,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.discord_server) {
|
|
const listItems = filter.discord_server.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
discord_serverId: {[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_serversId;
|
|
}
|
|
|
|
|
|
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.playback_logs.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(
|
|
'playback_logs',
|
|
'details',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.playback_logs.findAll({
|
|
attributes: [ 'id', 'details' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['details', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.details,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|