2022-12-22 17:02:34 +01:00
|
|
|
import {
|
|
|
|
SessionState,
|
|
|
|
GoogleSheetsGetOptions,
|
|
|
|
VariableWithValue,
|
2023-11-13 15:27:36 +01:00
|
|
|
ChatLog,
|
2023-03-15 08:35:16 +01:00
|
|
|
} from '@typebot.io/schemas'
|
2023-11-08 15:34:16 +01:00
|
|
|
import { isNotEmpty, byId, isDefined } from '@typebot.io/lib'
|
2023-03-15 12:21:52 +01:00
|
|
|
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
|
2023-09-20 15:26:52 +02:00
|
|
|
import { ExecuteIntegrationResponse } from '../../../types'
|
2023-05-04 14:00:50 -04:00
|
|
|
import { matchFilter } from './helpers/matchFilter'
|
2023-09-20 15:26:52 +02:00
|
|
|
import { deepParseVariables } from '../../../variables/deepParseVariables'
|
|
|
|
import { updateVariablesInSession } from '../../../variables/updateVariablesInSession'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
|
|
|
export const getRow = async (
|
|
|
|
state: SessionState,
|
|
|
|
{
|
|
|
|
outgoingEdgeId,
|
|
|
|
options,
|
|
|
|
}: { outgoingEdgeId?: string; options: GoogleSheetsGetOptions }
|
|
|
|
): Promise<ExecuteIntegrationResponse> => {
|
2023-11-13 15:27:36 +01:00
|
|
|
const logs: ChatLog[] = []
|
2023-08-24 07:48:30 +02:00
|
|
|
const { variables } = state.typebotsQueue[0].typebot
|
2023-11-08 15:34:16 +01:00
|
|
|
const { sheetId, cellsToExtract, filter, ...parsedOptions } =
|
2023-08-24 07:48:30 +02:00
|
|
|
deepParseVariables(variables)(options)
|
2023-01-25 11:27:47 +01:00
|
|
|
if (!sheetId) return { outgoingEdgeId }
|
|
|
|
|
2022-11-29 10:02:40 +01:00
|
|
|
const doc = await getAuthenticatedGoogleDoc({
|
|
|
|
credentialsId: options.credentialsId,
|
|
|
|
spreadsheetId: options.spreadsheetId,
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
await doc.loadInfo()
|
2023-07-15 10:46:36 +02:00
|
|
|
const sheet = doc.sheetsById[Number(sheetId)]
|
2022-11-29 10:02:40 +01:00
|
|
|
const rows = await sheet.getRows()
|
2023-05-12 11:21:24 -04:00
|
|
|
const filteredRows = getTotalRows(
|
|
|
|
options.totalRowsToExtract,
|
|
|
|
rows.filter((row) =>
|
2023-11-08 15:34:16 +01:00
|
|
|
'referenceCell' in parsedOptions && parsedOptions.referenceCell
|
|
|
|
? row.get(parsedOptions.referenceCell?.column as string) ===
|
|
|
|
parsedOptions.referenceCell?.value
|
2023-06-29 15:17:51 +02:00
|
|
|
: matchFilter(row, filter)
|
2023-05-12 11:21:24 -04:00
|
|
|
)
|
2022-11-29 10:02:40 +01:00
|
|
|
)
|
2022-12-22 17:02:34 +01:00
|
|
|
if (filteredRows.length === 0) {
|
2023-07-18 14:31:20 +02:00
|
|
|
logs.push({
|
2023-06-15 14:42:20 +02:00
|
|
|
status: 'info',
|
2023-01-25 11:27:47 +01:00
|
|
|
description: `Couldn't find any rows matching the filter`,
|
2023-06-15 14:42:20 +02:00
|
|
|
details: JSON.stringify(filter, null, 2),
|
2022-11-29 10:02:40 +01:00
|
|
|
})
|
2023-07-18 14:31:20 +02:00
|
|
|
return { outgoingEdgeId, logs }
|
2022-11-29 10:02:40 +01:00
|
|
|
}
|
2022-12-22 17:02:34 +01:00
|
|
|
const extractingColumns = cellsToExtract
|
2023-11-08 15:34:16 +01:00
|
|
|
?.map((cell) => cell.column)
|
2022-12-22 17:02:34 +01:00
|
|
|
.filter(isNotEmpty)
|
2023-11-08 15:34:16 +01:00
|
|
|
const selectedRows = filteredRows
|
|
|
|
.map((row) =>
|
|
|
|
extractingColumns?.reduce<{ [key: string]: string }>(
|
|
|
|
(obj, column) => ({ ...obj, [column]: row.get(column) }),
|
|
|
|
{}
|
|
|
|
)
|
2022-12-22 17:02:34 +01:00
|
|
|
)
|
2023-11-08 15:34:16 +01:00
|
|
|
.filter(isDefined)
|
2023-02-23 14:44:37 +01:00
|
|
|
if (!selectedRows) return { outgoingEdgeId }
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
const newVariables = options.cellsToExtract?.reduce<VariableWithValue[]>(
|
2022-11-29 10:02:40 +01:00
|
|
|
(newVariables, cell) => {
|
|
|
|
const existingVariable = variables.find(byId(cell.variableId))
|
2023-02-23 14:44:37 +01:00
|
|
|
const value = selectedRows.map((row) => row[cell.column ?? ''])
|
2022-11-29 10:02:40 +01:00
|
|
|
if (!existingVariable) return newVariables
|
|
|
|
return [
|
|
|
|
...newVariables,
|
|
|
|
{
|
|
|
|
...existingVariable,
|
2023-03-03 09:39:42 +01:00
|
|
|
value: value.length === 1 ? value[0] : value,
|
2022-11-29 10:02:40 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
2023-11-08 15:34:16 +01:00
|
|
|
if (!newVariables) return { outgoingEdgeId }
|
2023-09-20 15:26:52 +02:00
|
|
|
const newSessionState = updateVariablesInSession(state)(newVariables)
|
2022-11-29 10:02:40 +01:00
|
|
|
return {
|
|
|
|
outgoingEdgeId,
|
|
|
|
newSessionState,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2023-07-18 14:31:20 +02:00
|
|
|
logs.push({
|
2023-01-25 11:27:47 +01:00
|
|
|
status: 'error',
|
|
|
|
description: `An error occurred while fetching the spreadsheet data`,
|
|
|
|
details: err,
|
2022-11-29 10:02:40 +01:00
|
|
|
})
|
|
|
|
}
|
2023-07-18 14:31:20 +02:00
|
|
|
return { outgoingEdgeId, logs }
|
2022-11-29 10:02:40 +01:00
|
|
|
}
|
2023-05-12 11:21:24 -04: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)]]
|
|
|
|
}
|
|
|
|
}
|