From 712a1991a680c2ccbc1b566db6efb4e08d4eb59a Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Wed, 25 Aug 2021 12:52:38 +0400 Subject: [PATCH] forgot to send request --- account/src/lib/user-utils.ts | 33 ++++++++++++++++++++++++++++-- account/src/routers/api-router.ts | 23 +++++---------------- account/src/routers/auth-router.ts | 18 +++++++++++++++- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/account/src/lib/user-utils.ts b/account/src/lib/user-utils.ts index 403ef9a..9268919 100644 --- a/account/src/lib/user-utils.ts +++ b/account/src/lib/user-utils.ts @@ -1,11 +1,18 @@ import { v4 as uuidv4 } from "uuid"; -import { insertLingdocsUser } from "../lib/couch-db"; +import { + insertLingdocsUser, + addCouchDbAuthUser, + updateLingdocsUser, +} from "../lib/couch-db"; import { getHash, getEmailTokenAndHash, } from "../lib/password-utils"; import { getTimestamp } from "../lib/time-utils"; -import { sendVerificationEmail } from "../lib/mail-utils"; +import { + sendVerificationEmail, + sendAccountUpgradeMessage, +} from "../lib/mail-utils"; import { outsideProviders } from "../middleware/setup-passport"; import * as T from "../../../website/src/lib/account-types"; @@ -43,6 +50,28 @@ function getEmailFromGoogleProfile(profile: T.GoogleProfile): { email: string | }; } +export async function upgradeUser(userId: T.UUID): Promise { + // add user to couchdb authentication db + const { password, userDbName } = await addCouchDbAuthUser(userId); + // // create user db + // update LingdocsUser + const u = await updateLingdocsUser(userId, { + level: "student", + wordlistDbName: userDbName, + couchDbPassword: password, + requestedUpgradeToStudent: undefined, + }); + if (u.email) { + sendAccountUpgradeMessage(u).catch(console.error); + } + const upgraded: T.UpgradeUserResponse = { + ok: true, + message: "user upgraded to student", + user: u, + }; + return upgraded; +} + export async function createNewUser(input: { strategy: "local", email: string, diff --git a/account/src/routers/api-router.ts b/account/src/routers/api-router.ts index e3c38a2..f2cab33 100644 --- a/account/src/routers/api-router.ts +++ b/account/src/routers/api-router.ts @@ -3,7 +3,6 @@ import { deleteLingdocsUser, getLingdocsUser, updateLingdocsUser, - addCouchDbAuthUser, deleteCouchDbAuthUser, } from "../lib/couch-db"; import { @@ -12,10 +11,12 @@ import { getEmailTokenAndHash, } from "../lib/password-utils"; import { - sendAccountUpgradeMessage, sendUpgradeRequestToAdmin, sendVerificationEmail, } from "../lib/mail-utils"; +import { + upgradeUser, +} from "../lib/user-utils"; import * as T from "../../../website/src/lib/account-types"; import env from "../lib/env-vars"; @@ -132,22 +133,7 @@ apiRouter.put("/user/upgrade", async (req, res, next) => { res.send(alreadyUpgraded); return; } - // add user to couchdb authentication db - const { password, userDbName } = await addCouchDbAuthUser(userId); - // // create user db - // update LingdocsUser - const u = await updateLingdocsUser(userId, { - level: "student", - wordlistDbName: userDbName, - couchDbPassword: password, - requestedUpgradeToStudent: undefined, - }); - sendAccountUpgradeMessage(u).catch(console.error); - const upgraded: T.UpgradeUserResponse = { - ok: true, - message: "user upgraded to student", - user: u, - }; + const upgraded: T.UpgradeUserResponse = await upgradeUser(userId); res.send(upgraded); } catch (e) { next(e); @@ -163,6 +149,7 @@ apiRouter.post("/user/upgradeToStudentRequest", async (req, res, next) => { } sendUpgradeRequestToAdmin(req.user).catch(console.error); await updateLingdocsUser(req.user.userId, { requestedUpgradeToStudent: true }); + res.send({ ok: true, message: "request for upgrade sent" }); } catch (e) { next(e); } diff --git a/account/src/routers/auth-router.ts b/account/src/routers/auth-router.ts index d5d1b3f..4777289 100644 --- a/account/src/routers/auth-router.ts +++ b/account/src/routers/auth-router.ts @@ -12,6 +12,9 @@ import { compareToHash, getEmailTokenAndHash, } from "../lib/password-utils"; +import { + upgradeUser, +} from "../lib/user-utils"; import { validateReCaptcha } from "../lib/recaptcha"; import { getTimestamp, @@ -151,7 +154,20 @@ const authRouter = (passport: PassportStatic) => { } catch (e) { next(e); } - }) + }); + + router.post("/admin/upgradeToStudent/:userId", async (req, res, next) => { + try { + if (!req.user.admin) { + return res.redirect("/"); + } + const userId = req.params.userId; + await upgradeUser(userId as T.UUID); + res.redirect("/admin"); + } catch (e) { + next(e); + } + }); router.get("/email-verification/:uuid/:token", async (req, res, next) => { const page = "email-verification";