567 lines
14 KiB
JavaScript
567 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 Knockout_slotsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const knockout_slots = await db.knockout_slots.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
stage: data.stage
|
|
||
|
|
null
|
|
,
|
|
|
|
slot_code: data.slot_code
|
|
||
|
|
null
|
|
,
|
|
|
|
source_type: data.source_type
|
|
||
|
|
null
|
|
,
|
|
|
|
source_group_position: data.source_group_position
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await knockout_slots.setTournament( data.tournament || null, {
|
|
transaction,
|
|
});
|
|
|
|
await knockout_slots.setSource_group( data.source_group || null, {
|
|
transaction,
|
|
});
|
|
|
|
await knockout_slots.setSource_match( data.source_match || null, {
|
|
transaction,
|
|
});
|
|
|
|
await knockout_slots.setFixed_team( data.fixed_team || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return knockout_slots;
|
|
}
|
|
|
|
|
|
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 knockout_slotsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
stage: item.stage
|
|
||
|
|
null
|
|
,
|
|
|
|
slot_code: item.slot_code
|
|
||
|
|
null
|
|
,
|
|
|
|
source_type: item.source_type
|
|
||
|
|
null
|
|
,
|
|
|
|
source_group_position: item.source_group_position
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const knockout_slots = await db.knockout_slots.bulkCreate(knockout_slotsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return knockout_slots;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const knockout_slots = await db.knockout_slots.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.stage !== undefined) updatePayload.stage = data.stage;
|
|
|
|
|
|
if (data.slot_code !== undefined) updatePayload.slot_code = data.slot_code;
|
|
|
|
|
|
if (data.source_type !== undefined) updatePayload.source_type = data.source_type;
|
|
|
|
|
|
if (data.source_group_position !== undefined) updatePayload.source_group_position = data.source_group_position;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await knockout_slots.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.tournament !== undefined) {
|
|
await knockout_slots.setTournament(
|
|
|
|
data.tournament,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.source_group !== undefined) {
|
|
await knockout_slots.setSource_group(
|
|
|
|
data.source_group,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.source_match !== undefined) {
|
|
await knockout_slots.setSource_match(
|
|
|
|
data.source_match,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.fixed_team !== undefined) {
|
|
await knockout_slots.setFixed_team(
|
|
|
|
data.fixed_team,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return knockout_slots;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const knockout_slots = await db.knockout_slots.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of knockout_slots) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of knockout_slots) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return knockout_slots;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const knockout_slots = await db.knockout_slots.findByPk(id, options);
|
|
|
|
await knockout_slots.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await knockout_slots.destroy({
|
|
transaction
|
|
});
|
|
|
|
return knockout_slots;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const knockout_slots = await db.knockout_slots.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!knockout_slots) {
|
|
return knockout_slots;
|
|
}
|
|
|
|
const output = knockout_slots.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.tournament = await knockout_slots.getTournament({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.source_group = await knockout_slots.getSource_group({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.source_match = await knockout_slots.getSource_match({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.fixed_team = await knockout_slots.getFixed_team({
|
|
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.tournaments,
|
|
as: 'tournament',
|
|
|
|
where: filter.tournament ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.tournament.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.tournament.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.groups,
|
|
as: 'source_group',
|
|
|
|
where: filter.source_group ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.source_group.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.source_group.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.matches,
|
|
as: 'source_match',
|
|
|
|
where: filter.source_match ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.source_match.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
match_code: {
|
|
[Op.or]: filter.source_match.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.teams,
|
|
as: 'fixed_team',
|
|
|
|
where: filter.fixed_team ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.fixed_team.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.fixed_team.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.slot_code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'knockout_slots',
|
|
'slot_code',
|
|
filter.slot_code,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.source_group_positionRange) {
|
|
const [start, end] = filter.source_group_positionRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
source_group_position: {
|
|
...where.source_group_position,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
source_group_position: {
|
|
...where.source_group_position,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.stage) {
|
|
where = {
|
|
...where,
|
|
stage: filter.stage,
|
|
};
|
|
}
|
|
|
|
if (filter.source_type) {
|
|
where = {
|
|
...where,
|
|
source_type: filter.source_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.knockout_slots.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(
|
|
'knockout_slots',
|
|
'slot_code',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.knockout_slots.findAll({
|
|
attributes: [ 'id', 'slot_code' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['slot_code', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.slot_code,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|