diff --git a/account/src/lib/couch-db.ts b/account/src/lib/couch-db.ts index 485bb26..89c7b71 100644 --- a/account/src/lib/couch-db.ts +++ b/account/src/lib/couch-db.ts @@ -78,8 +78,6 @@ export async function deleteCouchDbAuthUser(uuid: T.UUID): Promise { await authUsers.destroy(u._id, u._rev); } -// TODO: TO MAKE THIS SAFER, PASS IN JUST THE UPDATING FIELDS!! -// TODO: take out the updated object - do just an ID, and then use the toUpdate safe thing export async function updateLingdocsUser(uuid: T.UUID, toUpdate: // TODO: OR USE REDUCER?? { name: string } | @@ -106,10 +104,17 @@ export async function updateLingdocsUser(uuid: T.UUID, toUpdate: { userTextOptionsRecord: T.UserTextOptionsRecord } | { upgradeToStudentRequest: "waiting" } | { upgradeToStudentRequest: "denied" } | - { lastActive: T.TimeStamp } + { lastActive: T.TimeStamp } | + { tests: T.TestResult[] } ): Promise { const user = await getLingdocsUser("userId", uuid); if (!user) throw new Error("unable to update - user not found " + uuid); + if ("tests" in toUpdate) { + return await insertLingdocsUser({ + ...user, + tests: [...user.tests, ...toUpdate.tests], + }); + } if ("password" in toUpdate) { const { passwordReset, ...u } = user; return await insertLingdocsUser({ diff --git a/account/src/routers/api-router.ts b/account/src/routers/api-router.ts index c5da5ff..443618a 100644 --- a/account/src/routers/api-router.ts +++ b/account/src/routers/api-router.ts @@ -45,6 +45,24 @@ apiRouter.get("/user", (req, res, next) => { sendResponse(res, { ok: true, user: req.user }); }); +/** + * adds (passed) test results to the record of the user signed in + */ +apiRouter.put("/user/tests", async (req, res, next) => { + if (!req.user) return next("user not found"); + try { + const { tests } = req.body as T.PostTestResultsBody; + await updateLingdocsUser(req.user.userId, { tests }); + return { + ok: true, + message: "posted test results", + tests, + }; + } catch(e) { + next(e); + } +}); + /** * receives a request to change or add a user's own password */ diff --git a/library.ts b/library.ts index 8d947c2..6b06f6c 100644 --- a/library.ts +++ b/library.ts @@ -10,6 +10,7 @@ import { upgradeToStudentRequest, updateUserTextOptionsRecord, getUser, + postTestResults, } from "./website/src/lib/backend-calls"; export { @@ -22,6 +23,7 @@ export { upgradeToStudentRequest, updateUserTextOptionsRecord, getUser, + postTestResults, // TYPES AT, FT, diff --git a/package.json b/package.json index e9b6d35..20c9322 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/lingdocs-main", - "version": "0.1.1", + "version": "0.1.2", "description": "types and functions for lingdocs stuff", "main": "dist/library.js", "module": "dist/library.js", diff --git a/website/src/lib/backend-calls.ts b/website/src/lib/backend-calls.ts index 7258a13..39121b0 100644 --- a/website/src/lib/backend-calls.ts +++ b/website/src/lib/backend-calls.ts @@ -35,6 +35,10 @@ export async function updateUserTextOptionsRecord(userTextOptionsRecord: AT.User return response; } +export async function postTestResults(tests: AT.TestResult[]): Promise { + return await myFetch("account", "user/tests", "PUT", { tests }) as AT.PostTestResultsResponse; +} + export async function signOut() { try { await myFetch("account", "sign-out", "POST"); @@ -63,7 +67,8 @@ export async function myFetch( service: Service, url: string, method: "GET" | "POST" | "PUT" | "DELETE" = "GET", - body?: FT.SubmissionsRequest | { password: string } | AT.UpdateUserTextOptionsRecordBody, + // better typing and safety of all this + body?: FT.SubmissionsRequest | { password: string } | AT.UpdateUserTextOptionsRecordBody | AT.PostTestResultsBody, ): Promise { const response = await fetch(baseUrl[service] + url, { method, diff --git a/website/src/types/account-types.ts b/website/src/types/account-types.ts index 45640eb..65db5e2 100644 --- a/website/src/types/account-types.ts +++ b/website/src/types/account-types.ts @@ -75,6 +75,13 @@ export type UpgradeUserResponse = { user: LingdocsUser, }; +export type PostTestResultsBody = { tests: TestResult[] }; +export type PostTestResultsResponse = { + ok: true, + message: "posted test results", + tests: TestResult[], +}; + export type UpdateUserTextOptionsRecordBody = { userTextOptionsRecord: UserTextOptionsRecord }; export type UpdateUserTextOptionsRecordResponse = {