Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @module ProposalCalculations */
  2.  
  3. const LAMBDA_NAME = 'securityTrax-editCustomer'
  4. const LambdaHelper = require('./LambdaHelperV2.js')
  5. const fs = require('fs')
  6. const request = require('request')
  7.  
  8. exports.handler = async (event, context) => {
  9.     const originalEvent = JSON.parse(JSON.stringify(event))
  10.  
  11.     try {
  12.         console.log('HI', event)
  13.         LambdaHelper.validateEvent(event, ['body', 'customer_id', 'ticket_id', 'account_fields_to_update', 'ticket_fields_to_update', 'appointment_fields_to_create', 'appointment_fields_to_update'])
  14.         // LambdaHelper.validateEvent(event.body, ['customer_id', 'ticket_id', 'account_fields_to_update', 'ticket_fields_to_update', 'appointment_fields_to_create', 'appointment_fields_to_update'])
  15.         LambdaHelper.validateEvent(event.body.account_fields_to_update, ['email'])
  16.         LambdaHelper.validateEvent(event.body.appointment_fields_to_update, ['tech_service_type_id', 'start_time', 'user_id', 'duration', 'notes'])
  17.         LambdaHelper.validateEvent(event.cookies, ['BIGipServer~stxna02~STX_K8S_EXTERNAL_HTTPS', 'PHPSESSID'])
  18.         // LambdaHelper.validateEvent(event.body.fundSetup, ['Term_In_Months__c', 'Interest_Rate__c'])
  19.         // LambdaHelper.validateEvent(event.body.fund, ['System_Price__c'])
  20.         let createAppointmentResults = await createAppointment(event)
  21.         console.log('RESULTS', createAppointmentResults)
  22.         let editAppointmentResults = editAppointmentResults(createAppointmentResults)
  23.  
  24.         if (event.responseType == 'file') {
  25.             await new Promise(function (resolve, reject) {
  26.                 fs.writeFile('myjsonfile.json', JSON.stringify(editAppointmentResults, null, 4), function (err) {
  27.                     if (err) {
  28.                         reject(err)
  29.                     } else {
  30.                         resolve('Saved JSON')
  31.                     }
  32.                 })
  33.             })
  34.         } else {
  35.             return LambdaHelper.formatResponseData(null, originalEvent, editAppointmentResults, LAMBDA_NAME)
  36.         }
  37.  
  38.     } catch (error) {
  39.         return LambdaHelper.formatResponseData(error, originalEvent, null, LAMBDA_NAME)
  40.     }
  41. }
  42.  
  43.  
  44. /**********************************************************************************************
  45. * @description Calls out to genability for the reduced usage savings
  46. * @param {JSON} event - event passed in
  47. * @param {JSON} proposalCalculationData - main proposal data
  48. * @return {JSON} response from genability-reducedUsageSavings lamba function
  49. ***********************************************************************************************/
  50.  
  51. async function createAppointment(event) {
  52.     // Here we want to run editcustomer, editTicket, and createAppointment all together
  53.  
  54.     for (let param of Object.keys(urlParamDict)) {
  55.         if (urlParamDict.hasOwnProperty(param)) {
  56.             URLSearchParams.push(param + '=' + urlParamDict[param])
  57.         }
  58.     }
  59.  
  60.     let editCustomerPayload = {
  61.         "body": {
  62.             "customer_id": "342516",
  63.             "fields_to_update": {
  64.                 "email1": "shaddock@techforcenational.com"
  65.             }
  66.         },
  67.         "cookies": {
  68.             "BIGipServer~stxna02~STX_K8S_EXTERNAL_HTTPS": "!xpTGjhIWYQvpGut/tyRaV273yZ3/LDqGZ4ZaEcy9q8AMbFzIwniyvpIwy6cv11AsY1+cjZiYnFV+2nopMVAu++UY1qnYeEkIpPWwcZbg+Vk=",
  69.             "PHPSESSID": "h73c1jpf4s947e9rccj1riuo39"
  70.         }
  71.     }
  72.  
  73.     let editTicketPayload = {
  74.         "body": {
  75.             "customer_id": "342516",
  76.             "ticket_id": "512110",
  77.             "fields_to_update": {
  78.                 "subject": "test1234"
  79.             }
  80.         },
  81.         "cookies": {
  82.             "BIGipServer~stxna02~STX_K8S_EXTERNAL_HTTPS": "!xpTGjhIWYQvpGut/tyRaV273yZ3/LDqGZ4ZaEcy9q8AMbFzIwniyvpIwy6cv11AsY1+cjZiYnFV+2nopMVAu++UY1qnYeEkIpPWwcZbg+Vk=",
  83.             "PHPSESSID": "h73c1jpf4s947e9rccj1riuo39"
  84.         }
  85.     }
  86.  
  87.     let createAppointmentPayload = {
  88.         "body": {
  89.             "customer_id": "342516",
  90.             "ticket_id": "512110",
  91.             "fields_to_update": {
  92.                 "tech_service_type_id": "1",
  93.                 "start_time": "1575822600",
  94.                 "user_id": "2564",
  95.                 "duration": "1800"
  96.             }
  97.         },
  98.         "cookies": {
  99.             "BIGipServer~stxna02~STX_K8S_EXTERNAL_HTTPS": "!Aami8AWUFNsFLzF/tyRaV273yZ3/LF1kjlXKW6uvXuYM8HVHCIFIcJ2mSp36Njz6y9/+wFLrkI3oPc1zQLS80RonJrfW+IL7yC4C+4WsG7M=",
  100.             "PHPSESSID": "22d0mgpumjib793u1jnlv1cqg1"
  101.         }
  102.     }
  103.  
  104.     let promises = [editCustomer(editCustomerPayload), editTicket(editTicketPayload), createAppointment(createAppointmentPayload)]
  105.     console.log('PROMISE ALL', Promise.all(promises))
  106.     return Promise.all(promises)
  107. }
  108.  
  109. function editCustomer(options) {
  110.     return new Promise(function (resolve, reject) {
  111.         request(options, function (error, response, body) {
  112.             if (error) {
  113.                 reject(error)
  114.             } else {
  115.                 resolve(JSON.parse(body))
  116.             }
  117.         })
  118.     })
  119. }
  120.  
  121. function editTicket(options) {
  122.     return new Promise(function (resolve, reject) {
  123.         request(options, function (error, response, body) {
  124.             if (error) {
  125.                 reject(error)
  126.             } else {
  127.                 resolve(JSON.parse(body))
  128.             }
  129.         })
  130.     })
  131. }
  132.  
  133. function createAppointment(options) {
  134.     return new Promise(function (resolve, reject) {
  135.         request(options, function (error, response, body) {
  136.             if (error) {
  137.                 reject(error)
  138.             } else {
  139.                 resolve(JSON.parse(body))
  140.             }
  141.         })
  142.     })
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement