import {z} from 'zod' export type Ownership = 'Personal' | 'Organization'; export type PhoneType = 'Mobile' | 'Landline'; export type Phone = {Ownership?: Ownership | undefined, Type?: PhoneType | undefined, Value?: (string | null) | undefined}; export type Email = {Ownership?: Ownership | undefined, Value?: (string | null) | undefined}; export type Address = {Ownership?: Ownership | undefined, Address1?: (string | null) | undefined, Address2?: (string | null) | undefined, Address3?: (string | null) | undefined, Address4?: (string | null) | undefined, Address5?: (string | null) | undefined, PostCode?: (string | null) | undefined}; export type CreateEmployeeCommand = {DisplayId?: (string | null) | undefined, TitleHonorific?: (string | null) | undefined, FirstName: string | null, KnownAs?: (string | null) | undefined, OtherName?: (string | null) | undefined, LastName: string | null, CostCentre?: (string | null) | undefined, NationalInsuranceNumber?: (string | null) | undefined, PayrollId?: (string | null) | undefined, TaxCode?: (string | null) | undefined, IncludeInPayroll?: (boolean | null) | undefined, EmploymentStartDate?: (string | null) | undefined, EmploymentLeftDate?: (string | null) | undefined, ContinuousServiceDate?: (string | null) | undefined, DateOfBirth?: (string | null) | undefined, LastWorkingDate?: (string | null) | undefined, Gender?: (string | null) | undefined, Ethnicity?: (string | null) | undefined, Nationality?: (string | null) | undefined, Religion?: (string | null) | undefined, LeaverReason?: (string | null) | undefined, MaritalStatus?: (string | null) | undefined, Phones?: (Array | null) | undefined, Emails?: (Array | null) | undefined, Addresses?: (Array
| null) | undefined, GenderIdentity?: (string | null) | undefined, WindowsUsername?: (string | null) | undefined, Initials?: (string | null) | undefined}; export type CreateApiEmployeesArgs = {body: CreateEmployeeCommand}; export type CreateApiEmployeesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createdEntitiyResponseModel = z.object({id: 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 }; /** * Creates a new Employee. * * POST /employees */ export const createApiEmployees = async (args: CreateApiEmployeesArgs, options?: CreateApiEmployeesOptions) => { const {baseUrl = "https://api.iris.co.uk/hr/v2", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/employees${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(createdEntitiyResponseModel, await res.json()) };