-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder-manager.js
80 lines (70 loc) · 1.96 KB
/
order-manager.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
'use strict'
const uuidv1 = require('uuid/v1');
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB.DocumentClient();
const kinesis = new AWS.kinesis();
const TABLE_NAME = process.env.OrderTableName;
const STREAM_NAME = process.env.orderStreamName;
module.export.createOrder = body => {
const order = {
orderId: uuidv1 (),
name: body. name,
address: body.address,
productId: body.productId,
quantity: body.quantity,
orderDate: Date.now(),
eventType: 'order_place'
};
return order;
}
module.export.placeNewOrder = order => {
return this.saveOrder(order).then((order) => {
return placeOrderStream(order);
});
}
module.exports.fulfillOrder = (orderId, fulfillmentId) => {
return getOrder(orderId).then(savedOrder => {
const order = createFullfilledOrder(savedOrder, fulfillmentId);
return savedOrder(order).then(order =>{
return placeOrderStream(order);
})
})
}
module.exports.updateOrderForDelivery = orderId => {
return getOrder(orderId).then(order =>{
order.sentToDeliveryDate = Date.now();
return order;
})
}
module.exports.saveOrder = order => {
const params = {
TableName: TABLE_NAME,
Item: order
};
return dynamo.put(params).promise();
}
function placeOrderStream(order) {
const params = {
Data: JSON.stringify(order),
PartitionKey: order.orderId,
StreamName: STREAM_NAME
}
return kinesis.putRecord(params).promise();
}
function getOrder(orderId){
const params = {
Key: {
orderId: orderId
},
TableName: TABLE_NAME
}
dynamo.get(params).promise().then(result => {
return result.Item;
})
}
function createFullfilledOrder(savedOrder, fulfillmentId) {
savedOrder.fulfillmentId = fulfillmentId;
savedOrder.fulfillmentDate = Date.now();
savedOrder.eventType = 'order_fulfilled';
return savedOrder;
}