forked from diegotorres03/serverless-e2e-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (107 loc) · 3.49 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const aws = require('aws-sdk')
const html = function (templates, ...values) {
let str = ''
templates.forEach((template, index) => {
str += template
str = values[index] ? str + values[index] : str
})
return str.trim()
}
const dynamo = new aws.DynamoDB.DocumentClient({ region: 'us-east-2' })
// const ordersTable = 'restApiStack-orders46FA7C19-1DABCQPL86S99'
const ordersTable = process.env.ORDERS_TABLE || require('../backend.json').backend.ordersTableName
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 })
}
}
/**
* @param {Order[]} orders
* @return {string}
*/
function toHtml(orders) {
console.log('orders', orders)
const string = orders.map(order => html`
<caffe-order-history-item customer="${order.customer}" id="${order.id}">
${order.items.map(item =>
html`<caffe-cart-item name="${item.name}" type="${item.type}" qty="${item.qty}" editable="false"></caffe-cart-item>`).join('\n')}
</caffe-order-history-item>
`)
console.log(string)
return string.join('\n')
}
/**
* @param {Order[]} orders
* @return {string}
*/
function toJson(orders) {
return JSON.stringify(orders)
}
// README: [apidoc](https://apidocjs.com)
/**
* @api {get} /orders list existing orders
* @apiName GetOrders
* @apiGroup Orders
*
*
* @apiSuccess (200) {Order[]} data 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 contentType = event.headers['content-type'] || event.headers['Content-Type']
console.log(contentType)
// [ ] 3.2.1: use table on getOrders - get all orders from dynamodb [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property)
const res = await dynamo.scan({
TableName: ordersTable,
}).promise()
console.log(res.Items)
const orders = res.Items.map(item => new Order(item))
const isHtml = contentType === 'text/html'
return {
body: isHtml ? toHtml(orders) : toJson(orders),
statusCode: 200,
headers: {
"Content-Type": isHtml ? "text/html" : "application/json",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,PATCH"
},
}
}
module.exports = { handler }