Exemplo n.º 1
0
        public HttpResponseMessage Post(CventMessage cventMessage) //public HttpResponseMessage Post(JObject  cventMessage)
        {
            //Extract Body, Cvent Objects and/or header information from posted data
            bool   LogSuccess = false;
            string headerValues;
            var    BodyData = "";
            var    MemberCustomFieldGUID = System.Configuration.ConfigurationManager.AppSettings.Get("Cvent.Webhooks.MemberCustomFieldGUID");

            try
            {
                BodyData = Newtonsoft.Json.JsonConvert.SerializeObject(cventMessage);
            }
            catch
            {
                BodyData = "Error converting from  object";
            }
            headerValues = (Request.Headers.Authorization == null) ? "Null" : Request.Headers.Authorization.ToString();

            var CventAttRepo = new CventAttendeeRepository();
            var APAP_Msnger  = new APAPRegMessenger();
            var HR_MRA_Repo  = new HR_MRA_Repository();
            var WL           = new WebhookManager(CventAttRepo, APAP_Msnger, HR_MRA_Repo);

            //Validate Authorization Header
            bool KeysMatch = IsValidAuth(headerValues);

            WL.LogJsonData(BodyData, headerValues, "Post", KeysMatch);

            //Log call to endpoint regardless of valid authcode and body of post
            if (cventMessage.message[0] != null && KeysMatch)
            {
                LogSuccess = WL.ProcessPostData(BodyData, cventMessage, headerValues, "Post", KeysMatch);
                WL.LogJsonData(WL.ProcessLogData, headerValues, "Post", KeysMatch);
            }
            else
            {
                LogSuccess = false;
            }

            //Return correct response based on valid auth header and/or valid post data
            if (KeysMatch == true)
            {
                if (LogSuccess == true)
                {
                    return(Request.CreateResponse <object>(System.Net.HttpStatusCode.Created, cventMessage));
                }
                else
                {
                    return(new HttpResponseMessage(HttpStatusCode.BadRequest));
                }
            }
            else
            {
                return(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }
        }
        public bool ProcessPostData(string BodyData, CventMessage CventBodyMsg, string cv_header, string cv_verb, bool Authorized)
        {
            var  LogData       = new WH_UpdateLog();
            bool SuccessForAll = true;

            cventAuthHeader = cv_header;
            verb            = cv_verb;
            messageStub     = CventBodyMsg.messageStub;
            eventType       = CventBodyMsg.eventType;
            postBodyData    = BodyData;
            authorized      = Authorized;
            CventAttendee cur_Attendee;
            //need to loop through all messages
            bool LoggedCallSuccessfully = false;

            SuccessfullyPushedReg = true;



            foreach (CventRegMessage reg_msg in CventBodyMsg.message)
            {
                if (reg_msg != null)
                {
                    if (reg_msg.isGuest != "true")
                    {
                        eventStub = reg_msg.eventStub;
                        var    CventRegRepo = new CventAttendeeRepository();
                        string ResponseFromPushCall;
                        string RegInJason = Newtonsoft.Json.JsonConvert.SerializeObject(reg_msg);


                        //CHECK TO SEE IF WE ALREADY PROCESS THIS ONE

                        string status      = hrMRARepo.RegProcessedStatus(reg_msg.inviteeStub);
                        var    tempInvitee = new InviteeToProcess {
                            InviteeIdGuid = reg_msg.inviteeStub.ToString(),
                        };


                        if (status != "Processed")
                        {
                            cur_Attendee = CventRegRepo.GetCventAttendee(reg_msg.inviteeStub, reg_msg.eventStub, APIEventPW);

                            cur_Attendee.PrimaryRegistration = (reg_msg.isGuest == "True") ? false : true;
                            Attendees.Add(cur_Attendee);

                            // if (cur_Attendee.excludeEmail == "no") ? false : true;
                            if (status == "Missing")
                            {
                                LoggedCallSuccessfully       = hrMRARepo.CreateRegLog(cur_Attendee, RegInJason, eventStub);
                                tempInvitee.ProcessingAction = "Created Log Record";
                            }
                            else
                            {
                                LoggedCallSuccessfully       = true;
                                tempInvitee.ProcessingAction = "Log Record Exists";
                            }


                            if (LoggedCallSuccessfully && cur_Attendee.PrimaryRegistration)
                            {
                                try
                                {
                                    if (cur_Attendee.personMembershipId.Length > 0)
                                    {
                                        ResponseFromPushCall = SendRegToClient(cur_Attendee);
                                    }
                                    else
                                    {
                                        ResponseFromPushCall          = "No Member Id";
                                        tempInvitee.ProblemWithRecord = ResponseFromPushCall;
                                    }

                                    if (ResponseFromPushCall.Length == 36)
                                    {
                                        cur_Attendee.ProcessedGUID = ResponseFromPushCall;
                                        hrMRARepo.UpdateRegLogAsProcessed(cur_Attendee);
                                        LogData.SuccessfullyProcessed   = +1;
                                        tempInvitee.PushToClientSuccess = true;
                                    }
                                    else
                                    {
                                        hrMRARepo.UpdateRegLogWithErrorMessage(cur_Attendee, ResponseFromPushCall);
                                        tempInvitee.PushToClientSuccess = false;
                                        LogData.FailedProcessed         = +1;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    tempInvitee.ProcessingException = ex.InnerException.ToString();
                                    SuccessfullyPushedReg           = false;
                                    LogData.FailedProcessed         = +1;
                                }
                            }
                            else
                            {
                                tempInvitee.ProcessingAction = "Already processed by Client";
                            }

                            SuccessForAll = false;
                        }
                        LogData.Invitees.Add(tempInvitee);
                    }
                }
            }
            LogData.ProcessingEndTime = DateTime.Now;


            ProcessLogData = Newtonsoft.Json.JsonConvert.SerializeObject(LogData);;
            return(true);
        }