cleanup
This commit is contained in:
parent
9956ed4550
commit
33e74b9c16
|
@ -104,18 +104,12 @@ export async function updateLingdocsUser(uuid: T.UUID, toUpdate:
|
||||||
): Promise<T.LingdocsUser> {
|
): Promise<T.LingdocsUser> {
|
||||||
const user = await getLingdocsUser("userId", uuid);
|
const user = await getLingdocsUser("userId", uuid);
|
||||||
if (!user) throw new Error("unable to update - user not found " + 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) {
|
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({
|
return await insertLingdocsUser({
|
||||||
...user,
|
...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) {
|
if ("password" in toUpdate) {
|
||||||
const { passwordReset, ...u } = user;
|
const { passwordReset, ...u } = user;
|
||||||
|
@ -209,23 +203,18 @@ function stringToHex(str: string) {
|
||||||
* @param newResults - the tests to be added to a users record
|
* @param newResults - the tests to be added to a users record
|
||||||
* @param amountToKeep - the amount of repeat tests to keep (defaults to 2)
|
* @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[] {
|
function addNewTests(existing: Readonly<T.TestResult[]>, toAdd: T.TestResult[], amountToKeep = 2): T.TestResult[] {
|
||||||
// console.log({ existing, toAdd, amountToKeep });
|
const tests = [...existing];
|
||||||
// return [
|
// check to make sure that we're only adding test results that are not already added
|
||||||
// ...existing,
|
const newTests = toAdd.filter((t) => !tests.some(x => x.time === t.time));
|
||||||
// ...toAdd,
|
newTests.forEach((nt) => {
|
||||||
// ];
|
const repeatPasses = tests.filter(t => t.id === nt.id);
|
||||||
// // const tests = [...existing];
|
if (repeatPasses.length > (amountToKeep - 1)) {
|
||||||
// // // check to make sure that we're only adding test results that are not already added
|
// already have enough repeat passes saved, remove the oldest one
|
||||||
// // const newTests = toAdd.filter((t) => !tests.some(x => x.time === t.time));
|
const i = tests.findIndex(x => x.id === nt.id);
|
||||||
// // newTests.forEach((nt) => {
|
if (i > -1) tests.splice(i, 1);
|
||||||
// // const repeatPasses = tests.filter(t => t.id === nt.id);
|
}
|
||||||
// // if (repeatPasses.length > (amountToKeep - 1)) {
|
tests.push(nt);
|
||||||
// // // already have enough repeat passes saved, remove the oldest one
|
});
|
||||||
// // const i = tests.findIndex(x => x.id === nt.id);
|
return tests;
|
||||||
// // if (i > -1) tests.splice(i, 1);
|
}
|
||||||
// // }
|
|
||||||
// // tests.push(nt);
|
|
||||||
// // });
|
|
||||||
// // return tests;
|
|
||||||
// }
|
|
|
@ -48,11 +48,9 @@ apiRouter.get("/user", (req, res, next) => {
|
||||||
* adds (passed) test results to the record of the user signed in
|
* adds (passed) test results to the record of the user signed in
|
||||||
*/
|
*/
|
||||||
apiRouter.put("/user/tests", async (req, res, next) => {
|
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");
|
if (!req.user) return next("user not found");
|
||||||
try {
|
try {
|
||||||
const { tests } = req.body as T.PostTestResultsBody;
|
const { tests } = req.body as T.PostTestResultsBody;
|
||||||
console.log("will update with", tests);
|
|
||||||
await updateLingdocsUser(req.user.userId, { tests });
|
await updateLingdocsUser(req.user.userId, { tests });
|
||||||
sendResponse(res, {
|
sendResponse(res, {
|
||||||
ok: true,
|
ok: true,
|
||||||
|
|
Loading…
Reference in New Issue