import {z} from 'zod' export type Address = {address1?: string | undefined, address2?: string | undefined, address3?: string | undefined, address4?: string | undefined, address5?: string | undefined, address6?: string | undefined, address7?: string | undefined, zip?: string | undefined, city?: string | undefined, country_code?: string | undefined}; export type CustomAgreements = {agreement_id: string, agreement_value: 0 | 1 | 2}; export type Agreement = {agreement_gtc?: (0 | 1 | 2) | undefined, agreement_profiling?: (0 | 1 | 2) | undefined, agreement_marketing_communication?: (0 | 1 | 2) | undefined, custom_agreements?: Array | undefined}; export type Consents = {consent_id: string, consent_value: 0 | 1 | 2, last_change?: string | undefined}; export type PersonalInformation = {salutation?: string | undefined, gender?: (1 | 2 | 3 | 4 | 5 | null) | undefined, first_name?: string | undefined, last_name?: string | undefined, pre_nominals?: string | undefined, post_nominals?: string | undefined, birthdate?: string | undefined, email?: string | undefined, phone?: string | undefined, language_id: string, store_id?: string | undefined, photo_url?: string | undefined, address?: Address | undefined, agreement?: Agreement | undefined, consents?: Array | undefined}; export type Customer = {customer_id?: string | undefined, personal_information: PersonalInformation, doi_email?: string | undefined, doi_phone?: string | undefined, last_change?: string | undefined, state?: (0 | 1 | 2) | undefined}; export type CustomerSourceRecord = {customer_source_record_id?: string | undefined, customer_source_id: string, customer_id?: string | undefined, external_id?: string | undefined, last_change?: string | undefined}; export type SocialNetworkCredentials = {social_network_id: string, social_network_token: string}; export type PropertyRecord = {property_record_id?: string | undefined, property_id: string, property_name?: string | undefined, property_value?: (string | number | number | boolean | Array | Record) | undefined, last_change?: string | undefined}; export type CreateApiCustomersArgs = {'Accept-Language'?: string | undefined, body: {customer: Customer, customer_source: CustomerSourceRecord, password?: string | undefined, autologin: boolean, social_network_credentials?: SocialNetworkCredentials | undefined, property_records?: Array | undefined, referral_code?: string | undefined}}; export type CreateApiCustomersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCustomersResponse = z.object({data: z.object({customer_id: z.string().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 }; /** * Create a new customer * * POST /customers * * Also accepts auth: basicAuth */ export const createApiCustomers = async (args: CreateApiCustomersArgs, options?: CreateApiCustomersOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", 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 (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers${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(createApiCustomersResponse, await res.json()) };