diff --git a/src/app/purchase/detail/edit/page.tsx b/src/app/purchase/detail/edit/page.tsx index a4d59ae2..f93d1618 100644 --- a/src/app/purchase/detail/edit/page.tsx +++ b/src/app/purchase/detail/edit/page.tsx @@ -3,7 +3,7 @@ import { useRouter, useSearchParams } from 'next/navigation'; import useSWR from 'swr'; import PurchaseRequestForm from '@/components/pages/purchase/form/request/PurchaseRequestForm'; -import { PurchaseRequestApi } from '@/services/api/purchase'; +import { PurchaseApi } from '@/services/api/purchase'; import { isResponseSuccess, isResponseError } from '@/lib/api-helper'; const PurchaseEdit = () => { @@ -14,7 +14,7 @@ const PurchaseEdit = () => { const { data: purchase, isLoading: isLoadingPurchase } = useSWR( purchaseId, - (id: number) => PurchaseRequestApi.getSingle(id) + (id: number) => PurchaseApi.getSingle(id) ); if (!purchaseId) { diff --git a/src/app/purchase/detail/page.tsx b/src/app/purchase/detail/page.tsx index 16c077b4..6ca2ffc7 100644 --- a/src/app/purchase/detail/page.tsx +++ b/src/app/purchase/detail/page.tsx @@ -3,7 +3,7 @@ import { useRouter, useSearchParams } from 'next/navigation'; import useSWR from 'swr'; import PurchaseOrderDetail from '@/components/pages/purchase/order/PurchaseOrderDetail'; -import { PurchaseRequestApi } from '@/services/api/purchase'; +import { PurchaseApi } from '@/services/api/purchase'; import { isResponseSuccess, isResponseError } from '@/lib/api-helper'; const PurchaseDetail = () => { @@ -14,7 +14,7 @@ const PurchaseDetail = () => { const { data: purchase, isLoading: isLoadingPurchase } = useSWR( purchaseId, - (id: number) => PurchaseRequestApi.getSingle(id) + (id: number) => PurchaseApi.getSingle(id) ); if (!purchaseId) { diff --git a/src/components/pages/purchase/PurchaseTable.tsx b/src/components/pages/purchase/PurchaseTable.tsx index 9912d13a..6ce26828 100644 --- a/src/components/pages/purchase/PurchaseTable.tsx +++ b/src/components/pages/purchase/PurchaseTable.tsx @@ -22,7 +22,7 @@ import { isResponseSuccess } from '@/lib/api-helper'; import { useTableFilter } from '@/services/hooks/useTableFilter'; import { ROWS_OPTIONS } from '@/config/constant'; import { Purchase } from '@/types/api/purchase/purchase'; -import { PurchaseRequestApi } from '@/services/api/purchase'; +import { PurchaseApi } from '@/services/api/purchase'; // ===== INTERFACES ===== interface RowOptionsMenuProps { @@ -110,8 +110,8 @@ const PurchaseTable = () => { isLoading, mutate: refreshPurchaseRequests, } = useSWR( - `${PurchaseRequestApi.basePath}${getTableFilterQueryString()}`, - PurchaseRequestApi.getAllFetcher + `${PurchaseApi.basePath}${getTableFilterQueryString()}`, + PurchaseApi.getAllFetcher ); // ===== TABLE COLUMNS DEFINITION ===== @@ -208,7 +208,7 @@ const PurchaseTable = () => { setIsDeleteLoading(true); try { - await PurchaseRequestApi.delete(selectedPurchase?.id as number); + await PurchaseApi.delete(selectedPurchase?.id as number); refreshPurchaseRequests(); deleteModal.closeModal(); toast.success('Berhasil menghapus data permintaan pembelian!'); diff --git a/src/components/pages/purchase/form/order/PurchaseOrderAcceptApprovalForm.tsx b/src/components/pages/purchase/form/order/PurchaseOrderAcceptApprovalForm.tsx index 109b70b4..fda6fb94 100644 --- a/src/components/pages/purchase/form/order/PurchaseOrderAcceptApprovalForm.tsx +++ b/src/components/pages/purchase/form/order/PurchaseOrderAcceptApprovalForm.tsx @@ -16,7 +16,7 @@ import { PurchaseRequestAcceptApprovalFormSchema, } from './PurchaseOrderForm.schema'; import { isResponseError } from '@/lib/api-helper'; -import { AcceptApprovalApi } from '@/services/api/purchase'; +import { PurchaseApi } from '@/services/api/purchase'; import { CreateAcceptApprovalRequestPayload, Purchase, @@ -93,7 +93,7 @@ const PurchaseOrderAcceptApprovalForm = ({ return; } - const res = await AcceptApprovalApi.acceptApproval( + const res = await PurchaseApi.acceptApproval.create( purchaseRequestId, payload ); @@ -113,7 +113,7 @@ const PurchaseOrderAcceptApprovalForm = ({ const updateAcceptApprovalHandler = useCallback( async (purchaseId: number, payload: CreateAcceptApprovalRequestPayload) => { - const res = await AcceptApprovalApi.acceptApproval(purchaseId, payload); + const res = await PurchaseApi.acceptApproval.create(purchaseId, payload); if (isResponseError(res)) { setPurchaseOrderFormErrorMessage(res.message); return; diff --git a/src/components/pages/purchase/form/order/PurchaseOrderStaffApprovalForm.tsx b/src/components/pages/purchase/form/order/PurchaseOrderStaffApprovalForm.tsx index 8c3daad5..2bf53209 100644 --- a/src/components/pages/purchase/form/order/PurchaseOrderStaffApprovalForm.tsx +++ b/src/components/pages/purchase/form/order/PurchaseOrderStaffApprovalForm.tsx @@ -15,7 +15,7 @@ import { PurchaseRequestStaffApprovalFormSchema, } from './PurchaseOrderForm.schema'; import { isResponseError } from '@/lib/api-helper'; -import { StaffApprovalApi } from '@/services/api/purchase'; +import { PurchaseApi } from '@/services/api/purchase'; import { CreateStaffApprovalRequestPayload, UpdateStaffApprovalRequestPayload, @@ -84,7 +84,7 @@ const PurchaseOrderStaffApprovalForm = ({ return; } - const res = await StaffApprovalApi.createStaffApproval( + const res = await PurchaseApi.staffApproval.create( purchaseRequestId, payload ); @@ -104,7 +104,7 @@ const PurchaseOrderStaffApprovalForm = ({ const updateStaffApprovalHandler = useCallback( async (purchaseId: number, payload: UpdateStaffApprovalRequestPayload) => { - const res = await StaffApprovalApi.updateStaffApproval( + const res = await PurchaseApi.staffApproval.update( purchaseId, payload ); diff --git a/src/components/pages/purchase/form/request/PurchaseRequestForm.tsx b/src/components/pages/purchase/form/request/PurchaseRequestForm.tsx index d039c1c9..7c6d64e1 100644 --- a/src/components/pages/purchase/form/request/PurchaseRequestForm.tsx +++ b/src/components/pages/purchase/form/request/PurchaseRequestForm.tsx @@ -33,7 +33,7 @@ import { import { Supplier } from '@/types/api/master-data/supplier'; import { Product } from '@/types/api/master-data/product'; import { isResponseSuccess, isResponseError } from '@/lib/api-helper'; -import { PurchaseRequestApi } from '@/services/api/purchase'; +import { PurchaseApi } from '@/services/api/purchase'; import Card from '@/components/Card'; import { @@ -99,7 +99,7 @@ const PurchaseRequestForm = ({ // ===== SUBMISSION HANDLERS ===== const createPurchaseRequestHandler = useCallback( async (payload: CreatePurchaseRequestPayload) => { - const res = await PurchaseRequestApi.create(payload); + const res = await PurchaseApi.create(payload); if (isResponseError(res)) { setPurchaseRequestFormErrorMessage(res.message); return; @@ -115,7 +115,7 @@ const PurchaseRequestForm = ({ purchaseRequestId: number, payload: CreatePurchaseRequestPayload ) => { - const res = await PurchaseRequestApi.update(purchaseRequestId, payload); + const res = await PurchaseApi.update(purchaseRequestId, payload); if (isResponseError(res)) { setPurchaseRequestFormErrorMessage(res.message); return; @@ -135,7 +135,7 @@ const PurchaseRequestForm = ({ if (!initialValues?.id) return; setIsDeleteLoading(true); - await PurchaseRequestApi.delete(initialValues.id); + await PurchaseApi.delete(initialValues.id); deleteModal.closeModal(); toast.success('Successfully delete Purchase Request!'); setIsDeleteLoading(false); diff --git a/src/components/pages/purchase/order/PurchaseOrderDetail.tsx b/src/components/pages/purchase/order/PurchaseOrderDetail.tsx index 904177bb..82f389c1 100644 --- a/src/components/pages/purchase/order/PurchaseOrderDetail.tsx +++ b/src/components/pages/purchase/order/PurchaseOrderDetail.tsx @@ -26,10 +26,7 @@ import { Purchase, PurchaseItem, } from '@/types/api/purchase/purchase'; -import { - ManagerApprovalApi, - PurchaseDeleteItemsApi, -} from '@/services/api/purchase'; +import { PurchaseApi } from '@/services/api/purchase'; import { isResponseError } from '@/lib/api-helper'; import { toast } from 'react-hot-toast'; import { useSearchParams } from 'next/navigation'; @@ -230,7 +227,7 @@ const PurchaseOrderDetail = ({ return; } - const res = await ManagerApprovalApi.createManagerApproval( + const res = await PurchaseApi.managerApproval.create( purchaseRequestId, payload ); @@ -276,7 +273,7 @@ const PurchaseOrderDetail = ({ setIsDeleteLoading(true); try { - const res = await PurchaseDeleteItemsApi.deleteItems(purchaseRequestId, { + const res = await PurchaseApi.items.delete(purchaseRequestId, { item_ids: itemIdsToDelete, }); diff --git a/src/services/api/purchase.ts b/src/services/api/purchase.ts index 7f25e9f9..d0438e88 100644 --- a/src/services/api/purchase.ts +++ b/src/services/api/purchase.ts @@ -11,123 +11,87 @@ import { import { BaseApiService } from '@/services/api/base'; import { BaseApiResponse } from '@/types/api/api-general'; -export const PurchaseRequestApi = new BaseApiService< +const basePurchaseApi = new BaseApiService< Purchase, CreatePurchaseRequestPayload, UpdatePurchaseRequestPayload >('/purchases'); -export class StaffApprovalService extends BaseApiService< - Purchase, - CreateStaffApprovalRequestPayload, - UpdateStaffApprovalRequestPayload -> { - constructor(basePath: string = '') { - super(basePath); - } +export const PurchaseApi = { + basePath: basePurchaseApi.basePath, + header: basePurchaseApi.header, + getAllFetcher: basePurchaseApi.getAllFetcher.bind(basePurchaseApi), + getSingle: basePurchaseApi.getSingle.bind(basePurchaseApi), + create: basePurchaseApi.create.bind(basePurchaseApi), + update: basePurchaseApi.update.bind(basePurchaseApi), + delete: basePurchaseApi.delete.bind(basePurchaseApi), + customRequest: basePurchaseApi.customRequest.bind(basePurchaseApi), - async createStaffApproval( - purchaseRequestId: number, - payload: CreateStaffApprovalRequestPayload - ): Promise | undefined> { - return await this.customRequest>( - `${purchaseRequestId}/approvals/staff`, - { + staffApproval: { + create: async ( + purchaseRequestId: number, + payload: CreateStaffApprovalRequestPayload + ): Promise | undefined> => { + return await basePurchaseApi.customRequest< + BaseApiResponse<{ message: string }> + >(`${purchaseRequestId}/approvals/staff`, { method: 'POST', payload, - } - ); - } + }); + }, - async updateStaffApproval( - purchaseRequestId: number, - payload: UpdateStaffApprovalRequestPayload - ): Promise | undefined> { - return await this.customRequest>( - `${purchaseRequestId}/approvals/staff`, - { + update: async ( + purchaseRequestId: number, + payload: UpdateStaffApprovalRequestPayload + ): Promise | undefined> => { + return await basePurchaseApi.customRequest< + BaseApiResponse<{ message: string }> + >(`${purchaseRequestId}/approvals/staff`, { method: 'POST', payload, - } - ); - } -} + }); + }, + }, -export class ManagerApprovalService extends BaseApiService< - Purchase, - CreateManagerApprovalRequestPayload, - CreateManagerApprovalRequestPayload -> { - constructor(basePath: string = '') { - super(basePath); - } - - async createManagerApproval( - purchaseRequestId: number, - payload: CreateManagerApprovalRequestPayload - ): Promise | undefined> { - return await this.customRequest>( - `${purchaseRequestId}/approvals/manager`, - { + managerApproval: { + create: async ( + purchaseRequestId: number, + payload: CreateManagerApprovalRequestPayload + ): Promise | undefined> => { + return await basePurchaseApi.customRequest< + BaseApiResponse<{ message: string }> + >(`${purchaseRequestId}/approvals/manager`, { method: 'POST', payload, - } - ); - } -} + }); + }, + }, -export class AcceptApprovalService extends BaseApiService< - Purchase, - CreateAcceptApprovalRequestPayload, - unknown -> { - constructor(basePath: string = '') { - super(basePath); - } - - async acceptApproval( - purchaseRequestId: number, - payload: CreateAcceptApprovalRequestPayload - ): Promise | undefined> { - return await this.customRequest>( - `${purchaseRequestId}/receipts`, - { + acceptApproval: { + create: async ( + purchaseRequestId: number, + payload: CreateAcceptApprovalRequestPayload + ): Promise | undefined> => { + return await basePurchaseApi.customRequest< + BaseApiResponse<{ message: string }> + >(`${purchaseRequestId}/receipts`, { method: 'POST', payload, - } - ); - } -} + }); + }, + }, -export class PurchaseDeleteItemsService extends BaseApiService< - Purchase, - DeletePurchaseRequestItemPayload, - unknown -> { - constructor(basePath: string = '') { - super(basePath); - } - - async deleteItems( - purchaseRequestId: number, - payload: DeletePurchaseRequestItemPayload - ): Promise | undefined> { - return await this.customRequest>( - `${purchaseRequestId}/items`, - { + items: { + delete: async ( + purchaseRequestId: number, + payload: DeletePurchaseRequestItemPayload + ): Promise | undefined> => { + return await basePurchaseApi.customRequest< + BaseApiResponse<{ message: string }> + >(`${purchaseRequestId}/items`, { method: 'DELETE', payload, - } - ); - } -} - -export const StaffApprovalApi = new StaffApprovalService('/purchases'); - -export const ManagerApprovalApi = new ManagerApprovalService('/purchases'); - -export const AcceptApprovalApi = new AcceptApprovalService('/purchases'); - -export const PurchaseDeleteItemsApi = new PurchaseDeleteItemsService( - '/purchases' -); + }); + }, + }, +};