import { z } from "zod"; import { BaseSchema } from "@server/routes/api/schema"; export const GranolaConnectSchema = BaseSchema.extend({ body: z.object({ /** A personal Granola API key. */ apiKey: z.string().trim().min(1).max(255), }), }); export type GranolaConnectReq = z.infer; export const GranolaFoldersSchema = BaseSchema.extend({ body: z.object({ /** The ID of the Granola integration. */ id: z.uuid(), }), }); export type GranolaFoldersReq = z.infer; const RuleSchema = z .object({ folderId: z.string().min(1).max(255).nullable(), folderName: z.string().max(255).optional(), collectionId: z.uuid().optional(), documentId: z.uuid().optional(), locationName: z.string().max(255).optional(), }) .refine((rule) => !rule.collectionId !== !rule.documentId, { message: "One of collectionId or documentId is required", }); export const GranolaUpdateSchema = BaseSchema.extend({ body: z.object({ /** The ID of the Granola integration. */ id: z.uuid(), /** The rules that choose where notes go. */ rules: z .array(RuleSchema) .max(50) .refine( (rules) => new Set(rules.map((rule) => rule.folderId)).size === rules.length, { message: "Each Granola folder can have only one rule" } ), }), }); export type GranolaUpdateReq = z.infer; export const GranolaWebhookSchema = BaseSchema.extend({ query: z.object({ /** The ID of the Granola integration that the webhook belongs to. */ id: z.uuid(), }), body: z.object({ event_type: z.string(), /** Missing on events that are not about a note, such as webhook.test. */ note_id: z.string().optional(), }), }); export type GranolaWebhookReq = z.infer;