From 74c85d6ec4bb8e37b57cb89f8722c91adf33c327 Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Tue, 24 Aug 2021 17:10:13 +0400 Subject: [PATCH] more debug, why! --- firebase.json | 4 +- functions/import-wordlist.js | 84 --------------------------------- functions/src/index.ts | 16 +++++-- website/src/screens/Account.tsx | 6 +-- 4 files changed, 16 insertions(+), 94 deletions(-) delete mode 100644 functions/import-wordlist.js diff --git a/firebase.json b/firebase.json index 1ac189b..ac7ffc8 100644 --- a/firebase.json +++ b/firebase.json @@ -11,8 +11,8 @@ "function": "/publishDictionary" }, { - "source": "/willError", - "function": "/willError" + "source": "/testAuth", + "function": "/testAuth" } ] } diff --git a/functions/import-wordlist.js b/functions/import-wordlist.js deleted file mode 100644 index e150bc1..0000000 --- a/functions/import-wordlist.js +++ /dev/null @@ -1,84 +0,0 @@ -const nano = require("nano"); -const oldCouch = nano(process.env.OLD_WORDLIST_COUCHDB); -const newCouch = nano(process.env.LINGDOCS_COUCHDB); -const email = process.argv[2]; -const newEmail = process.argv[3]; - -function stringToHex(str) { - const arr1 = []; - for (let n = 0, l = str.length; n < l; n ++) { - const hex = Number(str.charCodeAt(n)).toString(16); - arr1.push(hex); - } - return arr1.join(''); -} - -async function getOldWordList() { - const usersDb = oldCouch.use("_users"); - const res = await usersDb.find({ - selector: { - originalEmail: email, - }, - }); - const { name } = res.docs[0]; - const tag = stringToHex(name); - const userDb = oldCouch.db.use(`userdb-${tag}`); - const { rows } = await userDb.list({ include_docs: true }); - const allDocs = rows.map((row) => row.doc); - return allDocs -} - -function convertWordList(list) { - const now = Date.now(); - return list.map((item) => ({ - _id: item._id, - warmup: "done", - supermemo: { - interval: 0, - repetition: 0, - efactor: 2.5 - }, - dueDate: now, - entry: { ...item.w }, - notes: item.notes, - })); -} - -async function uploadToNewDb(wordlist) { - const usersDb = newCouch.use("_users"); - const res = await usersDb.find({ - selector: { - email: newEmail || email, - }, - }); - const { name } = res.docs[0]; - const tag = stringToHex(name); - const userDb = newCouch.db.use(`userdb-${tag}`); - await userDb.bulk({ docs: wordlist }); -} - -// async function updateWarmup() { -// const usersDb = newCouch.use("_users"); -// const res = await usersDb.find({ -// selector: { -// email: newEmail || email, -// }, -// }); -// const { name } = res.docs[0]; -// const tag = stringToHex(name); -// const userDb = newCouch.db.use(`userdb-${tag}`); -// const { rows } = await userDb.list({ include_docs: true }); -// const allDocs = rows.map((row) => row.doc); -// const updated = allDocs.map((d) => ({ ...d, warmup: "done" })); -// await userDb.bulk({ docs: updated }); -// } - -async function main() { - const oldWordList = await getOldWordList(); - const newWordList = convertWordList(oldWordList); - uploadToNewDb(newWordList) -} - -main(); - - diff --git a/functions/src/index.ts b/functions/src/index.ts index 118e158..e9a63a8 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -4,18 +4,24 @@ import auth from "./middleware/lingdocs-auth"; import publish from "./publish"; export const publishDictionary = functions.https.onRequest( - auth((req, res: functions.Response) => { + auth(async (req, res: functions.Response) => { if (req.user.level !== "editor") { res.status(403).send({ ok: false, error: "403 forbidden" }); return; } - publish().then(res.send); + try { + res.send(await publish()); + } catch (e) { + const error: string = ("message" in e && typeof e.message === "string") ? e.message : "server error"; + res.status(500).send({ ok: false, error }); + } }) ); -export const willError = functions.https.onRequest((req, res) => { - auth((req, res: functions.Response) => { - throw new Error("this is an error"); +export const testAuth = functions.https.onRequest((req, res) => { + auth(async (req, res) => { + // @ts-ignore + res.send({ ok: true, user: req.user }) }) }) diff --git a/website/src/screens/Account.tsx b/website/src/screens/Account.tsx index 07de905..829b349 100644 --- a/website/src/screens/Account.tsx +++ b/website/src/screens/Account.tsx @@ -30,9 +30,9 @@ const Account = ({ user, loadUser }: { user: AT.LingdocsUser | undefined, loadUs setUpgradeError(""); setWaiting(false); window.addEventListener("message", handleIncomingMessage); - console.log("send test erroring func"); - fetch("https://functions.lingdocs.com/willError", { credentials: "include" }).then((res) => res.text()).then((res) => { - console.log("test error here"); + console.log("send test auth func"); + fetch("https://functions.lingdocs.com/testAuth", { credentials: "include" }).then((res) => res.text()).then((res) => { + console.log("test auth here"); console.log(res); }); return () => {