-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtriggers.js
138 lines (108 loc) · 4.34 KB
/
triggers.js
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
138
// UpdateGameAndUserOnReviewInsert
exports = async function(changeEvent) {
const insertedReview = changeEvent.fullDocument;
const gameIdObj = BSON.ObjectId(insertedReview.gameID);
const author = insertedReview.author;
const gamesCollection = context.services.get("rotten-carrots-database").db("rotten-carrots-database").collection("games");
const game = await gamesCollection.findOne({_id: gameIdObj});
const reviewsCollection = context.services.get("rotten-carrots-database").db("rotten-carrots-database").collection("reviews");
const reviews = await reviewsCollection.find({gameID: insertedReview.gameID}).toArray();
const usersCollection = context.services.get("rotten-carrots-database").db("rotten-carrots-database").collection("users");
const user = await usersCollection.findOne({nickname: author});
// if user has already reviewed the game - delete the duplicate
if (user.reviews.length > 0){
if (user.reviews.some(review => String(review.gameID) === String(gameIdObj))) {
console.log("User has already reviewed the game.");
await reviewsCollection.deleteOne({_id: insertedReview._id})
return;
}
}
const userReviews = user.reviews;
userReviews.push(insertedReview)
if (reviews.length > 0) {
let totalRating = 0;
for (const review of reviews) {
totalRating += review.carrotRate;
}
const averageRating = totalRating / reviews.length;
await gamesCollection.updateOne(
{ _id: gameIdObj },
{
$set: {
carrotRate: averageRating,
reviews: reviews
}
}
);
await usersCollection.updateOne(
{nickname: author},
{
$set: {
reviews: userReviews
}
}
);
console.log("Game and User documents updated successfully.");
console.log(averageRating);
}
};
// ValidateNickname
exports = async function(changeEvent) {
const insertedUser = changeEvent.fullDocument;
const usersCollection = context.services.get("rotten-carrots-database").db("rotten-carrots-database").collection("users");
const usersWithNickname = await usersCollection.find({nickname: insertedUser.nickname}).toArray();
if(usersWithNickname.length > 1){
console.log("User with this nickname already exists");
await usersCollection.deleteOne(insertedUser);
}
};
// UpdateUserOnAuctionInsert
exports = async function(changeEvent) {
const insertedAuction = changeEvent.fullDocument;
const ownerIdObj = BSON.ObjectId(insertedAuction.ownerID);
const usersCollection = context.services.get("rotten-carrots-database").db("rotten-carrots-database").collection("users");
const user = await usersCollection.findOne({_id: ownerIdObj});
const userAuctions = user.auctions;
userAuctions.push(insertedAuction);
await usersCollection.updateOne(
{_id: ownerIdObj},
{
$set: {
auctions: userAuctions
}
}
);
};
// UpdateAuctionStatusInUser
exports = async function(changeEvent) {
const updatedAuction = changeEvent.fullDocument;
const ownerIdObj = BSON.ObjectId(updatedAuction.ownerID);
const usersCollection = context.services.get("rotten-carrots-database").db("rotten-carrots-database").collection("users");
const user = await usersCollection.findOne({_id: ownerIdObj});
const userAuctions = user.auctions;
for(auction of userAuctions){
if(auction._id.toString()==updatedAuction._id.toString()){
auction.isActive = updatedAuction.isActive;
break;
}
}
await usersCollection.updateOne(
{_id: ownerIdObj},
{
$set: {
auctions: userAuctions
}
}
);
};
// AddLogOnNewsInsert
exports = function(changeEvent) {
const { fullDocument } = changeEvent;
const logCollection = context.services.get("rotten-carrots-database").db("rotten-carrots-database").collection("news-log");
const logDocument = {
timestamp: new Date(),
message: "New news inserted",
newsId: fullDocument._id,
};
return logCollection.insertOne(logDocument);
};