import {z} from 'zod' export type ResourceID = string; export type ForecastedTransactionParentType = 'ACCOUNT' | 'ENTITY'; export type ForecastedTransactionAccountRef = {id: ResourceID, type: ForecastedTransactionParentType}; export type ForecastedTransactionEntityRef = {id: ResourceID, type: ForecastedTransactionParentType}; export type ForecastedTransactionParent = ForecastedTransactionAccountRef | ForecastedTransactionEntityRef; export type CurrencyCode = string; export type AmountIntegerValueInput = {currency: CurrencyCode, value: number}; export type AmountStringValueInput = {currency: CurrencyCode, stringValue: string}; export type AmountInput = AmountIntegerValueInput | AmountStringValueInput; export type Date = string; export type ForecastedTransactionOriginType = 'USER' | 'ACCOUNTS_RECEIVABLE' | 'ACCOUNTS_PAYABLE' | 'GENERATED' | 'ACCOUNTING_DOCUMENT'; export type ForecastedTransactionOrigin = {id: ResourceID, type: ForecastedTransactionOriginType}; export type MarketCode = string; export type NationalIdentifier = {type: 'CIVIC' | 'COMPANY', market: MarketCode, number: string}; export type CountryCode = string; export type CountrySubdivision = string; export type AccountIdentifierType = 'IBAN' | 'NUMBER' | 'ADYEN' | 'AU_BPAY_BILLER_CODE' | 'DK_FIK' | 'INVESTMENT_NUMBER' | 'MY_JOMPAY_BILLER_CODE' | 'NATIONAL_ID_NUMBER' | 'PAYPAL' | 'PROPRIETARY' | 'SE_BANKGIRO' | 'SE_PLUSGIRO' | 'SOLDO' | 'WALLET_ADYEN_BALANCE_PLATFORM' | 'WALLET_AIRWALLEX' | 'WALLET_AIRWALLEX_NUMBER' | 'WALLET_CURRENCYCLOUD' | 'WALLET_FLASH_PAYMENTS' | 'WALLET_FREEMARKET' | 'WALLET_HYPERWALLET' | 'WALLET_INPAY' | 'WALLET_LEAD' | 'WALLET_MERCURY' | 'WALLET_MONEYCORP' | 'WALLET_PAYHAWK' | 'WALLET_REVOLUT' | 'WALLET_SKRILL' | 'WALLET_STRIPE' | 'WALLET_SWISSQUOTE' | 'WALLET_UNIT'; export type AccountIdentifier = {type: AccountIdentifierType, market: MarketCode, number: string, invalid?: boolean | undefined}; export type RoutingIdentifierType = 'BIC' | 'AT_BLZ' | 'AU_BSB' | 'BD_BRN' | 'BR_BRANCH' | 'BR_COMPE' | 'BR_ISP' | 'CA_CPA' | 'CH_BCC' | 'CH_SIC' | 'CL_SBIF' | 'CN_APS' | 'CZ_NBC' | 'DE_BLZ' | 'ES_NCC' | 'GB_DSC' | 'GR_BIC' | 'HK_NCC' | 'IE_NCC' | 'IL_NCC' | 'IN_FSC' | 'IT_NCC' | 'JP_ZGN' | 'KR_BOK' | 'KR_KFTC' | 'MX_ABM' | 'MY_NCC' | 'NZ_NCC' | 'PH_BRSTN' | 'PL_KNR' | 'PT_NCC' | 'RU_CBC' | 'SE_SBA' | 'SG_IBG' | 'TH_CBC' | 'TW_NCC' | 'US_ABA' | 'US_PID' | 'VN_CITAD' | 'ZA_NCC'; export type RoutingConstraints = {accountNumberType?: AccountIdentifierType | undefined, scheme?: string | undefined}; export type RoutingIdentifier = {type: RoutingIdentifierType, number: string, invalid?: boolean | undefined, constraints?: RoutingConstraints | undefined}; export type CounterpartyAccount = {market?: MarketCode | undefined, identifiers: Array, routing?: Array | undefined, rawIdentifier: string}; export type CounterpartyDetails = {partyType?: ('INDIVIDUAL' | 'COMPANY') | undefined, name?: string | undefined, nationalIdentifier?: NationalIdentifier | undefined, address?: ({country?: CountryCode | undefined, countrySubdivision?: CountrySubdivision | undefined, city?: string | undefined, postalCode?: string | undefined, streetName?: string | undefined, streetNumber?: string | undefined, rawAddressLines?: Array | undefined}) | undefined, account?: CounterpartyAccount | undefined}; export type Metadata = {[key: string]: string} | null; export type ExternalID = string; export type CreateForecastedTransactionRequest = {parent: ForecastedTransactionParent, amount: AmountInput, date: Date, description: string, origin?: ForecastedTransactionOrigin | undefined, counterparty?: CounterpartyDetails | undefined, metadata?: Metadata | undefined, externalId?: ExternalID | undefined}; export type CreateApiAnalyticsV2betaForecastedTransactionsArgs = {body: CreateForecastedTransactionRequest}; export type CreateApiAnalyticsV2betaForecastedTransactionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const resourceID = z.string().min(32).max(64); export const organizationID = z.string(); export const externalID = z.string(); export const forecastedTransactionParentType = z.enum(["ACCOUNT", "ENTITY"]); export const forecastedTransactionAccountRef = z.object({id: resourceID, type: forecastedTransactionParentType}); export const forecastedTransactionEntityRef = z.object({id: resourceID, type: forecastedTransactionParentType}); export const forecastedTransactionParent = z.discriminatedUnion("type", [forecastedTransactionAccountRef, forecastedTransactionEntityRef]); export const currencyCode = z.string().min(3).max(3); export const amount = z.object({currency: currencyCode, stringValue: z.string(), value: z.number().int()}); export const date = z.string(); export const forecastedTransactionOriginType = z.enum(["USER", "ACCOUNTS_RECEIVABLE", "ACCOUNTS_PAYABLE", "GENERATED", "ACCOUNTING_DOCUMENT"]); export const forecastedTransactionOrigin = z.object({id: resourceID, type: forecastedTransactionOriginType}); export const metadata = z.record(z.string(), z.string()).nullable(); export const marketCode = z.string().min(2).max(2); export const nationalIdentifier = z.object({type: z.enum(["CIVIC", "COMPANY"]), market: marketCode, number: z.string()}); export const countryCode = z.string().min(2).max(2); export const countrySubdivision = z.string().max(6); export const accountIdentifierType = z.enum(["IBAN", "NUMBER", "ADYEN", "AU_BPAY_BILLER_CODE", "DK_FIK", "INVESTMENT_NUMBER", "MY_JOMPAY_BILLER_CODE", "NATIONAL_ID_NUMBER", "PAYPAL", "PROPRIETARY", "SE_BANKGIRO", "SE_PLUSGIRO", "SOLDO", "WALLET_ADYEN_BALANCE_PLATFORM", "WALLET_AIRWALLEX", "WALLET_AIRWALLEX_NUMBER", "WALLET_CURRENCYCLOUD", "WALLET_FLASH_PAYMENTS", "WALLET_FREEMARKET", "WALLET_HYPERWALLET", "WALLET_INPAY", "WALLET_LEAD", "WALLET_MERCURY", "WALLET_MONEYCORP", "WALLET_PAYHAWK", "WALLET_REVOLUT", "WALLET_SKRILL", "WALLET_STRIPE", "WALLET_SWISSQUOTE", "WALLET_UNIT"]); export const accountIdentifier = z.object({type: accountIdentifierType, market: marketCode, number: z.string().min(1).max(255), invalid: z.boolean().optional()}); export const routingIdentifierType = z.enum(["BIC", "AT_BLZ", "AU_BSB", "BD_BRN", "BR_BRANCH", "BR_COMPE", "BR_ISP", "CA_CPA", "CH_BCC", "CH_SIC", "CL_SBIF", "CN_APS", "CZ_NBC", "DE_BLZ", "ES_NCC", "GB_DSC", "GR_BIC", "HK_NCC", "IE_NCC", "IL_NCC", "IN_FSC", "IT_NCC", "JP_ZGN", "KR_BOK", "KR_KFTC", "MX_ABM", "MY_NCC", "NZ_NCC", "PH_BRSTN", "PL_KNR", "PT_NCC", "RU_CBC", "SE_SBA", "SG_IBG", "TH_CBC", "TW_NCC", "US_ABA", "US_PID", "VN_CITAD", "ZA_NCC"]); export const routingConstraints = z.object({accountNumberType: accountIdentifierType.optional(), scheme: z.string().optional()}); export const routingIdentifier = z.object({type: routingIdentifierType, number: z.string().min(1).max(255), invalid: z.boolean().optional(), constraints: routingConstraints.optional()}); export const counterpartyAccount = z.object({market: marketCode.optional(), identifiers: z.array(accountIdentifier), routing: z.array(routingIdentifier).optional(), rawIdentifier: z.string()}); export const counterpartyDetails = z.object({partyType: z.enum(["INDIVIDUAL", "COMPANY"]).optional(), name: z.string().optional(), nationalIdentifier: nationalIdentifier.optional(), address: z.object({country: countryCode.optional(), countrySubdivision: countrySubdivision.optional(), city: z.string().max(64).optional(), postalCode: z.string().max(16).optional(), streetName: z.string().max(64).optional(), streetNumber: z.string().max(16).optional(), rawAddressLines: z.array(z.string().max(255)).optional()}).optional(), account: counterpartyAccount.optional()}); export const createdTimestamp = z.string(); export const updatedTimestamp = z.string(); export const resourceVersion = z.number().int().gte(1); export const eTag = z.string(); export const forecastedTransaction = z.object({id: resourceID, organizationId: organizationID, scenarioId: resourceID, externalId: externalID.optional(), parent: forecastedTransactionParent, accountId: z.string().optional(), amount: amount, date: date, description: z.string(), origin: forecastedTransactionOrigin, metadata: metadata.optional(), counterparty: counterpartyDetails.optional(), created: createdTimestamp, updated: updatedTimestamp, version: resourceVersion, etag: eTag}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create forecasted transaction * * POST /analytics/v2beta/forecasted-transactions */ export const createApiAnalyticsV2betaForecastedTransactions = async (args: CreateApiAnalyticsV2betaForecastedTransactionsArgs, options?: CreateApiAnalyticsV2betaForecastedTransactionsOptions) => { const {baseUrl = "https://api.atlar.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/analytics/v2beta/forecasted-transactions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(forecastedTransaction, await res.json()) };