refactor(FE-212,213): unify purchase API service references in Purchase components

This commit is contained in:
rstubryan
2025-11-20 22:39:04 +07:00
parent 6e9d065bc6
commit 417d08e0fc
8 changed files with 85 additions and 124 deletions
+64 -100
View File
@@ -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<BaseApiResponse<{ message: string }> | undefined> {
return await this.customRequest<BaseApiResponse<{ message: string }>>(
`${purchaseRequestId}/approvals/staff`,
{
staffApproval: {
create: async (
purchaseRequestId: number,
payload: CreateStaffApprovalRequestPayload
): Promise<BaseApiResponse<{ message: string }> | undefined> => {
return await basePurchaseApi.customRequest<
BaseApiResponse<{ message: string }>
>(`${purchaseRequestId}/approvals/staff`, {
method: 'POST',
payload,
}
);
}
});
},
async updateStaffApproval(
purchaseRequestId: number,
payload: UpdateStaffApprovalRequestPayload
): Promise<BaseApiResponse<{ message: string }> | undefined> {
return await this.customRequest<BaseApiResponse<{ message: string }>>(
`${purchaseRequestId}/approvals/staff`,
{
update: async (
purchaseRequestId: number,
payload: UpdateStaffApprovalRequestPayload
): Promise<BaseApiResponse<{ message: string }> | 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<BaseApiResponse<{ message: string }> | undefined> {
return await this.customRequest<BaseApiResponse<{ message: string }>>(
`${purchaseRequestId}/approvals/manager`,
{
managerApproval: {
create: async (
purchaseRequestId: number,
payload: CreateManagerApprovalRequestPayload
): Promise<BaseApiResponse<{ message: string }> | 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<BaseApiResponse<{ message: string }> | undefined> {
return await this.customRequest<BaseApiResponse<{ message: string }>>(
`${purchaseRequestId}/receipts`,
{
acceptApproval: {
create: async (
purchaseRequestId: number,
payload: CreateAcceptApprovalRequestPayload
): Promise<BaseApiResponse<{ message: string }> | 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<BaseApiResponse<{ message: string }> | undefined> {
return await this.customRequest<BaseApiResponse<{ message: string }>>(
`${purchaseRequestId}/items`,
{
items: {
delete: async (
purchaseRequestId: number,
payload: DeletePurchaseRequestItemPayload
): Promise<BaseApiResponse<{ message: string }> | 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'
);
});
},
},
};