import { CLOSE_COMMENT_SCHEMA, CLOSE_VALUE_SCHEMA, closePageSchema, } from "@automate.ax/integration-contracts/close" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getCloseApi, CLOSE_ACCOUNT, CLOSE_ID_SCHEMA, CLOSE_PAGINATION_SCHEMA, } from "./lib" const CLOSE_COMMENT_BODY_SCHEMA = z .string() .trim() .regex( /^]*)?>[\s\S]*<\/body>$/i, "Wrap the Close rich-text comment in ....", ) /** Lists comments attached to one Close object. */ export const listCloseComments = defineAction("List Close comments") .describe("Lists comments attached to an object that Close supports.") .account(CLOSE_ACCOUNT) .input( z.object({ ...CLOSE_PAGINATION_SCHEMA, objectId: CLOSE_ID_SCHEMA, }), ) .output(closePageSchema(CLOSE_COMMENT_SCHEMA)) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getCloseApi(account.secret).request("comment/", { query: { _limit: input.limit, _skip: input.skip, object_id: input.objectId, }, responseSchema: closePageSchema(CLOSE_COMMENT_SCHEMA), }), ) /** Retrieves one Close comment. */ export const getCloseComment = defineAction("Get Close comment") .describe("Retrieves one Close comment by ID.") .account(CLOSE_ACCOUNT) .input(z.object({ commentId: CLOSE_ID_SCHEMA })) .output(CLOSE_COMMENT_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => getCloseApi(account.secret).request( `comment/${encodeURIComponent(input.commentId)}/`, { responseSchema: CLOSE_COMMENT_SCHEMA }, ), ) /** Creates a comment on a Close object. */ export const createCloseComment = defineAction("Create Close comment") .describe("Creates a rich-text comment on an object that Close supports.") .account(CLOSE_ACCOUNT) .input( z.object({ body: CLOSE_COMMENT_BODY_SCHEMA, objectId: CLOSE_ID_SCHEMA }), ) .output(CLOSE_COMMENT_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getCloseApi(account.secret).request("comment/", { body: input, method: "POST", responseSchema: CLOSE_COMMENT_SCHEMA, }), ) /** Updates a Close comment body. */ export const updateCloseComment = defineAction("Update Close comment") .describe("Replaces the body of one Close comment.") .account(CLOSE_ACCOUNT) .input( z.object({ body: CLOSE_COMMENT_BODY_SCHEMA, commentId: CLOSE_ID_SCHEMA, }), ) .output(CLOSE_COMMENT_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => getCloseApi(account.secret).request( `comment/${encodeURIComponent(input.commentId)}/`, { body: { body: input.body }, method: "PUT", responseSchema: CLOSE_COMMENT_SCHEMA, }, ), ) /** Removes a Close comment. */ export const deleteCloseComment = defineAction("Delete Close comment") .describe("Removes one Close comment.") .account(CLOSE_ACCOUNT) .input(z.object({ commentId: CLOSE_ID_SCHEMA })) .output(z.object({ commentId: z.string(), deleted: z.literal(true) })) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getCloseApi(account.secret).request( `comment/${encodeURIComponent(input.commentId)}/`, { method: "DELETE", responseSchema: CLOSE_VALUE_SCHEMA }, ) return { commentId: input.commentId, deleted: true as const } })