39920-vm/backend/src/db/api/country_skill_snapshots.js
2026-05-06 18:59:03 +00:00

1023 lines
28 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 Country_skill_snapshotsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const country_skill_snapshots = await db.country_skill_snapshots.create(
{
id: data.id || undefined,
top_players_count: data.top_players_count
||
null
,
overall_skill: data.overall_skill
||
null
,
stream_skill: data.stream_skill
||
null
,
chordjack_skill: data.chordjack_skill
||
null
,
jackspeed_skill: data.jackspeed_skill
||
null
,
stamina_skill: data.stamina_skill
||
null
,
ln_skill: data.ln_skill
||
null
,
sv_skill: data.sv_skill
||
null
,
vibro_skill: data.vibro_skill
||
null
,
accuracy_skill: data.accuracy_skill
||
null
,
tech_skill: data.tech_skill
||
null
,
overall_rank: data.overall_rank
||
null
,
ln_rank: data.ln_rank
||
null
,
chordjack_rank: data.chordjack_rank
||
null
,
stream_rank: data.stream_rank
||
null
,
accuracy_rank: data.accuracy_rank
||
null
,
sv_rank: data.sv_rank
||
null
,
snapshot_at: data.snapshot_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await country_skill_snapshots.setCountry( data.country || null, {
transaction,
});
return country_skill_snapshots;
}
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 country_skill_snapshotsData = data.map((item, index) => ({
id: item.id || undefined,
top_players_count: item.top_players_count
||
null
,
overall_skill: item.overall_skill
||
null
,
stream_skill: item.stream_skill
||
null
,
chordjack_skill: item.chordjack_skill
||
null
,
jackspeed_skill: item.jackspeed_skill
||
null
,
stamina_skill: item.stamina_skill
||
null
,
ln_skill: item.ln_skill
||
null
,
sv_skill: item.sv_skill
||
null
,
vibro_skill: item.vibro_skill
||
null
,
accuracy_skill: item.accuracy_skill
||
null
,
tech_skill: item.tech_skill
||
null
,
overall_rank: item.overall_rank
||
null
,
ln_rank: item.ln_rank
||
null
,
chordjack_rank: item.chordjack_rank
||
null
,
stream_rank: item.stream_rank
||
null
,
accuracy_rank: item.accuracy_rank
||
null
,
sv_rank: item.sv_rank
||
null
,
snapshot_at: item.snapshot_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const country_skill_snapshots = await db.country_skill_snapshots.bulkCreate(country_skill_snapshotsData, { transaction });
// For each item created, replace relation files
return country_skill_snapshots;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const country_skill_snapshots = await db.country_skill_snapshots.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.top_players_count !== undefined) updatePayload.top_players_count = data.top_players_count;
if (data.overall_skill !== undefined) updatePayload.overall_skill = data.overall_skill;
if (data.stream_skill !== undefined) updatePayload.stream_skill = data.stream_skill;
if (data.chordjack_skill !== undefined) updatePayload.chordjack_skill = data.chordjack_skill;
if (data.jackspeed_skill !== undefined) updatePayload.jackspeed_skill = data.jackspeed_skill;
if (data.stamina_skill !== undefined) updatePayload.stamina_skill = data.stamina_skill;
if (data.ln_skill !== undefined) updatePayload.ln_skill = data.ln_skill;
if (data.sv_skill !== undefined) updatePayload.sv_skill = data.sv_skill;
if (data.vibro_skill !== undefined) updatePayload.vibro_skill = data.vibro_skill;
if (data.accuracy_skill !== undefined) updatePayload.accuracy_skill = data.accuracy_skill;
if (data.tech_skill !== undefined) updatePayload.tech_skill = data.tech_skill;
if (data.overall_rank !== undefined) updatePayload.overall_rank = data.overall_rank;
if (data.ln_rank !== undefined) updatePayload.ln_rank = data.ln_rank;
if (data.chordjack_rank !== undefined) updatePayload.chordjack_rank = data.chordjack_rank;
if (data.stream_rank !== undefined) updatePayload.stream_rank = data.stream_rank;
if (data.accuracy_rank !== undefined) updatePayload.accuracy_rank = data.accuracy_rank;
if (data.sv_rank !== undefined) updatePayload.sv_rank = data.sv_rank;
if (data.snapshot_at !== undefined) updatePayload.snapshot_at = data.snapshot_at;
updatePayload.updatedById = currentUser.id;
await country_skill_snapshots.update(updatePayload, {transaction});
if (data.country !== undefined) {
await country_skill_snapshots.setCountry(
data.country,
{ transaction }
);
}
return country_skill_snapshots;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const country_skill_snapshots = await db.country_skill_snapshots.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of country_skill_snapshots) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of country_skill_snapshots) {
await record.destroy({transaction});
}
});
return country_skill_snapshots;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const country_skill_snapshots = await db.country_skill_snapshots.findByPk(id, options);
await country_skill_snapshots.update({
deletedBy: currentUser.id
}, {
transaction,
});
await country_skill_snapshots.destroy({
transaction
});
return country_skill_snapshots;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const country_skill_snapshots = await db.country_skill_snapshots.findOne(
{ where },
{ transaction },
);
if (!country_skill_snapshots) {
return country_skill_snapshots;
}
const output = country_skill_snapshots.get({plain: true});
output.country = await country_skill_snapshots.getCountry({
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}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.top_players_countRange) {
const [start, end] = filter.top_players_countRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
top_players_count: {
...where.top_players_count,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
top_players_count: {
...where.top_players_count,
[Op.lte]: end,
},
};
}
}
if (filter.overall_skillRange) {
const [start, end] = filter.overall_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
overall_skill: {
...where.overall_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
overall_skill: {
...where.overall_skill,
[Op.lte]: end,
},
};
}
}
if (filter.stream_skillRange) {
const [start, end] = filter.stream_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
stream_skill: {
...where.stream_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
stream_skill: {
...where.stream_skill,
[Op.lte]: end,
},
};
}
}
if (filter.chordjack_skillRange) {
const [start, end] = filter.chordjack_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
chordjack_skill: {
...where.chordjack_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
chordjack_skill: {
...where.chordjack_skill,
[Op.lte]: end,
},
};
}
}
if (filter.jackspeed_skillRange) {
const [start, end] = filter.jackspeed_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
jackspeed_skill: {
...where.jackspeed_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
jackspeed_skill: {
...where.jackspeed_skill,
[Op.lte]: end,
},
};
}
}
if (filter.stamina_skillRange) {
const [start, end] = filter.stamina_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
stamina_skill: {
...where.stamina_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
stamina_skill: {
...where.stamina_skill,
[Op.lte]: end,
},
};
}
}
if (filter.ln_skillRange) {
const [start, end] = filter.ln_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ln_skill: {
...where.ln_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ln_skill: {
...where.ln_skill,
[Op.lte]: end,
},
};
}
}
if (filter.sv_skillRange) {
const [start, end] = filter.sv_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
sv_skill: {
...where.sv_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
sv_skill: {
...where.sv_skill,
[Op.lte]: end,
},
};
}
}
if (filter.vibro_skillRange) {
const [start, end] = filter.vibro_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
vibro_skill: {
...where.vibro_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
vibro_skill: {
...where.vibro_skill,
[Op.lte]: end,
},
};
}
}
if (filter.accuracy_skillRange) {
const [start, end] = filter.accuracy_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
accuracy_skill: {
...where.accuracy_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
accuracy_skill: {
...where.accuracy_skill,
[Op.lte]: end,
},
};
}
}
if (filter.tech_skillRange) {
const [start, end] = filter.tech_skillRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
tech_skill: {
...where.tech_skill,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
tech_skill: {
...where.tech_skill,
[Op.lte]: end,
},
};
}
}
if (filter.overall_rankRange) {
const [start, end] = filter.overall_rankRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
overall_rank: {
...where.overall_rank,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
overall_rank: {
...where.overall_rank,
[Op.lte]: end,
},
};
}
}
if (filter.ln_rankRange) {
const [start, end] = filter.ln_rankRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ln_rank: {
...where.ln_rank,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ln_rank: {
...where.ln_rank,
[Op.lte]: end,
},
};
}
}
if (filter.chordjack_rankRange) {
const [start, end] = filter.chordjack_rankRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
chordjack_rank: {
...where.chordjack_rank,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
chordjack_rank: {
...where.chordjack_rank,
[Op.lte]: end,
},
};
}
}
if (filter.stream_rankRange) {
const [start, end] = filter.stream_rankRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
stream_rank: {
...where.stream_rank,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
stream_rank: {
...where.stream_rank,
[Op.lte]: end,
},
};
}
}
if (filter.accuracy_rankRange) {
const [start, end] = filter.accuracy_rankRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
accuracy_rank: {
...where.accuracy_rank,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
accuracy_rank: {
...where.accuracy_rank,
[Op.lte]: end,
},
};
}
}
if (filter.sv_rankRange) {
const [start, end] = filter.sv_rankRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
sv_rank: {
...where.sv_rank,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
sv_rank: {
...where.sv_rank,
[Op.lte]: end,
},
};
}
}
if (filter.snapshot_atRange) {
const [start, end] = filter.snapshot_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
snapshot_at: {
...where.snapshot_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
snapshot_at: {
...where.snapshot_at,
[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.country_skill_snapshots.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(
'country_skill_snapshots',
'snapshot_at',
query,
),
],
};
}
const records = await db.country_skill_snapshots.findAll({
attributes: [ 'id', 'snapshot_at' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['snapshot_at', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.snapshot_at,
}));
}
};