/* eslint-disable @typescript-eslint/no-explicit-any */ import type { EmployeeRequestProfileSummary, RequestEmployeeStatistics, RequestVacationStatsPeriod, RequestVacationTermsContext, User } from '@/types/api'; import type { RequestFn } from './utils'; export function createRequestsApi(request: RequestFn) { async function formDataRequest(url: string, method: string, data: Record, saveAs?: 'draft' | 'submit') { const fd = new FormData(); Object.keys(data).forEach((key) => { if (key === 'attachments' && Array.isArray(data[key])) { data[key].forEach((f: File) => fd.append('attachments', f)); } else if (key === 'attachment' && data[key] instanceof File) { fd.append('attachment', data[key]); } else if (Array.isArray(data[key])) { data[key].forEach((v: any) => fd.append(key, String(v))); } else if (data[key] !== null && data[key] !== undefined) { fd.append(key, String(data[key])); } }); if (saveAs) fd.append('save_as', saveAs); return request(url, { method, body: fd, timeoutMs: 120_000 }); } return { getRequests: (params?: Record) => { const qp = new URLSearchParams(); if (params) { const keys = ['status','type','search','page','limit','view','addressed_to_me','decision_required','employee_id','created_from','created_to','date_from','date_to']; keys.forEach(k => { if (params[k]) qp.append(k, String(params[k])); }); } const qs = qp.toString(); return request(`/api/v1/requests/${qs ? '?' + qs : ''}`); }, getRequest: (requestId: number) => request(`/api/v1/requests/${requestId}/`), markRequestNotificationsRead: (requestId: number): Promise<{ status: string; count: number; notification_ids: number[] }> => request(`/api/v1/requests/${requestId}/notifications/read/`, { method: 'POST' }), getRequestEmployeeStatistics: (requestId: number, period: 'all' | 'year' | 'month' = 'all'): Promise => request(`/api/v1/requests/${requestId}/employee-statistics/?period=${period}`), getEmployeeRequestStatistics: (employeeId: number, period: 'all' | 'year' | 'month' = 'all'): Promise => request(`/api/v1/requests/statistics/?employee_id=${employeeId}&period=${period}`), getEmployeeRequestProfile: (employeeId: number, period: 'all' | 'year' | 'month' = 'all', limit = 5, page = 1): Promise => request(`/api/v1/requests/employee-profile/?employee_id=${employeeId}&period=${period}&limit=${limit}&page=${page}`), getRequestRecipientOptions: (): Promise<{ restricted: boolean; results: User[] }> => request('/api/v1/requests/recipient-options/'), createRequest: (data: Record, saveAs?: 'draft' | 'submit') => formDataRequest('/api/v1/requests/', 'POST', data, saveAs), updateRequest: (requestId: number, data: Record, saveAs?: 'draft' | 'submit') => formDataRequest(`/api/v1/requests/${requestId}/`, 'PATCH', data, saveAs), deleteRequest: (requestId: number): Promise => request(`/api/v1/requests/${requestId}/`, { method: 'DELETE' }), getRequestComments: (requestId: number) => request(`/api/v1/requests/${requestId}/comments/`), deleteRequestComment: (requestId: number, commentId: number): Promise => request(`/api/v1/requests/${requestId}/comments/${commentId}/`, { method: 'DELETE' }), approveRequest: (requestId: number) => request(`/api/v1/requests/${requestId}/approve/`, { method: 'POST' }), getRequestVacationTerms: (requestId: number, period: RequestVacationStatsPeriod = 'half_year'): Promise => request(`/api/v1/requests/${requestId}/vacation-terms/?period=${period}`), saveRequestVacationTerms: (requestId: number, payload: { paid_days: number; record_to_finance: boolean }, period: RequestVacationStatsPeriod = 'half_year'): Promise => request(`/api/v1/requests/${requestId}/vacation-terms/?period=${period}`, { method: 'PUT', body: JSON.stringify(payload) }), rejectRequest: (requestId: number) => request(`/api/v1/requests/${requestId}/reject/`, { method: 'POST' }), cancelRequest: (requestId: number) => request(`/api/v1/requests/${requestId}/cancel/`, { method: 'POST' }), addRequestComment: (requestId: number, text: string) => request(`/api/v1/requests/${requestId}/comments/`, { method: 'POST', body: JSON.stringify({ text }) }), }; }