31081/backend/src/db/api/lottery_numbers.js
2025-04-29 09:05:45 +00:00

377 lines
9.2 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_numbersDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lottery_numbers = await db.lottery_numbers.create(
{
id: data.id || undefined,
number_array_1: data.number_array_1 || null,
number_array_2: data.number_array_2 || null,
number_array_3: data.number_array_3 || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await lottery_numbers.setUser(data.user || null, {
transaction,
});
return lottery_numbers;
}
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_numbersData = data.map((item, index) => ({
id: item.id || undefined,
number_array_1: item.number_array_1 || null,
number_array_2: item.number_array_2 || null,
number_array_3: item.number_array_3 || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const lottery_numbers = await db.lottery_numbers.bulkCreate(
lottery_numbersData,
{ transaction },
);
// For each item created, replace relation files
return lottery_numbers;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lottery_numbers = await db.lottery_numbers.findByPk(
id,
{},
{ transaction },
);
const updatePayload = {};
if (data.number_array_1 !== undefined)
updatePayload.number_array_1 = data.number_array_1;
if (data.number_array_2 !== undefined)
updatePayload.number_array_2 = data.number_array_2;
if (data.number_array_3 !== undefined)
updatePayload.number_array_3 = data.number_array_3;
updatePayload.updatedById = currentUser.id;
await lottery_numbers.update(updatePayload, { transaction });
if (data.user !== undefined) {
await lottery_numbers.setUser(
data.user,
{ transaction },
);
}
return lottery_numbers;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lottery_numbers = await db.lottery_numbers.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of lottery_numbers) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of lottery_numbers) {
await record.destroy({ transaction });
}
});
return lottery_numbers;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const lottery_numbers = await db.lottery_numbers.findByPk(id, options);
await lottery_numbers.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await lottery_numbers.destroy({
transaction,
});
return lottery_numbers;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const lottery_numbers = await db.lottery_numbers.findOne(
{ where },
{ transaction },
);
if (!lottery_numbers) {
return lottery_numbers;
}
const output = lottery_numbers.get({ plain: true });
output.user = await lottery_numbers.getUser({
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}%` })),
},
},
],
}
: {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.number_array_1Range) {
const [start, end] = filter.number_array_1Range;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
number_array_1: {
...where.number_array_1,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
number_array_1: {
...where.number_array_1,
[Op.lte]: end,
},
};
}
}
if (filter.number_array_2Range) {
const [start, end] = filter.number_array_2Range;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
number_array_2: {
...where.number_array_2,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
number_array_2: {
...where.number_array_2,
[Op.lte]: end,
},
};
}
}
if (filter.number_array_3Range) {
const [start, end] = filter.number_array_3Range;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
number_array_3: {
...where.number_array_3,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
number_array_3: {
...where.number_array_3,
[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.lottery_numbers.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_numbers', 'number_array_1', query),
],
};
}
const records = await db.lottery_numbers.findAll({
attributes: ['id', 'number_array_1'],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['number_array_1', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.number_array_1,
}));
}
};