2023-03-15 08:35:16 +01:00
|
|
|
import {
|
|
|
|
SessionState,
|
|
|
|
GoogleSheetsUpdateRowOptions,
|
|
|
|
ReplyLog,
|
|
|
|
} 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'
|
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'
|
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
|
2023-05-04 14:00:50 -04:00
|
|
|
const { sheetId, referenceCell, filter } =
|
|
|
|
deepParseVariables(variables)(options)
|
|
|
|
if (!options.cellsToUpsert || !sheetId || (!referenceCell && !filter))
|
2022-11-29 10:02:40 +01:00
|
|
|
return { outgoingEdgeId }
|
|
|
|
|
2023-07-18 14:31:20 +02:00
|
|
|
const logs: ReplyLog[] = []
|
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.cellsToUpsert)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-05-04 14:00:50 -04:00
|
|
|
for (const filteredRow of filteredRows) {
|
2023-07-15 10:46:36 +02:00
|
|
|
const rowIndex = filteredRow.rowNumber - 2 // -1 for 1-indexing, -1 for header row
|
2023-05-04 14:00:50 -04:00
|
|
|
for (const key in parsedValues) {
|
2023-07-15 10:46:36 +02:00
|
|
|
rows[rowIndex].set(key, parsedValues[key])
|
2023-05-04 14:00:50 -04:00
|
|
|
}
|
|
|
|
await rows[rowIndex].save()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|