2023-03-15 08:35:16 +01:00
|
|
|
import {
|
|
|
|
|
SessionState,
|
|
|
|
|
GoogleSheetsInsertRowOptions,
|
2023-11-13 15:27:36 +01:00
|
|
|
ChatLog,
|
2023-03-15 08:35:16 +01:00
|
|
|
} from '@typebot.io/schemas'
|
2023-03-15 12:21:52 +01:00
|
|
|
import { parseCellValues } from './helpers/parseCellValues'
|
|
|
|
|
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
|
2023-09-20 15:26:52 +02:00
|
|
|
import { ExecuteIntegrationResponse } from '../../../types'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
|
|
|
|
export const insertRow = async (
|
2023-08-24 07:48:30 +02:00
|
|
|
state: SessionState,
|
2022-11-29 10:02:40 +01:00
|
|
|
{
|
|
|
|
|
outgoingEdgeId,
|
|
|
|
|
options,
|
|
|
|
|
}: { outgoingEdgeId?: string; options: GoogleSheetsInsertRowOptions }
|
|
|
|
|
): Promise<ExecuteIntegrationResponse> => {
|
2023-08-24 07:48:30 +02:00
|
|
|
const { variables } = state.typebotsQueue[0].typebot
|
2022-11-29 10:02:40 +01:00
|
|
|
if (!options.cellsToInsert || !options.sheetId) return { outgoingEdgeId }
|
|
|
|
|
|
2023-11-13 15:27:36 +01:00
|
|
|
const logs: ChatLog[] = []
|
2023-01-25 11:27:47 +01:00
|
|
|
|
2022-11-29 10:02:40 +01:00
|
|
|
const doc = await getAuthenticatedGoogleDoc({
|
|
|
|
|
credentialsId: options.credentialsId,
|
|
|
|
|
spreadsheetId: options.spreadsheetId,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const parsedValues = parseCellValues(variables)(options.cellsToInsert)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await doc.loadInfo()
|
2023-07-15 10:46:36 +02:00
|
|
|
const sheet = doc.sheetsById[Number(options.sheetId)]
|
2022-11-29 10:02:40 +01:00
|
|
|
await sheet.addRow(parsedValues)
|
2023-07-18 14:31:20 +02:00
|
|
|
logs.push({
|
2023-01-25 11:27:47 +01:00
|
|
|
status: 'success',
|
|
|
|
|
description: `Succesfully inserted row in ${doc.title} > ${sheet.title}`,
|
2023-07-18 14:31:20 +02:00
|
|
|
})
|
2022-11-29 10:02:40 +01:00
|
|
|
} 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 occured while inserting the row`,
|
|
|
|
|
details: err,
|
2023-07-18 14:31:20 +02:00
|
|
|
})
|
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
|
|
|
}
|