import {z} from 'zod' export type SubscribeTeamToPlan = {userId?: (number | null) | undefined, nextPurchaseAt?: (string | null) | undefined}; export type CreateApiApiV1TeamsTeamIdPlansPlanIdSubscribeArgs = {teamId: number, planId: number, body: SubscribeTeamToPlan}; export type CreateApiApiV1TeamsTeamIdPlansPlanIdSubscribeOptions = {baseUrl?: string | undefined, BearerAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionState = z.enum(["pending", "active", "paused", "cancelling", "cancelled", "unpaid", "other"]); export const subscriptionValidityPeriod = z.object({from: z.string().nullable().optional(), to: z.string().nullable().optional()}); export const subscriptionCustomerType = z.enum(["team", "charge-point", "operator", "other"]); export const planServiceType = z.enum(["custom", "tax-refund", "charge-package"]); export const subscriptionCustomAdditionalFeeApplicationScope = z.enum(["all", "paid"]); export const subscriptionServiceConfig = z.union([z.object({serviceType: planServiceType, payoutPerKwh: z.number(), priceIncludesVat: z.boolean(), additionalFeeAbsolute: z.number().nullable(), roamingAdditionalFeeAbsolute: z.number().nullable().optional()}), z.object({serviceType: planServiceType, subscriptionAdditionalFeeApplicationScope: subscriptionCustomAdditionalFeeApplicationScope.nullable(), additionalFeePercentage: z.number().int(), additionalFeeAbsolute: z.number(), additionalStartingFee: z.number(), roamingAdditionalFeePercentage: z.number().int().nullable().optional(), roamingAdditionalFeeAbsolute: z.number().nullable().optional(), roamingAdditionalStartingFee: z.number().nullable().optional()})]); export const subscription = z.object({id: z.number().int().optional(), state: subscriptionState, nextPurchaseAt: z.string().nullable().optional(), cancelledAt: z.string().nullable().optional(), endAt: z.string().nullable().optional(), validityPeriod: subscriptionValidityPeriod.nullable().optional(), discountPercentage: z.number().optional(), discountAbsolute: z.number().optional(), discountAmount: z.number().optional(), originalAmount: z.number().optional(), totalAmount: z.number().optional(), purchaseCount: z.number().int().optional(), chargePoints: z.number().int().optional(), customerId: z.number().int().optional(), customerType: subscriptionCustomerType, planId: z.number().int().optional(), serviceConfig: subscriptionServiceConfig.nullable().optional(), createdAt: z.string(), updatedAt: z.string(), canCancel: z.boolean().optional(), note: z.string().nullable().optional(), deletedAt: z.string().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 }; /** * Subscribe a team to a plan * * POST /api/v1/teams/{teamId}/plans/{planId}/subscribe */ export const createApiApiV1TeamsTeamIdPlansPlanIdSubscribe = async (args: CreateApiApiV1TeamsTeamIdPlansPlanIdSubscribeArgs, options?: CreateApiApiV1TeamsTeamIdPlansPlanIdSubscribeOptions) => { const {baseUrl = "https://partner-api.monta.com", fetch: fetchFn = fetch, BearerAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAccessToken !== undefined) headers.set('authorization', `Bearer ${BearerAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/teams/${encodeURIComponent(String(args.teamId))}/plans/${encodeURIComponent(String(args.planId))}/subscribe${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(subscription, await res.json()) };