import {z} from 'zod' export type TimePeriod = {startDateTime?: string | undefined, endDateTime?: string | undefined}; export type IndividualIdentification = {identificationId?: string | undefined, identificationType?: string | undefined}; export type Characteristic = {name: string, valueType?: string | undefined, value: string}; export type MediumCharacteristic = {street1?: string | undefined, street2?: string | undefined, suburb?: string | undefined, city?: string | undefined, postCode?: string | undefined, stateOrProvince?: string | undefined, country?: string | undefined, addressLine1?: string | undefined, geocode?: string | undefined, subDoorNumber?: string | undefined, genericAddress?: string | undefined, phoneNumber?: string | undefined, xpos?: number | undefined, ypos?: number | undefined}; export type ContactMedium = {mediumType: string, characteristic: MediumCharacteristic}; export type PartyRef = {'@referredType': string, id: string, href?: string | undefined, name?: string | undefined, role?: string | undefined, individualIdentification?: Array | undefined, partyCharacteristic?: Array | undefined, contactMedium?: Array | undefined, op?: string | undefined}; export type CycleSpecification = {name?: string | undefined, preferredCurrency?: string | undefined, reportLevel?: string | undefined, suppressBilling?: string | undefined, suppressCycles?: string | undefined, suppressUntilDate?: string | undefined, transferredAccountNumber?: string | undefined, billEndDate?: string | undefined}; export type PresentationMedia = {name?: string | undefined}; export type Format = {name?: string | undefined, presentationMedia?: PresentationMedia | undefined, characteristic?: Array | undefined}; export type BillStructure = {cycleSpecification?: CycleSpecification | undefined, format?: Array | undefined}; export type PaymentPlan = {paymentMethod?: string | undefined, paymentLocation?: string | undefined, autoPay?: string | undefined}; export type Money = {unit?: string | undefined, value?: number | undefined}; export type DunningCase = {id?: string | undefined, name?: string | undefined, href?: string | undefined, status?: string | undefined, currentAmountDue?: Money | undefined}; export type AccountRef = {id?: string | undefined, name: string, accountNumber?: string | undefined, accountBalance?: string | undefined, href?: string | undefined, accountType?: string | undefined, creditLimit?: string | undefined, availableCredit?: string | undefined, billStructure?: BillStructure | undefined, paymentPlan?: PaymentPlan | undefined, dunningCase?: DunningCase | undefined}; export type CreditProfile = {creditProfileDate: string, creditRiskRating?: string | undefined, creditLimit?: string | undefined, creditLimitCurrency?: string | undefined, expectedRevenue?: string | undefined, expRevCurrency?: string | undefined, creditComments?: string | undefined, taxClass?: string | undefined, creditScore?: string | undefined, validFor: TimePeriod}; export type PaymentMethodRef = {'@type'?: ('bankAccountDebit' | 'bankCard') | undefined, details?: Record | undefined, id: string, href?: string | undefined, name?: string | undefined}; export type CaseRef = {id?: string | undefined, href?: string | undefined, status?: string | undefined, name?: string | undefined}; export type CustomerCreate = {name: string, status?: string | undefined, validFor?: TimePeriod | undefined, engagedParty: Array, characteristic?: Array | undefined, relatedParty?: Array | undefined, account?: Array | undefined, creditProfile?: CreditProfile | undefined, contactMedium?: Array | undefined, paymentMethod?: Array | undefined, case?: CaseRef | undefined}; export type CreateApiCustomerArgs = {'Ignored-Warning'?: string | undefined, body: CustomerCreate}; export type CreateApiCustomerOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const caseRef = z.object({id: z.string().optional(), href: z.string().optional(), status: z.string().optional(), name: z.string().optional()}); export const timePeriod = z.object({startDateTime: z.string().optional(), endDateTime: z.string().optional()}); export const individualIdentification = z.object({identificationId: z.string().optional(), identificationType: z.string().optional()}); export const characteristic = z.object({name: z.string(), valueType: z.string().optional(), value: z.string()}); export const mediumCharacteristic = z.object({street1: z.string().optional(), street2: z.string().optional(), suburb: z.string().optional(), city: z.string().optional(), postCode: z.string().optional(), stateOrProvince: z.string().optional(), country: z.string().optional(), addressLine1: z.string().optional(), geocode: z.string().optional(), subDoorNumber: z.string().optional(), genericAddress: z.string().optional(), phoneNumber: z.string().optional(), xpos: z.number().optional(), ypos: z.number().optional()}); export const contactMedium = z.object({mediumType: z.string(), characteristic: mediumCharacteristic}); export const partyRef = z.object({"@referredType": z.string(), id: z.string(), href: z.string().optional(), name: z.string().optional(), role: z.string().optional(), individualIdentification: z.array(individualIdentification).optional(), partyCharacteristic: z.array(characteristic).optional(), contactMedium: z.array(contactMedium).optional(), op: z.string().optional()}); export const cycleSpecification = z.object({name: z.string().optional(), preferredCurrency: z.string().optional(), reportLevel: z.string().optional(), suppressBilling: z.string().optional(), suppressCycles: z.string().optional(), suppressUntilDate: z.string().optional(), transferredAccountNumber: z.string().optional(), billEndDate: z.string().optional()}); export const presentationMedia = z.object({name: z.string().optional()}); export const format = z.object({name: z.string().optional(), presentationMedia: presentationMedia.optional(), characteristic: z.array(characteristic).optional()}); export const billStructure = z.object({cycleSpecification: cycleSpecification.optional(), format: z.array(format).optional()}); export const paymentPlan = z.object({paymentMethod: z.string().optional(), paymentLocation: z.string().optional(), autoPay: z.string().optional()}); export const money = z.object({unit: z.string().optional(), value: z.number().optional()}); export const dunningCase = z.object({id: z.string().optional(), name: z.string().optional(), href: z.string().optional(), status: z.string().optional(), currentAmountDue: money.optional()}); export const accountRef = z.object({id: z.string().optional(), name: z.string(), accountNumber: z.string().optional(), accountBalance: z.string().optional(), href: z.string().optional(), accountType: z.string().optional(), creditLimit: z.string().optional(), availableCredit: z.string().optional(), billStructure: billStructure.optional(), paymentPlan: paymentPlan.optional(), dunningCase: dunningCase.optional()}); export const creditProfile = z.object({creditProfileDate: z.string(), creditRiskRating: z.string().optional(), creditLimit: z.string().optional(), creditLimitCurrency: z.string().optional(), expectedRevenue: z.string().optional(), expRevCurrency: z.string().optional(), creditComments: z.string().optional(), taxClass: z.string().optional(), creditScore: z.string().optional(), validFor: timePeriod}); export const paymentMethodRef = z.object({"@type": z.enum(["bankAccountDebit", "bankCard"]).optional(), details: z.object({}).optional(), id: z.string(), href: z.string().optional(), name: z.string().optional()}); export const taxDefinition = z.object({taxType: z.string().optional()}); export const taxExemptionCertificate = z.object({id: z.string().optional(), documentId: z.string().optional(), description: z.string().optional(), validFor: timePeriod.optional(), taxDefinition: z.array(taxDefinition).optional()}); export const treatmentExemption = z.object({id: z.string().optional(), exemption: z.string().optional(), description: z.string().optional()}); export const treatmentSuspension = z.object({suspensionReasonCode: z.string().optional(), suspensionEndDate: z.string().optional(), id: z.string().optional(), suspensionDuration: z.string().optional()}); export const customer = z.object({id: z.string(), href: z.string(), case: caseRef.optional(), name: z.string().optional(), status: z.string().optional(), statusChangeReason: z.string().optional(), defaultDisplayAccount: z.string().optional(), validFor: timePeriod.optional(), engagedParty: z.array(partyRef).optional(), characteristic: z.array(characteristic).optional(), relatedParty: z.array(partyRef).optional(), account: z.array(accountRef).optional(), creditProfile: creditProfile.optional(), contactMedium: z.array(contactMedium).optional(), paymentMethod: z.array(paymentMethodRef).optional(), taxExemptionCertificate: z.array(taxExemptionCertificate).optional(), treatmentExemption: treatmentExemption.optional(), treatmentSuspension: treatmentSuspension.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 }; /** * Creates a Customer * * POST /customer * * Also accepts auth: OAuth2 */ export const createApiCustomer = async (args: CreateApiCustomerArgs, options?: CreateApiCustomerOptions) => { const {baseUrl = "https://serverRoot/tmf-api/customerManagement/v4", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json;charset=utf-8") headers.set('accept', "application/json;charset=utf-8") if (args['Ignored-Warning'] != null) headers.set("Ignored-Warning", String(args['Ignored-Warning'])) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/customer${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(customer, await res.json()) };