import {z} from 'zod' export type GetApiOrganisationsVatVrnFinancialDetailsPenaltyChargeReferenceArgs = {vrn: string, penaltyChargeReference: string, 'Gov-Test-Scenario'?: string | undefined}; export type GetApiOrganisationsVatVrnFinancialDetailsPenaltyChargeReferenceOptions = {baseUrl?: string | undefined, userRestricted?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOrganisationsVatVrnFinancialDetailsPenaltyChargeReferenceResponse = z.object({totalisations: z.object({totalOverdue: z.number().optional(), totalNotYetDue: z.number().optional(), totalBalance: z.number().optional(), totalCredit: z.number().optional(), totalCleared: z.number().optional(), additionalReceivableTotalisations: z.object({totalAccountPostedInterest: z.number().optional(), totalAccountAccruingInterest: z.number().optional()}).optional()}).optional(), documentDetails: z.array(z.object({postingDate: z.string().optional(), issueDate: z.string().optional(), documentInterestTotals: z.object({interestPostedAmount: z.number().optional(), interestAccruingAmount: z.number().optional()}).optional(), documentTotalAmount: z.number().optional(), documentClearedAmount: z.number().optional(), documentOutstandingAmount: z.number().optional(), lineItemDetails: z.array(z.object({chargeDescription: z.string().optional(), periodFromDate: z.string().optional(), periodToDate: z.string().optional(), netDueDate: z.string().optional(), amount: z.number().optional(), lineItemInterestDetails: z.object({interestStartDate: z.string().optional(), currentInterestRate: z.number().optional(), interestPostedAmount: z.number().optional(), interestAccruingAmount: z.number().optional()}).optional()})).optional()})).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 }; /** * Retrieve financial details * * GET /organisations/vat/{vrn}/financial-details/{penaltyChargeReference} */ export const getApiOrganisationsVatVrnFinancialDetailsPenaltyChargeReference = async (args: GetApiOrganisationsVatVrnFinancialDetailsPenaltyChargeReferenceArgs, options?: GetApiOrganisationsVatVrnFinancialDetailsPenaltyChargeReferenceOptions) => { const {baseUrl = "https://test-api.service.hmrc.gov.uk", fetch: fetchFn = fetch, userRestricted} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Gov-Test-Scenario'] != null) headers.set("Gov-Test-Scenario", String(args['Gov-Test-Scenario'])) if (userRestricted !== undefined) headers.set('authorization', `Bearer ${userRestricted}`) const url = `${baseUrl.replace(/\/+$/, '')}/organisations/vat/${encodeURIComponent(String(args.vrn))}/financial-details/${encodeURIComponent(String(args.penaltyChargeReference))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiOrganisationsVatVrnFinancialDetailsPenaltyChargeReferenceResponse, await res.json()) };