-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
264 lines (238 loc) · 7.5 KB
/
Copy pathapi.js
File metadata and controls
264 lines (238 loc) · 7.5 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const arc = require('@architect/functions')
// This describes the API
// Since I want a fairly close-mapping with the AWS example I focused on that, even though your data-server might need some other key-structure
// These make things easier to read & match the docs better
// util: convert js date into date-string YYYY-MM-DD
const dateFormat = d => (new Date(d)).toISOString().split('T').shift()
// names for your indexes, as they are in table
const GSI1 = 'SK-GSI1_SK-index'
const GSI2 = 'GSI2_PK-GSI1_SK-index'
const GSI3 = 'GSI1_SK-index'
// util: first date - Wed Dec 31 1969 16:00:00 GMT-0800
const EPOCH = new Date(0)
// util: current date
const NOW = () => new Date()
// util: a month ago
const MONTHAGO = () => new Date(Date.now() - 2.628e+9)
// this is organaized a class, but it has no state, so you could implement it as a bunch of plain methods, if you wanted
class Api {
// Look up Employee Details by Employee ID
// PK="HR-{employeeID}"
async employeeDetailsById (employeeID) {
const { hroe } = await arc.tables()
const r = await hroe.query({
KeyConditionExpression: 'PK=:employeeID',
ExpressionAttributeValues: {
':employeeID': `HR-${employeeID}`
}
})
if (!r.Items.length) {
return undefined
}
// make the detail-object look nice
const item = { PositionPast: [], ID: employeeID, Quotas: [] }
r.Items.forEach(({ PK, SK, GSI1_SK, GSI2_PK, ...record }) => {
Object.keys(record).forEach(k => {
item[k] = record[k]
})
if (SK === 'HR-CONFIDENTIAL') {
item.HireDate = GSI1_SK
}
if (SK.startsWith('J-')) {
item.PositionCurrent = {
ID: SK,
Title: GSI1_SK
}
}
if (SK.startsWith('JH-')) {
item.PositionPast.push({
ID: SK,
Title: GSI1_SK
})
}
if (SK.startsWith('QUOTA-')) {
item.Quotas.push({
Quarter: SK.replace('QUOTA-', ''),
Amount: parseFloat(GSI1_SK)
})
}
if (SK.includes('|')) {
item.Seat = GSI1_SK
item.Region = SK
}
})
return item
}
// Query Employee Details by Employee Name
// GSI1_PK={employeeName}
async employeeIdByName (employeeName) {
const { hroe } = await arc.tables()
const r = await hroe.query({
IndexName: GSI3,
KeyConditionExpression: 'GSI1_SK=:employeeName',
ExpressionAttributeValues: {
':employeeName': employeeName
}
})
return {
Name: employeeName,
ID: r.Items[0].SK
}
}
// Get an employee's current job details only
// PK="HR-{employeeID}", SK.beginsWith("J")
async employeeCurrentJob (employeeID) {
const { hroe } = await arc.tables()
const r = await hroe.query({
KeyConditionExpression: 'PK=:employeeID AND begins_with(SK, :prefix)',
ExpressionAttributeValues: {
':employeeID': `HR-${employeeID}`,
':prefix': 'J-'
}
})
return {
ID: employeeID,
PositionCurrent: {
ID: r.Items[0].SK,
Title: r.Items[0].GSI1_SK
}
}
}
// Get Orders for a customer for a date range
// SK={employeeID}, GSI1_SK.between("{status}-{start}", "{status}-{end}")
async ordersByCustomer (customerID, status = 'OPEN', start = EPOCH, end = NOW()) {
const { hroe } = await arc.tables()
const r = await hroe.query({
IndexName: GSI1,
KeyConditionExpression: 'SK=:customerID AND GSI1_SK BETWEEN :start AND :end',
ExpressionAttributeValues: {
':customerID': customerID,
':start': `${status}#${dateFormat(start)}`,
':end': `${status}#${dateFormat(end)}`
}
})
return r.Items.map(({ PK, SK, GSI1_SK, SalesRepID }) => {
const [Status, OrderDate] = GSI1_SK.split('#')
return {
ID: PK,
Customer: SK,
SalesRep: SalesRepID,
Status,
OrderDate
}
})
}
// Show all Orders in OPEN status for a date range across all customers
// GSI2_PK=parallell([0...N]), GSI1_SK.between("{status}-{start}", "{status}-{end}")
async ordersByStatus (start = 0, end = NOW, status = 'OPEN') {
const { hroe } = await arc.tables()
const r = await Promise.all([...new Array(15)].map((v, bucket) => {
return hroe.query({
IndexName: GSI2,
KeyConditionExpression: 'GSI2_PK=:bucket AND GSI1_SK BETWEEN :start AND :end',
ExpressionAttributeValues: {
':bucket': bucket,
':start': `${status}#${dateFormat(start)}`,
':end': `${status}#${dateFormat(end)}`
}
})
}))
// TODO: map to nicer structure
return r.map(i => i.Items).reduce((a, c) => [...a, ...c], [])
}
// All Employees Hired recently
// SK="HR-CONFIDENTIAL", GSI1_SK > {start}
async employeesRecent (start = MONTHAGO()) {
const { hroe } = await arc.tables()
// TODO: not working
const r = await hroe.query({
IndexName: GSI1,
KeyConditionExpression: 'SK=:key AND GSI1_SK > :start',
ExpressionAttributeValues: {
':start': dateFormat(start),
':key': 'HR-CONFIDENTIAL'
}
})
return {
ID: r.Items[0].PK.replace('HR-', ''),
StartDate: r.Items[0].GSI1_SK
}
}
// Find all employees in specific Warehouse
// GSI1_PK={warehouseID}
async employeesByWarehouse (warehouseID) {
const { hroe } = await arc.tables()
return hroe.query({
IndexName: GSI1,
KeyConditionExpression: 'SK=:warehouseID',
ExpressionAttributeValues: {
':warehouseID': warehouseID
}
})
}
// Get all Order items for a Product including warehouse location inventories
// GSI1_PK={productID}
async ordersByProduct (productID) {
const { hroe } = await arc.tables()
return hroe.query({
IndexName: GSI1,
KeyConditionExpression: 'SK=:productID',
ExpressionAttributeValues: {
':productID': productID
}
})
}
// Get customers by Account Rep
// GSI1_PK={employeeID}
async customerByRep (employeeID) {
const { hroe } = await arc.tables()
return hroe.query({
IndexName: GSI1,
KeyConditionExpression: 'SK=:employeeID',
ExpressionAttributeValues: {
':employeeID': employeeID
}
})
}
// Get orders by Account Rep and date
// GSI1_PK={employeeID}, GSI1_SK="{status}-{start}"
async ordersByRep (employeeID, status = 'OPEN', start = EPOCH) {
const { hroe } = await arc.tables()
return hroe.query({
IndexName: GSI1,
KeyConditionExpression: 'SK=:employeeID AND GSI1_SK=:start',
ExpressionAttributeValues: {
':employeeID': employeeID,
':start': `${status}#${dateFormat(start)}`
}
})
}
// Get all employees with specific Job Title
// GSI1_PK="JH-{title}"
async employeesByTitle (title) {
const { hroe } = await arc.tables()
return hroe.query({
IndexName: GSI1,
KeyConditionExpression: 'SK=:title',
ExpressionAttributeValues: {
':title': `JH-${title}`
}
})
}
// Get inventory by Product and Warehouse
// PK="OE-{productID}", SK={warehouseID}
async inventoryByWarehouse (productID, warehouseID) {
const { hroe } = await arc.tables()
}
// Get total product inventory
// PK="OE-{productID}", SK={productID}
async inventory (productID) {
const { hroe } = await arc.tables()
}
// Get Account Reps ranked by Order Total and Sales Period
// GSI1_PK={quarter}, scanindexForward=False
async accountRepsRankedByTotalAndQuarter (quarter) {
const { hroe } = await arc.tables()
}
}
module.exports = { Api, dateFormat, EPOCH, NOW, MONTHAGO }