2023-03-15 08:35:16 +01:00
|
|
|
import {
|
|
|
|
|
SessionState,
|
|
|
|
|
GoogleSheetsInsertRowOptions,
|
|
|
|
|
ReplyLog,
|
|
|
|
|
} from '@typebot.io/schemas'
|
2023-03-15 12:21:52 +01:00
|
|
|
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'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
|
|
|
|
export const insertRow = async (
|
|
|
|
|
{ result, typebot: { variables } }: SessionState,
|
|
|
|
|
{
|
|
|
|
|
outgoingEdgeId,
|
|
|
|
|
options,
|
|
|
|
|
}: { outgoingEdgeId?: string; options: GoogleSheetsInsertRowOptions }
|
|
|
|
|
): Promise<ExecuteIntegrationResponse> => {
|
|
|
|
|
if (!options.cellsToInsert || !options.sheetId) return { outgoingEdgeId }
|
|
|
|
|
|
2023-01-25 11:27:47 +01:00
|
|
|
let log: ReplyLog | undefined
|
|
|
|
|
|
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()
|
|
|
|
|
const sheet = doc.sheetsById[options.sheetId]
|
|
|
|
|
await sheet.addRow(parsedValues)
|
2023-01-25 11:27:47 +01:00
|
|
|
log = {
|
|
|
|
|
status: 'success',
|
|
|
|
|
description: `Succesfully inserted row in ${doc.title} > ${sheet.title}`,
|
|
|
|
|
}
|
2022-12-22 17:02:34 +01:00
|
|
|
result &&
|
|
|
|
|
(await saveSuccessLog({
|
|
|
|
|
resultId: result.id,
|
2023-01-25 11:27:47 +01:00
|
|
|
message: log?.description,
|
2022-12-22 17:02:34 +01:00
|
|
|
}))
|
2022-11-29 10:02:40 +01:00
|
|
|
} catch (err) {
|
2023-01-25 11:27:47 +01:00
|
|
|
log = {
|
|
|
|
|
status: 'error',
|
|
|
|
|
description: `An error occured while inserting the row`,
|
|
|
|
|
details: err,
|
|
|
|
|
}
|
2022-12-22 17:02:34 +01:00
|
|
|
result &&
|
|
|
|
|
(await saveErrorLog({
|
|
|
|
|
resultId: result.id,
|
2023-01-25 11:27:47 +01:00
|
|
|
message: log.description,
|
2022-12-22 17:02:34 +01:00
|
|
|
details: err,
|
|
|
|
|
}))
|
2022-11-29 10:02:40 +01:00
|
|
|
}
|
2023-01-25 11:27:47 +01:00
|
|
|
return { outgoingEdgeId, logs: log ? [log] : undefined }
|
2022-11-29 10:02:40 +01:00
|
|
|
}
|