import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGoogleFormsApi, normalizeForm, normalizeFormId, preserveItemIds, resolveFormItem, toGoogleFormItem, } from "@automate.ax/integration-contracts/google-forms" import { GOOGLE_FORMS_WRITE_REQUIREMENT } from "../lib/scopes" import { FORM_ITEM_INPUT_SCHEMA, FORM_REFERENCE_SCHEMA, FORM_SCHEMA, ITEM_REFERENCE_SCHEMA, } from "@automate.ax/integration-contracts/google-forms" /** Replaces a form item while preserving provider-generated IDs by default. */ export const updateGoogleFormItem = defineAction("Update Google form item") .describe("Replaces an item selected by ID or one-based position.") .account("google", GOOGLE_FORMS_WRITE_REQUIREMENT) .input( z.object({ /** Form ID or standard Google Forms URL. */ form: FORM_REFERENCE_SCHEMA, /** Replacement item. */ item: FORM_ITEM_INPUT_SCHEMA, /** Item ID or one-based item position. */ itemReference: ITEM_REFERENCE_SCHEMA, /** Rejects the update unless this is the latest revision ID. */ requiredRevisionId: z.string().min(1).optional(), }), ) .output(FORM_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const formsApi = getGoogleFormsApi(account.secret) const formId = normalizeFormId(input.form) const { index, item } = await resolveFormItem( formsApi, formId, input.itemReference, ) const { data } = await formsApi.forms.batchUpdate({ formId, requestBody: { includeFormInResponse: true, requests: [ { updateItem: { item: preserveItemIds(toGoogleFormItem(input.item), item), location: { index }, updateMask: "*", }, }, ], writeControl: input.requiredRevisionId ? { requiredRevisionId: input.requiredRevisionId } : undefined, }, }) if (!data.form) throw new Error("Google Forms did not return the form.") return normalizeForm(data.form) })