mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
215 lines
5.1 KiB
TypeScript
215 lines
5.1 KiB
TypeScript
import {
|
|
CreateProjectFlockPayload,
|
|
ProjectFlock,
|
|
UpdateProjectFlockPayload,
|
|
} from '@/types/api/production/project-flock';
|
|
import { BaseApiService } from '../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<BaseGroupedApproval[]>
|
|
| ErrorApiResponse
|
|
| SuccessApiResponse<BaseGroupedApproval[]>
|
|
| undefined
|
|
> {
|
|
const path = `/approvals`;
|
|
try {
|
|
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 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<BaseApiResponse<ErrorApiResponse>>(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}/location/${locationId.toString()}/periods`;
|
|
return await httpClient<
|
|
SuccessApiResponse<
|
|
{
|
|
id: number;
|
|
name: string;
|
|
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,
|
|
notes?: string
|
|
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
|
return await this.bulkApprovalAction([id], 'APPROVED', notes);
|
|
}
|
|
|
|
/**
|
|
* Reject single Project Flock
|
|
*/
|
|
async reject(
|
|
id: number,
|
|
notes?: string
|
|
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
|
return await this.bulkApprovalAction([id], 'REJECTED', notes);
|
|
}
|
|
|
|
/**
|
|
* Approve Bulk Project Flock
|
|
*/
|
|
async bulkApprove(
|
|
ids: number[],
|
|
notes?: string
|
|
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
|
return await this.bulkApprovalAction(ids, 'APPROVED', notes);
|
|
}
|
|
|
|
/**
|
|
* Reject Bulk Project Flock
|
|
*/
|
|
async bulkReject(
|
|
ids: number[],
|
|
notes?: string
|
|
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
|
return await this.bulkApprovalAction(ids, 'REJECTED', notes);
|
|
}
|
|
|
|
/**
|
|
* Approve Bulk Project Flock
|
|
*/
|
|
async bulkApprovalAction(
|
|
ids: number[],
|
|
action: 'APPROVED' | 'REJECTED',
|
|
notes?: string
|
|
): 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: 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'
|
|
);
|