upsert the beatsaver map
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Has been cancelled
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Has been cancelled
This commit is contained in:
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
|
||||||
token && uploader && metadata
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
({
|
const newMapData: BeatSaverMap =
|
||||||
|
token && uploader && metadata
|
||||||
|
? {
|
||||||
_id: hash,
|
_id: hash,
|
||||||
bsr: token.id,
|
bsr: token.id,
|
||||||
name: token.name,
|
name: token.name,
|
||||||
@ -47,11 +54,9 @@ 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 => ({
|
||||||
return {
|
|
||||||
njs: diff.njs,
|
njs: diff.njs,
|
||||||
offset: diff.offset,
|
offset: diff.offset,
|
||||||
notes: diff.notes,
|
notes: diff.notes,
|
||||||
@ -67,19 +72,24 @@ export default class BeatSaverService {
|
|||||||
cinema: diff.cinema,
|
cinema: diff.cinema,
|
||||||
maxScore: diff.maxScore,
|
maxScore: diff.maxScore,
|
||||||
label: diff.label,
|
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;
|
||||||
|
Reference in New Issue
Block a user