import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGoogleFormsApi, getSettingsUpdateMask, normalizeForm, normalizeFormId, toGoogleFormSettings, } from "@automate.ax/integration-contracts/google-forms" import { GOOGLE_FORMS_WRITE_REQUIREMENT } from "../lib/scopes" import { FORM_REFERENCE_SCHEMA, FORM_SCHEMA, FORM_SETTINGS_INPUT_SCHEMA, } from "@automate.ax/integration-contracts/google-forms" /** Updates quiz and respondent email-collection settings. */ export const updateGoogleFormSettings = defineAction( "Update Google form settings", ) .describe("Updates Google Form behavior and returns the current form.") .account("google", GOOGLE_FORMS_WRITE_REQUIREMENT) .input( z.object({ /** 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(), /** Quiz and email-collection settings to update. */ settings: FORM_SETTINGS_INPUT_SCHEMA, }), ) .output(FORM_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const { data } = await getGoogleFormsApi(account.secret).forms.batchUpdate({ formId: normalizeFormId(input.form), requestBody: { includeFormInResponse: true, requests: [ { updateSettings: { settings: toGoogleFormSettings(input.settings), updateMask: getSettingsUpdateMask(input.settings), }, }, ], 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) })