import {z} from 'zod' export type MembershipFrequency = 'DAILY' | 'WEEKLY' | 'BIWEEKLY' | 'MONTHLY' | 'QUARTERLY' | 'SEMI_ANNUALLY' | 'YEARLY'; export type MembershipDiscount = {id?: string | undefined, membershipId: string, itemId?: (string | null) | undefined, itemCategory?: (string | null) | undefined, isPhysical?: (boolean | null) | undefined, discountPercentage?: (number | null) | undefined, discountAmount?: (number | null) | undefined, creditAmount?: (number | null) | undefined, quantity?: (number | null) | undefined, rollover?: boolean | undefined, rolloverExpiryDays?: (number | null) | undefined, neverExpire?: boolean | undefined, group?: (string | null) | undefined, isArchived?: boolean | undefined}; export type MembershipDetail = {id?: string | undefined, externalId?: (string | null) | undefined, name: string, description?: (string | null) | undefined, price: number, globalDiscountPercentage?: (number | null) | undefined, patientCreditAmount?: number | undefined, patientCreditFrequency?: MembershipFrequency | undefined, patientCreditExpiryDays?: (number | null) | undefined, maxRenewals?: (number | null) | undefined, autoRenew?: boolean | undefined, setupFee?: number | undefined, freezeFee?: number | undefined, minimumBillingCycles?: number | undefined, billingFrequency?: MembershipFrequency | undefined, textColor?: (string | null) | undefined, isArchived?: boolean | undefined, ruleSetId?: (string | null) | undefined, discounts?: (Array | null) | undefined, memberCount?: (number | null) | undefined, totalRevenue?: (number | null) | undefined}; export type UpdateApiInventoryMembershipsMembershipIdArgs = {TENANT: string, 'API-KEY': string, membership_id: string, body: MembershipDetail}; export type UpdateApiInventoryMembershipsMembershipIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const membershipFrequency = z.enum(["DAILY", "WEEKLY", "BIWEEKLY", "MONTHLY", "QUARTERLY", "SEMI_ANNUALLY", "YEARLY"]); export const membershipDiscount = z.object({id: z.string().optional(), membershipId: z.string(), itemId: z.string().nullable().optional(), itemCategory: z.string().nullable().optional(), isPhysical: z.boolean().nullable().optional(), discountPercentage: z.number().nullable().optional(), discountAmount: z.number().int().nullable().optional(), creditAmount: z.number().int().nullable().optional(), quantity: z.number().int().nullable().optional(), rollover: z.boolean().optional(), rolloverExpiryDays: z.number().int().nullable().optional(), neverExpire: z.boolean().optional(), group: z.string().nullable().optional(), isArchived: z.boolean().optional()}); export const membershipDetail = z.object({id: z.string().optional(), externalId: z.string().nullable().optional(), name: z.string(), description: z.string().nullable().optional(), price: z.number().int(), globalDiscountPercentage: z.number().nullable().optional(), patientCreditAmount: z.number().int().optional(), patientCreditFrequency: membershipFrequency.optional(), patientCreditExpiryDays: z.number().int().nullable().optional(), maxRenewals: z.number().int().nullable().optional(), autoRenew: z.boolean().optional(), setupFee: z.number().int().optional(), freezeFee: z.number().int().optional(), minimumBillingCycles: z.number().int().optional(), billingFrequency: membershipFrequency.optional(), textColor: z.string().nullable().optional(), isArchived: z.boolean().optional(), ruleSetId: z.string().nullable().optional(), discounts: z.array(membershipDiscount).nullable().optional(), memberCount: z.number().int().nullable().optional(), totalRevenue: z.number().int().nullable().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 }; /** * Update Membership * * PUT /inventory/memberships/{membership_id} */ export const updateApiInventoryMembershipsMembershipId = async (args: UpdateApiInventoryMembershipsMembershipIdArgs, options: UpdateApiInventoryMembershipsMembershipIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) const url = `${baseUrl.replace(/\/+$/, '')}/inventory/memberships/${encodeURIComponent(String(args.membership_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(membershipDetail, await res.json()) };