import {z} from 'zod' export type CurrencyCode = string; export type RoundedMonetaryAmount = {amount: string, currency: CurrencyCode}; export type AccountCode = string; export type FieldAssignments = Array<{field_id: string, field_value_id: string}>; export type JournalEntryItemRequest = {description?: string | undefined, amount: RoundedMonetaryAmount, account_code: AccountCode, side: 'DEBIT' | 'CREDIT', fields?: FieldAssignments | undefined, vat_code?: string | undefined, vat_type?: string | undefined}; export type ExchangeRate = {base: CurrencyCode, target: CurrencyCode, rate: string, date: string}; export type JournalEntryRelatedEntity = {id: string, type: 'CUSTOMER' | 'VENDOR'}; export type CreateJournalEntryRequest = {name: string, currency: CurrencyCode, items: Array, date: string, reversal_date?: string | undefined, attachmentUrl?: string | undefined, exchange_rate?: ExchangeRate | undefined, subsidiary_id?: string | undefined, related_entity?: JournalEntryRelatedEntity | undefined, book_id?: string | undefined}; export type CreateApiJournalEntriesArgs = {body: CreateJournalEntryRequest}; export type CreateApiJournalEntriesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string(); export const journalEntryRelatedEntity = z.object({id: z.string(), type: z.enum(["CUSTOMER", "VENDOR"])}); export const roundedMonetaryAmount = z.object({amount: z.string(), currency: currencyCode}); export const accountCode = z.string(); export const fieldAssignments = z.array(z.object({field_id: z.string(), field_value_id: z.string()})); export const journalEntryItem = z.object({id: z.string(), description: z.string().optional(), amount: roundedMonetaryAmount, original_transaction_amount: roundedMonetaryAmount.nullable().optional(), account_id: z.string(), account_code: accountCode, side: z.enum(["DEBIT", "CREDIT"]), fields: fieldAssignments.optional()}); export const exchangeRate = z.object({base: currencyCode, target: currencyCode, rate: z.string(), date: z.string()}); export const journalEntry = z.object({id: z.string(), book_id: z.string(), subsidiary_id: z.string().optional(), name: z.string(), currency: currencyCode, related_entity: journalEntryRelatedEntity.optional(), items: z.array(journalEntryItem), date: z.string(), reversal_date: z.string().optional(), attachmentUrl: z.string().optional(), exchange_rate: exchangeRate.optional(), updated_at: z.string()}); 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 }; /** * Creates a journal entry * * POST /journal-entries */ export const createApiJournalEntries = async (args: CreateApiJournalEntriesArgs, options?: CreateApiJournalEntriesOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/journal-entries${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(journalEntry, await res.json()) };