@@ -3,42 +3,52 @@ const Order = require("../models/orderModel");
33// const { setCache } = require("../utils/cache");
44// const { sendToQueue } = require("../utils/sqs");
55const { v4 : uuidv4 } = require ( "uuid" ) ;
6-
7- const DynamoDBClient = require ( ' @aws-sdk/client -dynamodb' ) ;
6+ const { DynamoDBClient } = require ( "@aws-sdk/client-dynamodb" ) ;
7+ const { PutCommand , DynamoDBDocumentClient } = require ( " @aws-sdk/lib -dynamodb" ) ;
88const client = new DynamoDBClient ( { } ) ;
99const docClient = DynamoDBDocumentClient . from ( client ) ;
10-
11-
12- const dynamodb = new AWS . DynamoDB . DocumentClient ( ) ;
1310const ordersTable = "OrdersTable" ;
1411
1512
1613exports . handler = async ( event ) => {
1714 try {
1815
19- console . log ( 'Environment:' , process . env ) ;
16+ const { id , items } = event ;
2017
21- const command = new PutCommand ( {
22- TableName : ordersTable ,
23- Item : {
24- id : uuidv4 ( ) ,
25- name : "test1"
26- } ,
27- } ) ;
18+ if ( ! items || ! items . length ) {
19+ throw 'Items not configured in the payload'
20+ }
2821
29- const order = await docClient . send ( command ) ;
30- console . log ( order ) ;
22+ console . log ( 'Processing Order Id:' , id , ', Item Count: ' , items . length ) ;
3123
32- // await connectDB();
33-
34- // const { customerName, items } = JSON.parse(event.body);
24+ const order = {
25+ id,
26+ items
27+ } ;
3528
36- // const totalAmount = items.reduce((sum, item) => sum + item.price * item.quantity, 0);
29+ const command = new PutCommand ( {
30+ TableName : ordersTable ,
31+ Item : order ,
32+ } ) ;
3733
38- // const order = new Order({ orderId: uuidv4(), customerName, items, totalAmount });
39- // await order.save();
34+ const response = await docClient . send ( command ) ;
35+ console . log ( 'order-created:' , order , response ) ;
36+
37+ // Payload Sample
38+ // {
39+ // "id": "2",
40+ // "items": [
41+ // {
42+ // "id": "1",
43+ // "name": "tv"
44+ // },
45+ // {
46+ // "id": "2",
47+ // "name": "monitor"
48+ // }
49+ // ]
50+ // }
4051
41- // console.log('order-created:', order);
4252
4353 // await setCache(`order:${order.orderId}`, order);
4454
@@ -48,7 +58,7 @@ exports.handler = async (event) => {
4858
4959 return {
5060 statusCode : 201 ,
51- body : JSON . stringify ( { message : "Order created" , orderId : order . orderId } ) ,
61+ body : JSON . stringify ( { message : "Order created" , orderId : order . id } ) ,
5262 } ;
5363 } catch ( e ) {
5464 console . log ( e ) ;
0 commit comments