import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGoogleFormsApi, normalizeForm, normalizeFormId, } from "@automate.ax/integration-contracts/google-forms" import { GOOGLE_FORMS_WRITE_REQUIREMENT } from "../lib/scopes" import { FORM_REFERENCE_SCHEMA, FORM_SCHEMA, } from "@automate.ax/integration-contracts/google-forms" /** Updates the respondent-facing title or description of a form. */ export const updateGoogleForm = defineAction("Update Google form") .describe("Updates Google Form metadata and returns the current form.") .account("google", GOOGLE_FORMS_WRITE_REQUIREMENT) .input( z .object({ /** New description. Pass an empty string to clear it. */ description: z.string().optional(), /** Form ID or standard Google Forms URL. */ form: FORM_REFERENCE_SCHEMA, /** Rejects the update unless this is the latest revision ID. */ requiredRevisionId: z.string().min(1).optional(), /** New respondent-facing title. */ title: z.string().trim().min(1).optional(), }) .refine( ({ description, title }) => description !== undefined || title !== undefined, "Provide a title or description to update.", ), ) .output(FORM_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const { description, title } = input const { data } = await getGoogleFormsApi(account.secret).forms.batchUpdate({ formId: normalizeFormId(input.form), requestBody: { includeFormInResponse: true, requests: [ { updateFormInfo: { info: { description, title }, updateMask: [ title === undefined ? undefined : "title", description === undefined ? undefined : "description", ] .filter((field): field is string => Boolean(field)) .join(","), }, }, ], writeControl: input.requiredRevisionId ? { requiredRevisionId: input.requiredRevisionId } : undefined, }, }) if (!data.form) { throw new Error("Google Forms did not return the updated form.") } return normalizeForm(data.form) })