Regroup database queries of /sendMessage in one place

Potentially reduces the total queries to database and it will help to migrate to Edge runtime
This commit is contained in:
Baptiste Arnaud
2023-07-18 14:31:20 +02:00
parent 1095cf7f09
commit aa4c16dad7
32 changed files with 520 additions and 482 deletions

View File

@@ -7,11 +7,9 @@ import {
import { isNotEmpty, byId } from '@typebot.io/lib'
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
import { ExecuteIntegrationResponse } from '@/features/chat/types'
import { saveErrorLog } from '@/features/logs/saveErrorLog'
import { updateVariables } from '@/features/variables/updateVariables'
import { deepParseVariables } from '@/features/variables/deepParseVariable'
import { matchFilter } from './helpers/matchFilter'
import { saveInfoLog } from '@/features/logs/saveInfoLog'
export const getRow = async (
state: SessionState,
@@ -20,15 +18,13 @@ export const getRow = async (
options,
}: { outgoingEdgeId?: string; options: GoogleSheetsGetOptions }
): Promise<ExecuteIntegrationResponse> => {
const logs: ReplyLog[] = []
const { sheetId, cellsToExtract, referenceCell, filter } = deepParseVariables(
state.typebot.variables
)(options)
if (!sheetId) return { outgoingEdgeId }
let log: ReplyLog | undefined
const variables = state.typebot.variables
const resultId = state.result?.id
const doc = await getAuthenticatedGoogleDoc({
credentialsId: options.credentialsId,
@@ -48,16 +44,12 @@ export const getRow = async (
)
)
if (filteredRows.length === 0) {
log = {
logs.push({
status: 'info',
description: `Couldn't find any rows matching the filter`,
details: JSON.stringify(filter, null, 2),
}
await saveInfoLog({
resultId,
message: log.description,
})
return { outgoingEdgeId, logs: log ? [log] : undefined }
return { outgoingEdgeId, logs }
}
const extractingColumns = cellsToExtract
.map((cell) => cell.column)
@@ -85,24 +77,19 @@ export const getRow = async (
},
[]
)
const newSessionState = await updateVariables(state)(newVariables)
const newSessionState = updateVariables(state)(newVariables)
return {
outgoingEdgeId,
newSessionState,
}
} catch (err) {
log = {
logs.push({
status: 'error',
description: `An error occurred while fetching the spreadsheet data`,
details: err,
}
await saveErrorLog({
resultId,
message: log.description,
details: err,
})
}
return { outgoingEdgeId, logs: log ? [log] : undefined }
return { outgoingEdgeId, logs }
}
const getTotalRows = <T>(

View File

@@ -6,11 +6,9 @@ import {
import { parseCellValues } from './helpers/parseCellValues'
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
import { ExecuteIntegrationResponse } from '@/features/chat/types'
import { saveErrorLog } from '@/features/logs/saveErrorLog'
import { saveSuccessLog } from '@/features/logs/saveSuccessLog'
export const insertRow = async (
{ result, typebot: { variables } }: SessionState,
{ typebot: { variables } }: SessionState,
{
outgoingEdgeId,
options,
@@ -18,7 +16,7 @@ export const insertRow = async (
): Promise<ExecuteIntegrationResponse> => {
if (!options.cellsToInsert || !options.sheetId) return { outgoingEdgeId }
let log: ReplyLog | undefined
const logs: ReplyLog[] = []
const doc = await getAuthenticatedGoogleDoc({
credentialsId: options.credentialsId,
@@ -31,27 +29,17 @@ export const insertRow = async (
await doc.loadInfo()
const sheet = doc.sheetsById[Number(options.sheetId)]
await sheet.addRow(parsedValues)
log = {
logs.push({
status: 'success',
description: `Succesfully inserted row in ${doc.title} > ${sheet.title}`,
}
result &&
(await saveSuccessLog({
resultId: result.id,
message: log?.description,
}))
})
} catch (err) {
log = {
logs.push({
status: 'error',
description: `An error occured while inserting the row`,
details: err,
}
result &&
(await saveErrorLog({
resultId: result.id,
message: log.description,
details: err,
}))
})
}
return { outgoingEdgeId, logs: log ? [log] : undefined }
return { outgoingEdgeId, logs }
}

View File

@@ -7,13 +7,10 @@ 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 { matchFilter } from './helpers/matchFilter'
import { saveInfoLog } from '@/features/logs/saveInfoLog'
export const updateRow = async (
{ result, typebot: { variables } }: SessionState,
{ typebot: { variables } }: SessionState,
{
outgoingEdgeId,
options,
@@ -24,7 +21,7 @@ export const updateRow = async (
if (!options.cellsToUpsert || !sheetId || (!referenceCell && !filter))
return { outgoingEdgeId }
let log: ReplyLog | undefined
const logs: ReplyLog[] = []
const doc = await getAuthenticatedGoogleDoc({
credentialsId: options.credentialsId,
@@ -42,18 +39,12 @@ export const updateRow = async (
: matchFilter(row, filter as NonNullable<typeof filter>)
)
if (filteredRows.length === 0) {
log = {
logs.push({
status: 'info',
description: `Could not find any row that matches the filter`,
details: JSON.stringify(filter, null, 2),
}
result &&
(await saveInfoLog({
resultId: result.id,
message: log.description,
details: log.details,
}))
return { outgoingEdgeId, logs: log ? [log] : undefined }
details: filter,
})
return { outgoingEdgeId, logs }
}
try {
@@ -65,28 +56,17 @@ export const updateRow = async (
await rows[rowIndex].save()
}
log = log = {
logs.push({
status: 'success',
description: `Succesfully updated matching rows`,
}
result &&
(await saveSuccessLog({
resultId: result.id,
message: log.description,
}))
})
} catch (err) {
console.log(err)
log = {
logs.push({
status: 'error',
description: `An error occured while updating the row`,
details: err,
}
result &&
(await saveErrorLog({
resultId: result.id,
message: log.description,
details: err,
}))
})
}
return { outgoingEdgeId, logs: log ? [log] : undefined }
return { outgoingEdgeId, logs }
}