import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGoogleCalendarApi, normalizeAclPage, normalizeAclRule, resolveCalendarId, } from "@automate.ax/integration-contracts/google-calendar" import { GOOGLE_CALENDAR_REFERENCE_ACL_GET_REQUIREMENT, GOOGLE_CALENDAR_REFERENCE_ACL_READ_REQUIREMENT, GOOGLE_CALENDAR_REFERENCE_ACL_WRITE_REQUIREMENT, } from "../lib/scopes" import { ACL_PAGE_SCHEMA, ACL_ROLE_SCHEMA, ACL_RULE_SCHEMA, ACL_SCOPE_SCHEMA, CALENDAR_REFERENCE_SCHEMA, } from "@automate.ax/integration-contracts/google-calendar" /** Lists one page of access rules for a calendar. */ export const listGoogleCalendarAccessRules = defineAction( "List Google Calendar access rules", ) .describe("Lists calendar sharing and access-control rules.") .account("google", GOOGLE_CALENDAR_REFERENCE_ACL_READ_REQUIREMENT) .input( z .object({ /** Exact calendar title or ID. Defaults to the primary calendar. */ calendar: CALENDAR_REFERENCE_SCHEMA.optional(), /** Include deleted rules in incremental synchronization. */ includeDeleted: z.boolean().optional(), /** Maximum rules returned in this page. */ maxResults: z.number().int().min(1).max(250).optional(), /** Pagination token returned by a previous request. */ pageToken: z.string().min(1).optional(), /** Incremental synchronization token from a previous completed listing. */ syncToken: z.string().min(1).optional(), }) .refine( ({ includeDeleted, syncToken }) => !syncToken || includeDeleted !== false, "syncToken cannot be combined with includeDeleted: false.", ), ) .output(ACL_PAGE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const calendarApi = getGoogleCalendarApi(account.secret) const { data } = await calendarApi.acl.list({ calendarId: await resolveCalendarId(calendarApi, input.calendar), maxResults: input.maxResults, pageToken: input.pageToken, showDeleted: input.includeDeleted, syncToken: input.syncToken, }) return normalizeAclPage(data) }) /** Gets one calendar access rule by ID. */ export const getGoogleCalendarAccessRule = defineAction( "Get Google Calendar access rule", ) .describe("Gets one calendar sharing or access-control rule.") .account("google", GOOGLE_CALENDAR_REFERENCE_ACL_GET_REQUIREMENT) .input( z.object({ /** Exact calendar title or ID. Defaults to the primary calendar. */ calendar: CALENDAR_REFERENCE_SCHEMA.optional(), /** Immutable ACL rule ID. */ ruleId: z.string().min(1), }), ) .output(ACL_RULE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const calendarApi = getGoogleCalendarApi(account.secret) const { data } = await calendarApi.acl.get({ calendarId: await resolveCalendarId(calendarApi, input.calendar), ruleId: input.ruleId, }) return normalizeAclRule(data) }) /** Creates a calendar access rule. */ export const createGoogleCalendarAccessRule = defineAction( "Create Google Calendar access rule", ) .describe("Shares a calendar with a user, group, domain, or the public.") .account("google", GOOGLE_CALENDAR_REFERENCE_ACL_WRITE_REQUIREMENT) .input( z.object({ /** Exact calendar title or ID. Defaults to the primary calendar. */ calendar: CALENDAR_REFERENCE_SCHEMA.optional(), /** Permission role to grant. */ role: ACL_ROLE_SCHEMA, /** Whether Google should notify the recipient. */ sendNotifications: z.boolean().optional(), /** Principal or public scope receiving access. */ scope: ACL_SCOPE_SCHEMA, }), ) .output(ACL_RULE_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const calendarApi = getGoogleCalendarApi(account.secret) const { data } = await calendarApi.acl.insert({ calendarId: await resolveCalendarId(calendarApi, input.calendar), requestBody: { role: input.role, scope: input.scope, }, sendNotifications: input.sendNotifications, }) return normalizeAclRule(data) }) /** Updates the role of a calendar access rule. */ export const updateGoogleCalendarAccessRule = defineAction( "Update Google Calendar access rule", ) .describe("Updates a calendar access rule while preserving its scope.") .account("google", GOOGLE_CALENDAR_REFERENCE_ACL_WRITE_REQUIREMENT) .input( z.object({ /** Exact calendar title or ID. Defaults to the primary calendar. */ calendar: CALENDAR_REFERENCE_SCHEMA.optional(), /** Replacement permission role. */ role: ACL_ROLE_SCHEMA, /** Immutable ACL rule ID. */ ruleId: z.string().min(1), /** Whether Google should notify the recipient. */ sendNotifications: z.boolean().optional(), }), ) .output(ACL_RULE_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const calendarApi = getGoogleCalendarApi(account.secret) const calendarId = await resolveCalendarId(calendarApi, input.calendar) const { data: current } = await calendarApi.acl.get({ calendarId, ruleId: input.ruleId, }) const { data } = await calendarApi.acl.update({ calendarId, requestBody: { role: input.role, scope: current.scope, }, ruleId: input.ruleId, sendNotifications: input.sendNotifications, }) return normalizeAclRule(data) }) /** Deletes a calendar access rule and returns its identity. */ export const deleteGoogleCalendarAccessRule = defineAction( "Delete Google Calendar access rule", ) .describe("Removes one sharing or access-control rule.") .account("google", GOOGLE_CALENDAR_REFERENCE_ACL_WRITE_REQUIREMENT) .input( z.object({ /** Exact calendar title or ID. Defaults to the primary calendar. */ calendar: CALENDAR_REFERENCE_SCHEMA.optional(), /** Immutable ACL rule ID. */ ruleId: z.string().min(1), }), ) .output(z.object({ calendarId: z.string(), ruleId: z.string() })) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const calendarApi = getGoogleCalendarApi(account.secret) const calendarId = await resolveCalendarId(calendarApi, input.calendar) await calendarApi.acl.delete({ calendarId, ruleId: input.ruleId, }) return { calendarId, ruleId: input.ruleId } })