39907-vm/backend/src/db/api/beatmaps.js
2026-05-06 07:06:11 +00:00

703 lines
17 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 BeatmapsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const beatmaps = await db.beatmaps.create(
{
id: data.id || undefined,
osu_beatmap_numeric: data.osu_beatmap_numeric
||
null
,
title: data.title
||
null
,
version: data.version
||
null
,
artist_name: data.artist_name
||
null
,
mode: data.mode
||
null
,
status: data.status
||
null
,
playcount: data.playcount
||
null
,
background_image_url: data.background_image_url
||
null
,
last_synced_at: data.last_synced_at
||
null
,
is_active: data.is_active
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await beatmaps.setArtist( data.artist || null, {
transaction,
});
await beatmaps.setMapper( data.mapper || null, {
transaction,
});
await beatmaps.setBeatmap_set( data.beatmap_set || null, {
transaction,
});
return beatmaps;
}
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 beatmapsData = data.map((item, index) => ({
id: item.id || undefined,
osu_beatmap_numeric: item.osu_beatmap_numeric
||
null
,
title: item.title
||
null
,
version: item.version
||
null
,
artist_name: item.artist_name
||
null
,
mode: item.mode
||
null
,
status: item.status
||
null
,
playcount: item.playcount
||
null
,
background_image_url: item.background_image_url
||
null
,
last_synced_at: item.last_synced_at
||
null
,
is_active: item.is_active
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const beatmaps = await db.beatmaps.bulkCreate(beatmapsData, { transaction });
// For each item created, replace relation files
return beatmaps;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const beatmaps = await db.beatmaps.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.osu_beatmap_numeric !== undefined) updatePayload.osu_beatmap_numeric = data.osu_beatmap_numeric;
if (data.title !== undefined) updatePayload.title = data.title;
if (data.version !== undefined) updatePayload.version = data.version;
if (data.artist_name !== undefined) updatePayload.artist_name = data.artist_name;
if (data.mode !== undefined) updatePayload.mode = data.mode;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.playcount !== undefined) updatePayload.playcount = data.playcount;
if (data.background_image_url !== undefined) updatePayload.background_image_url = data.background_image_url;
if (data.last_synced_at !== undefined) updatePayload.last_synced_at = data.last_synced_at;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
updatePayload.updatedById = currentUser.id;
await beatmaps.update(updatePayload, {transaction});
if (data.artist !== undefined) {
await beatmaps.setArtist(
data.artist,
{ transaction }
);
}
if (data.mapper !== undefined) {
await beatmaps.setMapper(
data.mapper,
{ transaction }
);
}
if (data.beatmap_set !== undefined) {
await beatmaps.setBeatmap_set(
data.beatmap_set,
{ transaction }
);
}
return beatmaps;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const beatmaps = await db.beatmaps.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of beatmaps) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of beatmaps) {
await record.destroy({transaction});
}
});
return beatmaps;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const beatmaps = await db.beatmaps.findByPk(id, options);
await beatmaps.update({
deletedBy: currentUser.id
}, {
transaction,
});
await beatmaps.destroy({
transaction
});
return beatmaps;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const beatmaps = await db.beatmaps.findOne(
{ where },
{ transaction },
);
if (!beatmaps) {
return beatmaps;
}
const output = beatmaps.get({plain: true});
output.game_rounds_beatmap_a = await beatmaps.getGame_rounds_beatmap_a({
transaction
});
output.game_rounds_beatmap_b = await beatmaps.getGame_rounds_beatmap_b({
transaction
});
output.artist = await beatmaps.getArtist({
transaction
});
output.mapper = await beatmaps.getMapper({
transaction
});
output.beatmap_set = await beatmaps.getBeatmap_set({
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.artists,
as: 'artist',
where: filter.artist ? {
[Op.or]: [
{ id: { [Op.in]: filter.artist.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.artist.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.mappers,
as: 'mapper',
where: filter.mapper ? {
[Op.or]: [
{ id: { [Op.in]: filter.mapper.split('|').map(term => Utils.uuid(term)) } },
{
osu_username: {
[Op.or]: filter.mapper.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.beatmap_sets,
as: 'beatmap_set',
where: filter.beatmap_set ? {
[Op.or]: [
{ id: { [Op.in]: filter.beatmap_set.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.beatmap_set.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'beatmaps',
'title',
filter.title,
),
};
}
if (filter.version) {
where = {
...where,
[Op.and]: Utils.ilike(
'beatmaps',
'version',
filter.version,
),
};
}
if (filter.artist_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'beatmaps',
'artist_name',
filter.artist_name,
),
};
}
if (filter.background_image_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'beatmaps',
'background_image_url',
filter.background_image_url,
),
};
}
if (filter.osu_beatmap_numericRange) {
const [start, end] = filter.osu_beatmap_numericRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
osu_beatmap_numeric: {
...where.osu_beatmap_numeric,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
osu_beatmap_numeric: {
...where.osu_beatmap_numeric,
[Op.lte]: end,
},
};
}
}
if (filter.playcountRange) {
const [start, end] = filter.playcountRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
playcount: {
...where.playcount,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
playcount: {
...where.playcount,
[Op.lte]: end,
},
};
}
}
if (filter.last_synced_atRange) {
const [start, end] = filter.last_synced_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
last_synced_at: {
...where.last_synced_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
last_synced_at: {
...where.last_synced_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.mode) {
where = {
...where,
mode: filter.mode,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.is_active) {
where = {
...where,
is_active: filter.is_active,
};
}
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.beatmaps.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(
'beatmaps',
'title',
query,
),
],
};
}
const records = await db.beatmaps.findAll({
attributes: [ 'id', 'title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.title,
}));
}
};