2
0
Files
bot/packages/bot-engine/blocks/integrations/googleSheets/updateRow.ts

90 lines
2.6 KiB
TypeScript
Raw Normal View History

import {
SessionState,
GoogleSheetsUpdateRowOptions,
ChatLog,
} from '@typebot.io/schemas'
import { parseNewCellValuesObject } from './helpers/parseNewCellValuesObject'
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
import { ExecuteIntegrationResponse } from '../../../types'
import { matchFilter } from './helpers/matchFilter'
import { deepParseVariables } from '@typebot.io/variables/deepParseVariables'
2022-11-29 10:02:40 +01:00
export const updateRow = async (
state: SessionState,
2022-11-29 10:02:40 +01:00
{
outgoingEdgeId,
options,
}: { outgoingEdgeId?: string; options: GoogleSheetsUpdateRowOptions }
): Promise<ExecuteIntegrationResponse> => {
const { variables } = state.typebotsQueue[0].typebot
const { sheetId, filter, ...parsedOptions } = deepParseVariables(variables, {
removeEmptyStrings: true,
})(options)
const referenceCell =
'referenceCell' in parsedOptions && parsedOptions.referenceCell
? parsedOptions.referenceCell
: null
if (!options.cellsToUpsert || !sheetId || (!referenceCell && !filter))
2022-11-29 10:02:40 +01:00
return { outgoingEdgeId }
const logs: ChatLog[] = []
2022-11-29 10:02:40 +01:00
const doc = await getAuthenticatedGoogleDoc({
credentialsId: options.credentialsId,
spreadsheetId: options.spreadsheetId,
})
await doc.loadInfo()
2023-07-15 10:46:36 +02:00
const sheet = doc.sheetsById[Number(sheetId)]
const rows = await sheet.getRows()
const filteredRows = rows.filter((row) =>
referenceCell
2023-07-15 10:46:36 +02:00
? row.get(referenceCell.column as string) === referenceCell.value
: matchFilter(row, filter as NonNullable<typeof filter>)
)
if (filteredRows.length === 0) {
logs.push({
status: 'info',
description: `Could not find any row that matches the filter`,
details: filter,
})
return { outgoingEdgeId, logs }
}
const parsedValues = parseNewCellValuesObject(variables)(
options.cellsToUpsert,
sheet.headerValues
)
try {
for (const filteredRow of filteredRows) {
const cellsRange = filteredRow.a1Range.split('!').pop()
await sheet.loadCells(cellsRange)
const rowIndex = filteredRow.rowNumber - 1
for (const key in parsedValues) {
const cellToUpdate = sheet.getCell(
rowIndex,
parsedValues[key].columnIndex
)
cellToUpdate.value = parsedValues[key].value
}
await sheet.saveUpdatedCells()
}
logs.push({
status: 'success',
description: `Succesfully updated matching rows`,
})
2022-11-29 10:02:40 +01:00
} catch (err) {
console.log(err)
logs.push({
status: 'error',
description: `An error occured while updating the row`,
details: err,
})
2022-11-29 10:02:40 +01:00
}
return { outgoingEdgeId, logs }
2022-11-29 10:02:40 +01:00
}