import { tallySchema } from "@automate.ax/integration-contracts/tally" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getTallyApi, TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT, TALLY_EMPTY_SCHEMA, TALLY_ID_SCHEMA, } from "../lib" const FORM_ID_INPUT_SCHEMA = z.object({ formId: TALLY_ID_SCHEMA }) /** Lists forms visible to the connected Tally user. */ export const listTallyForms = defineAction("List Tally forms") .describe("Lists a page of forms, optionally limited to selected workspaces.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( z.object({ limit: z.number().int().min(1).max(500).optional(), page: z.number().int().positive().optional(), workspaceIds: TALLY_ID_SCHEMA.array().min(1).optional(), }), ) .output(tallySchema("GetFormsResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request("forms", { query: input, responseSchema: "GetFormsResponse", }), ) /** Creates a draft, blank, or published Tally form. */ export const createTallyForm = defineAction("Create Tally form") .describe("Creates a Tally form from typed blocks or a template.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(tallySchema("PostFormsRequest")) .output(tallySchema("PostFormsResponse")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => getTallyApi(account).request("forms", { body: input, method: "POST", responseSchema: "PostFormsResponse", }), ) /** Gets one Tally form with its settings and blocks. */ export const getTallyForm = defineAction("Get Tally form") .describe("Gets one form with its complete settings and block structure.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(FORM_ID_INPUT_SCHEMA) .output(tallySchema("GetFormsFormIdResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request(`forms/${encodeURIComponent(input.formId)}`, { responseSchema: "GetFormsFormIdResponse", }), ) /** Updates one Tally form's name, status, blocks, or settings. */ export const updateTallyForm = defineAction("Update Tally form") .describe("Updates a form's name, publication status, blocks, or settings.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( tallySchema("PatchFormsFormIdRequest") .refine( (body) => body.name !== undefined || body.status !== undefined || body.blocks !== undefined || body.settings !== undefined, { message: "Provide at least one Tally form field to update." }, ) .and(FORM_ID_INPUT_SCHEMA), ) .output(tallySchema("PatchFormsFormIdResponse")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { formId, ...body } }) => getTallyApi(account).request(`forms/${encodeURIComponent(formId)}`, { body, method: "PATCH", responseSchema: "PatchFormsFormIdResponse", }), ) /** Moves one Tally form to the trash. */ export const deleteTallyForm = defineAction("Delete Tally form") .describe("Moves a Tally form to the trash so it can be restored in Tally.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(FORM_ID_INPUT_SCHEMA) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `forms/${encodeURIComponent(input.formId)}`, { method: "DELETE" }, ) return {} }) /** Lists the questions and response state for one Tally form. */ export const listTallyFormQuestions = defineAction("List Tally form questions") .describe("Lists every question in a form and whether it has responses.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(FORM_ID_INPUT_SCHEMA) .output(tallySchema("GetFormsFormIdQuestionsResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request( `forms/${encodeURIComponent(input.formId)}/questions`, { responseSchema: "GetFormsFormIdQuestionsResponse" }, ), ) /** Replaces every block in one Tally form. */ export const updateTallyFormBlocks = defineAction("Update Tally form blocks") .describe( "Replaces the complete form block array and optionally updates settings.", ) .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(tallySchema("PatchFormsFormIdBlocksRequest").and(FORM_ID_INPUT_SCHEMA)) .output(tallySchema("PatchFormsFormIdBlocksResponse")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { formId, ...body } }) => getTallyApi(account).request(`forms/${encodeURIComponent(formId)}/blocks`, { body, method: "PATCH", responseSchema: "PatchFormsFormIdBlocksResponse", }), )