(engine) Improve engine overall robustness

This commit is contained in:
Baptiste Arnaud
2023-01-25 11:27:47 +01:00
parent ff62b922a0
commit 30baa611e5
210 changed files with 1820 additions and 1919 deletions

View File

@@ -4,6 +4,7 @@ import {
VariableWithValue,
ComparisonOperators,
LogicalOperator,
ReplyLog,
} from 'models'
import { saveErrorLog } from '@/features/logs/api'
import { getAuthenticatedGoogleDoc } from './helpers'
@@ -20,7 +21,9 @@ export const getRow = async (
}: { outgoingEdgeId?: string; options: GoogleSheetsGetOptions }
): Promise<ExecuteIntegrationResponse> => {
const { sheetId, cellsToExtract, referenceCell, filter } = options
if (!cellsToExtract || !sheetId || !referenceCell) return { outgoingEdgeId }
if (!sheetId) return { outgoingEdgeId }
let log: ReplyLog | undefined
const variables = state.typebot.variables
const resultId = state.result?.id
@@ -40,11 +43,15 @@ export const getRow = async (
: matchFilter(row, filter)
)
if (filteredRows.length === 0) {
log = {
status: 'error',
description: `Couldn't find any rows matching the filter`,
}
await saveErrorLog({
resultId,
message: "Couldn't find reference cell",
message: log.description,
})
return { outgoingEdgeId }
return { outgoingEdgeId, logs: log ? [log] : undefined }
}
const randomIndex = Math.floor(Math.random() * filteredRows.length)
const extractingColumns = cellsToExtract
@@ -81,13 +88,18 @@ export const getRow = async (
newSessionState,
}
} catch (err) {
log = {
status: 'error',
description: `An error occurred while fetching the spreadsheet data`,
details: err,
}
await saveErrorLog({
resultId,
message: "Couldn't fetch spreadsheet data",
message: log.description,
details: err,
})
}
return { outgoingEdgeId }
return { outgoingEdgeId, logs: log ? [log] : undefined }
}
const matchFilter = (

View File

@@ -1,4 +1,4 @@
import { SessionState, GoogleSheetsInsertRowOptions } from 'models'
import { SessionState, GoogleSheetsInsertRowOptions, ReplyLog } from 'models'
import { saveErrorLog, saveSuccessLog } from '@/features/logs/api'
import { getAuthenticatedGoogleDoc, parseCellValues } from './helpers'
import { ExecuteIntegrationResponse } from '@/features/chat'
@@ -10,8 +10,11 @@ export const insertRow = async (
options,
}: { outgoingEdgeId?: string; options: GoogleSheetsInsertRowOptions }
): Promise<ExecuteIntegrationResponse> => {
console.log('insertRow', options)
if (!options.cellsToInsert || !options.sheetId) return { outgoingEdgeId }
let log: ReplyLog | undefined
const doc = await getAuthenticatedGoogleDoc({
credentialsId: options.credentialsId,
spreadsheetId: options.spreadsheetId,
@@ -23,18 +26,27 @@ export const insertRow = async (
await doc.loadInfo()
const sheet = doc.sheetsById[options.sheetId]
await sheet.addRow(parsedValues)
log = {
status: 'success',
description: `Succesfully inserted row in ${doc.title} > ${sheet.title}`,
}
result &&
(await saveSuccessLog({
resultId: result.id,
message: 'Succesfully inserted row',
message: log?.description,
}))
} catch (err) {
log = {
status: 'error',
description: `An error occured while inserting the row`,
details: err,
}
result &&
(await saveErrorLog({
resultId: result.id,
message: "Couldn't fetch spreadsheet data",
message: log.description,
details: err,
}))
}
return { outgoingEdgeId }
return { outgoingEdgeId, logs: log ? [log] : undefined }
}

View File

@@ -1,4 +1,4 @@
import { SessionState, GoogleSheetsUpdateRowOptions } from 'models'
import { SessionState, GoogleSheetsUpdateRowOptions, ReplyLog } from 'models'
import { saveErrorLog, saveSuccessLog } from '@/features/logs/api'
import { getAuthenticatedGoogleDoc, parseCellValues } from './helpers'
import { TRPCError } from '@trpc/server'
@@ -16,6 +16,8 @@ export const updateRow = async (
if (!options.cellsToUpsert || !sheetId || !referenceCell)
return { outgoingEdgeId }
let log: ReplyLog | undefined
const doc = await getAuthenticatedGoogleDoc({
credentialsId: options.credentialsId,
spreadsheetId: options.spreadsheetId,
@@ -45,18 +47,27 @@ export const updateRow = async (
rows[updatingRowIndex][key] = parsedValues[key]
}
await rows[updatingRowIndex].save()
log = log = {
status: 'success',
description: `Succesfully updated row in ${doc.title} > ${sheet.title}`,
}
result &&
(await saveSuccessLog({
resultId: result.id,
message: 'Succesfully updated row',
message: log.description,
}))
} catch (err) {
log = {
status: 'error',
description: `An error occured while updating the row`,
details: err,
}
result &&
(await saveErrorLog({
resultId: result.id,
message: "Couldn't fetch spreadsheet data",
message: log.description,
details: err,
}))
}
return { outgoingEdgeId }
return { outgoingEdgeId, logs: log ? [log] : undefined }
}