-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdashboard.controller.ts
More file actions
71 lines (61 loc) · 2.41 KB
/
dashboard.controller.ts
File metadata and controls
71 lines (61 loc) · 2.41 KB
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
import type { Request, Response, NextFunction } from "express";
import type { DashboardService } from "../services/dashboard.service";
import { HttpError } from "../utils/http-error";
import { ServiceError } from "../utils/service-error";
import { UserType } from "../types/enums";
export function createDashboardController(dashboardService: DashboardService) {
return {
async getSellerDashboard(
req: Request,
res: Response,
next: NextFunction,
): Promise<void> {
try {
if (!req.user) {
throw new HttpError(401, "Authentication required");
}
// Only sellers can access seller dashboard
if (req.user.userType !== UserType.SELLER && req.user.userType !== UserType.BOTH) {
throw new HttpError(403, "Only sellers can access seller dashboard");
}
const metrics = await dashboardService.getSellerDashboard(req.user.id);
res.status(200).json({
success: true,
data: metrics,
});
} catch (error) {
if (error instanceof ServiceError) {
next(new HttpError(error.statusCode, error.message));
return;
}
next(error);
}
},
async getInvestorDashboard(
req: Request,
res: Response,
next: NextFunction,
): Promise<void> {
try {
if (!req.user) {
throw new HttpError(401, "Authentication required");
}
// Only investors can access investor dashboard
if (req.user.userType !== UserType.INVESTOR && req.user.userType !== UserType.BOTH) {
throw new HttpError(403, "Only investors can access investor dashboard");
}
const metrics = await dashboardService.getInvestorDashboard(req.user.id);
res.status(200).json({
success: true,
data: metrics,
});
} catch (error) {
if (error instanceof ServiceError) {
next(new HttpError(error.statusCode, error.message));
return;
}
next(error);
}
},
};
}