Skip to content

Commit 370635d

Browse files
authored
Merge pull request #2742 from awsdocs/add_item_tracker
Add DDB Item Tracker app for AWS SDK for JS (v3)
2 parents 9c4063d + b58cf22 commit 370635d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2384
-0
lines changed

javascriptv3/example_code/cross-services/ddb-item-tracker/README.md

Lines changed: 748 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
4+
Purpose:
5+
app.js is the main file in the Amazon DynamoDB Item Tracker example app.
6+
It interacts with the Express web framework to execute code.
7+
8+
INPUTS:
9+
- REGION
10+
11+
*/
12+
// snippet-start:[cross-service.JavaScript.ddb-item-tracker.app]
13+
import express from "express";
14+
import { v4 as uuidv4 } from "uuid";
15+
const port = process.env.PORT || 3000;
16+
const app = express();
17+
import bodyParser from "body-parser";
18+
import path from "path";
19+
import { fileURLToPath } from "url";
20+
const __filename = fileURLToPath(import.meta.url);
21+
import { UpdateCommand, PutCommand, ScanCommand } from "@aws-sdk/lib-dynamodb";
22+
import { ddbDocClient } from "./public/libs/ddbDocClient.js";
23+
import { SendEmailCommand } from "@aws-sdk/client-ses";
24+
import { sesClient, REGION } from "./public/libs/sesClient.js";
25+
import { s3Client } from "./public/libs/s3Client.js";
26+
import { PutObjectCommand } from "@aws-sdk/client-s3";
27+
28+
// Setting path for public directory
29+
const __dirname = path.dirname(__filename);
30+
const static_path = path.join(__dirname, "/public");
31+
app.use(express.static(static_path));
32+
app.use(express.urlencoded({ extended: true }));
33+
34+
// Variables to update.
35+
export const tableName = "TABLE_NAME";
36+
export const bucketName = "BUCKET_NAME";
37+
export const senderEmail = "SENDER_EMAIL_ADDRESS";
38+
39+
app.use(bodyParser.json());
40+
41+
app.post("/add", (req, res) => {
42+
const body = req.body;
43+
const id = uuidv4();
44+
const d = new Date();
45+
const month = d.getMonth() + 1;
46+
const day = d.getDate();
47+
const todaydate =
48+
d.getFullYear() +
49+
"/" +
50+
(month < 10 ? "0" : "") +
51+
month +
52+
"/" +
53+
(day < 10 ? "0" : "") +
54+
day;
55+
const params = {
56+
TableName: tableName,
57+
Item: {
58+
id: id,
59+
guide: body.guide,
60+
description: body.description,
61+
status: body.status,
62+
date: todaydate,
63+
},
64+
};
65+
const run = async () => {
66+
try {
67+
const data = await ddbDocClient.send(new PutCommand(params));
68+
console.log("Added item:", JSON.stringify(data, null, 2));
69+
console.log(data);
70+
res.contentType = "application/json";
71+
res.send(data);
72+
} catch (err) {
73+
console.error(
74+
"Unable to add item. Error JSON:",
75+
JSON.stringify(err, null, 2)
76+
);
77+
}
78+
};
79+
run();
80+
});
81+
82+
app.post("/request", (req, res) => {
83+
var params = {
84+
TableName: tableName,
85+
};
86+
87+
const run = async () => {
88+
try {
89+
const data = await ddbDocClient.send(new ScanCommand(params));
90+
console.log("data", data);
91+
res.send(data);
92+
} catch (err) {
93+
console.log("Error", err);
94+
}
95+
};
96+
run();
97+
});
98+
99+
app.post("/changewi", (req, res) => {
100+
const body = req.body;
101+
console.log(req.body);
102+
var params = {
103+
TableName: tableName,
104+
Key: {
105+
id: req.body.id,
106+
},
107+
UpdateExpression: "set #description=:d, #status=:s",
108+
ExpressionAttributeValues: {
109+
":d": req.body.description,
110+
":s": req.body.status,
111+
},
112+
ExpressionAttributeNames: {
113+
"#description": "description",
114+
"#status": "status",
115+
},
116+
};
117+
const run = async () => {
118+
try {
119+
const data = await ddbDocClient.send(new UpdateCommand(params));
120+
res.contentType = "application/json";
121+
res.send(data);
122+
} catch (err) {
123+
console.error(err);
124+
}
125+
};
126+
run();
127+
});
128+
129+
app.post("/uploadCSV", (req, res) => {
130+
const d = new Date();
131+
const month = d.getMonth() + 1;
132+
const day = d.getDate();
133+
const hour = d.getHours();
134+
const minute = d.getMinutes();
135+
const todaydate =
136+
d.getFullYear() +
137+
(month < 10 ? "0" : "") +
138+
month +
139+
(day < 10 ? "0" : "") +
140+
day +
141+
hour +
142+
minute;
143+
const uploadParams = {
144+
Bucket: bucketName,
145+
Body: req.body.csv,
146+
Key: "Items_" + todaydate + ".csv",
147+
};
148+
const run = async () => {
149+
try {
150+
const data = await s3Client.send(new PutObjectCommand(uploadParams));
151+
const linkToCSV =
152+
"https://s3.console.aws.amazon.com/s3/buckets/" +
153+
uploadParams.Bucket +
154+
"?region=" +
155+
REGION +
156+
"&tab=objects";
157+
console.log("Success. Report uploaded to " + linkToCSV + ".");
158+
console.log("Email", req.body.email);
159+
try {
160+
const emailParams = {
161+
Destination: {
162+
/* required */
163+
CcAddresses: [
164+
/* more items */
165+
],
166+
ToAddresses: [
167+
req.body.email, //RECEIVER_ADDRESS
168+
/* more To-email addresses */
169+
],
170+
},
171+
Message: {
172+
/* required */
173+
Body: {
174+
/* required */
175+
Html: {
176+
Charset: "UTF-8",
177+
Data:
178+
"<h1>Hello!</h1>" +
179+
"<p> The CSV has been uploaded to <a href=" +
180+
linkToCSV +
181+
">here.</a></p>",
182+
},
183+
},
184+
Subject: {
185+
Charset: "UTF-8",
186+
Data: "Report ready.",
187+
},
188+
},
189+
Source: "SENDER_EMAIL_ADDRESS",
190+
ReplyToAddresses: [
191+
/* more items */
192+
],
193+
};
194+
const data = await sesClient.send(new SendEmailCommand(emailParams));
195+
console.log("Success. Email sent.", data);
196+
res.contentType = "application/json";
197+
res.send(data);
198+
} catch (err) {
199+
console.log("Error", err);
200+
}
201+
} catch (err) {
202+
console.log("Error", err);
203+
}
204+
};
205+
run();
206+
module.exports = run();
207+
});
208+
209+
app.post("/report", (req, res) => {
210+
// Helper function to send an email to user.
211+
// Set the parameters
212+
console.log("This is the email address: ", req.body.email);
213+
const params = {
214+
Destination: {
215+
/* required */
216+
CcAddresses: [
217+
/* more items */
218+
],
219+
ToAddresses: [
220+
req.body.email, //RECEIVER_ADDRESS
221+
/* more To-email addresses */
222+
],
223+
},
224+
Message: {
225+
/* required */
226+
Body: {
227+
/* required */
228+
Html: {
229+
Charset: "UTF-8",
230+
Data:
231+
"<h1>Hello!</h1>" +
232+
"<p> The Amazon DynamoDB table " +
233+
tableName +
234+
" has been updated with PPE information <a href='https://" +
235+
REGION +
236+
".console.aws.amazon.com/dynamodb/home?region=" +
237+
REGION +
238+
"#item-explorer?table=" +
239+
tableName +
240+
"'>here.</a></p>",
241+
},
242+
},
243+
Subject: {
244+
Charset: "UTF-8",
245+
Data: "PPE image report ready.",
246+
},
247+
},
248+
Source: senderEmail,
249+
ReplyToAddresses: [
250+
/* more items */
251+
],
252+
};
253+
const run = async () => {
254+
try {
255+
const data = await sesClient.send(new SendEmailCommand(params));
256+
console.log("Success. Email sent.", data);
257+
res.contentType = "application/json";
258+
res.send(data);
259+
} catch (err) {
260+
console.log("Error", err);
261+
}
262+
};
263+
run();
264+
});
265+
266+
app.listen(port, () => {
267+
console.log(`Listening on port ${port}`);
268+
});
269+
// snippet-end:[cross-service.JavaScript.ddb-item-tracker.app]
31.7 KB
Loading
113 KB
Loading
42.2 KB
Loading
118 KB
Loading
85.3 KB
Loading
66.6 KB
Loading
7.29 KB
Loading
21.5 KB
Loading

0 commit comments

Comments
 (0)