forked from diegotorres03/serverless-e2e-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (92 loc) · 2.88 KB
/
index.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
const aws = require('aws-sdk')
class OrderItem {
constructor(params) {
this.name = params.name || ''
this.type = params.type || ''
this.qty = Number(params.qty) || 0
}
}
class Order {
/** @param {OrderJSON} json */
constructor(json) {
this.id = json.id
this.customer = json.customer
this.staff = json.staff
this._createdAt = json._createdAt || Date.now()
this._filledAt = json._createdAt || null
this._expireOn = json._expireOn || (new Date().getTime() / 1000) + 10 * 60
/** @param {OrderItem[]} items */
this.items = Array.isArray(json.items) ?
json.items.map(item => new OrderItem(item)) : []
}
addItem(name, type, qty) {
this.items.push({ name, type, qty })
}
}
const ordersTable = process.env.ORDERS_TABLE
const dynamo = new aws.DynamoDB.DocumentClient({ region: 'us-east-2' })
// README: [apidoc](https://apidocjs.com)
/**
* @api {patch} /orders/:cusomer/:id change status of an order
* @apiName UpdateOrderStatus
* @apiGroup Orders
*
* @apiParam {string} customer customer id
* @apiParam {string} id order id
* @apiParamExample {json} Request-Example:
* {
* "customer": "diegotrs",
* "id": "4711"
* }
*
* @apiBody {String} status new order status
*
*
* @apiSuccess (200) {Order} the newly created order
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "username": "alejo",
* "date": "1656017418934",
* "notes": [
* "sample text"
* ],
* "options": {
* "bowType": "recurve",
* "category": "junior",
* "gender": "male"
* },
* "value": 90,
* "_autoapprove": 1656018019
* }
*/
async function handler(event) {
const eventJson = JSON.stringify(event, null, 2)
console.log(eventJson)
const orderId = event.pathParameters.id
const cusotmer = event.pathParameters.customer
const { status, _filledAt } = JSON.parse(event.body)
let response = null
// [ ] 3.2.3: use table on updateOrder - update an order on dynamodb table
response = await dynamo.update({
TableName: ordersTable,
Key: {
id: orderId,
customer: cusotmer,
},
ExpressionAttributeNames: { '#status': 'status', '#filledAt': '_filledAt' },
ExpressionAttributeValues: { ':status': status, ':filledAt': _filledAt },
UpdateExpression: `set #status = :status, #filledAt = :filledAt`,
}).promise()
return {
body: JSON.stringify(response),
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Headers" : "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,PATCH"
},
}
}
module.exports = { handler }