Add some new endpoint work for manipulating UGPT Gamespecific settings

This commit is contained in:
zkldi
2021-10-01 01:35:39 +01:00
parent 235524724e
commit 6b426592bb
3 changed files with 91 additions and 1 deletions
@@ -20,6 +20,9 @@ t.test("GET /api/v1/users/:userID/games/:game/:playtype/settings", (t) => {
preferredSessionAlg: null,
preferredProfileAlg: null,
stats: [],
gameSpecific: {
display2DXTra: false,
},
},
});
@@ -60,6 +63,59 @@ t.test("PATCH /api/v1/users/:userID/games/:game/:playtype/settings", (t) => {
preferredSessionAlg: null,
preferredProfileAlg: null,
stats: [],
gameSpecific: {
display2DXTra: false,
},
},
},
"Should only update the mutated properties."
);
const data = await db["game-settings"].findOne({
userID: 1,
game: "iidx",
playtype: "SP",
});
t.equal(data?.preferences.preferredScoreAlg, "ktRating");
t.end();
});
t.test("Should update a user's game specific settings.", async (t) => {
await db["api-tokens"].insert({
userID: 1,
identifier: "api_token",
permissions: {
customise_profile: true,
},
token: "api_token",
});
const res = await mockApi
.patch("/api/v1/users/1/games/iidx/SP/settings")
.set("Authorization", "Bearer api_token")
.send({
preferredScoreAlg: "ktRating",
gameSpecific: {
display2DXTra: true,
},
});
t.strictSame(
res.body.body,
{
userID: 1,
game: "iidx",
playtype: "SP",
preferences: {
preferredScoreAlg: "ktRating",
preferredSessionAlg: null,
preferredProfileAlg: null,
stats: [],
gameSpecific: {
display2DXTra: true,
},
},
},
"Should only update the mutated properties."
@@ -35,6 +35,8 @@ router.patch(
preferredScoreAlg: p.optional(p.nullable(p.isIn(gptConfig.scoreRatingAlgs))),
preferredSessionAlg: p.optional(p.nullable(p.isIn(gptConfig.sessionRatingAlgs))),
preferredProfileAlg: p.optional(p.nullable(p.isIn(gptConfig.profileRatingAlgs))),
// This is a pretty stupid IIFE level hack, ah well.
gameSpecific: p.any,
});
if (err) {
@@ -44,6 +46,29 @@ router.patch(
});
}
if (req.body.gameSpecific) {
let schema = {};
if (game === "iidx") {
schema = {
display2DXTra: "boolean",
};
}
const err = p(
req.body,
{ gameSpecific: p.optional(schema) },
{},
{ allowExcessKeys: true }
);
if (err) {
return res.status(400).json({
success: false,
description: FormatPrError(err, "Invalid game-settings."),
});
}
}
const updateQuery: Record<string, string> = {};
// @warning Slightly icky dynamic prop assignment instead of copypasta.
@@ -55,6 +80,12 @@ router.patch(
}
}
if (req.body.gameSpecific) {
for (const key in req.body.gameSpecific) {
updateQuery[`preferences.gameSpecific.${key}`] = req.body.gameSpecific[key];
}
}
if (Object.keys(updateQuery).length === 0) {
const settings = await db["game-settings"].findOne({
userID: user.id,
@@ -7,7 +7,10 @@
"preferredScoreAlg": null,
"preferredSessionAlg": null,
"preferredProfileAlg": null,
"stats": []
"stats": [],
"gameSpecific": {
"display2DXTra": false
}
}
}
]