Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @module ScheduleAppointment */
  2.  
  3. const LAMBDA_NAME = 'securityTrax-scheduleAppointment'
  4. const LambdaHelper = require('./LambdaHelperV2.js')
  5. const request = require('request')
  6.  
  7. exports.handler = async (event, context) => {
  8.     const originalEvent = JSON.parse(JSON.stringify(event))
  9.  
  10.     try {
  11.         // Only put the fields that are required don't put in variable fields like email
  12.         LambdaHelper.validateEvent(event, ['body', 'cookies'])
  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.appointment_fields_to_update, ['tech_service_type_id', 'start_time', 'user_id', 'duration'])
  15.  
  16.  
  17.         let scheduleAppointmentResponses = await scheduleAppointment(event)
  18.  
  19.         for (var i = 0; i < scheduleAppointmentResponses.length; i++) {
  20.             if (response.success != true) {
  21.                 // check that each request was successful throw exception if not
  22.                 throw response
  23.             }
  24.         }
  25.  
  26.         // You want to return the response from the create appointment request because the response will contain the appointment id
  27.         // The results correspond to the order in which they were put into the promise array
  28.         let resultsToReturn = scheduleAppointmentResponses[2]
  29.  
  30.         return LambdaHelper.formatResponseData(null, originalEvent, resultsToReturn, LAMBDA_NAME)
  31.  
  32.     } catch (error) {
  33.         return LambdaHelper.formatResponseData(error, originalEvent, null, LAMBDA_NAME)
  34.     }
  35. }
  36.  
  37. async function scheduleAppointment(event) {
  38.     // Here we want to run editcustomer, editTicket, and createAppointment all together
  39.  
  40.     let promises = []
  41.  
  42.     // Edit Customer
  43.     let editCustomerAccountBody = {
  44.         customer_id: event.body.customer_id,
  45.         fields_to_update: {}
  46.     }
  47.  
  48.     // loop through and add in the fields from the initial event
  49.     for (var field of event.account_fields_to_update) {
  50.         editCustomerAccountBody.fields_to_update[field] = event.body.account_fields_to_update[field]
  51.     }
  52.  
  53.     promises.push(sendSecurityTraxRequest(event.cookies, editCustomerAccountBody))
  54.  
  55.     // Edit Ticket
  56.     let editTicketBody = {
  57.         customer_id: event.body.customer_id,
  58.         ticket_id: event.body.ticket_id,
  59.         fields_to_update: {}
  60.     }
  61.  
  62.     // loop through and add in the fields from the initial event and set editTicketBody.fieldsToUpdate
  63.     for (var field of event.body.ticket_fields_to_update) {
  64.         editTicketBody.fields_to_update[field] = event.body.ticket_fields_to_update[field]
  65.     }
  66.  
  67.     promises.push(sendSecurityTraxRequest(event.cookies, editTicketBody))
  68.  
  69.     // Go ahead and do create appointment and edit appointment just like the ones above.
  70.     // Create Appointment
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.     // Edit Appointment
  80.  
  81.    
  82.  
  83.     return Promise.all(promises)
  84. }
  85.  
  86. function sendSecurityTraxRequest(cookies, body) {
  87.     let options = {
  88.         body: body,
  89.         cookies: cookies
  90.     }
  91.  
  92.     return new Promise(function (resolve, reject) {
  93.         request(options, function (error, response, body) {
  94.             if (error) {
  95.                 reject(error)
  96.             } else {
  97.                 resolve(body)
  98.             }
  99.         })
  100.     })
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement