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,
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',