import axios from 'axios'; import { BaseApiService } from '@/services/api/base'; import { Closing, ClosingGeneralInformation, ClosingIncomingSapronak, ClosingOutgoingSapronak, ClosingOverhead, ClosingSapronakCalculation, } from '@/types/api/closing'; import { BaseApiResponse } from '@/types/api/api-general'; import { dummyGetAllFetcher, dummyGetSingle, dummyGetAllIncomingSapronakFetcher, dummyGetAllOutgoingSapronakFetcher, dummyGetGeneralInfo, dummyGetPerhitunganSapronak, dummyGetOverhead, } from '@/dummy/closing.dummy'; import { httpClient, httpClientFetcher } from '@/services/http/client'; import { ClosingSales } from '@/types/api/closing'; export class ClosingApiService extends BaseApiService { constructor(basePath: string) { super(basePath); } async getAllFetcher(endpoint: string): Promise> { // TODO: Remove this block when backend is ready // return await dummyGetAllFetcher(); // Uncomment this when backend is ready return await httpClientFetcher>(endpoint); } async getSingle(id: number): Promise | undefined> { // TODO: Remove this block when backend is ready // try { // return await dummyGetSingle(id); // } catch (error) { // if (axios.isAxiosError>(error)) { // return error.response?.data; // } // return undefined; // } // Uncomment this when backend is ready try { const getSinglePath = `${this.basePath}/${id}`; const getSingleRes = await httpClient>(getSinglePath); return getSingleRes; } catch (error) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } async getPenjualan( id: number ): Promise | undefined> { try { const getPenjualanPath = `${this.basePath}/${id}/penjualan`; const getPenjualanRes = await httpClient>(getPenjualanPath); return getPenjualanRes; } catch (error) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } async getAllIncomingSapronakFetcher( endpoint: string ): Promise> { // TODO: Remove this block when backend is ready // return await dummyGetAllIncomingSapronakFetcher(); // Uncomment this when backend is ready return await httpClientFetcher>( endpoint ); } async getAllOutgoingSapronakFetcher( endpoint: string ): Promise> { // TODO: Remove this block when backend is ready return await dummyGetAllOutgoingSapronakFetcher(); // Uncomment this when backend is ready // return await httpClientFetcher>( // endpoint // ); } async getGeneralInfo( id: number ): Promise | undefined> { // TODO: Remove this block when backend is ready // try { // return await dummyGetGeneralInfo(id); // } catch (error) { // if ( // axios.isAxiosError>(error) // ) { // return error.response?.data; // } // return undefined; // } // Uncomment this when backend is ready try { const getGeneralInfoPath = `${this.basePath}/${id}`; const getGeneralInfoRes = await httpClient>( getGeneralInfoPath ); return getGeneralInfoRes; } catch (error) { if ( axios.isAxiosError>(error) ) { return error.response?.data; } return undefined; } } async getPerhitunganSapronak( id: number ): Promise | undefined> { // TODO: Remove this block when backend is ready // try { // return await dummyGetPerhitunganSapronak(id); // } catch (error) { // if ( // axios.isAxiosError>(error) // ) { // return error.response?.data; // } // return undefined; // } // Uncomment this when backend is ready try { const path = `${this.basePath}/${id}/perhitungan_sapronak`; return await httpClient>( path, { method: 'GET', } ); } catch (error) { if ( axios.isAxiosError>(error) ) { return error.response?.data; } return undefined; } } async getOverhead( id: number ): Promise | undefined> { // TODO: Remove this block when backend is ready // try { // return await dummyGetOverhead(id); // } catch (error) { // if (axios.isAxiosError>(error)) { // return error.response?.data; // } // return undefined; // } // Uncomment this when backend is ready try { const path = `${this.basePath}/${id}/overhead`; return await httpClient>(path, { method: 'GET', }); } catch (error) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } } export const ClosingApi = new ClosingApiService('/closings');