380 lines
9.4 KiB
JavaScript
380 lines
9.4 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 Championship_laddersDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const championship_ladders = await db.championship_ladders.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
total_points: data.total_points || null,
|
|
total_wins: data.total_wins || null,
|
|
total_loses: data.total_loses || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await championship_ladders.setParticipants(data.participants || null, {
|
|
transaction,
|
|
});
|
|
|
|
return championship_ladders;
|
|
}
|
|
|
|
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 championship_laddersData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
total_points: item.total_points || null,
|
|
total_wins: item.total_wins || null,
|
|
total_loses: item.total_loses || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const championship_ladders = await db.championship_ladders.bulkCreate(
|
|
championship_laddersData,
|
|
{ transaction },
|
|
);
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return championship_ladders;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const championship_ladders = await db.championship_ladders.findByPk(
|
|
id,
|
|
{},
|
|
{ transaction },
|
|
);
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.total_points !== undefined)
|
|
updatePayload.total_points = data.total_points;
|
|
|
|
if (data.total_wins !== undefined)
|
|
updatePayload.total_wins = data.total_wins;
|
|
|
|
if (data.total_loses !== undefined)
|
|
updatePayload.total_loses = data.total_loses;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await championship_ladders.update(updatePayload, { transaction });
|
|
|
|
if (data.participants !== undefined) {
|
|
await championship_ladders.setParticipants(
|
|
data.participants,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return championship_ladders;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const championship_ladders = await db.championship_ladders.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of championship_ladders) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of championship_ladders) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return championship_ladders;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const championship_ladders = await db.championship_ladders.findByPk(
|
|
id,
|
|
options,
|
|
);
|
|
|
|
await championship_ladders.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await championship_ladders.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return championship_ladders;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const championship_ladders = await db.championship_ladders.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!championship_ladders) {
|
|
return championship_ladders;
|
|
}
|
|
|
|
const output = championship_ladders.get({ plain: true });
|
|
|
|
output.participants = await championship_ladders.getParticipants({
|
|
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.championship_registrations,
|
|
as: 'participants',
|
|
|
|
where: filter.participants
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.participants
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
id: {
|
|
[Op.or]: filter.participants
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.total_pointsRange) {
|
|
const [start, end] = filter.total_pointsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
total_points: {
|
|
...where.total_points,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
total_points: {
|
|
...where.total_points,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.total_winsRange) {
|
|
const [start, end] = filter.total_winsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
total_wins: {
|
|
...where.total_wins,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
total_wins: {
|
|
...where.total_wins,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.total_losesRange) {
|
|
const [start, end] = filter.total_losesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
total_loses: {
|
|
...where.total_loses,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
total_loses: {
|
|
...where.total_loses,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
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.championship_ladders.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('championship_ladders', 'id', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.championship_ladders.findAll({
|
|
attributes: ['id', 'id'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['id', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.id,
|
|
}));
|
|
}
|
|
};
|