39019-vm/backend/src/db/api/lottery_games.js
2026-03-05 22:23:42 +00:00

875 lines
23 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 Lottery_gamesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lottery_games = await db.lottery_games.create(
{
id: data.id || undefined,
name: data.name
||
null
,
slug: data.slug
||
null
,
status: data.status
||
null
,
draw_schedule_type: data.draw_schedule_type
||
null
,
draw_days: data.draw_days
||
null
,
timezone: data.timezone
||
null
,
numbers_per_ticket: data.numbers_per_ticket
||
null
,
min_number: data.min_number
||
null
,
max_number: data.max_number
||
null
,
bonus_numbers_count: data.bonus_numbers_count
||
null
,
bonus_min_number: data.bonus_min_number
||
null
,
bonus_max_number: data.bonus_max_number
||
null
,
future_draws_target: data.future_draws_target
||
null
,
first_draw_at: data.first_draw_at
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await lottery_games.setCountry( data.country || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.lottery_games.getTableName(),
belongsToColumn: 'rules_document',
belongsToId: lottery_games.id,
},
data.rules_document,
options,
);
return lottery_games;
}
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 lottery_gamesData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
slug: item.slug
||
null
,
status: item.status
||
null
,
draw_schedule_type: item.draw_schedule_type
||
null
,
draw_days: item.draw_days
||
null
,
timezone: item.timezone
||
null
,
numbers_per_ticket: item.numbers_per_ticket
||
null
,
min_number: item.min_number
||
null
,
max_number: item.max_number
||
null
,
bonus_numbers_count: item.bonus_numbers_count
||
null
,
bonus_min_number: item.bonus_min_number
||
null
,
bonus_max_number: item.bonus_max_number
||
null
,
future_draws_target: item.future_draws_target
||
null
,
first_draw_at: item.first_draw_at
||
null
,
notes: item.notes
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const lottery_games = await db.lottery_games.bulkCreate(lottery_gamesData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < lottery_games.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.lottery_games.getTableName(),
belongsToColumn: 'rules_document',
belongsToId: lottery_games[i].id,
},
data[i].rules_document,
options,
);
}
return lottery_games;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const lottery_games = await db.lottery_games.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.slug !== undefined) updatePayload.slug = data.slug;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.draw_schedule_type !== undefined) updatePayload.draw_schedule_type = data.draw_schedule_type;
if (data.draw_days !== undefined) updatePayload.draw_days = data.draw_days;
if (data.timezone !== undefined) updatePayload.timezone = data.timezone;
if (data.numbers_per_ticket !== undefined) updatePayload.numbers_per_ticket = data.numbers_per_ticket;
if (data.min_number !== undefined) updatePayload.min_number = data.min_number;
if (data.max_number !== undefined) updatePayload.max_number = data.max_number;
if (data.bonus_numbers_count !== undefined) updatePayload.bonus_numbers_count = data.bonus_numbers_count;
if (data.bonus_min_number !== undefined) updatePayload.bonus_min_number = data.bonus_min_number;
if (data.bonus_max_number !== undefined) updatePayload.bonus_max_number = data.bonus_max_number;
if (data.future_draws_target !== undefined) updatePayload.future_draws_target = data.future_draws_target;
if (data.first_draw_at !== undefined) updatePayload.first_draw_at = data.first_draw_at;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await lottery_games.update(updatePayload, {transaction});
if (data.country !== undefined) {
await lottery_games.setCountry(
data.country,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.lottery_games.getTableName(),
belongsToColumn: 'rules_document',
belongsToId: lottery_games.id,
},
data.rules_document,
options,
);
return lottery_games;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lottery_games = await db.lottery_games.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of lottery_games) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of lottery_games) {
await record.destroy({transaction});
}
});
return lottery_games;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const lottery_games = await db.lottery_games.findByPk(id, options);
await lottery_games.update({
deletedBy: currentUser.id
}, {
transaction,
});
await lottery_games.destroy({
transaction
});
return lottery_games;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const lottery_games = await db.lottery_games.findOne(
{ where },
{ transaction },
);
if (!lottery_games) {
return lottery_games;
}
const output = lottery_games.get({plain: true});
output.draws_game = await lottery_games.getDraws_game({
transaction
});
output.number_sets_game = await lottery_games.getNumber_sets_game({
transaction
});
output.generation_jobs_game = await lottery_games.getGeneration_jobs_game({
transaction
});
output.analysis_models_game = await lottery_games.getAnalysis_models_game({
transaction
});
output.model_runs_game = await lottery_games.getModel_runs_game({
transaction
});
output.imports_game = await lottery_games.getImports_game({
transaction
});
output.country = await lottery_games.getCountry({
transaction
});
output.rules_document = await lottery_games.getRules_document({
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.countries,
as: 'country',
where: filter.country ? {
[Op.or]: [
{ id: { [Op.in]: filter.country.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.country.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'rules_document',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'lottery_games',
'name',
filter.name,
),
};
}
if (filter.slug) {
where = {
...where,
[Op.and]: Utils.ilike(
'lottery_games',
'slug',
filter.slug,
),
};
}
if (filter.draw_days) {
where = {
...where,
[Op.and]: Utils.ilike(
'lottery_games',
'draw_days',
filter.draw_days,
),
};
}
if (filter.timezone) {
where = {
...where,
[Op.and]: Utils.ilike(
'lottery_games',
'timezone',
filter.timezone,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'lottery_games',
'notes',
filter.notes,
),
};
}
if (filter.numbers_per_ticketRange) {
const [start, end] = filter.numbers_per_ticketRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
numbers_per_ticket: {
...where.numbers_per_ticket,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
numbers_per_ticket: {
...where.numbers_per_ticket,
[Op.lte]: end,
},
};
}
}
if (filter.min_numberRange) {
const [start, end] = filter.min_numberRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
min_number: {
...where.min_number,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
min_number: {
...where.min_number,
[Op.lte]: end,
},
};
}
}
if (filter.max_numberRange) {
const [start, end] = filter.max_numberRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
max_number: {
...where.max_number,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
max_number: {
...where.max_number,
[Op.lte]: end,
},
};
}
}
if (filter.bonus_numbers_countRange) {
const [start, end] = filter.bonus_numbers_countRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
bonus_numbers_count: {
...where.bonus_numbers_count,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
bonus_numbers_count: {
...where.bonus_numbers_count,
[Op.lte]: end,
},
};
}
}
if (filter.bonus_min_numberRange) {
const [start, end] = filter.bonus_min_numberRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
bonus_min_number: {
...where.bonus_min_number,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
bonus_min_number: {
...where.bonus_min_number,
[Op.lte]: end,
},
};
}
}
if (filter.bonus_max_numberRange) {
const [start, end] = filter.bonus_max_numberRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
bonus_max_number: {
...where.bonus_max_number,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
bonus_max_number: {
...where.bonus_max_number,
[Op.lte]: end,
},
};
}
}
if (filter.future_draws_targetRange) {
const [start, end] = filter.future_draws_targetRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
future_draws_target: {
...where.future_draws_target,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
future_draws_target: {
...where.future_draws_target,
[Op.lte]: end,
},
};
}
}
if (filter.first_draw_atRange) {
const [start, end] = filter.first_draw_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
first_draw_at: {
...where.first_draw_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
first_draw_at: {
...where.first_draw_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.draw_schedule_type) {
where = {
...where,
draw_schedule_type: filter.draw_schedule_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.lottery_games.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(
'lottery_games',
'name',
query,
),
],
};
}
const records = await db.lottery_games.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,
}));
}
};