2022-06-07 08:49:12 +02:00
|
|
|
import { z } from 'zod'
|
2022-11-15 11:02:26 +01:00
|
|
|
import { blockBaseSchema, IntegrationBlockType } from '../shared'
|
2022-06-07 08:49:12 +02:00
|
|
|
|
|
|
|
export enum GoogleSheetsAction {
|
|
|
|
GET = 'Get data from sheet',
|
|
|
|
INSERT_ROW = 'Insert a row',
|
|
|
|
UPDATE_ROW = 'Update a row',
|
|
|
|
}
|
|
|
|
|
|
|
|
const cellSchema = z.object({
|
|
|
|
column: z.string().optional(),
|
|
|
|
value: z.string().optional(),
|
|
|
|
id: z.string(),
|
|
|
|
})
|
|
|
|
|
|
|
|
const extractingCellSchema = z.object({
|
|
|
|
column: z.string().optional(),
|
|
|
|
id: z.string(),
|
|
|
|
variableId: z.string().optional(),
|
|
|
|
})
|
|
|
|
|
|
|
|
const googleSheetsOptionsBaseSchema = z.object({
|
|
|
|
credentialsId: z.string().optional(),
|
|
|
|
sheetId: z.string().optional(),
|
|
|
|
spreadsheetId: z.string().optional(),
|
|
|
|
})
|
|
|
|
|
|
|
|
const googleSheetsGetOptionsSchema = googleSheetsOptionsBaseSchema.and(
|
|
|
|
z.object({
|
|
|
|
action: z.enum([GoogleSheetsAction.GET]),
|
|
|
|
referenceCell: cellSchema.optional(),
|
|
|
|
cellsToExtract: z.array(extractingCellSchema),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const googleSheetsInsertRowOptionsSchema = googleSheetsOptionsBaseSchema.and(
|
|
|
|
z.object({
|
|
|
|
action: z.enum([GoogleSheetsAction.INSERT_ROW]),
|
|
|
|
cellsToInsert: z.array(cellSchema),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const googleSheetsUpdateRowOptionsSchema = googleSheetsOptionsBaseSchema.and(
|
|
|
|
z.object({
|
|
|
|
action: z.enum([GoogleSheetsAction.UPDATE_ROW]),
|
|
|
|
cellsToUpsert: z.array(cellSchema),
|
|
|
|
referenceCell: cellSchema.optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
export const googleSheetsOptionsSchema = googleSheetsOptionsBaseSchema
|
|
|
|
.or(googleSheetsGetOptionsSchema)
|
|
|
|
.or(googleSheetsInsertRowOptionsSchema)
|
|
|
|
.or(googleSheetsUpdateRowOptionsSchema)
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const googleSheetsBlockSchema = blockBaseSchema.and(
|
2022-06-07 08:49:12 +02:00
|
|
|
z.object({
|
2022-06-11 07:27:38 +02:00
|
|
|
type: z.enum([IntegrationBlockType.GOOGLE_SHEETS]),
|
2022-06-07 08:49:12 +02:00
|
|
|
options: googleSheetsOptionsSchema,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
export const defaultGoogleSheetsOptions: GoogleSheetsOptions = {}
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export type GoogleSheetsBlock = z.infer<typeof googleSheetsBlockSchema>
|
2022-06-07 08:49:12 +02:00
|
|
|
export type GoogleSheetsOptions = z.infer<typeof googleSheetsOptionsSchema>
|
|
|
|
export type GoogleSheetsOptionsBase = z.infer<
|
|
|
|
typeof googleSheetsOptionsBaseSchema
|
|
|
|
>
|
|
|
|
export type GoogleSheetsGetOptions = z.infer<
|
|
|
|
typeof googleSheetsGetOptionsSchema
|
|
|
|
>
|
|
|
|
export type GoogleSheetsInsertRowOptions = z.infer<
|
|
|
|
typeof googleSheetsInsertRowOptionsSchema
|
|
|
|
>
|
|
|
|
export type GoogleSheetsUpdateRowOptions = z.infer<
|
|
|
|
typeof googleSheetsUpdateRowOptionsSchema
|
|
|
|
>
|
|
|
|
export type Cell = z.infer<typeof cellSchema>
|
|
|
|
export type ExtractingCell = z.infer<typeof extractingCellSchema>
|