🔊 (sheets) Improve update row error log when not found
This commit is contained in:
@@ -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,22 +31,35 @@ export const updateRow = async (
|
|||||||
|
|
||||||
const parsedValues = parseCellValues(variables)(options.cellsToUpsert)
|
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 {
|
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()
|
await rows[updatingRowIndex].save()
|
||||||
log = log = {
|
log = log = {
|
||||||
status: 'success',
|
status: 'success',
|
||||||
|
|||||||
Reference in New Issue
Block a user