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 WORKSPACE_INPUT_SCHEMA = z.object({ workspaceId: TALLY_ID_SCHEMA }) /** Lists workspaces visible to the connected Tally user. */ export const listTallyWorkspaces = defineAction("List Tally workspaces") .describe("Lists one page of workspaces and their organization context.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(z.object({ page: z.number().int().positive().optional() })) .output(tallySchema("GetWorkspacesResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request("workspaces", { query: input, responseSchema: "GetWorkspacesResponse", }), ) /** Creates a Pro Tally workspace. */ export const createTallyWorkspace = defineAction("Create Tally workspace") .describe("Creates a workspace; Tally requires a Pro subscription.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(tallySchema("PostWorkspacesRequest")) .output(tallySchema("PostWorkspacesResponse")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input }) => getTallyApi(account).request("workspaces", { body: input, method: "POST", responseSchema: "PostWorkspacesResponse", }), ) /** Gets one Tally workspace. */ export const getTallyWorkspace = defineAction("Get Tally workspace") .describe("Gets one workspace with its members, invites, and folders.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(WORKSPACE_INPUT_SCHEMA) .output(tallySchema("GetWorkspacesWorkspaceIdResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request( `workspaces/${encodeURIComponent(input.workspaceId)}`, { responseSchema: "GetWorkspacesWorkspaceIdResponse" }, ), ) /** Renames one Tally workspace. */ export const updateTallyWorkspace = defineAction("Update Tally workspace") .describe("Renames one workspace.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( z.object({ name: z.string().trim().min(1), workspaceId: TALLY_ID_SCHEMA }), ) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `workspaces/${encodeURIComponent(input.workspaceId)}`, { body: { name: input.name }, method: "PATCH" }, ) return {} }) /** Moves one Tally workspace and its forms to the trash. */ export const deleteTallyWorkspace = defineAction("Delete Tally workspace") .describe("Moves one workspace and all its forms to the Tally trash.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(WORKSPACE_INPUT_SCHEMA) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `workspaces/${encodeURIComponent(input.workspaceId)}`, { method: "DELETE" }, ) return {} }) /** Lists folders in one Pro Tally workspace. */ export const listTallyFolders = defineAction("List Tally folders") .describe("Lists the complete folder hierarchy in one Pro workspace.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(WORKSPACE_INPUT_SCHEMA) .output(tallySchema("GetWorkspacesWorkspaceIdFoldersResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request( `workspaces/${encodeURIComponent(input.workspaceId)}/folders`, { responseSchema: "GetWorkspacesWorkspaceIdFoldersResponse" }, ), ) /** Creates a folder in one Pro Tally workspace. */ export const createTallyFolder = defineAction("Create Tally folder") .describe("Creates a top-level or nested folder in a Pro workspace.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( tallySchema("PostWorkspacesWorkspaceIdFoldersRequest").and( WORKSPACE_INPUT_SCHEMA, ), ) .output(tallySchema("PostWorkspacesWorkspaceIdFoldersResponse")) .retry({ replaySafety: "unsafe" }) .handler(({ account, input: { workspaceId, ...body } }) => getTallyApi(account).request( `workspaces/${encodeURIComponent(workspaceId)}/folders`, { body, method: "POST", responseSchema: "PostWorkspacesWorkspaceIdFoldersResponse", }, ), ) /** Renames one folder in a Pro Tally workspace. */ export const updateTallyFolder = defineAction("Update Tally folder") .describe("Renames one workspace folder.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( z.object({ folderId: TALLY_ID_SCHEMA, name: z.string().trim().min(1), workspaceId: TALLY_ID_SCHEMA, }), ) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `workspaces/${encodeURIComponent(input.workspaceId)}/folders/${encodeURIComponent(input.folderId)}`, { body: { name: input.name }, method: "PATCH" }, ) return {} }) /** Moves one Tally folder subtree and its forms to the trash. */ export const deleteTallyFolder = defineAction("Delete Tally folder") .describe("Deletes a folder subtree and moves its forms to the Tally trash.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(z.object({ folderId: TALLY_ID_SCHEMA, workspaceId: TALLY_ID_SCHEMA })) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `workspaces/${encodeURIComponent(input.workspaceId)}/folders/${encodeURIComponent(input.folderId)}`, { method: "DELETE" }, ) return {} })