30347/backend/src/db/api/contests.js
2025-05-10 10:57:46 +00:00

359 lines
8.5 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 ContestsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const contests = await db.contests.create(
{
id: data.id || undefined,
name: data.name || null,
type: data.type || null,
entry_fee: data.entry_fee || null,
prize_pool: data.prize_pool || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await contests.setMatch(data.match || null, {
transaction,
});
return contests;
}
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 contestsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name || null,
type: item.type || null,
entry_fee: item.entry_fee || null,
prize_pool: item.prize_pool || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const contests = await db.contests.bulkCreate(contestsData, {
transaction,
});
// For each item created, replace relation files
return contests;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const contests = await db.contests.findByPk(id, {}, { transaction });
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.type !== undefined) updatePayload.type = data.type;
if (data.entry_fee !== undefined) updatePayload.entry_fee = data.entry_fee;
if (data.prize_pool !== undefined)
updatePayload.prize_pool = data.prize_pool;
updatePayload.updatedById = currentUser.id;
await contests.update(updatePayload, { transaction });
if (data.match !== undefined) {
await contests.setMatch(
data.match,
{ transaction },
);
}
return contests;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const contests = await db.contests.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of contests) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of contests) {
await record.destroy({ transaction });
}
});
return contests;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const contests = await db.contests.findByPk(id, options);
await contests.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await contests.destroy({
transaction,
});
return contests;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const contests = await db.contests.findOne({ where }, { transaction });
if (!contests) {
return contests;
}
const output = contests.get({ plain: true });
output.match = await contests.getMatch({
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.matches,
as: 'match',
where: filter.match
? {
[Op.or]: [
{
id: {
[Op.in]: filter.match
.split('|')
.map((term) => Utils.uuid(term)),
},
},
{
title: {
[Op.or]: filter.match
.split('|')
.map((term) => ({ [Op.iLike]: `%${term}%` })),
},
},
],
}
: {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike('contests', 'name', filter.name),
};
}
if (filter.entry_feeRange) {
const [start, end] = filter.entry_feeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
entry_fee: {
...where.entry_fee,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
entry_fee: {
...where.entry_fee,
[Op.lte]: end,
},
};
}
}
if (filter.prize_poolRange) {
const [start, end] = filter.prize_poolRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
prize_pool: {
...where.prize_pool,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
prize_pool: {
...where.prize_pool,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true',
};
}
if (filter.type) {
where = {
...where,
type: filter.type,
};
}
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.contests.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('contests', 'name', query),
],
};
}
const records = await db.contests.findAll({
attributes: ['id', 'name'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};