import {z} from 'zod' export type AttachmentTypeFilterValueDto = {name: 'UNASSIGNED' | 'CV'} | {id: string}; export type DateISO = string; export type AttachmentCustomAttributePathDto = string; export type AppliedAttachmentCustomTextFilterDto = {type: 'text', operator: 'contains' | 'equals' | 'startsWith' | 'notEquals', path: AttachmentCustomAttributePathDto, value: string}; export type AppliedAttachmentCustomDateFilterDto = {type: 'date', operator: 'lessThanOrEqual' | 'greaterThanOrEqual' | 'equals' | 'notEquals', path: AttachmentCustomAttributePathDto, value: DateISO}; export type AppliedAttachmentCustomEmptyDateFilterDto = {type: 'date', operator: 'isEmpty' | 'isNotEmpty', path: AttachmentCustomAttributePathDto}; export type AppliedAttachmentCustomDateRangeFilterDto = {type: 'date', operator: 'between', path: AttachmentCustomAttributePathDto, value: {from: DateISO, to: DateISO}}; export type AppliedAttachmentCustomNumberFilterDto = {type: 'number', operator: 'greaterThanOrEqual' | 'lessThanOrEqual', path: AttachmentCustomAttributePathDto, value: number}; export type AppliedAttachmentCustomNumberRangeFilterDto = {type: 'number', operator: 'between', path: AttachmentCustomAttributePathDto, value: {from: number, to: number}}; export type AppliedAttachmentCustomEntitySelectFilterDto = {type: 'entitySelect', operator: 'in' | 'notIn', path: AttachmentCustomAttributePathDto, value: string | null}; export type AppliedAttachmentCustomEntityMultiSelectFilterDto = {type: 'entitySelect', operator: 'in' | 'notIn' | 'isAllOf', path: AttachmentCustomAttributePathDto, value: Array}; export type AppliedAttachmentCustomBooleanFilterDto = {type: 'boolean', operator: 'equals' | 'notEquals', path: AttachmentCustomAttributePathDto, value: boolean}; export type AppliedAttachmentCustomEnumSelectFilterDto = {type: 'enumSelect', operator: 'in' | 'notIn' | 'isAllOf', path: AttachmentCustomAttributePathDto, value: Array}; export type AppliedAttachmentCustomEnumListSelectFilterDto = {type: 'enumListSelect', operator: 'contains' | 'doesNotContain', path: AttachmentCustomAttributePathDto, value: string | null}; export type DmCurrency = 'AED' | 'AFN' | 'ALL' | 'AMD' | 'ANG' | 'AOA' | 'ARS' | 'AUD' | 'AWG' | 'AZN' | 'BAM' | 'BBD' | 'BDT' | 'BGN' | 'BHD' | 'BIF' | 'BMD' | 'BND' | 'BOB' | 'BRL' | 'BSD' | 'BTC' | 'BTN' | 'BWP' | 'BYN' | 'BZD' | 'CAD' | 'CDF' | 'CHF' | 'CLF' | 'CLP' | 'CNH' | 'CNY' | 'COP' | 'CRC' | 'CUC' | 'CUP' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EGP' | 'ERN' | 'ETB' | 'EUR' | 'FJD' | 'FKP' | 'GBP' | 'GEL' | 'GGP' | 'GHS' | 'GIP' | 'GMD' | 'GNF' | 'GTQ' | 'GYD' | 'HKD' | 'HNL' | 'HRK' | 'HTG' | 'HUF' | 'IDR' | 'ILS' | 'IMP' | 'INR' | 'IQD' | 'IRR' | 'ISK' | 'JEP' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KGS' | 'KHR' | 'KMF' | 'KPW' | 'KRW' | 'KWD' | 'KYD' | 'KZT' | 'LAK' | 'LBP' | 'LKR' | 'LRD' | 'LSL' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MNT' | 'MOP' | 'MRU' | 'MUR' | 'MVR' | 'MWK' | 'MXN' | 'MYR' | 'MZN' | 'NAD' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'NZD' | 'OMR' | 'PAB' | 'PEN' | 'PGK' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RON' | 'RSD' | 'RUB' | 'RWF' | 'SAR' | 'SBD' | 'SCR' | 'SDG' | 'SEK' | 'SGD' | 'SHP' | 'SLE' | 'SLL' | 'SOS' | 'SRD' | 'SSP' | 'STD' | 'STN' | 'SVC' | 'SYP' | 'SZL' | 'THB' | 'TJS' | 'TMT' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TWD' | 'TZS' | 'UAH' | 'UGX' | 'USD' | 'UYU' | 'UZS' | 'VEF' | 'VES' | 'VND' | 'VUV' | 'WST' | 'XAF' | 'XAG' | 'XAU' | 'XCD' | 'XCG' | 'XDR' | 'XOF' | 'XPD' | 'XPF' | 'XPT' | 'YER' | 'ZAR' | 'ZMW' | 'ZWG' | 'ZWL'; export type AppliedAttachmentCustomCurrencyFilterDto = {type: 'currency', operator: 'greaterThanOrEqual' | 'lessThanOrEqual', path: AttachmentCustomAttributePathDto, value: {amount: number, currency: DmCurrency}}; export type AppliedAttachmentCustomDurationFilterDto = {type: 'duration', operator: 'greaterThanOrEqual' | 'lessThanOrEqual', path: AttachmentCustomAttributePathDto, value: number}; export type AppliedAttachmentCustomUrlFilterDto = {type: 'url', operator: 'equals' | 'notEquals' | 'contains' | 'startsWith', path: AttachmentCustomAttributePathDto, value: string}; export type AppliedAttachmentCustomTimeStampFilterDto = {type: 'timestamp', operator: 'greaterThanOrEqual' | 'lessThanOrEqual', path: AttachmentCustomAttributePathDto, value: DateISO}; export type AppliedAttachmentCustomRatingFilterDto = {type: 'rating', operator: 'greaterThanOrEqual' | 'lessThanOrEqual' | 'equals', path: AttachmentCustomAttributePathDto, value: number}; export type AppliedAttachmentFilterDto = {type: 'entitySelect', operator: 'in' | 'notIn', path: 'attachment.candidate' | 'attachment.vacancy' | 'attachment.client', value: Array} | {type: 'enumSelect', operator: 'in' | 'notIn', path: 'attachment.type', value: Array} | {type: 'date', operator: 'lessThanOrEqual' | 'greaterThanOrEqual' | 'equals' | 'notEquals', path: 'attachment.expiryDate' | 'attachment.validFrom', value: DateISO} | {type: 'date', operator: 'isEmpty' | 'isNotEmpty', path: 'attachment.expiryDate' | 'attachment.validFrom'} | {type: 'date', operator: 'between', path: 'attachment.expiryDate' | 'attachment.validFrom', value: {from: DateISO, to: DateISO}} | AppliedAttachmentCustomTextFilterDto | AppliedAttachmentCustomDateFilterDto | AppliedAttachmentCustomEmptyDateFilterDto | AppliedAttachmentCustomDateRangeFilterDto | AppliedAttachmentCustomNumberFilterDto | AppliedAttachmentCustomNumberRangeFilterDto | AppliedAttachmentCustomEntitySelectFilterDto | AppliedAttachmentCustomEntityMultiSelectFilterDto | AppliedAttachmentCustomBooleanFilterDto | AppliedAttachmentCustomEnumSelectFilterDto | AppliedAttachmentCustomEnumListSelectFilterDto | AppliedAttachmentCustomCurrencyFilterDto | AppliedAttachmentCustomDurationFilterDto | AppliedAttachmentCustomUrlFilterDto | AppliedAttachmentCustomTimeStampFilterDto | AppliedAttachmentCustomRatingFilterDto; export type AppliedAttachmentSortDto = {sortId: 'attachment.createdAt', sortDirection: 'ASC' | 'DESC', attributePath: 'attachment.createdAt', sortType: 'predefined'}; export type SearchAttachmentsDto = {filters?: Array | undefined, sort?: AppliedAttachmentSortDto | undefined, page?: number | undefined, pageSize?: number | undefined}; export type CreateApiAttachmentsSearchArgs = {body: SearchAttachmentsDto}; export type CreateApiAttachmentsSearchOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const searchAttachmentsPaginationDto = z.object({hasNextPage: z.boolean()}); export const searchAttachmentTypeDto = z.union([z.object({id: z.string(), name: z.string()}), z.object({name: z.enum(["UNASSIGNED", "CV"])})]); export const dateISO = z.string(); export const idDto = z.object({id: z.string().min(1)}); export const attachmentLinksDto = z.object({candidates: z.array(idDto), clients: z.array(idDto), clientContacts: z.array(idDto), vacancies: z.array(idDto), opportunities: z.array(idDto), placements: z.array(idDto), applications: z.array(idDto)}); export const attachmentSearchItemDto = z.object({attachmentId: z.string(), attachmentName: z.string().nullable(), type: searchAttachmentTypeDto.nullable(), createdAt: dateISO, extension: z.string().nullable(), size: z.number().nullable(), links: attachmentLinksDto}); export const searchAttachmentsResponseDto = z.object({pagination: searchAttachmentsPaginationDto, items: z.array(attachmentSearchItemDto)}); 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 }; /** * Search attachments * * POST /attachments/_search */ export const createApiAttachmentsSearch = async (args: CreateApiAttachmentsSearchArgs, options: CreateApiAttachmentsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/attachments/_search${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(searchAttachmentsResponseDto, await res.json()) };