post test results functionality

This commit is contained in:
lingdocs 2021-09-18 23:11:51 -04:00
parent 560e450dc1
commit 2323ce1f03
6 changed files with 42 additions and 5 deletions

View File

@ -78,8 +78,6 @@ export async function deleteCouchDbAuthUser(uuid: T.UUID): Promise<void> {
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<T.LingdocsUser> {
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({

View File

@ -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
*/

View File

@ -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,

View File

@ -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",

View File

@ -35,6 +35,10 @@ export async function updateUserTextOptionsRecord(userTextOptionsRecord: AT.User
return response;
}
export async function postTestResults(tests: AT.TestResult[]): Promise<AT.PostTestResultsResponse> {
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<AT.APIResponse> {
const response = await fetch(baseUrl[service] + url, {
method,

View File

@ -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 = {