import {z} from 'zod' export type PageRequestV1 = {number?: number | undefined, size?: number | undefined}; export type GetRegistrationsRequestV1 = {startDate?: string | undefined, endDate?: string | undefined, workerId?: string | undefined, placementId?: string | undefined, page?: PageRequestV1 | undefined}; export type CreateApiPublicRegistrationsV1GetRegistrationsArgs = {body: GetRegistrationsRequestV1}; export type CreateApiPublicRegistrationsV1GetRegistrationsOptions = {baseUrl: string, oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const registrationLineStatusV1 = z.enum(["REGISTERED", "FOR_APPROVAL", "COMPANY_REGISTERED", "APPROVED", "REJECTED", "EXPIRED"]); export const registrationLineWageComponentV1 = z.enum(["REGULAR_HOURS", "SURCHARGE", "OVERTIME", "TRAVEL_ALLOWANCE", "GROSS_COMPENSATION", "OTHER"]); export const registrationLineV1 = z.object({id: z.string(), placementId: z.string(), workerId: z.string(), date: z.string(), status: registrationLineStatusV1, wageComponent: registrationLineWageComponentV1, wageComponentDescription: z.string().optional(), quantity: z.number(), unit: z.enum(["HOUR", "KM", "DAY", "EURO"]), startTime: z.string().optional(), endTime: z.string().optional(), breakDuration: z.string().optional(), surchargeFactor: z.number(), price: z.number().optional(), pricePerUnit: z.number().optional(), wageAmount: z.number().optional(), wagePerUnit: z.number().optional()}); export const registrationV1 = z.object({registrationId: z.string(), lines: z.array(registrationLineV1)}); export const pageV1 = z.object({number: z.number().int(), size: z.number().int(), totalElements: z.number().int(), totalPages: z.number().int()}); export const getRegistrationsResponseV1 = z.object({registrations: z.array(registrationV1), page: pageV1}); 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 }; /** * Get registrations for the calling company * * POST /public/registrations/v1/get-registrations */ export const createApiPublicRegistrationsV1GetRegistrations = async (args: CreateApiPublicRegistrationsV1GetRegistrationsArgs, options: CreateApiPublicRegistrationsV1GetRegistrationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oauth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/registrations/v1/get-registrations${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(getRegistrationsResponseV1, await res.json()) };