All files / services fundingApi.ts

87.5% Statements 7/8
100% Branches 0/0
83.33% Functions 5/6
87.5% Lines 7/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                                                                                                1x 1x                       4x   4x           1x           1x                   1x  
import {api} from './api.ts';
 
// ============================================================================
// Types
// ============================================================================
 
export interface FundingRequest {
	fundingRequestId: number;
	requestType: 'deposit' | 'withdrawal';
	amount: string;
	note: string | null;
	status: string;
	createdAt: string;
}
 
interface SubmitFundingResponse {
	success: boolean;
	message: string;
	data: {request: FundingRequest};
}
 
export interface SubmitFundingArgs {
	type: 'deposit' | 'withdrawal';
	amount: string;
	note?: string;
}
 
/**
 * Admin-queue view of a funding request — adds the investor identity and review
 * timestamp the submitter's own view doesn't carry.
 */
export interface AdminFundingRequest {
	fundingRequestId: number;
	investorId: number;
	investorName: string | null;
	investorEmail: string | null;
	requestType: 'deposit' | 'withdrawal';
	amount: string;
	note: string | null;
	status: string;
	createdAt: string;
	reviewedAt: string | null;
}
 
interface PendingFundingResponse {
	success: boolean;
	data: {fundingRequests: AdminFundingRequest[]};
}
 
interface UpdateFundingStatusResponse {
	success: boolean;
	message: string;
	data: {fundingRequest: AdminFundingRequest};
}
 
export interface UpdateFundingStatusArgs {
	fundingRequestId: number;
	status: 'completed' | 'rejected';
}
 
// ============================================================================
// API Slice (FSC-21)
// ============================================================================
 
export const fundingApi = api.injectEndpoints({
	endpoints: (builder) => ({
		submitFundingRequest: builder.mutation<SubmitFundingResponse, SubmitFundingArgs>({
			query: (body) => ({
				url: '/me/funding-requests',
				method: 'POST',
				body
			}),
			invalidatesTags: ['FundingRequest']
		}),
 
		// Admin queue: pending deposit/withdrawal requests awaiting action.
		getPendingFundingRequests: builder.query<AdminFundingRequest[], void>({
			query: () => '/admin/funding-requests/pending',
			transformResponse: (response: PendingFundingResponse) =>
				response.data.fundingRequests,
			providesTags: ['FundingRequest']
		}),
 
		// Admin action: mark a pending request completed or rejected.
		updateFundingRequestStatus: builder.mutation<AdminFundingRequest, UpdateFundingStatusArgs>({
			query: ({fundingRequestId, status}) => ({
				url: `/admin/funding-requests/${String(fundingRequestId)}/status`,
				method: 'POST',
				body: {status}
			}),
			transformResponse: (response: UpdateFundingStatusResponse) =>
				response.data.fundingRequest,
			invalidatesTags: ['FundingRequest']
		})
	})
});
 
export const {
	useSubmitFundingRequestMutation,
	useGetPendingFundingRequestsQuery,
	useUpdateFundingRequestStatusMutation
} = fundingApi;