forgot to send request
This commit is contained in:
parent
497d36048b
commit
712a1991a6
|
@ -1,11 +1,18 @@
|
||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
import { insertLingdocsUser } from "../lib/couch-db";
|
import {
|
||||||
|
insertLingdocsUser,
|
||||||
|
addCouchDbAuthUser,
|
||||||
|
updateLingdocsUser,
|
||||||
|
} from "../lib/couch-db";
|
||||||
import {
|
import {
|
||||||
getHash,
|
getHash,
|
||||||
getEmailTokenAndHash,
|
getEmailTokenAndHash,
|
||||||
} from "../lib/password-utils";
|
} from "../lib/password-utils";
|
||||||
import { getTimestamp } from "../lib/time-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 { outsideProviders } from "../middleware/setup-passport";
|
||||||
import * as T from "../../../website/src/lib/account-types";
|
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<T.UpgradeUserResponse> {
|
||||||
|
// 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: {
|
export async function createNewUser(input: {
|
||||||
strategy: "local",
|
strategy: "local",
|
||||||
email: string,
|
email: string,
|
||||||
|
|
|
@ -3,7 +3,6 @@ import {
|
||||||
deleteLingdocsUser,
|
deleteLingdocsUser,
|
||||||
getLingdocsUser,
|
getLingdocsUser,
|
||||||
updateLingdocsUser,
|
updateLingdocsUser,
|
||||||
addCouchDbAuthUser,
|
|
||||||
deleteCouchDbAuthUser,
|
deleteCouchDbAuthUser,
|
||||||
} from "../lib/couch-db";
|
} from "../lib/couch-db";
|
||||||
import {
|
import {
|
||||||
|
@ -12,10 +11,12 @@ import {
|
||||||
getEmailTokenAndHash,
|
getEmailTokenAndHash,
|
||||||
} from "../lib/password-utils";
|
} from "../lib/password-utils";
|
||||||
import {
|
import {
|
||||||
sendAccountUpgradeMessage,
|
|
||||||
sendUpgradeRequestToAdmin,
|
sendUpgradeRequestToAdmin,
|
||||||
sendVerificationEmail,
|
sendVerificationEmail,
|
||||||
} from "../lib/mail-utils";
|
} from "../lib/mail-utils";
|
||||||
|
import {
|
||||||
|
upgradeUser,
|
||||||
|
} from "../lib/user-utils";
|
||||||
import * as T from "../../../website/src/lib/account-types";
|
import * as T from "../../../website/src/lib/account-types";
|
||||||
import env from "../lib/env-vars";
|
import env from "../lib/env-vars";
|
||||||
|
|
||||||
|
@ -132,22 +133,7 @@ apiRouter.put("/user/upgrade", async (req, res, next) => {
|
||||||
res.send(alreadyUpgraded);
|
res.send(alreadyUpgraded);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// add user to couchdb authentication db
|
const upgraded: T.UpgradeUserResponse = await upgradeUser(userId);
|
||||||
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,
|
|
||||||
};
|
|
||||||
res.send(upgraded);
|
res.send(upgraded);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
next(e);
|
next(e);
|
||||||
|
@ -163,6 +149,7 @@ apiRouter.post("/user/upgradeToStudentRequest", async (req, res, next) => {
|
||||||
}
|
}
|
||||||
sendUpgradeRequestToAdmin(req.user).catch(console.error);
|
sendUpgradeRequestToAdmin(req.user).catch(console.error);
|
||||||
await updateLingdocsUser(req.user.userId, { requestedUpgradeToStudent: true });
|
await updateLingdocsUser(req.user.userId, { requestedUpgradeToStudent: true });
|
||||||
|
res.send({ ok: true, message: "request for upgrade sent" });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,9 @@ import {
|
||||||
compareToHash,
|
compareToHash,
|
||||||
getEmailTokenAndHash,
|
getEmailTokenAndHash,
|
||||||
} from "../lib/password-utils";
|
} from "../lib/password-utils";
|
||||||
|
import {
|
||||||
|
upgradeUser,
|
||||||
|
} from "../lib/user-utils";
|
||||||
import { validateReCaptcha } from "../lib/recaptcha";
|
import { validateReCaptcha } from "../lib/recaptcha";
|
||||||
import {
|
import {
|
||||||
getTimestamp,
|
getTimestamp,
|
||||||
|
@ -151,7 +154,20 @@ const authRouter = (passport: PassportStatic) => {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
next(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) => {
|
router.get("/email-verification/:uuid/:token", async (req, res, next) => {
|
||||||
const page = "email-verification";
|
const page = "email-verification";
|
||||||
|
|
Loading…
Reference in New Issue