import {z} from 'zod' export type InviteWorkerRequestV1 = {emailAddress: string, firstName: string, gender?: ('MALE' | 'FEMALE') | undefined, initials?: string | undefined, lastName?: string | undefined, shouldNotifyWorkerOfAccountCreation?: boolean | undefined, mobileNumber: string, city?: string | undefined, dateOfBirth?: string | undefined, houseNumber?: string | undefined, houseNumberAddition?: string | undefined, iban?: string | undefined, nationality?: ('DUTCH' | 'OTHER') | undefined, middleName?: string | undefined, paymentPeriod?: ('FOUR_WEEKLY' | 'WEEKLY') | undefined, street?: string | undefined, wageTaxRebate?: boolean | undefined, zip: string, freelanceChamberOfCommerceNumber?: string | undefined, freelanceVatNumber?: string | undefined, freelanceVatType?: ('HIGH' | 'LOW' | 'SHIFTED' | 'NONE' | 'KOR') | undefined}; export type CreateApiPublicOnboardingV1InviteWorkerArgs = {body: InviteWorkerRequestV1}; export type CreateApiPublicOnboardingV1InviteWorkerOptions = {baseUrl: string, oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const inviteWorkerResponseV1 = z.object({message: z.string(), workerId: z.string()}); 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 }; /** * Invite a worker to join Maqqie * * POST /public/onboarding/v1/invite-worker */ export const createApiPublicOnboardingV1InviteWorker = async (args: CreateApiPublicOnboardingV1InviteWorkerArgs, options: CreateApiPublicOnboardingV1InviteWorkerOptions) => { 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/onboarding/v1/invite-worker${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(inviteWorkerResponseV1, await res.json()) };