import {z} from 'zod' export type OrganisationDistrictAttributesDto = {districtId?: number | undefined, managerId?: number | undefined}; export type OrganisationSectionAttributesDto = {sectionId?: number | undefined, managerGroupId?: number | undefined, costCentre?: number | undefined, managerId?: number | undefined}; export type OrganisationAddressDto = {country?: string | undefined, address?: string | undefined, zipCode?: string | undefined, city?: string | undefined, municipality?: string | undefined, timeZone?: string | undefined}; export type OrganisationShiftRulesDto = {maxHoursPerDay?: number | undefined, maxHoursPerWeek?: number | undefined, minRest?: number | undefined, numberOfBreaks?: number | undefined}; export type OrganisationUnitAttributesDto = {unitId?: number | undefined, address?: OrganisationAddressDto | undefined, costCentre?: number | undefined, managerId?: number | undefined, currency?: string | undefined, publishedTo?: string | undefined, blockPasswordLogin?: boolean | undefined, shiftRules?: OrganisationShiftRulesDto | undefined, shiftRulesDto?: OrganisationShiftRulesDto | undefined}; export type OrganisationGroupCreationRequest = {name?: string | undefined, groupType?: string | undefined, parentGroupId: number, districtAttributesDto?: OrganisationDistrictAttributesDto | undefined, sectionAttributesDto?: OrganisationSectionAttributesDto | undefined, unitAttributesDto?: OrganisationUnitAttributesDto | undefined}; export type CreateApiV2OrganisationGroupsArgs = {body: OrganisationGroupCreationRequest}; export type CreateApiV2OrganisationGroupsOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const organisationDistrictAttributesDto = z.object({districtId: z.number().int().optional(), managerId: z.number().int().optional()}); export const organisationSectionAttributesDto = z.object({sectionId: z.number().int().optional(), managerGroupId: z.number().int().optional(), costCentre: z.number().int().optional(), managerId: z.number().int().optional()}); export const organisationAddressDto = z.object({country: z.string().optional(), address: z.string().optional(), zipCode: z.string().optional(), city: z.string().optional(), municipality: z.string().optional(), timeZone: z.string().optional()}); export const organisationShiftRulesDto = z.object({maxHoursPerDay: z.number().optional(), maxHoursPerWeek: z.number().optional(), minRest: z.number().int().optional(), numberOfBreaks: z.number().int().optional()}); export const organisationUnitAttributesDto = z.object({unitId: z.number().int().optional(), address: organisationAddressDto.optional(), costCentre: z.number().int().optional(), managerId: z.number().int().optional(), currency: z.string().optional(), publishedTo: z.string().optional(), blockPasswordLogin: z.boolean().optional(), shiftRules: organisationShiftRulesDto.optional(), shiftRulesDto: organisationShiftRulesDto.optional()}); export const organisationGroupDetailsResponseDto = z.object({id: z.number().int().optional(), name: z.string().optional(), groupType: z.enum(["domain", "district", "unit", "section"]).optional(), parentGroupId: z.number().int().optional(), districtAttributesDto: organisationDistrictAttributesDto.optional(), sectionAttributesDto: organisationSectionAttributesDto.optional(), unitAttributesDto: organisationUnitAttributesDto.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 }; /** * Create group * * POST /v2/organisation/groups */ export const createApiV2OrganisationGroups = async (args: CreateApiV2OrganisationGroupsArgs, options?: CreateApiV2OrganisationGroupsOptions) => { const {baseUrl = "https://api.quinyx.com", 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(/\/+$/, '')}/v2/organisation/groups${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(organisationGroupDetailsResponseDto, await res.json()) };