import { CLICKUP_GOAL_SCHEMA, CLICKUP_GOAL_LIST_ITEM_SCHEMA, CLICKUP_KEY_RESULT_SCHEMA, } from "@automate.ax/integration-contracts/clickup" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { CLICKUP_ACCOUNT, CLICKUP_ACCOUNT_REQUIREMENT, CLICKUP_ID_SCHEMA, CLICKUP_TIMESTAMP_SCHEMA, CLICKUP_VOID_RESPONSE_SCHEMA, compactClickUpInput, getClickUpApi, } from "./lib" const GOAL_RESPONSE_SCHEMA = z.object({ goal: CLICKUP_GOAL_SCHEMA }) const KEY_RESULT_RESPONSE_SCHEMA = z.object({ keyResult: CLICKUP_KEY_RESULT_SCHEMA, }) /** Lists Goals in a ClickUp Workspace. */ export const listClickUpGoals = defineAction("List ClickUp Goals") .describe("Lists Goals available in a ClickUp Workspace.") .account(CLICKUP_ACCOUNT, CLICKUP_ACCOUNT_REQUIREMENT) .input( z.object({ includeCompleted: z.boolean().prefault(false), workspaceId: CLICKUP_ID_SCHEMA, }), ) .output(z.object({ goals: CLICKUP_GOAL_LIST_ITEM_SCHEMA.array() })) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => ({ goals: ( await getClickUpApi(account).request(`team/${input.workspaceId}/goal`, { query: { includeCompleted: input.includeCompleted }, responseSchema: z.object({ folders: z.unknown().array().prefault([]), goals: CLICKUP_GOAL_LIST_ITEM_SCHEMA.array(), }), }) ).goals, })) /** Gets one ClickUp Goal and its Targets. */ export const getClickUpGoal = defineAction("Get ClickUp Goal") .describe("Gets one ClickUp Goal and its Targets.") .account(CLICKUP_ACCOUNT, CLICKUP_ACCOUNT_REQUIREMENT) .input(z.object({ goalId: CLICKUP_ID_SCHEMA })) .output(CLICKUP_GOAL_SCHEMA) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => ( await getClickUpApi(account).request(`goal/${input.goalId}`, { responseSchema: GOAL_RESPONSE_SCHEMA, }) ).goal, ) /** Creates a ClickUp Goal. */ export const createClickUpGoal = defineAction("Create ClickUp Goal") .describe("Creates a Goal in a ClickUp Workspace.") .account(CLICKUP_ACCOUNT, CLICKUP_ACCOUNT_REQUIREMENT) .input( z.object({ color: z.string(), description: z.string(), dueDate: CLICKUP_TIMESTAMP_SCHEMA, multipleOwners: z.boolean(), name: z.string().trim().min(1), owners: z.number().int().array(), workspaceId: CLICKUP_ID_SCHEMA, }), ) .output(CLICKUP_GOAL_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const { workspaceId, ...body } = input return ( await getClickUpApi(account).request(`team/${workspaceId}/goal`, { body: compactClickUpInput(body), method: "POST", responseSchema: GOAL_RESPONSE_SCHEMA, }) ).goal }) /** Updates a ClickUp Goal. */ export const updateClickUpGoal = defineAction("Update ClickUp Goal") .describe("Updates Goal details and owners.") .account(CLICKUP_ACCOUNT, CLICKUP_ACCOUNT_REQUIREMENT) .input( z.object({ addOwners: z.number().int().array(), color: z.string(), description: z.string(), dueDate: CLICKUP_TIMESTAMP_SCHEMA, goalId: CLICKUP_ID_SCHEMA, name: z.string().trim().min(1), removeOwners: z.number().int().array(), }), ) .output(CLICKUP_GOAL_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { goalId, removeOwners, ...rest } = input return ( await getClickUpApi(account).request(`goal/${goalId}`, { body: compactClickUpInput({ ...rest, remOwners: removeOwners }), method: "PUT", responseSchema: GOAL_RESPONSE_SCHEMA, }) ).goal }) /** Deletes a ClickUp Goal. */ export const deleteClickUpGoal = defineAction("Delete ClickUp Goal") .describe("Deletes a ClickUp Goal.") .account(CLICKUP_ACCOUNT, CLICKUP_ACCOUNT_REQUIREMENT) .input(z.object({ goalId: CLICKUP_ID_SCHEMA })) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getClickUpApi(account).request(`goal/${input.goalId}`, { method: "DELETE", responseSchema: CLICKUP_VOID_RESPONSE_SCHEMA, }), ) /** Creates a Target on a ClickUp Goal. */ export const createClickUpGoalTarget = defineAction( "Create ClickUp Goal Target", ) .describe("Creates a numeric, currency, percentage, or automatic Target.") .account(CLICKUP_ACCOUNT, CLICKUP_ACCOUNT_REQUIREMENT) .input( z.object({ goalId: CLICKUP_ID_SCHEMA, listIds: CLICKUP_ID_SCHEMA.array().prefault([]), name: z.string().trim().min(1), owners: z.number().int().array().prefault([]), stepsEnd: z.number().int(), stepsStart: z.number().int().prefault(0), taskIds: CLICKUP_ID_SCHEMA.array().prefault([]), type: z.enum([ "automatic", "boolean", "currency", "number", "percentage", ]), unit: z.string(), }), ) .output(CLICKUP_KEY_RESULT_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const { goalId, ...body } = input return ( await getClickUpApi(account).request(`goal/${goalId}/key_result`, { body: compactClickUpInput(body), method: "POST", responseSchema: KEY_RESULT_RESPONSE_SCHEMA, }) ).keyResult }) /** Updates progress on a ClickUp Goal Target. */ export const updateClickUpGoalTarget = defineAction( "Update ClickUp Goal Target", ) .describe("Updates Target progress and records a note.") .account(CLICKUP_ACCOUNT, CLICKUP_ACCOUNT_REQUIREMENT) .input( z.object({ note: z.string(), stepsCurrent: z.number().int(), targetId: CLICKUP_ID_SCHEMA, }), ) .output(CLICKUP_KEY_RESULT_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { targetId, ...body } = input return ( await getClickUpApi(account).request(`key_result/${targetId}`, { body: compactClickUpInput(body), method: "PUT", responseSchema: KEY_RESULT_RESPONSE_SCHEMA, }) ).keyResult }) /** Deletes a ClickUp Goal Target. */ export const deleteClickUpGoalTarget = defineAction( "Delete ClickUp Goal Target", ) .describe("Deletes a Target from a ClickUp Goal.") .account(CLICKUP_ACCOUNT, CLICKUP_ACCOUNT_REQUIREMENT) .input(z.object({ targetId: CLICKUP_ID_SCHEMA })) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getClickUpApi(account).request(`key_result/${input.targetId}`, { method: "DELETE", responseSchema: CLICKUP_VOID_RESPONSE_SCHEMA, }), )