2
0

🔊 (sheets) Improve update row error log when not found

This commit is contained in:
Baptiste Arnaud
2023-05-02 14:14:35 -04:00
parent 2b0e2b09f5
commit fd0fd51c1d

View File

@@ -3,13 +3,13 @@ import {
GoogleSheetsUpdateRowOptions, GoogleSheetsUpdateRowOptions,
ReplyLog, ReplyLog,
} from '@typebot.io/schemas' } from '@typebot.io/schemas'
import { TRPCError } from '@trpc/server'
import { parseCellValues } from './helpers/parseCellValues' import { parseCellValues } from './helpers/parseCellValues'
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc' import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
import { deepParseVariables } from '@/features/variables/deepParseVariable' import { deepParseVariables } from '@/features/variables/deepParseVariable'
import { ExecuteIntegrationResponse } from '@/features/chat/types' import { ExecuteIntegrationResponse } from '@/features/chat/types'
import { saveErrorLog } from '@/features/logs/saveErrorLog' import { saveErrorLog } from '@/features/logs/saveErrorLog'
import { saveSuccessLog } from '@/features/logs/saveSuccessLog' import { saveSuccessLog } from '@/features/logs/saveSuccessLog'
import { TRPCError } from '@trpc/server'
export const updateRow = async ( export const updateRow = async (
{ result, typebot: { variables } }: SessionState, { result, typebot: { variables } }: SessionState,
@@ -31,7 +31,6 @@ export const updateRow = async (
const parsedValues = parseCellValues(variables)(options.cellsToUpsert) const parsedValues = parseCellValues(variables)(options.cellsToUpsert)
try {
await doc.loadInfo() await doc.loadInfo()
const sheet = doc.sheetsById[sheetId] const sheet = doc.sheetsById[sheetId]
const rows = await sheet.getRows() const rows = await sheet.getRows()
@@ -39,14 +38,28 @@ export const updateRow = async (
(row) => row[referenceCell.column as string] === referenceCell.value (row) => row[referenceCell.column as string] === referenceCell.value
) )
if (updatingRowIndex === -1) { if (updatingRowIndex === -1) {
new TRPCError({ 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', code: 'NOT_FOUND',
message: "Couldn't find row to update", message: `Couldn't find row with ${referenceCell.column} that equals to "${referenceCell.value}"`,
}) })
} }
for (const key in parsedValues) { for (const key in parsedValues) {
rows[updatingRowIndex][key] = parsedValues[key] rows[updatingRowIndex][key] = parsedValues[key]
} }
try {
await rows[updatingRowIndex].save() await rows[updatingRowIndex].save()
log = log = { log = log = {
status: 'success', status: 'success',