1
+ import { SQSClient , SendMessageCommand } from "@aws-sdk/client-sqs" ;
2
+ const sqsClient = new SQSClient ( { region : "eu-central-1" } ) ;
3
+
1
4
import { DynamoDBClient , PutItemCommand } from "@aws-sdk/client-dynamodb" ; // ES Modules import
2
5
3
6
const dynamoDBClient = new DynamoDBClient ( ) ;
4
7
8
+
5
9
export const handler = async ( event ) => {
6
- const orderId = event . queryStringParameters . orderId
7
- const connectionId = event . requestContext . connectionId
10
+ let response ;
11
+ let dataSQS ;
8
12
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
+ }
11
21
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' ,
14
74
Item : {
15
- orderId : { S : orderId } ,
16
- connectionId : { S : connectionId }
75
+ orderId : { S : randomString } ,
76
+ basket : { S : JSON . stringify ( basket ) } ,
77
+ orderStatus : { S : "pending" }
17
78
}
18
79
}
19
80
20
- const command = new PutItemCommand ( params ) ;
81
+ const command = new PutItemCommand ( paramsDynamo ) ;
21
82
22
83
try {
23
84
const res = await dynamoDBClient . send ( command ) ;
24
-
25
85
console . log ( res )
26
- return {
27
- statusCode : 200 ,
28
- body : JSON . stringify ( 'Record added successfully' )
29
- } ;
30
86
}
31
87
catch ( error ) {
32
88
console . log ( error )
@@ -35,4 +91,57 @@ export const handler = async (event) => {
35
91
body : JSON . stringify ( 'Error adding record' )
36
92
} ;
37
93
}
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
+ }
38
147
} ;
0 commit comments