Skip to content

Commit 8bd66e6

Browse files
Alberto RizziAlberto Rizzi
authored andcommitted
fix vari
1 parent c006181 commit 8bd66e6

File tree

3 files changed

+124
-15
lines changed

3 files changed

+124
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Il presente progetto ha lo scopo di implementare un prototipo di flusso di un e-
33

44
## Autori
55
- Lorenzo Marcolli (mat. 08302A)
6-
- Alberto Rizzi (mart. 08303A)
6+
- Alberto Rizzi (mat. 08303A)
77

88
## Diagram flow
99
<div align="center">

assets/presentazione_rizzi.pdf

37.7 KB
Binary file not shown.

lambda/PurchaseProduct.js

Lines changed: 123 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,88 @@
1+
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
2+
const sqsClient = new SQSClient({ region: "eu-central-1" });
3+
14
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; // ES Modules import
25

36
const dynamoDBClient = new DynamoDBClient();
47

8+
59
export const handler = async (event) => {
6-
const orderId = event.queryStringParameters.orderId
7-
const connectionId = event.requestContext.connectionId
10+
let response;
11+
let dataSQS;
812

9-
console.log(orderId)
10-
console.log(connectionId)
13+
// check if body is empty
14+
if (!event.body) {
15+
response = {
16+
statusCode: 400,
17+
body: JSON.stringify({ "message": "missing_information" }),
18+
}
19+
return response
20+
}
1121

12-
const params = {
13-
TableName: 'CallbackDB',
22+
// check if body is valid JSON
23+
try {
24+
JSON.parse(event.body)
25+
}
26+
catch (error) {
27+
response = {
28+
statusCode: 400,
29+
body: JSON.stringify({ "message": "invalid_json" }),
30+
}
31+
return response
32+
}
33+
34+
// check if basket is empty
35+
if (!JSON.parse(event.body).basket) {
36+
response = {
37+
statusCode: 400,
38+
body: JSON.stringify({ "message": "empty_basket" }),
39+
}
40+
return response
41+
}
42+
43+
// check if event has shipping address
44+
if (!JSON.parse(event.body).shippingAddress) {
45+
response = {
46+
statusCode: 400,
47+
body: JSON.stringify({ "message": "missing_shipping_address" }),
48+
}
49+
return response
50+
}
51+
52+
// check if event has paymentDetails
53+
if (!JSON.parse(event.body).paymentDetails) {
54+
response = {
55+
statusCode: 400,
56+
body: JSON.stringify({ "message": "missing_payment_details" }),
57+
}
58+
return response
59+
}
60+
61+
// create random string to identify order
62+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
63+
let randomString = '';
64+
65+
for (let i = 0; i < 20; i++) {
66+
const randomIndex = Math.floor(Math.random() * characters.length);
67+
randomString += characters.charAt(randomIndex);
68+
}
69+
70+
const basket = JSON.parse(event.body).basket
71+
72+
const paramsDynamo = {
73+
TableName: 'Order',
1474
Item: {
15-
orderId: { S: orderId },
16-
connectionId: { S: connectionId }
75+
orderId: { S: randomString },
76+
basket: { S: JSON.stringify(basket) },
77+
orderStatus: { S: "pending" }
1778
}
1879
}
1980

20-
const command = new PutItemCommand(params);
81+
const command = new PutItemCommand(paramsDynamo);
2182

2283
try {
2384
const res = await dynamoDBClient.send(command);
24-
2585
console.log(res)
26-
return {
27-
statusCode: 200,
28-
body: JSON.stringify('Record added successfully')
29-
};
3086
}
3187
catch (error) {
3288
console.log(error)
@@ -35,4 +91,57 @@ export const handler = async (event) => {
3591
body: JSON.stringify('Error adding record')
3692
};
3793
}
94+
95+
dataSQS = JSON.parse(event.body)
96+
dataSQS.orderId = randomString
97+
98+
const params = {
99+
DelaySeconds: 0,
100+
MessageAttributes: {
101+
Author: {
102+
DataType: "String",
103+
StringValue: "Preeti",
104+
}
105+
},
106+
MessageBody: JSON.stringify(dataSQS),
107+
MessageGroupId: randomString,
108+
FifoQueue: true,
109+
MessageDeduplicationId: randomString,
110+
MessageRetentionPeriod: 345600,
111+
QueueUrl: "https://sqs.eu-central-1.amazonaws.com/495456954059/OrdersQueueFIFO.fifo"
112+
};
113+
114+
// Send the order to SQS
115+
try {
116+
const data = await sqsClient.send(new SendMessageCommand(params));
117+
if (data) {
118+
console.log("Success, message sent. MessageID:", data.MessageId);
119+
const bodyMessage = {
120+
message: 'Message Send to SQS - MessageId: ' + data.MessageId,
121+
orderId: randomString,
122+
callbackUrl: "wss://wkr7p95088.execute-api.eu-central-1.amazonaws.com/production/"
123+
}
124+
125+
response = {
126+
headers: {
127+
"Access-Control-Allow-Headers": "Content-Type",
128+
"Access-Control-Allow-Origin": "*",
129+
"Access-Control-Allow-Methods": "POST"
130+
},
131+
132+
statusCode: 200,
133+
body: JSON.stringify(bodyMessage),
134+
};
135+
}
136+
else {
137+
response = {
138+
statusCode: 500,
139+
body: JSON.stringify({ "message": "error_sending_message_to_SQS" }),
140+
};
141+
}
142+
return response;
143+
}
144+
catch (err) {
145+
console.log("Error", err);
146+
}
38147
};

0 commit comments

Comments
 (0)