2026-01-26 23:17:12 +00:00

641 lines
15 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 BetsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const bets = await db.bets.create(
{
id: data.id || undefined,
reference: data.reference
||
null
,
stake: data.stake
||
null
,
odds: data.odds
||
null
,
potential_payout: data.potential_payout
||
null
,
status: data.status
||
null
,
placed_at: data.placed_at
||
null
,
settled_at: data.settled_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await bets.setUser( data.user || null, {
transaction,
});
await bets.setMarket( data.market || null, {
transaction,
});
return bets;
}
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 betsData = data.map((item, index) => ({
id: item.id || undefined,
reference: item.reference
||
null
,
stake: item.stake
||
null
,
odds: item.odds
||
null
,
potential_payout: item.potential_payout
||
null
,
status: item.status
||
null
,
placed_at: item.placed_at
||
null
,
settled_at: item.settled_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const bets = await db.bets.bulkCreate(betsData, { transaction });
// For each item created, replace relation files
return bets;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const bets = await db.bets.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.reference !== undefined) updatePayload.reference = data.reference;
if (data.stake !== undefined) updatePayload.stake = data.stake;
if (data.odds !== undefined) updatePayload.odds = data.odds;
if (data.potential_payout !== undefined) updatePayload.potential_payout = data.potential_payout;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.placed_at !== undefined) updatePayload.placed_at = data.placed_at;
if (data.settled_at !== undefined) updatePayload.settled_at = data.settled_at;
updatePayload.updatedById = currentUser.id;
await bets.update(updatePayload, {transaction});
if (data.user !== undefined) {
await bets.setUser(
data.user,
{ transaction }
);
}
if (data.market !== undefined) {
await bets.setMarket(
data.market,
{ transaction }
);
}
return bets;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const bets = await db.bets.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of bets) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of bets) {
await record.destroy({transaction});
}
});
return bets;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const bets = await db.bets.findByPk(id, options);
await bets.update({
deletedBy: currentUser.id
}, {
transaction,
});
await bets.destroy({
transaction
});
return bets;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const bets = await db.bets.findOne(
{ where },
{ transaction },
);
if (!bets) {
return bets;
}
const output = bets.get({plain: true});
output.user = await bets.getUser({
transaction
});
output.market = await bets.getMarket({
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.users,
as: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.sports_markets,
as: 'market',
where: filter.market ? {
[Op.or]: [
{ id: { [Op.in]: filter.market.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.market.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.reference) {
where = {
...where,
[Op.and]: Utils.ilike(
'bets',
'reference',
filter.reference,
),
};
}
if (filter.calendarStart && filter.calendarEnd) {
where = {
...where,
[Op.or]: [
{
placed_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
{
settled_at: {
[Op.between]: [filter.calendarStart, filter.calendarEnd],
},
},
],
};
}
if (filter.stakeRange) {
const [start, end] = filter.stakeRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
stake: {
...where.stake,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
stake: {
...where.stake,
[Op.lte]: end,
},
};
}
}
if (filter.oddsRange) {
const [start, end] = filter.oddsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
odds: {
...where.odds,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
odds: {
...where.odds,
[Op.lte]: end,
},
};
}
}
if (filter.potential_payoutRange) {
const [start, end] = filter.potential_payoutRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
potential_payout: {
...where.potential_payout,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
potential_payout: {
...where.potential_payout,
[Op.lte]: end,
},
};
}
}
if (filter.placed_atRange) {
const [start, end] = filter.placed_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
placed_at: {
...where.placed_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
placed_at: {
...where.placed_at,
[Op.lte]: end,
},
};
}
}
if (filter.settled_atRange) {
const [start, end] = filter.settled_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
settled_at: {
...where.settled_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
settled_at: {
...where.settled_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.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.bets.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(
'bets',
'reference',
query,
),
],
};
}
const records = await db.bets.findAll({
attributes: [ 'id', 'reference' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['reference', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.reference,
}));
}
};