mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-21 05:45:46 +00:00
refactor(FE): Refactor project flock api & implement approval component in project flock details
This commit is contained in:
@@ -4,10 +4,19 @@ import {
|
||||
UpdateProjectFlockPayload,
|
||||
} from '@/types/api/production/project-flock';
|
||||
import { BaseApiService } from '../base';
|
||||
import { BaseApiResponse } from '@/types/api/api-general';
|
||||
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,
|
||||
@@ -17,14 +26,183 @@ export class ProjectFlockService extends BaseApiService<
|
||||
constructor(basePath: string = '') {
|
||||
super(basePath);
|
||||
}
|
||||
async getSingleProjectFlockKandang(): Promise<BaseApiResponse | undefined> {
|
||||
/**
|
||||
* Get Approval Lines
|
||||
*/
|
||||
async getApprovalLines(
|
||||
id: number
|
||||
): Promise<
|
||||
| BaseApiResponse<BaseGroupedApproval[]>
|
||||
| ErrorApiResponse
|
||||
| SuccessApiResponse<BaseGroupedApproval[]>
|
||||
| undefined
|
||||
> {
|
||||
const path = `/approvals`;
|
||||
try {
|
||||
console.log({
|
||||
module_id: id,
|
||||
module_name: 'PROJECT_FLOCKS',
|
||||
group_step_number: true,
|
||||
});
|
||||
return await httpClient<SuccessApiResponse<BaseGroupedApproval[]>>(path, {
|
||||
method: 'GET',
|
||||
query: {
|
||||
module_id: id,
|
||||
module_name: 'PROJECT_FLOCKS',
|
||||
group_step_number: true,
|
||||
},
|
||||
} as RequestOptions);
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError<BaseApiResponse>(error)) {
|
||||
return error.response?.data;
|
||||
return error.response?.data as ErrorApiResponse;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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<BaseApiResponse<ErrorApiResponse>>(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<BaseApiResponse<ErrorApiResponse>>(error)) {
|
||||
return error.response?.data as ErrorApiResponse;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve single Project Flock
|
||||
*/
|
||||
async approve(
|
||||
id: number
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
return await this.bulkApprovalAction([id], 'APPROVED');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject single Project Flock
|
||||
*/
|
||||
async reject(
|
||||
id: number
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
return await this.bulkApprovalAction([id], 'REJECTED');
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve Bulk Project Flock
|
||||
*/
|
||||
async bulkApprove(
|
||||
ids: number[]
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
return await this.bulkApprovalAction(ids, 'APPROVED');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject Bulk Project Flock
|
||||
*/
|
||||
async bulkReject(
|
||||
ids: number[]
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
return await this.bulkApprovalAction(ids, 'REJECTED');
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve Bulk Project Flock
|
||||
*/
|
||||
async bulkApprovalAction(
|
||||
ids: number[],
|
||||
action: 'APPROVED' | 'REJECTED'
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
try {
|
||||
const path = `${this.basePath}/approvals`;
|
||||
return await httpClient<BaseApiResponse<{ message: string }>>(path, {
|
||||
method: 'POST',
|
||||
body: {
|
||||
action: action,
|
||||
approvable_ids: ids,
|
||||
notes: `Bulk ${action} Project Flock ${ids.join(', ')}`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError<BaseApiResponse<{ message: string }>>(error)) {
|
||||
return error.response?.data;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const ProjectFlockApi = new ProjectFlockService(
|
||||
'/production/project-flocks'
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user