import axios from 'axios'; import { BaseApiService } from '@/services/api/base'; import { BaseApiResponse } from '@/types/api/api-general'; import { httpClient, httpClientFetcher } from '@/services/http/client'; import { CreateFinancePayment, CreateInitialBalance, CreateInjection, Finance, UpdateFinancePayment, UpdateInitialBalance, UpdateInjection, } from '@/types/api/finance/finance'; export class FinanceApiService extends BaseApiService< Finance, unknown, unknown > { constructor(basePath: string) { super(basePath); } async getSingle(id: number): Promise> { return await httpClientFetcher>( `${this.basePath}/transactions/${id}` ); } async create(payload: CreateFinancePayment) { const isFormData = typeof FormData !== 'undefined' && payload instanceof FormData; try { const headers = isFormData ? { ...(this.header ?? {}) } : { 'Content-Type': 'application/json', ...(this.header ?? {}) }; const createRes = await httpClient>( `${this.basePath}/payments`, { method: 'POST', body: payload, headers, } ); return createRes; } catch (error: unknown) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } async createInitialBalances(payload: CreateInitialBalance) { const isFormData = typeof FormData !== 'undefined' && payload instanceof FormData; try { const headers = isFormData ? { ...(this.header ?? {}) } : { 'Content-Type': 'application/json', ...(this.header ?? {}) }; const createRes = await httpClient>( `${this.basePath}/initial-balances`, { method: 'POST', body: payload, headers, } ); return createRes; } catch (error: unknown) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } async createInjections(payload: CreateInjection) { const isFormData = typeof FormData !== 'undefined' && payload instanceof FormData; try { const headers = isFormData ? { ...(this.header ?? {}) } : { 'Content-Type': 'application/json', ...(this.header ?? {}) }; const createRes = await httpClient>( `${this.basePath}/injections`, { method: 'POST', body: payload, headers, } ); return createRes; } catch (error: unknown) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } async update(id: number, payload: UpdateFinancePayment) { const isFormData = typeof FormData !== 'undefined' && payload instanceof FormData; try { const updatePath = `${this.basePath}/payments/${id}`; const headers = isFormData ? { ...(this.header ?? {}) } : { 'Content-Type': 'application/json', ...(this.header ?? {}) }; const updateRes = await httpClient>(updatePath, { method: 'PATCH', body: payload, headers, }); return updateRes; } catch (error: unknown) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } async updateInitialBalances(id: number, payload: UpdateInitialBalance) { const isFormData = typeof FormData !== 'undefined' && payload instanceof FormData; try { const updatePath = `${this.basePath}/initial-balances/${id}`; const headers = isFormData ? { ...(this.header ?? {}) } : { 'Content-Type': 'application/json', ...(this.header ?? {}) }; const updateRes = await httpClient>(updatePath, { method: 'PATCH', body: payload, headers, }); return updateRes; } catch (error: unknown) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } async updateInjections(id: number, payload: UpdateInjection) { const isFormData = typeof FormData !== 'undefined' && payload instanceof FormData; try { const updatePath = `${this.basePath}/injections/${id}`; const headers = isFormData ? { ...(this.header ?? {}) } : { 'Content-Type': 'application/json', ...(this.header ?? {}) }; const updateRes = await httpClient>(updatePath, { method: 'PATCH', body: payload, headers, }); return updateRes; } catch (error: unknown) { if (axios.isAxiosError>(error)) { return error.response?.data; } return undefined; } } async delete(id: number) { try { const deletePath = `${this.basePath}/transactions/${id}`; const deleteRes = await httpClient(deletePath, { method: 'DELETE', }); return deleteRes; } catch (error) { if (axios.isAxiosError(error)) { return error.response?.data; } return undefined; } } } export const FinanceApi = new FinanceApiService('/finance');