From fd0fd51c1dd13764f5cdccfd10ea18edc0d4e1bc Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Tue, 2 May 2023 14:14:35 -0400 Subject: [PATCH] :loud_sound: (sheets) Improve update row error log when not found --- .../integrations/googleSheets/updateRow.ts | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/apps/viewer/src/features/blocks/integrations/googleSheets/updateRow.ts b/apps/viewer/src/features/blocks/integrations/googleSheets/updateRow.ts index 05e429955..5704a7c98 100644 --- a/apps/viewer/src/features/blocks/integrations/googleSheets/updateRow.ts +++ b/apps/viewer/src/features/blocks/integrations/googleSheets/updateRow.ts @@ -3,13 +3,13 @@ import { GoogleSheetsUpdateRowOptions, ReplyLog, } from '@typebot.io/schemas' -import { TRPCError } from '@trpc/server' import { parseCellValues } from './helpers/parseCellValues' import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc' import { deepParseVariables } from '@/features/variables/deepParseVariable' import { ExecuteIntegrationResponse } from '@/features/chat/types' import { saveErrorLog } from '@/features/logs/saveErrorLog' import { saveSuccessLog } from '@/features/logs/saveSuccessLog' +import { TRPCError } from '@trpc/server' export const updateRow = async ( { result, typebot: { variables } }: SessionState, @@ -31,22 +31,35 @@ export const updateRow = async ( const parsedValues = parseCellValues(variables)(options.cellsToUpsert) + await doc.loadInfo() + const sheet = doc.sheetsById[sheetId] + const rows = await sheet.getRows() + const updatingRowIndex = rows.findIndex( + (row) => row[referenceCell.column as string] === referenceCell.value + ) + if (updatingRowIndex === -1) { + log = { + status: 'error', + description: `Could not find row to update`, + details: `Looked for row with ${referenceCell.column} equals to "${referenceCell.value}"`, + } + result && + (await saveErrorLog({ + resultId: result.id, + message: log.description, + details: log.details, + })) + throw new TRPCError({ + code: 'NOT_FOUND', + message: `Couldn't find row with ${referenceCell.column} that equals to "${referenceCell.value}"`, + }) + } + + for (const key in parsedValues) { + rows[updatingRowIndex][key] = parsedValues[key] + } + try { - await doc.loadInfo() - const sheet = doc.sheetsById[sheetId] - const rows = await sheet.getRows() - const updatingRowIndex = rows.findIndex( - (row) => row[referenceCell.column as string] === referenceCell.value - ) - if (updatingRowIndex === -1) { - new TRPCError({ - code: 'NOT_FOUND', - message: "Couldn't find row to update", - }) - } - for (const key in parsedValues) { - rows[updatingRowIndex][key] = parsedValues[key] - } await rows[updatingRowIndex].save() log = log = { status: 'success',