⚡ (sheets) Add rows filtering to update multiple rows at the same time
This commit is contained in:
@ -214,21 +214,61 @@ const ActionOptions = ({
|
|||||||
)
|
)
|
||||||
case GoogleSheetsAction.UPDATE_ROW:
|
case GoogleSheetsAction.UPDATE_ROW:
|
||||||
return (
|
return (
|
||||||
<Stack>
|
<Accordion allowMultiple>
|
||||||
<Text>Row to select</Text>
|
{options.referenceCell && (
|
||||||
<CellWithValueStack
|
<AccordionItem>
|
||||||
columns={sheet?.columns ?? []}
|
<AccordionButton>
|
||||||
item={options.referenceCell ?? { id: 'reference' }}
|
<Text w="full" textAlign="left">
|
||||||
onItemChange={handleReferenceCellChange}
|
Row to update
|
||||||
/>
|
</Text>
|
||||||
<Text>Cells to update</Text>
|
<AccordionIcon />
|
||||||
<TableList<Cell>
|
</AccordionButton>
|
||||||
initialItems={options.cellsToUpsert}
|
|
||||||
onItemsChange={handleUpsertColumnsChange}
|
<AccordionPanel pt="4">
|
||||||
Item={UpdatingCellItem}
|
<CellWithValueStack
|
||||||
addLabel="Add a value"
|
columns={sheet?.columns ?? []}
|
||||||
/>
|
item={options.referenceCell ?? { id: 'reference' }}
|
||||||
</Stack>
|
onItemChange={handleReferenceCellChange}
|
||||||
|
/>
|
||||||
|
</AccordionPanel>
|
||||||
|
</AccordionItem>
|
||||||
|
)}
|
||||||
|
{!options.referenceCell && (
|
||||||
|
<AccordionItem>
|
||||||
|
<AccordionButton>
|
||||||
|
<Text w="full" textAlign="left">
|
||||||
|
Row(s) to update
|
||||||
|
</Text>
|
||||||
|
<AccordionIcon />
|
||||||
|
</AccordionButton>
|
||||||
|
|
||||||
|
<AccordionPanel pt="4">
|
||||||
|
<RowsFilterTableList
|
||||||
|
columns={sheet?.columns ?? []}
|
||||||
|
filter={options.filter}
|
||||||
|
onFilterChange={handleFilterChange}
|
||||||
|
/>
|
||||||
|
</AccordionPanel>
|
||||||
|
</AccordionItem>
|
||||||
|
)}
|
||||||
|
<AccordionItem>
|
||||||
|
<AccordionButton>
|
||||||
|
<Text w="full" textAlign="left">
|
||||||
|
Cells to update
|
||||||
|
</Text>
|
||||||
|
<AccordionIcon />
|
||||||
|
</AccordionButton>
|
||||||
|
|
||||||
|
<AccordionPanel pt="4">
|
||||||
|
<TableList<Cell>
|
||||||
|
initialItems={options.cellsToUpsert}
|
||||||
|
onItemsChange={handleUpsertColumnsChange}
|
||||||
|
Item={UpdatingCellItem}
|
||||||
|
addLabel="Add a value"
|
||||||
|
/>
|
||||||
|
</AccordionPanel>
|
||||||
|
</AccordionItem>
|
||||||
|
</Accordion>
|
||||||
)
|
)
|
||||||
case GoogleSheetsAction.GET:
|
case GoogleSheetsAction.GET:
|
||||||
return (
|
return (
|
||||||
|
@ -58,12 +58,16 @@ test.describe.parallel('Google sheets integration', () => {
|
|||||||
await page.click('text=Select an operation')
|
await page.click('text=Select an operation')
|
||||||
await page.click('text=Update a row')
|
await page.click('text=Update a row')
|
||||||
|
|
||||||
await page.click('text=Add a value')
|
await page.getByRole('button', { name: 'Row(s) to update' }).click()
|
||||||
|
await page.getByRole('button', { name: 'Add filter rule' }).click()
|
||||||
await page.click('text=Select a column')
|
await page.click('text=Select a column')
|
||||||
await page.click('button >> text="Email"')
|
await page.click('button >> text="Email"')
|
||||||
|
await page.getByRole('button', { name: 'Select an operator' }).click()
|
||||||
|
await page.getByRole('menuitem', { name: 'Equal to' }).click()
|
||||||
await page.click('[aria-label="Insert a variable"]')
|
await page.click('[aria-label="Insert a variable"]')
|
||||||
await page.click('button >> text="Email" >> nth=1')
|
await page.click('button >> text="Email" >> nth=1')
|
||||||
|
|
||||||
|
await page.getByRole('button', { name: 'Cells to update' }).click()
|
||||||
await page.click('text=Add a value')
|
await page.click('text=Add a value')
|
||||||
await page.click('text=Select a column')
|
await page.click('text=Select a column')
|
||||||
await page.click('text=Last name')
|
await page.click('text=Last name')
|
||||||
@ -82,7 +86,7 @@ test.describe.parallel('Google sheets integration', () => {
|
|||||||
.locator('input[placeholder="Type your email..."]')
|
.locator('input[placeholder="Type your email..."]')
|
||||||
.press('Enter')
|
.press('Enter')
|
||||||
await expect(
|
await expect(
|
||||||
page.getByText('Succesfully updated row in CRM > Sheet1').nth(0)
|
page.getByText('Succesfully updated matching rows').nth(0)
|
||||||
).toBeVisible()
|
).toBeVisible()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -2,17 +2,15 @@ import {
|
|||||||
SessionState,
|
SessionState,
|
||||||
GoogleSheetsGetOptions,
|
GoogleSheetsGetOptions,
|
||||||
VariableWithValue,
|
VariableWithValue,
|
||||||
ComparisonOperators,
|
|
||||||
LogicalOperator,
|
|
||||||
ReplyLog,
|
ReplyLog,
|
||||||
} from '@typebot.io/schemas'
|
} from '@typebot.io/schemas'
|
||||||
import { isNotEmpty, byId, isDefined } from '@typebot.io/lib'
|
import { isNotEmpty, byId } from '@typebot.io/lib'
|
||||||
import type { GoogleSpreadsheetRow } from 'google-spreadsheet'
|
|
||||||
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
|
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
|
||||||
import { ExecuteIntegrationResponse } from '@/features/chat/types'
|
import { ExecuteIntegrationResponse } from '@/features/chat/types'
|
||||||
import { saveErrorLog } from '@/features/logs/saveErrorLog'
|
import { saveErrorLog } from '@/features/logs/saveErrorLog'
|
||||||
import { updateVariables } from '@/features/variables/updateVariables'
|
import { updateVariables } from '@/features/variables/updateVariables'
|
||||||
import { deepParseVariables } from '@/features/variables/deepParseVariable'
|
import { deepParseVariables } from '@/features/variables/deepParseVariable'
|
||||||
|
import { matchFilter } from './helpers/matchFilter'
|
||||||
|
|
||||||
export const getRow = async (
|
export const getRow = async (
|
||||||
state: SessionState,
|
state: SessionState,
|
||||||
@ -101,56 +99,3 @@ export const getRow = async (
|
|||||||
}
|
}
|
||||||
return { outgoingEdgeId, logs: log ? [log] : undefined }
|
return { outgoingEdgeId, logs: log ? [log] : undefined }
|
||||||
}
|
}
|
||||||
|
|
||||||
const matchFilter = (
|
|
||||||
row: GoogleSpreadsheetRow,
|
|
||||||
filter: NonNullable<GoogleSheetsGetOptions['filter']>
|
|
||||||
) => {
|
|
||||||
return filter.logicalOperator === LogicalOperator.AND
|
|
||||||
? filter.comparisons.every(
|
|
||||||
(comparison) =>
|
|
||||||
comparison.column &&
|
|
||||||
matchComparison(
|
|
||||||
row[comparison.column],
|
|
||||||
comparison.comparisonOperator,
|
|
||||||
comparison.value
|
|
||||||
)
|
|
||||||
)
|
|
||||||
: filter.comparisons.some(
|
|
||||||
(comparison) =>
|
|
||||||
comparison.column &&
|
|
||||||
matchComparison(
|
|
||||||
row[comparison.column],
|
|
||||||
comparison.comparisonOperator,
|
|
||||||
comparison.value
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const matchComparison = (
|
|
||||||
inputValue?: string,
|
|
||||||
comparisonOperator?: ComparisonOperators,
|
|
||||||
value?: string
|
|
||||||
) => {
|
|
||||||
if (!inputValue || !comparisonOperator || !value) return false
|
|
||||||
switch (comparisonOperator) {
|
|
||||||
case ComparisonOperators.CONTAINS: {
|
|
||||||
return inputValue.toLowerCase().includes(value.toLowerCase())
|
|
||||||
}
|
|
||||||
case ComparisonOperators.EQUAL: {
|
|
||||||
return inputValue === value
|
|
||||||
}
|
|
||||||
case ComparisonOperators.NOT_EQUAL: {
|
|
||||||
return inputValue !== value
|
|
||||||
}
|
|
||||||
case ComparisonOperators.GREATER: {
|
|
||||||
return parseFloat(inputValue) > parseFloat(value)
|
|
||||||
}
|
|
||||||
case ComparisonOperators.LESS: {
|
|
||||||
return parseFloat(inputValue) < parseFloat(value)
|
|
||||||
}
|
|
||||||
case ComparisonOperators.IS_SET: {
|
|
||||||
return isDefined(inputValue) && inputValue.length > 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -0,0 +1,72 @@
|
|||||||
|
import { isDefined } from '@typebot.io/lib'
|
||||||
|
import {
|
||||||
|
GoogleSheetsGetOptions,
|
||||||
|
LogicalOperator,
|
||||||
|
ComparisonOperators,
|
||||||
|
} from '@typebot.io/schemas'
|
||||||
|
import { GoogleSpreadsheetRow } from 'google-spreadsheet'
|
||||||
|
|
||||||
|
export const matchFilter = (
|
||||||
|
row: GoogleSpreadsheetRow,
|
||||||
|
filter: NonNullable<GoogleSheetsGetOptions['filter']>
|
||||||
|
) => {
|
||||||
|
return filter.logicalOperator === LogicalOperator.AND
|
||||||
|
? filter.comparisons.every(
|
||||||
|
(comparison) =>
|
||||||
|
comparison.column &&
|
||||||
|
matchComparison(
|
||||||
|
row[comparison.column],
|
||||||
|
comparison.comparisonOperator,
|
||||||
|
comparison.value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
: filter.comparisons.some(
|
||||||
|
(comparison) =>
|
||||||
|
comparison.column &&
|
||||||
|
matchComparison(
|
||||||
|
row[comparison.column],
|
||||||
|
comparison.comparisonOperator,
|
||||||
|
comparison.value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const matchComparison = (
|
||||||
|
inputValue?: string,
|
||||||
|
comparisonOperator?: ComparisonOperators,
|
||||||
|
value?: string
|
||||||
|
): boolean => {
|
||||||
|
if (!inputValue || !comparisonOperator || !value) return false
|
||||||
|
switch (comparisonOperator) {
|
||||||
|
case ComparisonOperators.CONTAINS: {
|
||||||
|
return inputValue.toLowerCase().includes(value.toLowerCase())
|
||||||
|
}
|
||||||
|
case ComparisonOperators.EQUAL: {
|
||||||
|
return inputValue === value
|
||||||
|
}
|
||||||
|
case ComparisonOperators.NOT_EQUAL: {
|
||||||
|
return inputValue !== value
|
||||||
|
}
|
||||||
|
case ComparisonOperators.GREATER: {
|
||||||
|
return parseFloat(inputValue) > parseFloat(value)
|
||||||
|
}
|
||||||
|
case ComparisonOperators.LESS: {
|
||||||
|
return parseFloat(inputValue) < parseFloat(value)
|
||||||
|
}
|
||||||
|
case ComparisonOperators.IS_SET: {
|
||||||
|
return isDefined(inputValue) && inputValue.length > 0
|
||||||
|
}
|
||||||
|
case ComparisonOperators.IS_EMPTY: {
|
||||||
|
return !isDefined(inputValue) || inputValue.length === 0
|
||||||
|
}
|
||||||
|
case ComparisonOperators.STARTS_WITH: {
|
||||||
|
return inputValue.toLowerCase().startsWith(value.toLowerCase())
|
||||||
|
}
|
||||||
|
case ComparisonOperators.ENDS_WITH: {
|
||||||
|
return inputValue.toLowerCase().endsWith(value.toLowerCase())
|
||||||
|
}
|
||||||
|
case ComparisonOperators.NOT_CONTAINS: {
|
||||||
|
return !inputValue.toLowerCase().includes(value.toLowerCase())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,7 @@ import { ExecuteIntegrationResponse } from '@/features/chat/types'
|
|||||||
import { saveErrorLog } from '@/features/logs/saveErrorLog'
|
import { saveErrorLog } from '@/features/logs/saveErrorLog'
|
||||||
import { saveSuccessLog } from '@/features/logs/saveSuccessLog'
|
import { saveSuccessLog } from '@/features/logs/saveSuccessLog'
|
||||||
import { TRPCError } from '@trpc/server'
|
import { TRPCError } from '@trpc/server'
|
||||||
|
import { matchFilter } from './helpers/matchFilter'
|
||||||
|
|
||||||
export const updateRow = async (
|
export const updateRow = async (
|
||||||
{ result, typebot: { variables } }: SessionState,
|
{ result, typebot: { variables } }: SessionState,
|
||||||
@ -18,8 +19,9 @@ export const updateRow = async (
|
|||||||
options,
|
options,
|
||||||
}: { outgoingEdgeId?: string; options: GoogleSheetsUpdateRowOptions }
|
}: { outgoingEdgeId?: string; options: GoogleSheetsUpdateRowOptions }
|
||||||
): Promise<ExecuteIntegrationResponse> => {
|
): Promise<ExecuteIntegrationResponse> => {
|
||||||
const { sheetId, referenceCell } = deepParseVariables(variables)(options)
|
const { sheetId, referenceCell, filter } =
|
||||||
if (!options.cellsToUpsert || !sheetId || !referenceCell)
|
deepParseVariables(variables)(options)
|
||||||
|
if (!options.cellsToUpsert || !sheetId || (!referenceCell && !filter))
|
||||||
return { outgoingEdgeId }
|
return { outgoingEdgeId }
|
||||||
|
|
||||||
let log: ReplyLog | undefined
|
let log: ReplyLog | undefined
|
||||||
@ -34,14 +36,16 @@ export const updateRow = async (
|
|||||||
await doc.loadInfo()
|
await doc.loadInfo()
|
||||||
const sheet = doc.sheetsById[sheetId]
|
const sheet = doc.sheetsById[sheetId]
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
const updatingRowIndex = rows.findIndex(
|
const filteredRows = rows.filter((row) =>
|
||||||
(row) => row[referenceCell.column as string] === referenceCell.value
|
referenceCell
|
||||||
|
? row[referenceCell.column as string] === referenceCell.value
|
||||||
|
: matchFilter(row, filter as NonNullable<typeof filter>)
|
||||||
)
|
)
|
||||||
if (updatingRowIndex === -1) {
|
if (filteredRows.length === 0) {
|
||||||
log = {
|
log = {
|
||||||
status: 'error',
|
status: 'error',
|
||||||
description: `Could not find row to update`,
|
description: `Could not find any row that matches the filter`,
|
||||||
details: `Looked for row with ${referenceCell.column} equals to "${referenceCell.value}"`,
|
details: JSON.stringify(filter, null, 2),
|
||||||
}
|
}
|
||||||
result &&
|
result &&
|
||||||
(await saveErrorLog({
|
(await saveErrorLog({
|
||||||
@ -51,19 +55,22 @@ export const updateRow = async (
|
|||||||
}))
|
}))
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: 'NOT_FOUND',
|
code: 'NOT_FOUND',
|
||||||
message: `Couldn't find row with ${referenceCell.column} that equals to "${referenceCell.value}"`,
|
message: `Could not find any row that matches the filter`,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key in parsedValues) {
|
|
||||||
rows[updatingRowIndex][key] = parsedValues[key]
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await rows[updatingRowIndex].save()
|
for (const filteredRow of filteredRows) {
|
||||||
|
const rowIndex = rows.findIndex((row) => row.id === filteredRow.id)
|
||||||
|
for (const key in parsedValues) {
|
||||||
|
rows[rowIndex][key] = parsedValues[key]
|
||||||
|
}
|
||||||
|
await rows[rowIndex].save()
|
||||||
|
}
|
||||||
|
|
||||||
log = log = {
|
log = log = {
|
||||||
status: 'success',
|
status: 'success',
|
||||||
description: `Succesfully updated row in ${doc.title} > ${sheet.title}`,
|
description: `Succesfully updated matching rows`,
|
||||||
}
|
}
|
||||||
result &&
|
result &&
|
||||||
(await saveSuccessLog({
|
(await saveSuccessLog({
|
||||||
|
@ -38,8 +38,9 @@ const initialGoogleSheetsOptionsSchema = googleSheetsOptionsBaseSchema.merge(
|
|||||||
const googleSheetsGetOptionsSchema = googleSheetsOptionsBaseSchema.merge(
|
const googleSheetsGetOptionsSchema = googleSheetsOptionsBaseSchema.merge(
|
||||||
z.object({
|
z.object({
|
||||||
action: z.enum([GoogleSheetsAction.GET]),
|
action: z.enum([GoogleSheetsAction.GET]),
|
||||||
// TODO: remove referenceCell once migrated to filtering
|
referenceCell: cellSchema
|
||||||
referenceCell: cellSchema.optional(),
|
.optional()
|
||||||
|
.describe('Deprecated. Use `filter` instead.'),
|
||||||
filter: z
|
filter: z
|
||||||
.object({
|
.object({
|
||||||
comparisons: z.array(rowsFilterComparisonSchema),
|
comparisons: z.array(rowsFilterComparisonSchema),
|
||||||
@ -61,7 +62,15 @@ const googleSheetsUpdateRowOptionsSchema = googleSheetsOptionsBaseSchema.merge(
|
|||||||
z.object({
|
z.object({
|
||||||
action: z.enum([GoogleSheetsAction.UPDATE_ROW]),
|
action: z.enum([GoogleSheetsAction.UPDATE_ROW]),
|
||||||
cellsToUpsert: z.array(cellSchema),
|
cellsToUpsert: z.array(cellSchema),
|
||||||
referenceCell: cellSchema.optional(),
|
referenceCell: cellSchema
|
||||||
|
.optional()
|
||||||
|
.describe('Deprecated. Use `filter` instead.'),
|
||||||
|
filter: z
|
||||||
|
.object({
|
||||||
|
comparisons: z.array(rowsFilterComparisonSchema),
|
||||||
|
logicalOperator: z.nativeEnum(LogicalOperator),
|
||||||
|
})
|
||||||
|
.optional(),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user