-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
111 lines (94 loc) · 3.17 KB
/
handler.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
"use strict";
const orderManagement = require("./order-manager");
const kinesisHelper = require("./kinesis-helper");
const cakeProducerManager = require("./cake-producer-manager");
const deliveryManager = require("./delivery-manager");
const createResponse = (statusCode, message) => {
return {
statusCode: statusCode,
body: JSON.stringify(message)
}
}
module.exports.createOrder = async (event) => {
const body = JSON.parse(event.body);
const order = orderManagement.createOrder(body);
return orderManagement.placeNewOrder(order).then(() => {
return createResponse(201, "Order Created!");
}).catch((error)=>{
return createResponse(400, error);
})
};
module.exports.orderFulfillment = async(event) => {
const body = JSON.parse(event.body);
const orderId = body.orderId;
const fulfillmentId = body.fulfillmentId;
return orderManagement.fulfillOrder(orderId, fulfillmentId).then(() => {
return createResponse(200, `Order with orderId: ${orderId} was sent to delivery!`);
}).catch(error =>{
return createResponse(400, error);
})
}
module.exports.notifyCakeProducer = async (event) => {
const data = kinesisHelper.getRecords(event);
const ordersPlaced = records.filter((r) => r.eventType == "order_placed");
if (ordersPlaced <= 0) {
return "There is no data";
}
cakeProducerManager
.handlePlaceOrders(ordersPlaced)
.then(() => {
return "everything went well";
})
.catch((error) => {
return error;
});
};
module.exports.notifyExternalParties = async (event) => {
const data = kinesisHelper.getRecords(event);
const ordersPlaced = records.filter((r) => r.eventType == "order_placed");
const cakeProducerPromise = getCakeProducerPromise(records);
const deliveryPromise = getDeliveryPromise(records);
return Promise.all([cakeProducerPromise, deliveryPromise]).then(() => {
return "everything went well";
})
.catch((error) => {
return error;
});
};
module.exports.notifyDeliveryCompany = async (event) =>{
console.log('delivery company endpoint');
return 'done';
}
module.exports.orderDelivered = async(event) => {
const body = JSON. parse (event.body);
const orderId = body.orderId;
const deliveryCompanyId = body.deliveryCompanyId;
const orderReview = body.orderReview;
return deliveryManager.orderDelivered(orderId, deliveryCompanyId, orderReview). then (() => {
return createResponse (200, 'Order with ${orderId} was delivered successfully by companyId ${deliveryCompanyId})');
}).catch(error => {
return createResponse (400, error);
});
}
module.exports.notifyCustomerService = async (event) =>{
console.log('customer service endpoint');
return 'done';
}
function getCakeProducerPromise(records) {
const ordersPlaced = records.filter((r) => r.eventType === "order_placed");
if (ordersPlaced.length > 0) {
return cakeProducerManager.handlePlaced0rders(ordersPlaced);
} else {
return null;
}
}
function getDeliveryPromise(records) {
const orderFulfilled = records.filter(
(r) => r.eventType === "order fulfilled"
);
if (orderFulfilled.length > 0) {
return deliveryManager.deliveryOrder(orderFulfilled);
} else {
return null;
}
}