This commit is contained in:
lingdocs 2022-09-01 18:58:13 +04:00
parent 9956ed4550
commit 33e74b9c16
2 changed files with 17 additions and 30 deletions

View File

@ -104,18 +104,12 @@ export async function updateLingdocsUser(uuid: T.UUID, toUpdate:
): Promise<T.LingdocsUser> {
const user = await getLingdocsUser("userId", uuid);
if (!user) throw new Error("unable to update - user not found " + uuid);
console.log("inUpdateLingdocsUser", toUpdate);
// console.log("inUpdateLingdocsUser", toUpdate);
if ("tests" in toUpdate) {
const newTests = toUpdate.tests.filter((t) => !user.tests.some(x => x.time === t.time));
console.log("will try to add test");
return await insertLingdocsUser({
...user,
tests: [...user.tests, ...newTests],
tests: addNewTests(user.tests, toUpdate.tests, 2),
});
// return await insertLingdocsUser({
// ...user,
// tests: addNewTests(user.tests, toUpdate.tests, 2),
// });
}
if ("password" in toUpdate) {
const { passwordReset, ...u } = user;
@ -209,23 +203,18 @@ function stringToHex(str: string) {
* @param newResults - the tests to be added to a users record
* @param amountToKeep - the amount of repeat tests to keep (defaults to 2)
*/
// function addNewTests(existing: Readonly<T.TestResult[]>, toAdd: T.TestResult[], amountToKeep = 2): T.TestResult[] {
// console.log({ existing, toAdd, amountToKeep });
// return [
// ...existing,
// ...toAdd,
// ];
// // const tests = [...existing];
// // // check to make sure that we're only adding test results that are not already added
// // const newTests = toAdd.filter((t) => !tests.some(x => x.time === t.time));
// // newTests.forEach((nt) => {
// // const repeatPasses = tests.filter(t => t.id === nt.id);
// // if (repeatPasses.length > (amountToKeep - 1)) {
// // // already have enough repeat passes saved, remove the oldest one
// // const i = tests.findIndex(x => x.id === nt.id);
// // if (i > -1) tests.splice(i, 1);
// // }
// // tests.push(nt);
// // });
// // return tests;
// }
function addNewTests(existing: Readonly<T.TestResult[]>, toAdd: T.TestResult[], amountToKeep = 2): T.TestResult[] {
const tests = [...existing];
// check to make sure that we're only adding test results that are not already added
const newTests = toAdd.filter((t) => !tests.some(x => x.time === t.time));
newTests.forEach((nt) => {
const repeatPasses = tests.filter(t => t.id === nt.id);
if (repeatPasses.length > (amountToKeep - 1)) {
// already have enough repeat passes saved, remove the oldest one
const i = tests.findIndex(x => x.id === nt.id);
if (i > -1) tests.splice(i, 1);
}
tests.push(nt);
});
return tests;
}

View File

@ -48,11 +48,9 @@ apiRouter.get("/user", (req, res, next) => {
* adds (passed) test results to the record of the user signed in
*/
apiRouter.put("/user/tests", async (req, res, next) => {
console.log("came into the tests route", req.body.tests);
if (!req.user) return next("user not found");
try {
const { tests } = req.body as T.PostTestResultsBody;
console.log("will update with", tests);
await updateLingdocsUser(req.user.userId, { tests });
sendResponse(res, {
ok: true,