hitherto_insignia

Untitled

Apr 16th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. var AWS = require('aws-sdk');
  2. AWS.config.update({region: process.env.AWS_REGION});
  3. var ddb = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
  4. let response;
  5. let err;
  6.  
  7. /**
  8. * Lambda function for fetching user details.
  9. */
  10. exports.lambdaHandler = function(event, context, callback) {
  11. try{
  12. console.log('checking event data for userId and bookingId');
  13. console.log(JSON.stringify(event));
  14.  
  15. //get query string values
  16. let pgId = event.queryStringParameters.pgId + '_users';
  17. let userId = event.pathParameters.user;
  18. let bookingId = event.queryStringParameters.bookingId;
  19.  
  20. console.log('pgId, userId, bookingId', pgId, userId, bookingId);
  21.  
  22. if(userId != undefined) {
  23. findDetailsByUserId(pgId, userId, callback)
  24. .then(callback(null, response));
  25. } else {
  26. findDetailsByBookingId(pgId, bookingId);
  27. }
  28.  
  29. } catch(err) {
  30. console.log(err);
  31. callback("Error", err);
  32. }
  33. }
  34.  
  35. function findDetailsByBookingId(pgId, bookingId){
  36. console.log('entered find details by bookingId stage...');
  37. let params = {
  38. Table: 'smartpg',
  39.  
  40. }
  41. }
  42.  
  43. async function findDetailsByUserId(pgId, userId, callback){
  44. console.log('entered find details by userId stage...');
  45.  
  46. let params = {
  47. TableName: 'smartpg',
  48. Key: {
  49. 'ID' : pgId,
  50. 'Metadata' : userId
  51. }
  52. }
  53.  
  54. console.log('About to execute ddb get call...');
  55. ddb.get(params, function(err, data) {
  56. console.log('fetching from ddb stage...');
  57.  
  58. if(err) {
  59. console.log('error encountered when fetching from database');
  60. return err;
  61. } else {
  62. console.log('successfully fetched from database',data);
  63. response = {
  64. 'statusCode' : 200,
  65. 'headers' : {
  66. 'cache-control' : 'private, max-age = 3600',
  67. 'Access-Control-Allow-Origin' : '*',
  68. 'content-type' : 'application/json',
  69. },
  70. 'body' : JSON.stringify(data)
  71. }
  72. console.log(response);
  73. return response;
  74. }
  75. });
  76. }
Add Comment
Please, Sign In to add comment