import {z} from 'zod' export type Partner = number; export type BusinessUnitId = number; export type DocumentNumber = number; export type DueDate = string; export type FineDate = string; export type PrincipalValue = number; export type RebateValue = number; export type CollectionOrderProductType = '0 - Ammount' | '1 - Percentage'; export type ReceiverContactInfo = {Mail?: string | undefined, Phone?: string | undefined}; export type ReceiverAccountInfo = {PixKey?: string | undefined, TaxNumber: string, Bank?: string | undefined, BankBranch?: string | undefined, BankAccount?: string | undefined, BankAccountDigit?: string | undefined}; export type Receiver = Array<{SplitValue: number, Name: string, Nickname?: string | undefined, BirthDate?: string | undefined, LegalName?: string | undefined, TradingName?: string | undefined, ConstitutionDate?: string | undefined, ReceiverContactInfo: ReceiverContactInfo, ReceiverAccountInfo: ReceiverAccountInfo}>; export type Receivers = {CollectionOrderProductType: CollectionOrderProductType, Receiver: Receiver}; export type Changes = {DueDate?: DueDate | undefined, FineDate?: FineDate | undefined, PrincipalValue?: PrincipalValue | undefined, RebateValue?: RebateValue | undefined, Receivers?: Receivers | undefined}; export type CreateApiChangeCollectionOrderArgs = {body: {Method: string, PartnerId: Partner, BusinessUnitId: BusinessUnitId, DocumentNumber: DocumentNumber, Changes: Changes}}; export type CreateApiChangeCollectionOrderOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiChangeCollectionOrderResponse = z.object({Success: z.string().optional(), Message: z.string().optional(), Validation: z.array(z.object({Key: z.string().optional(), Value: z.array(z.string()).optional()})).optional(), ErrorCode: z.number().int().optional()}); 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 }; /** * Change Collection Order * * POST /ChangeCollectionOrder */ export const createApiChangeCollectionOrder = async (args: CreateApiChangeCollectionOrderArgs, options?: CreateApiChangeCollectionOrderOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, BASE64UsernamePassword} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BASE64UsernamePassword !== undefined) headers.set('authorization', `Basic ${btoa(`${BASE64UsernamePassword.username}:${BASE64UsernamePassword.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/ChangeCollectionOrder${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(createApiChangeCollectionOrderResponse, await res.json()) };