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 ORGANIZATION_INPUT_SCHEMA = z.object({ organizationId: TALLY_ID_SCHEMA }) /** Gets the user associated with the connected Tally API key. */ export const getTallyCurrentUser = defineAction("Get Tally current user") .describe("Gets the connected user, organization, and subscription plan.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(z.object({ timezone: z.string().trim().min(1).optional() })) .output(tallySchema("GetUsersMeResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request("users/me", { query: input, responseSchema: "GetUsersMeResponse", }), ) /** Lists users in one Tally organization. */ export const listTallyOrganizationUsers = defineAction( "List Tally organization users", ) .describe("Lists users and account details in an organization.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(ORGANIZATION_INPUT_SCHEMA) .output(tallySchema("GetOrganizationsOrganizationIdUsersResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request( `organizations/${encodeURIComponent(input.organizationId)}/users`, { responseSchema: "GetOrganizationsOrganizationIdUsersResponse" }, ), ) /** Removes one user from a Tally organization. */ export const removeTallyOrganizationUser = defineAction( "Remove Tally organization user", ) .describe( "Removes one organization member; only the creator can remove other users.", ) .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(z.object({ organizationId: TALLY_ID_SCHEMA, userId: TALLY_ID_SCHEMA })) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `organizations/${encodeURIComponent(input.organizationId)}/users/${encodeURIComponent(input.userId)}`, { method: "DELETE" }, ) return {} }) /** Lists pending invitations for one Tally organization. */ export const listTallyOrganizationInvites = defineAction( "List Tally organization invites", ) .describe("Lists pending invitations for one organization.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(ORGANIZATION_INPUT_SCHEMA) .output(tallySchema("GetOrganizationsOrganizationIdInvitesResponse")) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request( `organizations/${encodeURIComponent(input.organizationId)}/invites`, { responseSchema: "GetOrganizationsOrganizationIdInvitesResponse" }, ), ) /** Invites users to selected Tally workspaces. */ export const createTallyOrganizationInvites = defineAction( "Create Tally organization invites", ) .describe("Invites one or more email addresses to selected workspaces.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( z.object({ emails: z.email().array().min(1), organizationId: TALLY_ID_SCHEMA, workspaceIds: TALLY_ID_SCHEMA.array().min(1), }), ) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `organizations/${encodeURIComponent(input.organizationId)}/invites`, { body: { emails: input.emails.join(","), workspaceIds: input.workspaceIds, }, method: "POST", }, ) return {} }) /** Cancels one pending Tally organization invitation. */ export const cancelTallyOrganizationInvite = defineAction( "Cancel Tally organization invite", ) .describe("Cancels one pending organization invitation.") .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input( z.object({ inviteId: TALLY_ID_SCHEMA, organizationId: TALLY_ID_SCHEMA }), ) .output(TALLY_EMPTY_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTallyApi(account).requestVoid( `organizations/${encodeURIComponent(input.organizationId)}/invites/${encodeURIComponent(input.inviteId)}`, { method: "DELETE" }, ) return {} })