forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreviewAuth.js
More file actions
51 lines (46 loc) · 1.51 KB
/
Copy pathreviewAuth.js
File metadata and controls
51 lines (46 loc) · 1.51 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
import User from "../models/User.js";
import { APIError } from "../middlewares/errorHandler.js";
/**
* Ensures user has purchased/enrolled or created the course/book.
* @param {object} params
* @param {import("mongoose").Types.ObjectId|string} params.userId
* @param {object} params.item - Course or Book Mongoose doc
* @param {"course" | "book"} params.itemType
*/
export const verifyItemPurchase = async ({ userId, item, itemType }) => {
const userIdStr = userId.toString();
if (itemType === "course") {
if (item.createdBy && item.createdBy.toString() === userIdStr) {
return true;
}
if (
item.enrolledUsers &&
item.enrolledUsers.some((id) => id.toString() === userIdStr)
) {
return true;
}
} else if (itemType === "book") {
if (item.author && item.author.toString() === userIdStr) {
return true;
}
}
const user = await User.findById(userId).select("purchasedCourses purchasedBooks");
if (!user) {
throw new APIError("User not found", 404);
}
if (itemType === "course") {
const isPurchased = user.purchasedCourses?.some(
(p) => p.courseId && p.courseId.toString() === item._id.toString()
);
if (isPurchased) return true;
} else if (itemType === "book") {
const isPurchased = user.purchasedBooks?.some(
(p) => p.bookId && p.bookId.toString() === item._id.toString()
);
if (isPurchased) return true;
}
throw new APIError(
`You must purchase or enroll in this ${itemType} to submit a review`,
403
);
};