refactor(FE-212): add DeletePurchaseRequestItemPayload and implement PurchaseDeleteItemsService

This commit is contained in:
rstubryan
2025-11-17 10:13:30 +07:00
parent 69a8899cac
commit 30ed70b669
6 changed files with 82 additions and 20 deletions
@@ -102,17 +102,17 @@ const PurchaseOrderAcceptApprovalForm = ({
// ===== SUBMISSION HANDLERS ===== // ===== SUBMISSION HANDLERS =====
const createAcceptApprovalHandler = useCallback( const createAcceptApprovalHandler = useCallback(
async (payload: CreateAcceptApprovalRequestPayload) => { async (payload: CreateAcceptApprovalRequestPayload) => {
const purchaseRequisitionId = searchParams.get('purchaseId') const purchaseRequestId = searchParams.get('purchaseId')
? parseInt(searchParams.get('purchaseId')!) ? parseInt(searchParams.get('purchaseId')!)
: initialValues?.id || 1; : initialValues?.id || 1;
if (!purchaseRequisitionId) { if (!purchaseRequestId) {
setPurchaseOrderFormErrorMessage('Purchase Requisition ID is required'); setPurchaseOrderFormErrorMessage('Purchase Request ID is required');
return; return;
} }
const res = await AcceptApprovalApi.acceptApproval( const res = await AcceptApprovalApi.acceptApproval(
purchaseRequisitionId, purchaseRequestId,
payload payload
); );
@@ -81,6 +81,10 @@ export type PurchaseAcceptApprovalItemSchema = {
transport_total: number | string; transport_total: number | string;
}; };
export type PurchaseDeleteItemsSchema = {
item_ids: number[];
};
const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema<PurchaseStaffApprovalItemSchema> = const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema<PurchaseStaffApprovalItemSchema> =
Yup.object({ Yup.object({
purchase_item: Yup.object({ purchase_item: Yup.object({
@@ -249,6 +253,20 @@ export const PurchaseRequestStaffApprovalFormSchema: Yup.ObjectSchema<PurchaseRe
.typeError('Item pembelian wajib diisi!'), .typeError('Item pembelian wajib diisi!'),
}); });
export const PurchaseDeleteItemsSchema: Yup.ObjectSchema<PurchaseDeleteItemsSchema> =
Yup.object({
item_ids: Yup.array()
.of(
Yup.number()
.min(1, 'Item ID tidak valid!')
.required('Item ID tidak valid!')
.typeError('Item ID tidak valid!')
)
.min(1, 'Minimal harus ada 1 item yang dihapus!')
.required('Item yang dihapus wajib diisi!')
.typeError('Item yang dihapus wajib diisi!'),
});
export const PurchaseRequestStaffApprovalFormInitialValues: PurchaseRequestStaffApprovalFormSchemaType = export const PurchaseRequestStaffApprovalFormInitialValues: PurchaseRequestStaffApprovalFormSchemaType =
{ {
notes: '', notes: '',
@@ -368,3 +386,11 @@ export const PurchaseRequestAcceptApprovalFormDefaultValues = (
export type PurchaseRequestAcceptApprovalFormValues = Yup.InferType< export type PurchaseRequestAcceptApprovalFormValues = Yup.InferType<
typeof PurchaseRequestAcceptApprovalFormSchema typeof PurchaseRequestAcceptApprovalFormSchema
>; >;
export const PurchaseDeleteItemsInitialValues: PurchaseDeleteItemsSchema = {
item_ids: [],
};
export type PurchaseDeleteItemsFormValues = Yup.InferType<
typeof PurchaseDeleteItemsSchema
>;
@@ -79,17 +79,17 @@ const PurchaseOrderStaffApprovalForm = ({
// ===== SUBMISSION HANDLERS ===== // ===== SUBMISSION HANDLERS =====
const createStaffApprovalHandler = useCallback( const createStaffApprovalHandler = useCallback(
async (payload: CreateStaffApprovalRequestPayload) => { async (payload: CreateStaffApprovalRequestPayload) => {
const purchaseRequisitionId = searchParams.get('purchaseId') const purchaseRequestId = searchParams.get('purchaseId')
? parseInt(searchParams.get('purchaseId')!) ? parseInt(searchParams.get('purchaseId')!)
: initialValues?.id || 1; : initialValues?.id || 1;
if (!purchaseRequisitionId) { if (!purchaseRequestId) {
setPurchaseOrderFormErrorMessage('Purchase Requisition ID is required'); setPurchaseOrderFormErrorMessage('Purchase Request ID is required');
return; return;
} }
const res = await StaffApprovalApi.createStaffApproval( const res = await StaffApprovalApi.createStaffApproval(
purchaseRequisitionId, purchaseRequestId,
payload payload
); );
@@ -318,17 +318,17 @@ const PurchaseOrderDetail = ({
// ===== SUBMISSION HANDLER ===== // ===== SUBMISSION HANDLER =====
const createManagerApprovalHandler = useCallback( const createManagerApprovalHandler = useCallback(
async (payload: CreateManagerApprovalRequestPayload) => { async (payload: CreateManagerApprovalRequestPayload) => {
const purchaseRequisitionId = searchParams.get('purchaseId') const purchaseRequestId = searchParams.get('purchaseId')
? parseInt(searchParams.get('purchaseId')!) ? parseInt(searchParams.get('purchaseId')!)
: purchaseData?.id || 1; : purchaseData?.id || 1;
if (!purchaseRequisitionId) { if (!purchaseRequestId) {
toast.error('Purchase Requisition ID is required'); toast.error('Purchase Request ID is required');
return; return;
} }
const res = await ManagerApprovalApi.createManagerApproval( const res = await ManagerApprovalApi.createManagerApproval(
purchaseRequisitionId, purchaseRequestId,
payload payload
); );
@@ -377,6 +377,10 @@ const PurchaseOrderDetail = ({
}; };
const purchaseOrderColumns: ColumnDef<PurchaseItem>[] = [ const purchaseOrderColumns: ColumnDef<PurchaseItem>[] = [
{
header: 'No',
cell: (props) => props.row.index + 1,
},
{ {
accessorKey: 'product.name', accessorKey: 'product.name',
header: 'Produk', header: 'Produk',
+36 -8
View File
@@ -5,6 +5,7 @@ import {
CreateStaffApprovalRequestPayload, CreateStaffApprovalRequestPayload,
CreateManagerApprovalRequestPayload, CreateManagerApprovalRequestPayload,
CreateAcceptApprovalRequestPayload, CreateAcceptApprovalRequestPayload,
DeletePurchaseRequestItemPayload,
} from '@/types/api/purchase/purchase'; } from '@/types/api/purchase/purchase';
import { BaseApiService } from '@/services/api/base'; import { BaseApiService } from '@/services/api/base';
import { BaseApiResponse } from '@/types/api/api-general'; import { BaseApiResponse } from '@/types/api/api-general';
@@ -18,18 +19,18 @@ export const PurchaseRequestApi = new BaseApiService<
export class StaffApprovalService extends BaseApiService< export class StaffApprovalService extends BaseApiService<
Purchase, Purchase,
CreateStaffApprovalRequestPayload, CreateStaffApprovalRequestPayload,
CreateAcceptApprovalRequestPayload unknown
> { > {
constructor(basePath: string = '') { constructor(basePath: string = '') {
super(basePath); super(basePath);
} }
async createStaffApproval( async createStaffApproval(
purchaseRequisitionId: number, purchaseRequestId: number,
payload: CreateStaffApprovalRequestPayload payload: CreateStaffApprovalRequestPayload
): Promise<BaseApiResponse<{ message: string }> | undefined> { ): Promise<BaseApiResponse<{ message: string }> | undefined> {
return await this.customRequest<BaseApiResponse<{ message: string }>>( return await this.customRequest<BaseApiResponse<{ message: string }>>(
`${purchaseRequisitionId}/approvals/staff`, `${purchaseRequestId}/approvals/staff`,
{ {
method: 'POST', method: 'POST',
payload, payload,
@@ -48,11 +49,11 @@ export class ManagerApprovalService extends BaseApiService<
} }
async createManagerApproval( async createManagerApproval(
purchaseRequisitionId: number, purchaseRequestId: number,
payload: CreateManagerApprovalRequestPayload payload: CreateManagerApprovalRequestPayload
): Promise<BaseApiResponse<{ message: string }> | undefined> { ): Promise<BaseApiResponse<{ message: string }> | undefined> {
return await this.customRequest<BaseApiResponse<{ message: string }>>( return await this.customRequest<BaseApiResponse<{ message: string }>>(
`${purchaseRequisitionId}/approvals/manager`, `${purchaseRequestId}/approvals/manager`,
{ {
method: 'POST', method: 'POST',
payload, payload,
@@ -64,18 +65,41 @@ export class ManagerApprovalService extends BaseApiService<
export class AcceptApprovalService extends BaseApiService< export class AcceptApprovalService extends BaseApiService<
Purchase, Purchase,
CreateAcceptApprovalRequestPayload, CreateAcceptApprovalRequestPayload,
CreateAcceptApprovalRequestPayload unknown
> { > {
constructor(basePath: string = '') { constructor(basePath: string = '') {
super(basePath); super(basePath);
} }
async acceptApproval( async acceptApproval(
purchaseRequisitionId: number, purchaseRequestId: number,
payload: CreateAcceptApprovalRequestPayload payload: CreateAcceptApprovalRequestPayload
): Promise<BaseApiResponse<{ message: string }> | undefined> { ): Promise<BaseApiResponse<{ message: string }> | undefined> {
return await this.customRequest<BaseApiResponse<{ message: string }>>( return await this.customRequest<BaseApiResponse<{ message: string }>>(
`${purchaseRequisitionId}/approvals/receipts`, `${purchaseRequestId}/approvals/receipts`,
{
method: 'POST',
payload,
}
);
}
}
export class PurchaseDeleteItemsService extends BaseApiService<
Purchase,
DeletePurchaseRequestItemPayload,
unknown
> {
constructor(basePath: string = '') {
super(basePath);
}
async deleteItems(
purchaseRequestId: number,
payload: DeletePurchaseRequestItemPayload
): Promise<BaseApiResponse<{ message: string }> | undefined> {
return await this.customRequest<BaseApiResponse<{ message: string }>>(
`${purchaseRequestId}/items`,
{ {
method: 'POST', method: 'POST',
payload, payload,
@@ -89,3 +113,7 @@ export const StaffApprovalApi = new StaffApprovalService('/purchases');
export const ManagerApprovalApi = new ManagerApprovalService('/purchases'); export const ManagerApprovalApi = new ManagerApprovalService('/purchases');
export const AcceptApprovalApi = new AcceptApprovalService('/purchases'); export const AcceptApprovalApi = new AcceptApprovalService('/purchases');
export const PurchaseDeleteItemsApi = new PurchaseDeleteItemsService(
'/purchases'
);
+4
View File
@@ -87,4 +87,8 @@ export type CreateAcceptApprovalRequestPayload = {
}[]; }[];
}; };
export type DeletePurchaseRequestItemPayload = {
item_ids: number[];
};
export type UpdatePurchaseRequestPayload = CreatePurchaseRequestPayload; export type UpdatePurchaseRequestPayload = CreatePurchaseRequestPayload;