upsert the beatsaver map
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Has been cancelled

This commit is contained in:
Lee 2024-10-23 15:40:28 +01:00
parent 0f68b2b69e
commit 584af8c5a4

@ -3,33 +3,40 @@ import { BeatSaverMap, BeatSaverMapModel } from "@ssr/common/model/beatsaver/map
export default class BeatSaverService { export default class BeatSaverService {
/** /**
* Gets a map by its hash. * Gets a map by its hash, updates if necessary, or inserts if not found.
* *
* @param hash the hash of the map * @param hash the hash of the map
* @returns the beatsaver map * @returns the beatsaver map or undefined if not found
*/ */
public static async getMap(hash: string): Promise<BeatSaverMap | undefined> { public static async getMap(hash: string): Promise<BeatSaverMap | undefined> {
// Try to find the existing map by its hash
let map = await BeatSaverMapModel.findById(hash); let map = await BeatSaverMapModel.findById(hash);
if (map != undefined) {
if (map) {
const toObject = map.toObject() as BeatSaverMap; const toObject = map.toObject() as BeatSaverMap;
// If the map is not found, return undefined
if (toObject.notFound) { if (toObject.notFound) {
return undefined; return undefined;
} }
// Return the map if it doesn't need to be refreshed
// If the map does not need to be refreshed, return it
if (!toObject.shouldRefresh()) { if (!toObject.shouldRefresh()) {
return toObject; return toObject;
} }
} }
// Map needs to be fetched or refreshed
const token = await beatsaverService.lookupMap(hash); const token = await beatsaverService.lookupMap(hash);
const uploader = token?.uploader; const uploader = token?.uploader;
const metadata = token?.metadata; const metadata = token?.metadata;
map = await BeatSaverMapModel.create( // Create the new map object based on fetched data
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const newMapData: BeatSaverMap =
token && uploader && metadata token && uploader && metadata
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment ? {
// @ts-expect-error
({
_id: hash, _id: hash,
bsr: token.id, bsr: token.id,
name: token.name, name: token.name,
@ -47,39 +54,42 @@ export default class BeatSaverService {
songName: metadata.songName, songName: metadata.songName,
songSubName: metadata.songSubName, songSubName: metadata.songSubName,
}, },
versions: token.versions.map(version => { versions: token.versions.map(version => ({
return { hash: version.hash.toUpperCase(),
hash: version.hash.toUpperCase(), difficulties: version.diffs.map(diff => ({
difficulties: version.diffs.map(diff => { njs: diff.njs,
return { offset: diff.offset,
njs: diff.njs, notes: diff.notes,
offset: diff.offset, bombs: diff.bombs,
notes: diff.notes, obstacles: diff.obstacles,
bombs: diff.bombs, nps: diff.nps,
obstacles: diff.obstacles, characteristic: diff.characteristic,
nps: diff.nps, difficulty: diff.difficulty,
characteristic: diff.characteristic, events: diff.events,
difficulty: diff.difficulty, chroma: diff.chroma,
events: diff.events, mappingExtensions: diff.me,
chroma: diff.chroma, noodleExtensions: diff.ne,
mappingExtensions: diff.me, cinema: diff.cinema,
noodleExtensions: diff.ne, maxScore: diff.maxScore,
cinema: diff.cinema, label: diff.label,
maxScore: diff.maxScore, })),
label: diff.label, createdAt: new Date(version.createdAt),
}; })),
}),
createdAt: new Date(version.createdAt),
};
}),
lastRefreshed: new Date(), lastRefreshed: new Date(),
} as BeatSaverMap) }
: { : {
_id: hash, _id: hash,
notFound: true, notFound: true,
} };
);
if (map.notFound) { // Upsert the map: if it exists, update it; if not, create a new one
map = await BeatSaverMapModel.findOneAndUpdate({ _id: hash }, newMapData, {
upsert: true,
new: true,
setDefaultsOnInsert: true,
});
if (map == null || map.notFound) {
return undefined; return undefined;
} }
return map.toObject() as BeatSaverMap; return map.toObject() as BeatSaverMap;