52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import { Response } from 'express';
|
|
import { AuthRequest } from '../middleware/auth';
|
|
import * as fundApprovalService from '../services/fundApprovalService';
|
|
|
|
export async function grantCredit(req: AuthRequest, res: Response): Promise<void> {
|
|
try {
|
|
const userId = req.params.userId;
|
|
const { walletTypeId, adjustmentType, amountRaw, reason } = req.body as {
|
|
walletTypeId?: number;
|
|
adjustmentType?: 'bonus' | 'admin_adjustment';
|
|
amountRaw?: string;
|
|
reason?: string;
|
|
};
|
|
|
|
if (!walletTypeId || !adjustmentType || !amountRaw || !reason) {
|
|
res.status(400).json({
|
|
error: 'walletTypeId, adjustmentType, amountRaw, and reason are required',
|
|
});
|
|
return;
|
|
}
|
|
|
|
const result = await fundApprovalService.createFundRequest({
|
|
operationType: 'ledger_credit',
|
|
payload: { userId, walletTypeId, adjustmentType, amountRaw, reason },
|
|
requestedBy: req.user!.userId,
|
|
});
|
|
|
|
res.status(202).json({
|
|
requestId: result.id,
|
|
status: result.status,
|
|
message: 'Credit request submitted — pending super-admin approval',
|
|
});
|
|
} catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Credit request failed';
|
|
res.status(400).json({ error: msg });
|
|
}
|
|
}
|
|
|
|
export async function listCredits(req: AuthRequest, res: Response): Promise<void> {
|
|
try {
|
|
const userId = (req.query.userId as string) || undefined;
|
|
const limit = parseInt((req.query.limit as string) || '50', 10);
|
|
const offset = parseInt((req.query.offset as string) || '0', 10);
|
|
const { listLedgerCredits } = await import('../services/adminCreditService');
|
|
const result = await listLedgerCredits({ userId, limit, offset });
|
|
res.json(result);
|
|
} catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Failed to list credits';
|
|
res.status(500).json({ error: msg });
|
|
}
|
|
}
|