mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-21 05:45:46 +00:00
feat(FE-337): slicing ui form finance and API integration
This commit is contained in:
+174
-37
@@ -1,11 +1,16 @@
|
||||
import axios from 'axios';
|
||||
import { BaseApiService } from '@/services/api/base';
|
||||
import { BaseApiResponse } from '@/types/api/api-general';
|
||||
import { httpClient } from '@/services/http/client';
|
||||
import { Finance } from '@/types/api/finance/finance';
|
||||
// DUMMY_START
|
||||
import { getAllFetcher, getFetcher } from '@/dummy/finance/finance.dummy';
|
||||
// DUMMY_END
|
||||
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,
|
||||
@@ -16,41 +21,173 @@ export class FinanceApiService extends BaseApiService<
|
||||
super(basePath);
|
||||
}
|
||||
|
||||
async getAllFetcher(): Promise<BaseApiResponse<Finance[]>> {
|
||||
// DUMMY_START
|
||||
return await getAllFetcher();
|
||||
// DUMMY_END
|
||||
|
||||
// LIVE_START
|
||||
// try {
|
||||
// const path = `${this.basePath}/`;
|
||||
// return await httpClient<BaseApiResponse<Finance[]>>(path);
|
||||
// } catch (error) {
|
||||
// if (axios.isAxiosError<BaseApiResponse<Finance[]>>(error)) {
|
||||
// return error.response?.data;
|
||||
// }
|
||||
// return undefined;
|
||||
// }
|
||||
// LIVE_END
|
||||
async getSingle(id: number): Promise<BaseApiResponse<Finance>> {
|
||||
return await httpClientFetcher<BaseApiResponse<Finance>>(
|
||||
`${this.basePath}/transactions/${id}`
|
||||
);
|
||||
}
|
||||
|
||||
async getSingle(id: number): Promise<BaseApiResponse<Finance>> {
|
||||
// DUMMY_START
|
||||
return await getFetcher(id);
|
||||
// DUMMY_END
|
||||
async create(payload: CreateFinancePayment) {
|
||||
const isFormData =
|
||||
typeof FormData !== 'undefined' && payload instanceof FormData;
|
||||
try {
|
||||
const headers = isFormData
|
||||
? { ...(this.header ?? {}) }
|
||||
: { 'Content-Type': 'application/json', ...(this.header ?? {}) };
|
||||
|
||||
// LIVE_START
|
||||
// try {
|
||||
// const path = `${this.basePath}/`;
|
||||
// return await httpClient<BaseApiResponse<Finance[]>>(path);
|
||||
// } catch (error) {
|
||||
// if (axios.isAxiosError<BaseApiResponse<Finance[]>>(error)) {
|
||||
// return error.response?.data;
|
||||
// }
|
||||
// return undefined;
|
||||
// }
|
||||
// LIVE_END
|
||||
const createRes = await httpClient<BaseApiResponse<Finance>>(
|
||||
`${this.basePath}/payments`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: payload,
|
||||
headers,
|
||||
}
|
||||
);
|
||||
return createRes;
|
||||
} catch (error: unknown) {
|
||||
if (axios.isAxiosError<BaseApiResponse<Finance>>(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<BaseApiResponse<Finance>>(
|
||||
`${this.basePath}/initial-balances`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: payload,
|
||||
headers,
|
||||
}
|
||||
);
|
||||
return createRes;
|
||||
} catch (error: unknown) {
|
||||
if (axios.isAxiosError<BaseApiResponse<Finance>>(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<BaseApiResponse<Finance>>(
|
||||
`${this.basePath}/injections`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: payload,
|
||||
headers,
|
||||
}
|
||||
);
|
||||
return createRes;
|
||||
} catch (error: unknown) {
|
||||
if (axios.isAxiosError<BaseApiResponse<Finance>>(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<BaseApiResponse<Finance>>(updatePath, {
|
||||
method: 'PATCH',
|
||||
body: payload,
|
||||
headers,
|
||||
});
|
||||
return updateRes;
|
||||
} catch (error: unknown) {
|
||||
if (axios.isAxiosError<BaseApiResponse<Finance>>(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<BaseApiResponse<Finance>>(updatePath, {
|
||||
method: 'PATCH',
|
||||
body: payload,
|
||||
headers,
|
||||
});
|
||||
return updateRes;
|
||||
} catch (error: unknown) {
|
||||
if (axios.isAxiosError<BaseApiResponse<Finance>>(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<BaseApiResponse<Finance>>(updatePath, {
|
||||
method: 'PATCH',
|
||||
body: payload,
|
||||
headers,
|
||||
});
|
||||
return updateRes;
|
||||
} catch (error: unknown) {
|
||||
if (axios.isAxiosError<BaseApiResponse<Finance>>(error)) {
|
||||
return error.response?.data;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async delete(id: number) {
|
||||
try {
|
||||
const deletePath = `${this.basePath}/transactions/${id}`;
|
||||
const deleteRes = await httpClient<BaseApiResponse>(deletePath, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
return deleteRes;
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError<BaseApiResponse>(error)) {
|
||||
return error.response?.data;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const FinanceApi = new FinanceApiService('/finances');
|
||||
export const FinanceApi = new FinanceApiService('/finance');
|
||||
|
||||
Reference in New Issue
Block a user