97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
import { Response } from 'express';
|
|
import { AuthRequest } from '../middleware/auth';
|
|
import * as fundApprovalService from '../services/fundApprovalService';
|
|
import type { FundOperationStatus, FundOperationType } from '../repositories/fundOperationRequestRepository';
|
|
|
|
export async function createFundRequest(req: AuthRequest, res: Response): Promise<void> {
|
|
try {
|
|
const { operationType, payload } = req.body as {
|
|
operationType?: FundOperationType;
|
|
payload?: Record<string, unknown>;
|
|
};
|
|
if (!operationType || !payload) {
|
|
res.status(400).json({ error: 'operationType and payload are required' });
|
|
return;
|
|
}
|
|
const result = await fundApprovalService.createFundRequest({
|
|
operationType,
|
|
payload,
|
|
requestedBy: req.user!.userId,
|
|
});
|
|
res.status(202).json({ requestId: result.id, status: result.status, request: result });
|
|
} catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to create request';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
|
|
export async function listFundRequests(req: AuthRequest, res: Response): Promise<void> {
|
|
try {
|
|
const status = (req.query.status as FundOperationStatus) || undefined;
|
|
const operationType = (req.query.operationType as FundOperationType) || undefined;
|
|
const limit = parseInt((req.query.limit as string) || '50', 10);
|
|
const offset = parseInt((req.query.offset as string) || '0', 10);
|
|
const result = await fundApprovalService.listFundRequests({
|
|
status,
|
|
operationType,
|
|
limit,
|
|
offset,
|
|
});
|
|
res.json(result);
|
|
} catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to list requests' });
|
|
}
|
|
}
|
|
|
|
export async function getFundRequest(req: AuthRequest, res: Response): Promise<void> {
|
|
try {
|
|
const result = await fundApprovalService.getFundRequest(req.params.id);
|
|
if (!result) {
|
|
res.status(404).json({ error: 'Request not found' });
|
|
return;
|
|
}
|
|
res.json(result);
|
|
} catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to load request' });
|
|
}
|
|
}
|
|
|
|
export async function approveFundRequest(req: AuthRequest, res: Response): Promise<void> {
|
|
try {
|
|
const result = await fundApprovalService.approveFundRequest(req.params.id, req.user!.userId);
|
|
res.json(result);
|
|
} catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Approval failed';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
|
|
export async function rejectFundRequest(req: AuthRequest, res: Response): Promise<void> {
|
|
try {
|
|
const { reason } = req.body as { reason?: string };
|
|
if (!reason?.trim()) {
|
|
res.status(400).json({ error: 'reason is required' });
|
|
return;
|
|
}
|
|
const result = await fundApprovalService.rejectFundRequest(
|
|
req.params.id,
|
|
req.user!.userId,
|
|
reason
|
|
);
|
|
res.json(result);
|
|
} catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Rejection failed';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
|
|
export async function cancelFundRequest(req: AuthRequest, res: Response): Promise<void> {
|
|
try {
|
|
const result = await fundApprovalService.cancelFundRequest(req.params.id, req.user!.userId);
|
|
res.json(result);
|
|
} catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Cancel failed';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|