import { CreateProjectFlockPayload, ProjectFlock, UpdateProjectFlockPayload, } from '@/types/api/production/project-flock'; import { BaseApiService } from '../base'; import { BaseApiResponse, BaseGroupedApproval, ErrorApiResponse, GroupedApprovals, SuccessApiResponse, } from '@/types/api/api-general'; import { sleep } from '@/lib/helper'; import { httpClient } from '@/services/http/client'; import axios from 'axios'; import { Flock } from '@/types/api/master-data/flock'; 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 { console.log({ module_id: id, module_name: 'PROJECT_FLOCKS', group_step_number: true, }); 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(id: string): Promise< | BaseApiResponse<{ flock: Flock; next_period: number; }> | ErrorApiResponse | SuccessApiResponse<{ flock: Flock; next_period: number; }> | undefined > { try { const path = `${this.basePath}/kandangs/${id}`; return await httpClient< SuccessApiResponse<{ flock: Flock; next_period: number; }> >(path, { method: 'GET', }); } catch (error) { if (axios.isAxiosError>(error)) { return error.response?.data as ErrorApiResponse; } else { return undefined; } } } /** * Approve single Project Flock */ async approve( id: number ): Promise | undefined> { return await this.bulkApprovalAction([id], 'APPROVED'); } /** * Reject single Project Flock */ async reject( id: number ): Promise | undefined> { return await this.bulkApprovalAction([id], 'REJECTED'); } /** * Approve Bulk Project Flock */ async bulkApprove( ids: number[] ): Promise | undefined> { return await this.bulkApprovalAction(ids, 'APPROVED'); } /** * Reject Bulk Project Flock */ async bulkReject( ids: number[] ): Promise | undefined> { return await this.bulkApprovalAction(ids, 'REJECTED'); } /** * Approve Bulk Project Flock */ async bulkApprovalAction( ids: number[], action: 'APPROVED' | 'REJECTED' ): Promise | undefined> { try { const path = `${this.basePath}/approvals`; return await httpClient>(path, { method: 'POST', body: { action: action, approvable_ids: ids, 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' );