Skip to content

Commit

Permalink
🔊 (sheets) Improve update row error log when not found
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed May 2, 2023
1 parent 2b0e2b0 commit fd0fd51
Showing 1 changed file with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,22 +31,35 @@ export const updateRow = async (

const parsedValues = parseCellValues(variables)(options.cellsToUpsert)

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 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 rows[updatingRowIndex].save()
log = log = {
status: 'success',
Expand Down

0 comments on commit fd0fd51

Please sign in to comment.