2
0
Files
bot/apps/viewer/src/features/blocks/integrations/googleSheets/getRow.ts

124 lines
3.6 KiB
TypeScript
Raw Normal View History

import {
SessionState,
GoogleSheetsGetOptions,
VariableWithValue,
ReplyLog,
} from '@typebot.io/schemas'
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'
2022-11-29 10:02:40 +01:00
export const getRow = async (
state: SessionState,
{
outgoingEdgeId,
options,
}: { outgoingEdgeId?: string; options: GoogleSheetsGetOptions }
): Promise<ExecuteIntegrationResponse> => {
const { sheetId, cellsToExtract, referenceCell, filter } = deepParseVariables(
state.typebot.variables
)(options)
if (!sheetId) return { outgoingEdgeId }
let log: ReplyLog | undefined
2022-11-29 10:02:40 +01:00
const variables = state.typebot.variables
const resultId = state.result?.id
2022-11-29 10:02:40 +01:00
const doc = await getAuthenticatedGoogleDoc({
credentialsId: options.credentialsId,
spreadsheetId: options.spreadsheetId,
})
try {
await doc.loadInfo()
const sheet = doc.sheetsById[sheetId]
const rows = await sheet.getRows()
const filteredRows = getTotalRows(
options.totalRowsToExtract,
rows.filter((row) =>
referenceCell
? row[referenceCell.column as string] === referenceCell.value
: matchFilter(row, filter as NonNullable<typeof filter>)
)
2022-11-29 10:02:40 +01:00
)
if (filteredRows.length === 0) {
log = {
status: 'info',
description: `Couldn't find any rows matching the filter`,
details: JSON.stringify(filter, null, 2),
}
await saveInfoLog({
2022-11-29 10:02:40 +01:00
resultId,
message: log.description,
2022-11-29 10:02:40 +01:00
})
return { outgoingEdgeId, logs: log ? [log] : undefined }
2022-11-29 10:02:40 +01:00
}
const extractingColumns = cellsToExtract
.map((cell) => cell.column)
.filter(isNotEmpty)
const selectedRows = filteredRows.map((row) =>
extractingColumns.reduce<{ [key: string]: string }>(
(obj, column) => ({ ...obj, [column]: row[column] }),
{}
)
)
if (!selectedRows) return { outgoingEdgeId }
2022-11-29 10:02:40 +01:00
const newVariables = options.cellsToExtract.reduce<VariableWithValue[]>(
(newVariables, cell) => {
const existingVariable = variables.find(byId(cell.variableId))
const value = selectedRows.map((row) => row[cell.column ?? ''])
2022-11-29 10:02:40 +01:00
if (!existingVariable) return newVariables
return [
...newVariables,
{
...existingVariable,
value: value.length === 1 ? value[0] : value,
2022-11-29 10:02:40 +01:00
},
]
},
[]
)
const newSessionState = await updateVariables(state)(newVariables)
return {
outgoingEdgeId,
newSessionState,
}
} catch (err) {
log = {
status: 'error',
description: `An error occurred while fetching the spreadsheet data`,
details: err,
}
2022-11-29 10:02:40 +01:00
await saveErrorLog({
resultId,
message: log.description,
2022-11-29 10:02:40 +01:00
details: err,
})
}
return { outgoingEdgeId, logs: log ? [log] : undefined }
2022-11-29 10:02:40 +01:00
}
const getTotalRows = <T>(
totalRowsToExtract: GoogleSheetsGetOptions['totalRowsToExtract'],
rows: T[]
): T[] => {
switch (totalRowsToExtract) {
case 'All':
case undefined:
return rows
case 'First':
return rows.slice(0, 1)
case 'Last':
return rows.slice(-1)
case 'Random':
return [rows[Math.floor(Math.random() * rows.length)]]
}
}