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 WEBHOOK_ID_INPUT_SCHEMA = z.object({ webhookId: TALLY_ID_SCHEMA }) /** Lists webhooks visible to the connected Tally user. */ export const listTallyWebhooks = defineAction("List Tally webhooks") .describe("Lists one page of webhooks across accessible forms.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( z.object({ limit: z.number().int().min(1).max(100).optional(), page: z.number().int().positive().optional(), }), ) .output(tallySchema("GetWebhooksResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request("webhooks", { query: input, responseSchema: "GetWebhooksResponse", }), ) /** Creates an independent Tally form webhook. */ export const createTallyWebhook = defineAction("Create Tally webhook") .describe("Creates a signed or unsigned form-response webhook.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(tallySchema("PostWebhooksRequest")) .output(tallySchema("PostWebhooksResponse")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => getTallyApi(account).request("webhooks", { body: input, method: "POST", responseSchema: "PostWebhooksResponse", }), ) /** Updates one Tally webhook. */ export const updateTallyWebhook = defineAction("Update Tally webhook") .describe("Replaces one webhook's form, URL, events, security, and state.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( tallySchema("PatchWebhooksWebhookIdRequest").and(WEBHOOK_ID_INPUT_SCHEMA), ) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input: { webhookId, ...body } }) => { await getTallyApi(account).requestVoid( `webhooks/${encodeURIComponent(webhookId)}`, { body, method: "PATCH" }, ) return {} }) /** Deletes one Tally webhook. */ export const deleteTallyWebhook = defineAction("Delete Tally webhook") .describe("Deletes one webhook from its form.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(WEBHOOK_ID_INPUT_SCHEMA) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `webhooks/${encodeURIComponent(input.webhookId)}`, { method: "DELETE" }, ) return {} }) /** Lists delivery attempts for one Tally webhook. */ export const listTallyWebhookEvents = defineAction("List Tally webhook events") .describe("Lists delivery status, retries, responses, and payloads.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( z.object({ page: z.number().int().positive().optional(), webhookId: TALLY_ID_SCHEMA, }), ) .output(tallySchema("GetWebhooksWebhookIdEventsResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request( `webhooks/${encodeURIComponent(input.webhookId)}/events`, { query: { page: input.page }, responseSchema: "GetWebhooksWebhookIdEventsResponse", }, ), ) /** Retries one failed Tally webhook delivery. */ export const retryTallyWebhookEvent = defineAction("Retry Tally webhook event") .describe("Requests immediate redelivery of one failed webhook event.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(z.object({ eventId: TALLY_ID_SCHEMA, webhookId: TALLY_ID_SCHEMA })) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `webhooks/${encodeURIComponent(input.webhookId)}/events/${encodeURIComponent(input.eventId)}`, { method: "POST" }, ) return {} })