From ce77308ee564c76cb15e298c4f58f0c6a211fa71 Mon Sep 17 00:00:00 2001 From: adueck Date: Fri, 18 Nov 2022 13:31:19 +0500 Subject: [PATCH] proper checking for duplicate ts in publish --- functions/src/index.ts | 30 ++++++++++++++++-------------- functions/src/publish.ts | 21 +++++++++++++++------ 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/functions/src/index.ts b/functions/src/index.ts index bf7b0d7..acffc06 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -7,21 +7,23 @@ import publish from "./publish"; export const publishDictionary = functions.runWith({ timeoutSeconds: 60, memory: "2GB" -}).https.onRequest(lingdocsAuth( - async (req, res: functions.Response) => { - if (req.user.level !== "editor") { - res.status(403).send({ ok: false, error: "403 forbidden" }); - return; +}).https.onRequest( + lingdocsAuth( + async (req, res: functions.Response) => { + if (req.user.level !== "editor") { + res.status(403).send({ ok: false, error: "403 forbidden" }); + return; + } + try { + const response = await publish(); + res.send(response); + } catch (e) { + // @ts-ignore + res.status(500).send({ ok: false, error: e.message }); + } } - try { - const response = await publish(); - res.send(response); - } catch (e) { - // @ts-ignore - res.status(500).send({ ok: false, error: e.message }); - } - } -)); + ) +); export const submissions = functions.runWith({ timeoutSeconds: 30, diff --git a/functions/src/publish.ts b/functions/src/publish.ts index b75041c..c81872e 100644 --- a/functions/src/publish.ts +++ b/functions/src/publish.ts @@ -96,6 +96,7 @@ async function getRawEntries(): Promise { const sheet = doc.sheetsByIndex[0]; const rows = await sheet.getRows(); async function deleteRow(i: number) { + console.log("WILL DELETE ROW", rows[i].p, rows[i].ts, rows[i].f); await rows[i].delete(); } return await makeEntries(rows, deleteRow); @@ -104,17 +105,25 @@ async function getRawEntries(): Promise { async function makeEntries(rows: GoogleSpreadsheetRow[], deleteRow: (i: number) => Promise): Promise { const entries: T.DictionaryEntry[] = []; let sheetIndex = 0; + // get the rows in order of ts for easy detection of duplicate entries + rows.sort((a, b) => a.ts > b.ts ? -1 : a.ts < b.ts ? 1 : 0); for (let i = 0; i < rows.length; i++) { + function sameEntry(a: any, b: any): boolean { + return a.p === b.p && a.f === b.f && a.e === b.e; + } sheetIndex++; const row = rows[i]; const nextRow = rows[i+1] || undefined; if (row.ts === nextRow?.ts) { - if (row.p !== nextRow.p) throw new Error(`ts ${row.ts} is a duplicate of a different entry`); - // this looks like a duplicate entry made by the sheets api - // delete it and keep going - await deleteRow(sheetIndex); - sheetIndex--; - continue; + if (sameEntry(row, nextRow)) { + // this looks like a duplicate entry made by the sheets api + // delete it and keep going + await deleteRow(sheetIndex); + sheetIndex--; + continue; + } else { + throw new Error(`ts ${row.ts} is a duplicate ts of a different entry`); + } } const e: T.DictionaryEntry = { i: 1,