Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @module ScheduleAppointment */
- const LAMBDA_NAME = 'securityTrax-scheduleAppointment'
- const LambdaHelper = require('./LambdaHelperV2.js')
- const request = require('request')
- exports.handler = async (event, context) => {
- const originalEvent = JSON.parse(JSON.stringify(event))
- try {
- // Only put the fields that are required don't put in variable fields like email
- LambdaHelper.validateEvent(event, ['body', 'cookies'])
- LambdaHelper.validateEvent(event.body, ['customer_id', 'ticket_id', 'account_fields_to_update', 'ticket_fields_to_update', 'appointment_fields_to_create', 'appointment_fields_to_update'])
- LambdaHelper.validateEvent(event.body.appointment_fields_to_update, ['tech_service_type_id', 'start_time', 'user_id', 'duration'])
- let scheduleAppointmentResponses = await scheduleAppointment(event)
- for (var i = 0; i < scheduleAppointmentResponses.length; i++) {
- if (response.success != true) {
- // check that each request was successful throw exception if not
- throw response
- }
- }
- // You want to return the response from the create appointment request because the response will contain the appointment id
- // The results correspond to the order in which they were put into the promise array
- let resultsToReturn = scheduleAppointmentResponses[2]
- return LambdaHelper.formatResponseData(null, originalEvent, resultsToReturn, LAMBDA_NAME)
- } catch (error) {
- return LambdaHelper.formatResponseData(error, originalEvent, null, LAMBDA_NAME)
- }
- }
- async function scheduleAppointment(event) {
- // Here we want to run editcustomer, editTicket, and createAppointment all together
- let promises = []
- // Edit Customer
- let editCustomerAccountBody = {
- customer_id: event.body.customer_id,
- fields_to_update: {}
- }
- // loop through and add in the fields from the initial event
- for (var field of event.account_fields_to_update) {
- editCustomerAccountBody.fields_to_update[field] = event.body.account_fields_to_update[field]
- }
- promises.push(sendSecurityTraxRequest(event.cookies, editCustomerAccountBody))
- // Edit Ticket
- let editTicketBody = {
- customer_id: event.body.customer_id,
- ticket_id: event.body.ticket_id,
- fields_to_update: {}
- }
- // loop through and add in the fields from the initial event and set editTicketBody.fieldsToUpdate
- for (var field of event.body.ticket_fields_to_update) {
- editTicketBody.fields_to_update[field] = event.body.ticket_fields_to_update[field]
- }
- promises.push(sendSecurityTraxRequest(event.cookies, editTicketBody))
- // Go ahead and do create appointment and edit appointment just like the ones above.
- // Create Appointment
- // Edit Appointment
- return Promise.all(promises)
- }
- function sendSecurityTraxRequest(cookies, body) {
- let options = {
- body: body,
- cookies: cookies
- }
- return new Promise(function (resolve, reject) {
- request(options, function (error, response, body) {
- if (error) {
- reject(error)
- } else {
- resolve(body)
- }
- })
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement