import * as z from "zod" import { defineAction } from "../../../automation/actions" import { TRELLO_READ_SCOPE, TRELLO_WRITE_SCOPE } from "./scopes" import { getTrelloApi } from "@automate.ax/integration-contracts/trello" import { TRELLO_LABEL_SCHEMA, TRELLO_PROVIDER_LABEL_SCHEMA, toTrelloLabel, } from "@automate.ax/integration-contracts/trello" const TRELLO_ACCOUNT = "trello" const TRELLO_LABEL_COLOR_SCHEMA = z.enum([ "black", "blue", "green", "lime", "orange", "pink", "purple", "red", "sky", "yellow", ]) /** Retrieves one Trello label. */ export const getTrelloLabel = defineAction("Get Trello label") .describe("Retrieves one Trello label by ID.") .account(TRELLO_ACCOUNT, TRELLO_READ_SCOPE) .input(z.object({ labelId: z.string().trim().min(1) })) .output(TRELLO_LABEL_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => toTrelloLabel( await getTrelloApi(account.secret).request( `labels/${encodeURIComponent(input.labelId)}`, { query: { fields: "id,idBoard,name,color" }, responseSchema: TRELLO_PROVIDER_LABEL_SCHEMA, }, ), ), ) /** Updates a Trello label. */ export const updateTrelloLabel = defineAction("Update Trello label") .describe("Updates a Trello label's name or color.") .account(TRELLO_ACCOUNT, TRELLO_WRITE_SCOPE) .input( z .object({ /** New label color, or null for a colorless label. */ color: TRELLO_LABEL_COLOR_SCHEMA.nullable().optional(), /** Stable Trello label ID. */ labelId: z.string().trim().min(1), /** New label name. An empty string clears the name. */ name: z.string().optional(), }) .refine( ({ color, name }) => color !== undefined || name !== undefined, "Provide a label name or color to update.", ), ) .output(TRELLO_LABEL_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => toTrelloLabel( await getTrelloApi(account.secret).request( `labels/${encodeURIComponent(input.labelId)}`, { method: "PUT", query: { color: input.color === null ? "null" : input.color, name: input.name, }, responseSchema: TRELLO_PROVIDER_LABEL_SCHEMA, }, ), ), ) /** Permanently deletes a Trello label. */ export const deleteTrelloLabel = defineAction("Delete Trello label") .describe("Permanently deletes a Trello label from its board.") .account(TRELLO_ACCOUNT, TRELLO_WRITE_SCOPE) .input(z.object({ labelId: z.string().trim().min(1) })) .output(z.object({ deleted: z.literal(true), labelId: z.string() })) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getTrelloApi(account.secret).request( `labels/${encodeURIComponent(input.labelId)}`, { method: "DELETE", responseSchema: z.unknown() }, ) return { deleted: true as const, labelId: input.labelId } })