import { RESEND_CONTACT_TOPIC_SCHEMA, RESEND_CONTACT_TOPICS_UPDATE_SCHEMA, RESEND_CONTACT_TOPICS_UPDATE_WIRE_SCHEMA, resendListSchema, resendListWireSchema, } from "@automate.ax/integration-contracts/resend" import * as z from "zod" import { defineAction } from "../../automation/actions" import { getResendApi, RESEND_FULL_ACCESS_ACCOUNT_OPTIONS } from "./lib" const RESEND_ACCOUNT = "resend" const CONTACT_TOPIC_LIST_SCHEMA = resendListSchema(RESEND_CONTACT_TOPIC_SCHEMA) const CONTACT_TOPIC_LIST_WIRE_SCHEMA = resendListWireSchema( RESEND_CONTACT_TOPIC_SCHEMA, ) const PAGE_SCHEMA = z .object({ after: z.string().min(1).optional(), before: z.string().min(1).optional(), contactId: z.string().min(1), limit: z.number().int().min(1).max(100).prefault(20), }) .refine(({ after, before }) => !(after && before), { message: "after and before cannot be used together.", }) /** Lists topic subscriptions for one Resend contact. */ export const listResendContactTopics = defineAction( "List Resend contact topics", ) .describe("Lists the topic subscription choices for one contact.") .account(RESEND_ACCOUNT, RESEND_FULL_ACCESS_ACCOUNT_OPTIONS) .input(PAGE_SCHEMA) .output(CONTACT_TOPIC_LIST_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { contactId, ...query } = input return await getResendApi(account).call( `/contacts/${encodeURIComponent(contactId)}/topics`, { query, responseSchema: CONTACT_TOPIC_LIST_WIRE_SCHEMA }, ) }) /** Replaces selected topic subscriptions for one Resend contact. */ export const updateResendContactTopics = defineAction( "Update Resend contact topics", ) .describe("Updates one or more topic subscription choices for a contact.") .account(RESEND_ACCOUNT, RESEND_FULL_ACCESS_ACCOUNT_OPTIONS) .input( z.object({ contactId: z.string().min(1), topics: z .object({ id: z.string().min(1), subscription: z.enum(["opt_in", "opt_out"]), }) .array() .min(1), }), ) .output(RESEND_CONTACT_TOPICS_UPDATE_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { return { contactId: ( await getResendApi(account).call( `/contacts/${encodeURIComponent(input.contactId)}/topics`, { body: input.topics, httpMethod: "PATCH", responseSchema: RESEND_CONTACT_TOPICS_UPDATE_WIRE_SCHEMA, }, ) ).id, object: "contact_topics" as const, topics: input.topics, } })