import { Events, type IPortalApi, generateTraceId, sdkLogger, } from '@portal-hq/utils' import type { NoahGetPaymentMethodsRequest, NoahGetPaymentMethodsResponse, NoahGetPayoutChannelFormResponse, NoahGetPayoutChannelsRequest, NoahGetPayoutChannelsResponse, NoahGetPayoutCountriesResponse, NoahGetPayoutQuoteRequest, NoahGetPayoutQuoteResponse, NoahInitiateKycRequest, NoahInitiateKycResponse, NoahInitiatePayinRequest, NoahInitiatePayinResponse, NoahInitiatePayoutRequest, NoahInitiatePayoutResponse, NoahSimulatePayinRequest, NoahSimulatePayinResponse, } from './types' const NoahEndpoint = { BASE_PATH: '/api/v3/clients/me/integrations/noah', kyc(): string { return `${this.BASE_PATH}/customers/kyc` }, payins(): string { return `${this.BASE_PATH}/payins` }, payinsSimulate(): string { return `${this.BASE_PATH}/payins/simulate` }, payoutCountries(): string { return `${this.BASE_PATH}/payouts/countries` }, payoutChannels(): string { return `${this.BASE_PATH}/payouts/channels` }, payoutChannelForm(channelId: string): string { const id = encodeURIComponent(channelId) return `${this.BASE_PATH}/payouts/channels/${id}/form` }, payoutQuote(): string { return `${this.BASE_PATH}/payouts/quote` }, payouts(): string { return `${this.BASE_PATH}/payouts` }, paymentMethods(): string { return `${this.BASE_PATH}/payouts/payment-methods` }, } as const export interface IPortalNoahApi { initiateKyc(request: NoahInitiateKycRequest): Promise initiatePayin( request: NoahInitiatePayinRequest, ): Promise simulatePayin( request: NoahSimulatePayinRequest, ): Promise getPayoutCountries(): Promise getPayoutChannels( request: NoahGetPayoutChannelsRequest, ): Promise getPayoutChannelForm( channelId: string, ): Promise getPayoutQuote( request: NoahGetPayoutQuoteRequest, ): Promise initiatePayout( request: NoahInitiatePayoutRequest, ): Promise getPaymentMethods( request?: NoahGetPaymentMethodsRequest, ): Promise } export interface PortalNoahApiOptions { api: IPortalApi } export class PortalNoahApi implements IPortalNoahApi { private readonly api: IPortalApi constructor(options: PortalNoahApiOptions) { this.api = options.api } public async initiateKyc( request: NoahInitiateKycRequest, ): Promise { const path = NoahEndpoint.kyc() const response = await this.post< NoahInitiateKycResponse, NoahInitiateKycRequest >(path, request) await this.trackEvent(Events.NoahInitiateKyc, { path, customerType: request.customerType, hasFiatOptions: !!request.fiatOptions, }) return response } public async initiatePayin( request: NoahInitiatePayinRequest, ): Promise { const path = NoahEndpoint.payins() const response = await this.post< NoahInitiatePayinResponse, NoahInitiatePayinRequest >(path, request) await this.trackEvent(Events.NoahInitiatePayin, { path, fiatCurrency: request.fiatCurrency, cryptoCurrency: request.cryptoCurrency, network: request.network, }) return response } public async simulatePayin( request: NoahSimulatePayinRequest, ): Promise { const path = NoahEndpoint.payinsSimulate() const response = await this.post< NoahSimulatePayinResponse, NoahSimulatePayinRequest >(path, request) await this.trackEvent(Events.NoahSimulatePayin, { path, fiatCurrency: request.fiatCurrency, fiatAmount: request.fiatAmount, }) return response } public async getPayoutCountries(): Promise { const path = NoahEndpoint.payoutCountries() const response = await this.get(path) await this.trackEvent(Events.NoahGetPayoutCountries, { path, }) return response } public async getPayoutChannels( request: NoahGetPayoutChannelsRequest, ): Promise { const queryParams = this.buildQueryParams({ country: request.country, cryptoCurrency: request.cryptoCurrency, fiatCurrency: request.fiatCurrency, fiatAmount: request.fiatAmount, paymentMethodId: request.paymentMethodId, pageSize: request.pageSize, pageToken: request.pageToken, }) const path = queryParams ? `${NoahEndpoint.payoutChannels()}?${queryParams}` : NoahEndpoint.payoutChannels() const response = await this.get(path) await this.trackEvent(Events.NoahGetPayoutChannels, { path: NoahEndpoint.payoutChannels(), country: request.country, cryptoCurrency: request.cryptoCurrency, fiatCurrency: request.fiatCurrency, hasFiatAmount: !!request.fiatAmount, pageSize: request.pageSize, hasPageToken: !!request.pageToken, hasPaymentMethodId: !!request.paymentMethodId, }) return response } public async getPayoutChannelForm( channelId: string, ): Promise { const path = NoahEndpoint.payoutChannelForm(channelId) const response = await this.get(path) await this.trackEvent(Events.NoahGetPayoutChannelForm, { path: NoahEndpoint.payoutChannelForm(channelId), channelId, }) return response } public async getPayoutQuote( request: NoahGetPayoutQuoteRequest, ): Promise { const path = NoahEndpoint.payoutQuote() const response = await this.post< NoahGetPayoutQuoteResponse, NoahGetPayoutQuoteRequest >(path, request) await this.trackEvent(Events.NoahGetPayoutQuote, { path, channelId: request.channelId, cryptoCurrency: request.cryptoCurrency, fiatAmount: request.fiatAmount, hasFiatCurrency: !!request.fiatCurrency, hasPaymentMethodId: !!request.paymentMethodId, }) return response } public async initiatePayout( request: NoahInitiatePayoutRequest, ): Promise { const path = NoahEndpoint.payouts() const response = await this.post< NoahInitiatePayoutResponse, NoahInitiatePayoutRequest >(path, request) await this.trackEvent(Events.NoahInitiatePayout, { path, payoutId: request.payoutId, network: request.network, hasTrigger: !!request.trigger, }) return response } public async getPaymentMethods( request?: NoahGetPaymentMethodsRequest, ): Promise { const queryParams = request ? this.buildQueryParams({ pageSize: request.pageSize, pageToken: request.pageToken, capability: request.capability, }) : undefined const path = queryParams ? `${NoahEndpoint.paymentMethods()}?${queryParams}` : NoahEndpoint.paymentMethods() const response = await this.get(path) await this.trackEvent(Events.NoahGetPaymentMethods, { path: NoahEndpoint.paymentMethods(), pageSize: request?.pageSize, hasPageToken: !!request?.pageToken, capability: request?.capability, hasCapability: !!request?.capability, }) return response } private buildQueryParams(params: Record): string { const queryParts: string[] = [] for (const [key, value] of Object.entries(params)) { if (value !== undefined && value !== null) { const encodedValue = encodeURIComponent(String(value)) queryParts.push(`${key}=${encodedValue}`) } } return queryParts.join('&') } private async get(path: string): Promise { return this.api.requests.get(path, { headers: { Authorization: `Bearer ${this.api.apiKey}`, Accept: 'application/json', }, traceId: generateTraceId(), }) } private async post(path: string, body: B): Promise { return this.api.requests.post(path, { headers: { Authorization: `Bearer ${this.api.apiKey}`, Accept: 'application/json', 'Content-Type': 'application/json', }, body, traceId: generateTraceId(), }) } private async trackEvent( event: string, properties: Record, ): Promise { try { await this.api.track(event, properties) } catch (error) { sdkLogger.warn(`[PortalNoahApi] Failed to track event "${event}":`, error) } } } export default PortalNoahApi