-
Notifications
You must be signed in to change notification settings - Fork 4
/
CreateOrderRequestHandler.js
56 lines (44 loc) · 1.44 KB
/
CreateOrderRequestHandler.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
const orderCollectionName = "Order";
const requestCollectionName = "CreateOrderRequest";
const serviceName = "mongodb-atlas";
const databaseName = "ObjectsDatabase";
exports = async function(changeEvent) {
try {
var fullDoc = changeEvent.fullDocument;
if (fullDoc.status !== "Pending") {
return;
}
const requestId = changeEvent.documentKey._id;
const db = context.services.get(serviceName).db(databaseName);
const content = fullDoc.content;
const creatorId = fullDoc._creatorId;
var orderId;
var status;
var rejectedReason;
if(content.items == undefined || content.items.length < 1) {
status = "Rejected";
rejectedReason = "There are no items in this order!";
} else {
const orderCollection = db.collection(orderCollectionName);
status = "Approved";
const order = {
"_id" : new BSON.ObjectId(),
"_creatorId" : creatorId,
"status": "Approved",
"content": content,
};
orderId = (await orderCollection.insertOne(order)).insertedId;
}
const requestCollection = db.collection(requestCollectionName);
const update = {
"$set": {
"status" : status,
"order": orderId,
"rejectedReason": rejectedReason
}
}
await requestCollection.updateOne({"_id": requestId}, update);
} catch(err) {
console.log("error while creating response: ", err.message);
}
};