✨ (googleSheets) Advanced get filtering
Allows you to select rows based on advanced conditions / comparisons
This commit is contained in:
@ -11,7 +11,6 @@ import {
|
||||
Cell,
|
||||
Variable,
|
||||
} from 'models'
|
||||
import { stringify } from 'qs'
|
||||
import { sendRequest, byId } from 'utils'
|
||||
|
||||
export const executeGoogleSheetBlock = async (
|
||||
@ -45,12 +44,13 @@ const insertRowInGoogleSheets = (
|
||||
})
|
||||
return
|
||||
}
|
||||
const params = stringify({ resultId })
|
||||
sendRequest({
|
||||
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}?${params}`,
|
||||
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
|
||||
method: 'POST',
|
||||
body: {
|
||||
action: GoogleSheetsAction.INSERT_ROW,
|
||||
credentialsId: options.credentialsId,
|
||||
resultId,
|
||||
values: parseCellValues(options.cellsToInsert, variables),
|
||||
},
|
||||
}).then(({ error }) => {
|
||||
@ -69,13 +69,14 @@ const updateRowInGoogleSheets = (
|
||||
{ variables, apiHost, onNewLog, resultId }: IntegrationState
|
||||
) => {
|
||||
if (!options.cellsToUpsert || !options.referenceCell) return
|
||||
const params = stringify({ resultId })
|
||||
sendRequest({
|
||||
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}?${params}`,
|
||||
method: 'PATCH',
|
||||
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
|
||||
method: 'POST',
|
||||
body: {
|
||||
action: GoogleSheetsAction.UPDATE_ROW,
|
||||
credentialsId: options.credentialsId,
|
||||
values: parseCellValues(options.cellsToUpsert, variables),
|
||||
resultId,
|
||||
referenceCell: {
|
||||
column: options.referenceCell.column,
|
||||
value: parseVariables(variables)(options.referenceCell.value ?? ''),
|
||||
@ -103,22 +104,33 @@ const getRowFromGoogleSheets = async (
|
||||
resultId,
|
||||
}: IntegrationState
|
||||
) => {
|
||||
if (!options.referenceCell || !options.cellsToExtract) return
|
||||
const queryParams = stringify(
|
||||
{
|
||||
if (!options.cellsToExtract) return
|
||||
const { data, error } = await sendRequest<{
|
||||
rows: { [key: string]: string }[]
|
||||
}>({
|
||||
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}`,
|
||||
method: 'POST',
|
||||
body: {
|
||||
action: GoogleSheetsAction.GET,
|
||||
credentialsId: options.credentialsId,
|
||||
referenceCell: {
|
||||
column: options.referenceCell.column,
|
||||
value: parseVariables(variables)(options.referenceCell.value ?? ''),
|
||||
},
|
||||
referenceCell: options.referenceCell
|
||||
? {
|
||||
column: options.referenceCell.column,
|
||||
value: parseVariables(variables)(options.referenceCell.value ?? ''),
|
||||
}
|
||||
: undefined,
|
||||
filter: options.filter
|
||||
? {
|
||||
comparisons: options.filter.comparisons.map((comparison) => ({
|
||||
...comparison,
|
||||
value: parseVariables(variables)(comparison.value),
|
||||
})),
|
||||
logicalOperator: options.filter?.logicalOperator ?? 'AND',
|
||||
}
|
||||
: undefined,
|
||||
columns: options.cellsToExtract.map((cell) => cell.column),
|
||||
resultId,
|
||||
},
|
||||
{ indices: false }
|
||||
)
|
||||
const { data, error } = await sendRequest<{ [key: string]: string }>({
|
||||
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}?${queryParams}`,
|
||||
method: 'GET',
|
||||
})
|
||||
onNewLog(
|
||||
parseLog(
|
||||
@ -131,7 +143,9 @@ const getRowFromGoogleSheets = async (
|
||||
const newVariables = options.cellsToExtract.reduce<VariableWithValue[]>(
|
||||
(newVariables, cell) => {
|
||||
const existingVariable = variables.find(byId(cell.variableId))
|
||||
const value = data[cell.column ?? ''] ?? null
|
||||
const rows = data.rows
|
||||
const randomRow = rows[Math.floor(Math.random() * rows.length)]
|
||||
const value = randomRow[cell.column ?? ''] ?? null
|
||||
if (!existingVariable) return newVariables
|
||||
updateVariableValue(existingVariable.id, value)
|
||||
return [
|
||||
|
@ -1 +1 @@
|
||||
export { executeCondition } from './utils/executeCondition'
|
||||
export * from './utils'
|
||||
|
@ -31,25 +31,33 @@ const executeComparison =
|
||||
variables.find((v) => v.id === comparison.variableId)?.value ?? ''
|
||||
).trim()
|
||||
const value = parseVariables(variables)(comparison.value).trim()
|
||||
if (isNotDefined(value)) return false
|
||||
switch (comparison.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
|
||||
}
|
||||
if (isNotDefined(value) || !comparison.comparisonOperator) return false
|
||||
return matchComparison(inputValue, comparison.comparisonOperator, value)
|
||||
}
|
||||
|
||||
const matchComparison = (
|
||||
inputValue: string,
|
||||
comparisonOperator: ComparisonOperators,
|
||||
value: string
|
||||
) => {
|
||||
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 @@
|
||||
export * from './executeCondition'
|
@ -9,4 +9,5 @@ export default defineConfig((options) => ({
|
||||
loader: {
|
||||
'.css': 'text',
|
||||
},
|
||||
external: ['cuid'],
|
||||
}))
|
||||
|
@ -9,10 +9,11 @@
|
||||
"zod": "3.19.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "4.9.3",
|
||||
"next": "13.0.6",
|
||||
"cuid": "2.1.8",
|
||||
"db": "workspace:*",
|
||||
"tsconfig": "workspace:*"
|
||||
"next": "13.0.6",
|
||||
"tsconfig": "workspace:*",
|
||||
"typescript": "4.9.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"next": "13.0.0"
|
||||
|
@ -1,5 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
import { ComparisonOperators, LogicalOperator } from '../logic/condition'
|
||||
import { blockBaseSchema, IntegrationBlockType } from '../shared'
|
||||
import cuid from 'cuid'
|
||||
|
||||
export enum GoogleSheetsAction {
|
||||
GET = 'Get data from sheet',
|
||||
@ -25,10 +27,22 @@ const googleSheetsOptionsBaseSchema = z.object({
|
||||
spreadsheetId: z.string().optional(),
|
||||
})
|
||||
|
||||
const rowsFilterComparisonSchema = z.object({
|
||||
id: z.string(),
|
||||
column: z.string().optional(),
|
||||
comparisonOperator: z.nativeEnum(ComparisonOperators).optional(),
|
||||
value: z.string().optional(),
|
||||
})
|
||||
|
||||
const googleSheetsGetOptionsSchema = googleSheetsOptionsBaseSchema.and(
|
||||
z.object({
|
||||
action: z.enum([GoogleSheetsAction.GET]),
|
||||
// TODO: remove referenceCell once migrated to filtering
|
||||
referenceCell: cellSchema.optional(),
|
||||
filter: z.object({
|
||||
comparisons: z.array(rowsFilterComparisonSchema),
|
||||
logicalOperator: z.nativeEnum(LogicalOperator),
|
||||
}),
|
||||
cellsToExtract: z.array(extractingCellSchema),
|
||||
})
|
||||
)
|
||||
@ -62,6 +76,41 @@ export const googleSheetsBlockSchema = blockBaseSchema.and(
|
||||
|
||||
export const defaultGoogleSheetsOptions: GoogleSheetsOptions = {}
|
||||
|
||||
export const defaultGoogleSheetsGetOptions: GoogleSheetsGetOptions = {
|
||||
action: GoogleSheetsAction.GET,
|
||||
cellsToExtract: [
|
||||
{
|
||||
id: cuid(),
|
||||
},
|
||||
],
|
||||
filter: {
|
||||
comparisons: [
|
||||
{
|
||||
id: cuid(),
|
||||
},
|
||||
],
|
||||
logicalOperator: LogicalOperator.AND,
|
||||
},
|
||||
}
|
||||
|
||||
export const defaultGoogleSheetsInsertOptions: GoogleSheetsInsertRowOptions = {
|
||||
action: GoogleSheetsAction.INSERT_ROW,
|
||||
cellsToInsert: [
|
||||
{
|
||||
id: cuid(),
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
export const defaultGoogleSheetsUpdateOptions: GoogleSheetsUpdateRowOptions = {
|
||||
action: GoogleSheetsAction.UPDATE_ROW,
|
||||
cellsToUpsert: [
|
||||
{
|
||||
id: cuid(),
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
export type GoogleSheetsBlock = z.infer<typeof googleSheetsBlockSchema>
|
||||
export type GoogleSheetsOptions = z.infer<typeof googleSheetsOptionsSchema>
|
||||
export type GoogleSheetsOptionsBase = z.infer<
|
||||
@ -78,3 +127,4 @@ export type GoogleSheetsUpdateRowOptions = z.infer<
|
||||
>
|
||||
export type Cell = z.infer<typeof cellSchema>
|
||||
export type ExtractingCell = z.infer<typeof extractingCellSchema>
|
||||
export type RowsFilterComparison = z.infer<typeof rowsFilterComparisonSchema>
|
||||
|
Reference in New Issue
Block a user