38699-vm/backend/src/db/api/leaderboard_snapshots.js
2026-02-23 04:34:58 +00:00

582 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 Leaderboard_snapshotsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const leaderboard_snapshots = await db.leaderboard_snapshots.create(
{
id: data.id || undefined,
scope: data.scope
||
null
,
period: data.period
||
null
,
metric: data.metric
||
null
,
period_start_at: data.period_start_at
||
null
,
period_end_at: data.period_end_at
||
null
,
generated_at: data.generated_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await leaderboard_snapshots.setGym( data.gym || null, {
transaction,
});
await leaderboard_snapshots.setSpray_wall( data.spray_wall || null, {
transaction,
});
return leaderboard_snapshots;
}
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 leaderboard_snapshotsData = data.map((item, index) => ({
id: item.id || undefined,
scope: item.scope
||
null
,
period: item.period
||
null
,
metric: item.metric
||
null
,
period_start_at: item.period_start_at
||
null
,
period_end_at: item.period_end_at
||
null
,
generated_at: item.generated_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const leaderboard_snapshots = await db.leaderboard_snapshots.bulkCreate(leaderboard_snapshotsData, { transaction });
// For each item created, replace relation files
return leaderboard_snapshots;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const leaderboard_snapshots = await db.leaderboard_snapshots.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.scope !== undefined) updatePayload.scope = data.scope;
if (data.period !== undefined) updatePayload.period = data.period;
if (data.metric !== undefined) updatePayload.metric = data.metric;
if (data.period_start_at !== undefined) updatePayload.period_start_at = data.period_start_at;
if (data.period_end_at !== undefined) updatePayload.period_end_at = data.period_end_at;
if (data.generated_at !== undefined) updatePayload.generated_at = data.generated_at;
updatePayload.updatedById = currentUser.id;
await leaderboard_snapshots.update(updatePayload, {transaction});
if (data.gym !== undefined) {
await leaderboard_snapshots.setGym(
data.gym,
{ transaction }
);
}
if (data.spray_wall !== undefined) {
await leaderboard_snapshots.setSpray_wall(
data.spray_wall,
{ transaction }
);
}
return leaderboard_snapshots;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const leaderboard_snapshots = await db.leaderboard_snapshots.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of leaderboard_snapshots) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of leaderboard_snapshots) {
await record.destroy({transaction});
}
});
return leaderboard_snapshots;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const leaderboard_snapshots = await db.leaderboard_snapshots.findByPk(id, options);
await leaderboard_snapshots.update({
deletedBy: currentUser.id
}, {
transaction,
});
await leaderboard_snapshots.destroy({
transaction
});
return leaderboard_snapshots;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const leaderboard_snapshots = await db.leaderboard_snapshots.findOne(
{ where },
{ transaction },
);
if (!leaderboard_snapshots) {
return leaderboard_snapshots;
}
const output = leaderboard_snapshots.get({plain: true});
output.leaderboard_entries_snapshot = await leaderboard_snapshots.getLeaderboard_entries_snapshot({
transaction
});
output.gym = await leaderboard_snapshots.getGym({
transaction
});
output.spray_wall = await leaderboard_snapshots.getSpray_wall({
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.gyms,
as: 'gym',
where: filter.gym ? {
[Op.or]: [
{ id: { [Op.in]: filter.gym.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.gym.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.spray_walls,
as: 'spray_wall',
where: filter.spray_wall ? {
[Op.or]: [
{ id: { [Op.in]: filter.spray_wall.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.spray_wall.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
period_start_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
period_end_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.period_start_atRange) {
const [start, end] = filter.period_start_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
period_start_at: {
...where.period_start_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
period_start_at: {
...where.period_start_at,
[Op.lte]: end,
},
};
}
}
if (filter.period_end_atRange) {
const [start, end] = filter.period_end_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
period_end_at: {
...where.period_end_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
period_end_at: {
...where.period_end_at,
[Op.lte]: end,
},
};
}
}
if (filter.generated_atRange) {
const [start, end] = filter.generated_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
generated_at: {
...where.generated_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
generated_at: {
...where.generated_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.scope) {
where = {
...where,
scope: filter.scope,
};
}
if (filter.period) {
where = {
...where,
period: filter.period,
};
}
if (filter.metric) {
where = {
...where,
metric: filter.metric,
};
}
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.leaderboard_snapshots.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(
'leaderboard_snapshots',
'metric',
query,
),
],
};
}
const records = await db.leaderboard_snapshots.findAll({
attributes: [ 'id', 'metric' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['metric', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.metric,
}));
}
};