All files / services adminApi.ts

66.66% Statements 16/24
100% Branches 14/14
46.66% Functions 7/15
66.66% Lines 16/24

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215                                                                                                                                                                                                                                                              1x 1x     26x 25x             38x       38x 8x   38x 1x   38x 1x     38x     38x                           2x         1x                                                                             1x
import {api} from './api.ts';
 
// ============================================================================
// Types
// ============================================================================
 
export interface AdminStats {
	totalInvestors: number;
	activeInvestors: number;
	pendingKyc: number;
	totalAccounts: number;
	activeAccounts: number;
	pendingAccounts: number;
	totalAum: string;
	totalAvailableForLoan: string;
}
 
// Investor list item (simplified for table)
export interface AdminInvestorListItem {
	investorId: number;
	firstName: string;
	lastName: string;
	email: string;
	phone: string | null;
	kycStatus: 'pending' | 'verified' | 'rejected';
	status: 'active' | 'inactive' | 'suspended';
	createdAt: string;
	accountId: number | null;
	accountNumber: string | null;
	balance: string | null;
	accountStatus: string | null;
	userId: number | null;
}
 
export interface AdminInvestorsListResponse {
	investors: AdminInvestorListItem[];
	total: number;
	page: number;
	limit: number;
}
 
export interface AdminInvestorDetail {
	investorId: number;
	firstName: string;
	lastName: string;
	email: string;
	phone: string | null;
	dateOfBirth: string;
	addressLine1: string | null;
	addressLine2: string | null;
	city: string | null;
	state: string | null;
	zipCode: string | null;
	country: string | null;
	kycStatus: string;
	status: string;
	createdAt: string;
	updatedAt: string;
	accountId: number | null;
	accountNumber: string | null;
	balance: string | null;
	availableBalance: string | null;
	availableForLoan: string | null;
	interestRate: string | null;
	loanToValueRatio: string | null;
	accountStatus: string | null;
	openedDate: string | null;
	userId: number | null;
	username: string | null;
	role: string | null;
}
 
export interface ImpersonateResponse {
	user: {
		userId: number;
		username: string;
		email: string;
		role: string;
	};
	accessToken: string;
	refreshToken: string;
	expiresIn: number;
	tokenType: string;
	impersonatedBy: number;
}
 
export interface GetInvestorsParams {
	page?: number;
	limit?: number;
	search?: string;
	kycStatus?: string;
	status?: string;
}
 
// Account creation response
export interface CreateAccountResponse {
	accountId: number;
	investorId: number;
	accountNumber: string;
	balance: string;
	status: string;
	createdAt: string;
}
 
// Transaction types
export interface CreateTransactionParams {
	accountId: number;
	transactionType: 'deposit' | 'withdrawal' | 'interest' | 'fee';
	amount: string;
	description?: string;
}
 
export interface TransactionResponse {
	transactionId: number;
	accountId: number;
	transactionType: string;
	amount: string;
	balanceBefore: string;
	balanceAfter: string;
	description: string | null;
	createdAt: string;
}
 
// ============================================================================
// API Endpoints
// ============================================================================
 
export const adminApi = api.injectEndpoints({
	endpoints: (builder) => ({
		// Get admin dashboard stats
		getAdminStats: builder.query<AdminStats, void>({
			query: () => '/admin/stats',
			transformResponse: (response: { success: boolean; data: AdminStats }) => response.data,
			providesTags: ['AdminStats']
		}),
 
		// Get paginated investors list
		getAdminInvestors: builder.query<AdminInvestorsListResponse, GetInvestorsParams>({
			query: ({page = 1, limit = 20, search, kycStatus, status}) => {
				const params = new URLSearchParams({
					page: page.toString(),
					limit: limit.toString()
				});
				if (search !== undefined && search !== '') {
					params.append('search', search);
				}
				if (kycStatus !== undefined && kycStatus !== '') {
					params.append('kycStatus', kycStatus);
				}
				if (status !== undefined && status !== '') {
					params.append('status', status);
				}
 
				return `/admin/investors?${params.toString()}`;
			},
			transformResponse: (response: { success: boolean; data: AdminInvestorsListResponse }) =>
				response.data,
			providesTags: ['AdminInvestors']
		}),
 
		// Get investor detail
		getAdminInvestorDetail: builder.query<AdminInvestorDetail, number>({
			query: (investorId) => `/admin/investors/${String(investorId)}`,
			transformResponse: (response: { success: boolean; data: AdminInvestorDetail }) =>
				response.data,
			providesTags: (_result, _error, id) => [{type: 'Investor', id}]
		}),
 
		// Impersonate user
		impersonateUser: builder.mutation<ImpersonateResponse, number>({
			query: (userId) => ({
				url: `/admin/impersonate/${String(userId)}`,
				method: 'POST'
			}),
			transformResponse: (response: { success: boolean; data: ImpersonateResponse }) =>
				response.data
		}),
 
		// Create investment account for an investor
		createInvestorAccount: builder.mutation<CreateAccountResponse, number>({
			query: (investorId) => ({
				url: `/admin/investors/${String(investorId)}/account`,
				method: 'POST'
			}),
			transformResponse: (response: { success: boolean; data: CreateAccountResponse }) =>
				response.data,
			invalidatesTags: (_result, _error, investorId) => [
				{type: 'Investor', id: investorId},
				'AdminInvestors',
				'AdminStats'
			]
		}),
 
		// Create transaction for an account (admin)
		createInvestorTransaction: builder.mutation<TransactionResponse, CreateTransactionParams>({
			query: ({accountId, ...body}) => ({
				url: `/admin/accounts/${String(accountId)}/transactions`,
				method: 'POST',
				body
			}),
			transformResponse: (response: { success: boolean; data: TransactionResponse }) =>
				response.data,
			invalidatesTags: ['AdminInvestors', 'AdminStats', 'Account', 'AccountSummary']
		})
	})
});
 
export const {
	useGetAdminStatsQuery,
	useGetAdminInvestorsQuery,
	useGetAdminInvestorDetailQuery,
	useImpersonateUserMutation,
	useCreateInvestorAccountMutation,
	useCreateInvestorTransactionMutation
} = adminApi;