diff --git a/account/src/lib/couch-db.ts b/account/src/lib/couch-db.ts index 041bb57..47d97ff 100644 --- a/account/src/lib/couch-db.ts +++ b/account/src/lib/couch-db.ts @@ -104,18 +104,12 @@ export async function updateLingdocsUser(uuid: T.UUID, toUpdate: ): Promise { 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, 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; -// } \ No newline at end of file +function addNewTests(existing: Readonly, 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; +} \ No newline at end of file diff --git a/account/src/routers/api-router.ts b/account/src/routers/api-router.ts index 5d96f3d..f18eb5c 100644 --- a/account/src/routers/api-router.ts +++ b/account/src/routers/api-router.ts @@ -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,