import {z} from 'zod' export type DayAvailabilityTimeFrame = {startInMin: number, endInMin: number, preferred?: boolean | undefined}; export type DayAvailabilityDetailDto = {isOn: boolean, day: number, timeFrames?: Array | undefined}; export type UpdatePublicDayAvailabilityRequest = {fromDate?: number | undefined, toDate?: number | undefined, note: string, preferredMinHoursPerWeek?: number | undefined, preferredMaxHoursPerWeek?: number | undefined, preferredMaxHoursPerPayPeriod?: number | undefined, preferredMaxShiftsPerDay?: number | undefined, preferredMaxShiftsPerWeek?: number | undefined, preferredMaxWeekDays?: number | undefined, preferredMaxWeekendDays?: number | undefined, preferredMaxRestingHours?: number | undefined, preferredTeamIds?: Array | undefined, preferredLocationIds?: Array | undefined, preferredSkillIds?: Array | undefined, allowReplacementsOutsideAvailabilities?: boolean | undefined, approverId?: string | undefined, days?: Array | undefined}; export type PatchApiV1PublicAvailabilitiesWeekdaysAvailabilityIdArgs = {availabilityId: string, body: UpdatePublicDayAvailabilityRequest}; export type PatchApiV1PublicAvailabilitiesWeekdaysAvailabilityIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const approval = z.object({status: z.string(), approverId: z.string(), refuserId: z.string(), approverCode: z.string(), createdAt: z.number(), updatedAt: z.number(), updatedByMemberId: z.string(), updatedByMemberCode: z.string(), reason: z.string()}); export const dayAvailabilityDetail = z.object({}); export const simplifiedTeam = z.object({_id: z.string(), names: z.record(z.string(), z.string()), code: z.string()}); export const simplifiedSkill = z.object({_id: z.string(), names: z.record(z.string(), z.string()), code: z.string()}); export const locData = z.object({abbrev: z.string(), code: z.string(), name: z.string(), address: z.string(), geo: z.object({})}); export const simplifiedLocation = z.object({_id: z.string(), locData: locData}); export const publicDayAvailability = z.object({uid: z.string(), allowReplacementsOutsideAvailabilities: z.boolean().optional(), approval: approval, createdAt: z.number(), createdByMemberId: z.string(), createdByMemberCode: z.string(), fromDate: z.number().optional(), groupId: z.string(), memberId: z.string(), memberCode: z.string(), note: z.string(), preferredLocationIds: z.array(z.string()).optional(), preferredLocationCodes: z.array(z.string()).optional(), preferredMaxHoursPerPayPeriod: z.number().optional(), preferredMaxHoursPerWeek: z.number().optional(), preferredMaxRestingHours: z.number().optional(), preferredMaxShiftsPerDay: z.number().optional(), preferredMaxShiftsPerWeek: z.number().optional(), preferredMaxWeekDays: z.number().optional(), preferredMaxWeekendDays: z.number().optional(), preferredMinHoursPerWeek: z.number().optional(), preferredSkillIds: z.array(z.string()).optional(), preferredSkillCodes: z.array(z.string()).optional(), preferredTeamIds: z.array(z.string()).optional(), preferredTeamCodes: z.array(z.string()).optional(), toDate: z.number().optional(), days: z.array(dayAvailabilityDetail).optional(), preferredTeams: z.array(simplifiedTeam).optional(), preferredSkills: z.array(simplifiedSkill).optional(), preferredLocations: z.array(simplifiedLocation).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 weekdays availabilities * * PATCH /v1/public/availabilities/weekdays/{availabilityId} */ export const patchApiV1PublicAvailabilitiesWeekdaysAvailabilityId = async (args: PatchApiV1PublicAvailabilitiesWeekdaysAvailabilityIdArgs, options?: PatchApiV1PublicAvailabilitiesWeekdaysAvailabilityIdOptions) => { const {baseUrl = "https://api.evolia.com", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/public/availabilities/weekdays/${encodeURIComponent(String(args.availabilityId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(publicDayAvailability, await res.json()) };