2023-03-15 08:35:16 +01:00
|
|
|
import {
|
|
|
|
SessionState,
|
|
|
|
GoogleSheetsUpdateRowOptions,
|
2023-11-13 15:27:36 +01:00
|
|
|
ChatLog,
|
2023-03-15 08:35:16 +01:00
|
|
|
} from '@typebot.io/schemas'
|
2024-06-27 11:25:43 +02:00
|
|
|
import { parseNewCellValuesObject } from './helpers/parseNewCellValuesObject'
|
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-12-13 10:22:02 +01:00
|
|
|
import { deepParseVariables } from '@typebot.io/variables/deepParseVariables'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
|
|
|
export const updateRow = async (
|
2023-08-24 07:48:30 +02:00
|
|
|
state: SessionState,
|
2022-11-29 10:02:40 +01:00
|
|
|
{
|
|
|
|
outgoingEdgeId,
|
|
|
|
options,
|
|
|
|
}: { outgoingEdgeId?: string; options: GoogleSheetsUpdateRowOptions }
|
|
|
|
): Promise<ExecuteIntegrationResponse> => {
|
2023-08-24 07:48:30 +02:00
|
|
|
const { variables } = state.typebotsQueue[0].typebot
|
2024-04-18 10:17:25 +02:00
|
|
|
const { sheetId, filter, ...parsedOptions } = deepParseVariables(variables, {
|
|
|
|
removeEmptyStrings: true,
|
|
|
|
})(options)
|
2023-11-08 15:34:16 +01:00
|
|
|
|
|
|
|
const referenceCell =
|
|
|
|
'referenceCell' in parsedOptions && parsedOptions.referenceCell
|
|
|
|
? parsedOptions.referenceCell
|
|
|
|
: null
|
|
|
|
|
2023-05-04 14:00:50 -04:00
|
|
|
if (!options.cellsToUpsert || !sheetId || (!referenceCell && !filter))
|
2022-11-29 10:02:40 +01:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
2023-05-02 14:14:35 -04:00
|
|
|
await doc.loadInfo()
|
2023-07-15 10:46:36 +02:00
|
|
|
const sheet = doc.sheetsById[Number(sheetId)]
|
2023-05-02 14:14:35 -04:00
|
|
|
const rows = await sheet.getRows()
|
2023-05-04 14:00:50 -04:00
|
|
|
const filteredRows = rows.filter((row) =>
|
|
|
|
referenceCell
|
2023-07-15 10:46:36 +02:00
|
|
|
? row.get(referenceCell.column as string) === referenceCell.value
|
2023-05-04 14:00:50 -04:00
|
|
|
: matchFilter(row, filter as NonNullable<typeof filter>)
|
2023-05-02 14:14:35 -04:00
|
|
|
)
|
2023-05-04 14:00:50 -04: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-05-04 14:00:50 -04:00
|
|
|
description: `Could not find any row that matches the filter`,
|
2023-07-18 14:31:20 +02:00
|
|
|
details: filter,
|
|
|
|
})
|
|
|
|
return { outgoingEdgeId, logs }
|
2023-05-02 14:14:35 -04:00
|
|
|
}
|
|
|
|
|
2024-06-27 11:25:43 +02:00
|
|
|
const parsedValues = parseNewCellValuesObject(variables)(
|
|
|
|
options.cellsToUpsert,
|
|
|
|
sheet.headerValues
|
|
|
|
)
|
|
|
|
|
2023-05-02 14:14:35 -04:00
|
|
|
try {
|
2023-05-04 14:00:50 -04:00
|
|
|
for (const filteredRow of filteredRows) {
|
2024-06-27 11:25:43 +02:00
|
|
|
const cellsRange = filteredRow.a1Range.split('!').pop()
|
|
|
|
await sheet.loadCells(cellsRange)
|
|
|
|
const rowIndex = filteredRow.rowNumber - 1
|
2023-05-04 14:00:50 -04:00
|
|
|
for (const key in parsedValues) {
|
2024-06-27 11:25:43 +02:00
|
|
|
const cellToUpdate = sheet.getCell(
|
|
|
|
rowIndex,
|
|
|
|
parsedValues[key].columnIndex
|
|
|
|
)
|
|
|
|
cellToUpdate.value = parsedValues[key].value
|
2023-05-04 14:00:50 -04:00
|
|
|
}
|
2024-06-27 11:25:43 +02:00
|
|
|
await sheet.saveUpdatedCells()
|
2023-05-04 14:00:50 -04:00
|
|
|
}
|
|
|
|
|
2023-07-18 14:31:20 +02:00
|
|
|
logs.push({
|
2023-01-25 11:27:47 +01:00
|
|
|
status: 'success',
|
2023-05-04 14:00:50 -04:00
|
|
|
description: `Succesfully updated matching rows`,
|
2023-07-18 14:31:20 +02:00
|
|
|
})
|
2022-11-29 10:02:40 +01:00
|
|
|
} catch (err) {
|
2023-05-05 10:31:03 -04:00
|
|
|
console.log(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 updating 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
|
|
|
}
|