public ActionResult CallTrackingMetricWebhook()
        {
            Request.InputStream.Seek(0, SeekOrigin.Begin);
            string jsonData = new StreamReader(Request.InputStream).ReadToEnd();

            CallTrackingMetricsWebhookData data = null;

            try
            {
                data = JsonConvert.DeserializeObject <CallTrackingMetricsWebhookData>(jsonData);
            }
            catch (Exception e)
            {
                Trace.TraceError("Failed to parse jsondata from CallTrackingMetrics.", new object[] { e, jsonData });
                throw;
            }

            var contactId = data.id.ToString();

            var hasCallInDb =
                contactRepository.Where(
                    l =>
                    l.Property.Any(
                        lp => lp.Type == "CTMId" && lp.Value == contactId)).Any();

            Contact phoneCall;

            if (hasCallInDb)
            {
                var callTrackingMetricsId = data.id.ToString();
                phoneCall = contactRepository.Where(l => l.Property.Any(lp => lp.Type == "CTMId" && lp.Value == callTrackingMetricsId)).First();
                externalDataProvider.MapPhoneDataToContact(data, phoneCall);
                contactAutoRating.SetAutoRating(phoneCall);
            }
            else
            {
                var hasMatchingClient = clientRepository.Where(c => c.CallTrackingMetricId == data.account_id).Any();
                if (!hasMatchingClient)
                {
                    Trace.TraceError("Recived ctm webhook data. Found no matching client for given account id: " + data.account_id);
                    return(Json("Ok", JsonRequestBehavior.AllowGet));
                }
                phoneCall = externalDataProvider.MapPhoneDataToContact(data);
                contactAutoRating.SetAutoRating(phoneCall);
                contactRepository.Add(phoneCall);
            }

            contactRepository.SaveChanges();

            var isPhoneCallNewerThanTenMinutes = phoneCall.Date > serverTime.RequestStarted.AddMinutes(-10);

            if (!hasCallInDb && isPhoneCallNewerThanTenMinutes)
            {
                contactService.NotifyClientsForNewContactWithEmail(phoneCall.Id);
                contactService.NotifyClientsForNewContactWithPhoneNotification(phoneCall.Id);
                contactService.NotifyClientsForNewContactWithSmsNotification(phoneCall.Id);
            }

            return(Json("ok", JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 2
0
        public void PerformTask(InsideModel.Models.Task taskMessage)
        {
            var updatePhoneCallsMessage =
                JsonConvert.DeserializeObject <UpdatePhonecallsForClientsTaskMessage>(taskMessage.Message);

            var callTrackingMetricsId =
                clientRepository.Single(c => c.Id == updatePhoneCallsMessage.ClientId).CallTrackingMetricId;

            if (callTrackingMetricsId == null)
            {
                throw new Exception("No Call tracking metrics Id found for client:" + updatePhoneCallsMessage.ClientId);
            }

            string  pageUrl = null;
            CtmData phoneData;

            do
            {
                string phoneDataString;
                if (pageUrl == null)
                {
                    phoneDataString = externalDataProvider.GetPhoneData((int)callTrackingMetricsId,
                                                                        updatePhoneCallsMessage.StartDate, updatePhoneCallsMessage.EndDate);
                }
                else
                {
                    phoneDataString = externalDataProvider.GetPhoneData(pageUrl);
                }

                if (phoneDataString.IsNullOrWhiteSpace())
                {
                    return;
                }

                phoneData = JsonConvert.DeserializeObject <CtmData>(phoneDataString);
                foreach (var call in phoneData.calls)
                {
                    var hasCallInDb = contactRepository.Where(l => l.Property.Any(lp => lp.Type == "CTMId" && lp.Value == call.id.ToString())).Any();

                    if (hasCallInDb)
                    {
                        var contactToUpdate =
                            contactRepository.Single(
                                l => l.Property.Any(lp => lp.Type == "CTMId" && lp.Value == call.id.ToString()));


                        externalDataProvider.MapPhoneDataToContact(call, contactToUpdate);
                        contactAutoRating.SetAutoRating(contactToUpdate);
                    }
                    else
                    {
                        var hasMatchingClient = clientRepository.Where(c => c.CallTrackingMetricId == call.account_id).Any();
                        if (!hasMatchingClient)
                        {
                            throw new Exception("Recived ctm webhook data. Found no matching client for given account id: " + call.account_id);
                        }
                        var contact = externalDataProvider.MapPhoneDataToContact(call);
                        contactAutoRating.SetAutoRating(contact);
                        contactRepository.Add(contact);
                    }
                }
                contactRepository.SaveChanges();
                pageUrl = phoneData.next_page;
            } while (!phoneData.next_page.IsNullOrWhiteSpace());
        }