import { CreateProjectFlockPayload, ProjectFlock, UpdateProjectFlockPayload, } from '@/types/api/production/project-flock'; import { BaseApiService } from '@/services/api/base'; import { BaseApiResponse, BaseGroupedApproval, ErrorApiResponse, SuccessApiResponse, } from '@/types/api/api-general'; import { httpClient } from '@/services/http/client'; import axios from 'axios'; import { Kandang } from '@/types/api/master-data/kandang'; import { RequestOptions } from '@/services/http/base'; export class ProjectFlockService extends BaseApiService< ProjectFlock, CreateProjectFlockPayload, UpdateProjectFlockPayload > { constructor(basePath: string = '') { super(basePath); } /** * Get Approval Lines */ async getApprovalLines( id: number ): Promise< | BaseApiResponse | ErrorApiResponse | SuccessApiResponse | undefined > { const path = `/approvals`; try { return await httpClient>(path, { method: 'GET', query: { module_id: id, module_name: 'PROJECT_FLOCKS', group_step_number: true, }, } as RequestOptions); } catch (error) { if (axios.isAxiosError(error)) { return error.response?.data as ErrorApiResponse; } else { return undefined; } } } /** * Lookup for Project Flock Kandang */ async lookupProjectFlockKandang( projectFlockId: number, kandangId: number ): Promise< | BaseApiResponse< | ErrorApiResponse | SuccessApiResponse<{ id: number; kandang_id: Kandang; project_flock: ProjectFlock; available_quantity: number; }> > | undefined > { try { const path = `${this.basePath}/kandangs/lookup`; return await httpClient< BaseApiResponse< SuccessApiResponse<{ id: number; kandang_id: Kandang; project_flock: ProjectFlock; available_quantity: number; }> > >(path, { method: 'GET', body: { project_flock_id: projectFlockId, kandang_id: kandangId, }, }); } catch (error) { if (axios.isAxiosError>(error)) { return error.response?.data; } else { return undefined; } } } /** * Get Next Period of Project Flock */ async getNextPeriod(locationId: number): Promise< | BaseApiResponse< { id: number; name: string; period: number; }[] > | ErrorApiResponse | SuccessApiResponse< { id: number; name: string; period: number; }[] > | undefined > { try { const path = `${this.basePath}/locations/${locationId.toString()}/periods`; return await httpClient< SuccessApiResponse< { id: number; name: string; period: number; }[] > >(path, { method: 'GET', }); } catch (error) { if (axios.isAxiosError>(error)) { return error.response?.data as ErrorApiResponse; } else { return undefined; } } } /** * Resubmit Project Flock */ async resubmit( id: number, payload: UpdateProjectFlockPayload ): Promise | undefined> { try { const updatePath = `${this.basePath}/${id}/resubmit`; const headers = { 'Content-Type': 'application/json', ...(this.header ?? {}), }; const updateRes = await httpClient>( updatePath, { method: 'PUT', body: payload, headers, } ); return updateRes; } catch (error: unknown) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } /** * Approve single Project Flock */ async approve( id: number, notes?: string ): Promise | undefined> { return await this.bulkApprovalAction([id], 'APPROVED', notes); } /** * Reject single Project Flock */ async reject( id: number, notes?: string ): Promise | undefined> { return await this.bulkApprovalAction([id], 'REJECTED', notes); } /** * Approve Bulk Project Flock */ async bulkApprove( ids: number[], notes?: string ): Promise | undefined> { return await this.bulkApprovalAction(ids, 'APPROVED', notes); } /** * Reject Bulk Project Flock */ async bulkReject( ids: number[], notes?: string ): Promise | undefined> { return await this.bulkApprovalAction(ids, 'REJECTED', notes); } /** * Approve Bulk Project Flock */ async bulkApprovalAction( ids: number[], action: 'APPROVED' | 'REJECTED', notes?: string ): Promise | undefined> { try { const path = `${this.basePath}/approvals`; return await httpClient>(path, { method: 'POST', body: { action: action, approvable_ids: ids, notes: notes ?? `Bulk ${action} Project Flock ${ids.join(', ')}`, }, }); } catch (error) { if (axios.isAxiosError>(error)) { return error.response?.data; } else { return undefined; } } } } export const ProjectFlockApi = new ProjectFlockService( '/production/project-flocks' );