forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserRoutes.js
More file actions
137 lines (127 loc) · 3.54 KB
/
Copy pathuserRoutes.js
File metadata and controls
137 lines (127 loc) · 3.54 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
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
import express from "express";
import { protect } from "../middlewares/authMiddleware.js";
import { uploadImage } from "../middlewares/upload.js";
import {
updateUser,
getUser,
deleteUser,
followUser,
unfollowUser,
getFollowers,
getFollowing,
getFollowersCount,
getFollowingCount,
checkIfFollowing,
getRecommendations,
getUserStats,
getLearningDashboard,
} from "../controllers/userController.js";
import { searchAll } from "../controllers/searchController.js";
import {
cacheMiddleware,
invalidateCacheMiddleware,
} from "../middlewares/cache.js";
import { CACHE_TTL, CACHE_KEYS } from "../utils/cache.js";
const router = express.Router();
// Cache key generators
const userCacheKey = (req) => `${CACHE_KEYS.USER}${req.params.id}`;
const userStatsCacheKey = (req) => `${CACHE_KEYS.USER}${req.params.id}:stats`;
const followersCacheKey = (req) =>
`${CACHE_KEYS.USER}${req.params.userId}:followers`;
const followingCacheKey = (req) =>
`${CACHE_KEYS.USER}${req.params.userId}:following`;
// Get personalized recommendations - cached for 10 minutes (must be before /:id)
router.get(
"/recommendations",
protect,
cacheMiddleware(CACHE_TTL.USERS, (req) =>
`${CACHE_KEYS.USER}${req.user._id}:recommendations`
),
getRecommendations
);
// Update user profile (with avatar upload) - invalidates user cache
router.put(
"/update/:id",
protect,
(req, res, next) => {
if (req.user.role !== "admin" && req.user._id.toString() !== req.params.id) {
return res.status(403).json({ success: false, message: "Not authorized to update this profile", data: null });
}
next();
},
uploadImage.single("avatar"),
invalidateCacheMiddleware([`${CACHE_KEYS.USER}*`]),
updateUser
);
// Get user by ID - cached for 10 minutes
router.get(
"/:id",
protect,
cacheMiddleware(CACHE_TTL.USERS, userCacheKey),
getUser
);
// Delete user - invalidates user cache
router.delete(
"/:id",
protect,
(req, res, next) => {
if (req.user.role !== "admin" && req.user._id.toString() !== req.params.id) {
return res.status(403).json({ success: false, message: "Not authorized to delete this user", data: null });
}
next();
},
invalidateCacheMiddleware([`${CACHE_KEYS.USER}*`]),
deleteUser
);
// Follow/Unfollow routes - invalidates follower/following caches
router.post(
"/follow/:userId",
protect,
invalidateCacheMiddleware([`${CACHE_KEYS.USER}*:followers`, `${CACHE_KEYS.USER}*:following`]),
followUser
);
router.delete(
"/unfollow/:userId",
protect,
invalidateCacheMiddleware([`${CACHE_KEYS.USER}*:followers`, `${CACHE_KEYS.USER}*:following`]),
unfollowUser
);
// Get followers/following - cached for 10 minutes
router.get(
"/:userId/followers",
protect,
cacheMiddleware(CACHE_TTL.USERS, followersCacheKey),
getFollowers
);
router.get(
"/:userId/following",
protect,
cacheMiddleware(CACHE_TTL.USERS, followingCacheKey),
getFollowing
);
router.get(
"/:userId/followers/count",
protect,
cacheMiddleware(CACHE_TTL.USERS, (req) =>
`${CACHE_KEYS.USER}${req.params.userId}:followers:count`
),
getFollowersCount
);
router.get(
"/:userId/following/count",
protect,
cacheMiddleware(CACHE_TTL.USERS, (req) =>
`${CACHE_KEYS.USER}${req.params.userId}:following:count`
),
getFollowingCount
);
router.get("/:userId/check-following", protect, checkIfFollowing);
// Get user statistics - cached for 10 minutes
router.get(
"/:id/stats",
protect,
cacheMiddleware(CACHE_TTL.USERS, userStatsCacheKey),
getUserStats
);
router.get("/me/learning", protect, getLearningDashboard);
export default router;