import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGoogleFormsApi, normalizeForm, normalizeFormId, resolveItemIndex, } from "@automate.ax/integration-contracts/google-forms" import { GOOGLE_FORMS_WRITE_REQUIREMENT } from "../lib/scopes" import { FORM_REFERENCE_SCHEMA, FORM_SCHEMA, ITEM_REFERENCE_SCHEMA, } from "@automate.ax/integration-contracts/google-forms" /** Deletes a question or other form item. */ export const deleteGoogleFormItem = defineAction("Delete Google form item") .describe("Deletes 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, /** 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 { data } = await formsApi.forms.batchUpdate({ formId, requestBody: { includeFormInResponse: true, requests: [ { deleteItem: { location: { index: await resolveItemIndex( formsApi, formId, input.itemReference, ), }, }, }, ], writeControl: input.requiredRevisionId ? { requiredRevisionId: input.requiredRevisionId } : undefined, }, }) if (!data.form) throw new Error("Google Forms did not return the form.") return normalizeForm(data.form) })