39220-vm/backend/src/db/api/aberturas_pacotes_itens.js
2026-03-16 19:36:59 +00:00

479 lines
12 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 Aberturas_pacotes_itensDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const aberturas_pacotes_itens = await db.aberturas_pacotes_itens.create(
{
id: data.id || undefined,
ordem_revelacao: data.ordem_revelacao
||
null
,
era_desejada: data.era_desejada
||
false
,
era_repetida: data.era_repetida
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await aberturas_pacotes_itens.setAbertura( data.abertura || null, {
transaction,
});
await aberturas_pacotes_itens.setFigurinha( data.figurinha || null, {
transaction,
});
return aberturas_pacotes_itens;
}
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 aberturas_pacotes_itensData = data.map((item, index) => ({
id: item.id || undefined,
ordem_revelacao: item.ordem_revelacao
||
null
,
era_desejada: item.era_desejada
||
false
,
era_repetida: item.era_repetida
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const aberturas_pacotes_itens = await db.aberturas_pacotes_itens.bulkCreate(aberturas_pacotes_itensData, { transaction });
// For each item created, replace relation files
return aberturas_pacotes_itens;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const aberturas_pacotes_itens = await db.aberturas_pacotes_itens.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.ordem_revelacao !== undefined) updatePayload.ordem_revelacao = data.ordem_revelacao;
if (data.era_desejada !== undefined) updatePayload.era_desejada = data.era_desejada;
if (data.era_repetida !== undefined) updatePayload.era_repetida = data.era_repetida;
updatePayload.updatedById = currentUser.id;
await aberturas_pacotes_itens.update(updatePayload, {transaction});
if (data.abertura !== undefined) {
await aberturas_pacotes_itens.setAbertura(
data.abertura,
{ transaction }
);
}
if (data.figurinha !== undefined) {
await aberturas_pacotes_itens.setFigurinha(
data.figurinha,
{ transaction }
);
}
return aberturas_pacotes_itens;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const aberturas_pacotes_itens = await db.aberturas_pacotes_itens.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of aberturas_pacotes_itens) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of aberturas_pacotes_itens) {
await record.destroy({transaction});
}
});
return aberturas_pacotes_itens;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const aberturas_pacotes_itens = await db.aberturas_pacotes_itens.findByPk(id, options);
await aberturas_pacotes_itens.update({
deletedBy: currentUser.id
}, {
transaction,
});
await aberturas_pacotes_itens.destroy({
transaction
});
return aberturas_pacotes_itens;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const aberturas_pacotes_itens = await db.aberturas_pacotes_itens.findOne(
{ where },
{ transaction },
);
if (!aberturas_pacotes_itens) {
return aberturas_pacotes_itens;
}
const output = aberturas_pacotes_itens.get({plain: true});
output.abertura = await aberturas_pacotes_itens.getAbertura({
transaction
});
output.figurinha = await aberturas_pacotes_itens.getFigurinha({
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.aberturas_pacotes,
as: 'abertura',
where: filter.abertura ? {
[Op.or]: [
{ id: { [Op.in]: filter.abertura.split('|').map(term => Utils.uuid(term)) } },
{
tipo_pacote: {
[Op.or]: filter.abertura.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.figurinhas,
as: 'figurinha',
where: filter.figurinha ? {
[Op.or]: [
{ id: { [Op.in]: filter.figurinha.split('|').map(term => Utils.uuid(term)) } },
{
nome: {
[Op.or]: filter.figurinha.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.ordem_revelacaoRange) {
const [start, end] = filter.ordem_revelacaoRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ordem_revelacao: {
...where.ordem_revelacao,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ordem_revelacao: {
...where.ordem_revelacao,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.era_desejada) {
where = {
...where,
era_desejada: filter.era_desejada,
};
}
if (filter.era_repetida) {
where = {
...where,
era_repetida: filter.era_repetida,
};
}
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.aberturas_pacotes_itens.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(
'aberturas_pacotes_itens',
'ordem_revelacao',
query,
),
],
};
}
const records = await db.aberturas_pacotes_itens.findAll({
attributes: [ 'id', 'ordem_revelacao' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['ordem_revelacao', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.ordem_revelacao,
}));
}
};