Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 158 additions & 2 deletions backend/src/routes/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,28 @@ const router = Router();

// ── Aggregation ───────────────────────────────────────────────────────────────

// Daily volume + count summary
/**
* @swagger
* /api/analytics/summary/daily:
* get:
* summary: Get daily transaction volume and count summary
* tags: [Analytics]
* parameters:
* - in: query
* name: from
* schema: { type: string, format: date-time }
* - in: query
* name: to
* schema: { type: string, format: date-time }
* - in: query
* name: userId
* schema: { type: string }
* responses:
* 200:
* description: Daily summary data
* 500:
* description: Server error
*/
router.get('/summary/daily', async (req, res) => {
try {
const { from, to, userId } = req.query;
Expand All @@ -16,7 +37,28 @@ router.get('/summary/daily', async (req, res) => {
}
});

// Overall totals
/**
* @swagger
* /api/analytics/summary/totals:
* get:
* summary: Get overall transaction totals
* tags: [Analytics]
* parameters:
* - in: query
* name: from
* schema: { type: string, format: date-time }
* - in: query
* name: to
* schema: { type: string, format: date-time }
* - in: query
* name: userId
* schema: { type: string }
* responses:
* 200:
* description: Totals data
* 500:
* description: Server error
*/
router.get('/summary/totals', async (req, res) => {
try {
const { from, to, userId } = req.query;
Expand All @@ -28,6 +70,27 @@ router.get('/summary/totals', async (req, res) => {

// ── User Behaviour ────────────────────────────────────────────────────────────

/**
* @swagger
* /api/analytics/users/{userId}/behaviour:
* get:
* summary: Get behaviour profile for a user
* tags: [Analytics]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: userId
* required: true
* schema: { type: string }
* responses:
* 200:
* description: User behaviour profile
* 401:
* description: Unauthorized
* 500:
* description: Server error
*/
router.get('/users/:userId/behaviour', requireAuth, async (req, res) => {
try {
res.json(await userBehavior.getProfile(req.params.userId));
Expand All @@ -38,6 +101,28 @@ router.get('/users/:userId/behaviour', requireAuth, async (req, res) => {

// ── Pattern Analysis ──────────────────────────────────────────────────────────

/**
* @swagger
* /api/analytics/patterns:
* get:
* summary: Analyze transaction patterns
* tags: [Analytics]
* parameters:
* - in: query
* name: userId
* schema: { type: string }
* - in: query
* name: from
* schema: { type: string, format: date-time }
* - in: query
* name: to
* schema: { type: string, format: date-time }
* responses:
* 200:
* description: Pattern analysis result
* 500:
* description: Server error
*/
router.get('/patterns', async (req, res) => {
try {
const { userId, from, to } = req.query;
Expand All @@ -49,6 +134,29 @@ router.get('/patterns', async (req, res) => {

// ── Fraud Detection ───────────────────────────────────────────────────────────

/**
* @swagger
* /api/analytics/fraud/flags:
* get:
* summary: Get fraud detection flags
* tags: [Analytics]
* security:
* - bearerAuth: []
* parameters:
* - in: query
* name: from
* schema: { type: string, format: date-time }
* - in: query
* name: to
* schema: { type: string, format: date-time }
* responses:
* 200:
* description: Fraud flags
* 401:
* description: Unauthorized
* 500:
* description: Server error
*/
router.get('/fraud/flags', requireAuth, async (req, res) => {
try {
const { from, to } = req.query;
Expand All @@ -61,6 +169,25 @@ router.get('/fraud/flags', requireAuth, async (req, res) => {

// ── Dashboard (combined) ──────────────────────────────────────────────────────

/**
* @swagger
* /api/analytics/dashboard:
* get:
* summary: Get combined analytics dashboard data
* tags: [Analytics]
* parameters:
* - in: query
* name: from
* schema: { type: string, format: date-time }
* - in: query
* name: to
* schema: { type: string, format: date-time }
* responses:
* 200:
* description: Dashboard data (totals, daily, patterns)
* 500:
* description: Server error
*/
router.get('/dashboard', async (req, res) => {
try {
const { from, to } = req.query;
Expand All @@ -77,6 +204,35 @@ router.get('/dashboard', async (req, res) => {

// ── Export ────────────────────────────────────────────────────────────────────

/**
* @swagger
* /api/analytics/export:
* get:
* summary: Export transaction analytics data
* tags: [Analytics]
* security:
* - bearerAuth: []
* parameters:
* - in: query
* name: userId
* schema: { type: string }
* - in: query
* name: from
* schema: { type: string, format: date-time }
* - in: query
* name: to
* schema: { type: string, format: date-time }
* - in: query
* name: format
* schema: { type: string, enum: [json, csv], default: json }
* responses:
* 200:
* description: Exported data (JSON or CSV)
* 401:
* description: Unauthorized
* 500:
* description: Server error
*/
router.get('/export', requireAuth, async (req, res) => {
try {
const { userId, from, to, format = 'json' } = req.query;
Expand Down
147 changes: 143 additions & 4 deletions backend/src/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,40 @@ const userRules = [
body('password').isLength({ min: 8 }).withMessage('Password must be at least 8 chars'),
];

// POST /api/auth/register
/**
* @swagger
* /api/auth/register:
* post:
* summary: Register a new user
* tags: [Auth]
* security: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [username, password]
* properties:
* username:
* type: string
* minLength: 3
* maxLength: 32
* password:
* type: string
* minLength: 8
* responses:
* 201:
* description: User created
* 409:
* description: Username already taken
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 422:
* description: Validation error
*/
router.post('/register', userRules, validateBody, async (req, res) => {
try {
const { username, password } = req.body;
Expand All @@ -32,6 +65,46 @@ router.post('/register', userRules, validateBody, async (req, res) => {
}
});

/**
* @swagger
* /api/auth/login:
* post:
* summary: Log in and receive JWT tokens
* tags: [Auth]
* security: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [username, password]
* properties:
* username:
* type: string
* password:
* type: string
* responses:
* 200:
* description: Tokens issued
* content:
* application/json:
* schema:
* type: object
* properties:
* accessToken: { type: string }
* refreshToken: { type: string }
* recovered: { type: boolean }
* 401:
* description: Invalid credentials
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 422:
* description: Validation error
*/
router.post('/login', userRules, validateBody, async (req, res) => {
// Stricter rate limit for login endpoint (10 req/min)
const loginRateLimiter = createRateLimiter({
windowMs: 60000,
Expand Down Expand Up @@ -70,7 +143,37 @@ router.post('/login', loginRateLimiter, userRules, validateBody, async (req, res
});
});

// POST /api/auth/refresh
/**
* @swagger
* /api/auth/refresh:
* post:
* summary: Refresh access token
* tags: [Auth]
* security: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [refreshToken]
* properties:
* refreshToken:
* type: string
* responses:
* 200:
* description: New access token
* content:
* application/json:
* schema:
* type: object
* properties:
* accessToken: { type: string }
* 400:
* description: refreshToken missing
* 401:
* description: Invalid or expired refresh token
*/
router.post('/refresh', (req, res) => {
const { refreshToken } = req.body;
if (!refreshToken) return res.status(400).json({ error: 'refreshToken required' });
Expand All @@ -82,12 +185,48 @@ router.post('/refresh', (req, res) => {
}
});

// POST /api/auth/logout (client should discard tokens; server-side blacklist can be added later)
/**
* @swagger
* /api/auth/logout:
* post:
* summary: Log out (client should discard tokens)
* tags: [Auth]
* security:
* - bearerAuth: []
* responses:
* 200:
* description: Logged out
* 401:
* description: Unauthorized
*/
router.post('/logout', requireAuth, (_req, res) => {
res.json({ message: 'Logged out successfully' });
});

// GET /api/auth/profile
/**
* @swagger
* /api/auth/profile:
* get:
* summary: Get authenticated user profile
* tags: [Auth]
* security:
* - bearerAuth: []
* responses:
* 200:
* description: User profile
* content:
* application/json:
* schema:
* type: object
* properties:
* id: { type: string }
* username: { type: string }
* createdAt: { type: string, format: date-time }
* 401:
* description: Unauthorized
* 404:
* description: User not found
*/
router.get('/profile', requireAuth, (req, res) => {
const user = getUserById(req.user.sub);
if (!user) return res.status(404).json({ error: 'User not found' });
Expand Down
Loading
Loading