public static void Trigger(Object stateInfo) { try { if (emailNotifierIsProcessing) { return; } emailNotifierIsProcessing = true; Logger.Current.Verbose("WebVisitEmailNotifier triggered"); Logger.Current.Verbose("Getting visits"); GetCurrentWebVisitNotificationsResponse webVisitsToBeNotified = webAnalyticsService.GetCurrentWebVistNotifications(new GetCurrentWebVisitNotificationsRequest()); Logger.Current.Informational("WebVisitsToBeNotified Count" + webVisitsToBeNotified.CurrentVisits.Count()); var accounts = webVisitsToBeNotified.CurrentVisits.Select(c => c.AccountId).ToList().Distinct(); var emailResults = new List <KeyValuePair <IEnumerable <string>, string> >(); var failedEmails = new List <KeyValuePair <IEnumerable <string>, string> >(); foreach (int accountId in accounts) { var userIds = webVisitsToBeNotified.CurrentVisits .Where(c => c.AccountId == accountId) .Select(c => c.OwnerID).Distinct().ToList(); if (userIds.IsAny()) { Dictionary <Guid, string> emailProvider = accountService.GetTransactionalProviderDetails(accountId); GetDropdownValueResponse lifeCycleStages = dropdownService.GetDropdownValue(new GetDropdownValueRequest() { AccountId = accountId, DropdownID = (byte)DropdownFieldTypes.LifeCycle }); IEnumerable <UserBasicInfo> optedUsers = userService.GetUsersOptedInstantWebVisitEmail(new GetUsersOptedInstantWebVisitEmailRequest() { AccountId = accountId }).Users; List <UserBasicInfo> owners = userService.GetUsersByUserIDs(new GetUsersByUserIDsRequest() { UserIDs = userIds }).Users.ToList(); //add empty owner var eo = new UserBasicInfo() { UserID = 0, Email = defaultEmail }; if (!owners.Any(f => f.Email == defaultEmail)) { owners.Add(eo); } var account = accountRepository.GetAccountBasicDetails(accountId); if (account.Status != (byte)AccountStatus.Suspend) { #region == Send email to each user == //is account subscribed var providerResponse = accountService.GetAccountWebAnalyticsProviders(new GetWebAnalyticsProvidersRequest() { AccountId = accountId }); WebAnalyticsProvider provider = providerResponse.WebAnalyticsProviders.FirstOrDefault(); foreach (var owner in owners) { IEnumerable <WebVisitReport> relatedVisits = null; if (owner.Email == defaultEmail) { relatedVisits = webVisitsToBeNotified.CurrentVisits.Where(c => c.AccountId == accountId && c.OwnerID == null).OrderByDescending(c => c.VisitedOn).ToList(); owner.FirstName = "Not"; owner.LastName = "Assigned"; owner.TimeZone = account.TimeZone; } else { relatedVisits = webVisitsToBeNotified.CurrentVisits.Where(c => c.AccountId == accountId && c.OwnerID == owner.UserID).OrderByDescending(c => c.VisitedOn).ToList(); } if (relatedVisits.IsAny()) { try { if (provider.NotificationStatus) { //when owner is default user or un assigned, email will not be sent to owner. NotifyByEmail(accountId, relatedVisits, owner, "SmartTouch Current Web Visit Alert", lifeCycleStages, emailProvider, owner.Email); } foreach (var optedUser in optedUsers) { try { NotifyByEmail(accountId, relatedVisits, owner, "SmartTouch Current Web Visit Alert (Admin)", lifeCycleStages, emailProvider, optedUser.Email); } catch (Exception ex) { ex.Data.Clear(); ex.Data.Add("Copy User", optedUser.UserID); Logger.Current.Error("Unable to send web visit email to user: "******", email: " + optedUser.Email, ex); } } emailResults.Add(new KeyValuePair <IEnumerable <string>, string>(relatedVisits.Select(c => c.VisitReference).ToList(), "Success")); } catch (Exception ex) { ex.Data.Clear(); ex.Data.Add("Copy User", owner.UserID); Logger.Current.Error("Unable to send web visit email to user: "******"Failed")); } } } #endregion } emailResults.AddRange(failedEmails); } } #region == Update Audits == if (emailResults != null && emailResults.Any()) { webAnalyticsService.UpdateWebVisitNotifications(new UpdateWebVisitNotificationsRequest() { VisitReferences = emailResults }); } #endregion emailNotifierIsProcessing = false; } catch (Exception ex) { emailNotifierIsProcessing = false; Logger.Current.Error("Exception occured in Email Notifier.", ex); } finally { //emailNotifierIsProcessing = false; } }
protected override void ExecuteInternal(IJobExecutionContext context) { var webVisitsToBeNotified = _webAnalyticsService.GetCurrentWebVistNotifications(new GetCurrentWebVisitNotificationsRequest()); var accountIds = webVisitsToBeNotified .CurrentVisits .Select(c => c.AccountId) .Distinct() .ToArray(); var emailResults = new List <KeyValuePair <IEnumerable <string>, string> >(); foreach (var accountId in accountIds) { var accountInfo = _accountRepository.GetAccountBasicDetails(accountId); if (accountInfo.Status == (byte)AccountStatus.Suspend) { continue; } Log.Informational($"Process Real-Time Alerts for AccountID: {accountId}"); var userIds = webVisitsToBeNotified.CurrentVisits .Where(c => c.AccountId == accountId) .Select(c => c.OwnerID) .Distinct() .ToArray(); if (!userIds.IsAny()) { continue; } var emailProvider = _accountService.GetTransactionalProviderDetails(accountId); var lifeCycleStages = _dropdownService.GetDropdownValue(new GetDropdownValueRequest { AccountId = accountId, DropdownID = (byte)DropdownFieldTypes.LifeCycle }); var usersToNotify = _userService.GetUsersOptedInstantWebVisitEmail(new GetUsersOptedInstantWebVisitEmailRequest { AccountId = accountId }) .Users .ToArray(); var ownersToNotify = _userService.GetUsersByUserIDs(new GetUsersByUserIDsRequest { UserIDs = userIds }) .Users .ToList(); //add empty owner var emptyOwner = new UserBasicInfo { UserID = 0, Email = DefaultEmail }; if (ownersToNotify.All(f => f.Email != DefaultEmail)) { ownersToNotify.Add(emptyOwner); } //is account subscribed var provider = _accountService.GetAccountWebAnalyticsProviders( new GetWebAnalyticsProvidersRequest { AccountId = accountId }).WebAnalyticsProviders.First(); foreach (var owner in ownersToNotify) { List <WebVisitReport> relatedVisits; if (owner.Email == DefaultEmail) { relatedVisits = webVisitsToBeNotified.CurrentVisits .Where(c => c.AccountId == accountId && c.OwnerID == null) .OrderByDescending(c => c.VisitedOn) .ToList(); owner.FirstName = "Not"; owner.LastName = "Assigned"; owner.TimeZone = accountInfo.TimeZone; } else { relatedVisits = webVisitsToBeNotified.CurrentVisits .Where(c => c.AccountId == accountId && c.OwnerID == owner.UserID) .OrderByDescending(c => c.VisitedOn) .ToList(); } if (!relatedVisits.IsAny()) { continue; } try { if (provider.NotificationStatus) { //when owner is default user or unassigned, email will not be sent to owner. NotifyByEmail(accountInfo, relatedVisits, owner, "SmartTouch Current Web Visit Alert", lifeCycleStages, emailProvider, owner.Email); } foreach (var user in usersToNotify) { try { NotifyByEmail(accountInfo, relatedVisits, owner, "SmartTouch Current Web Visit Alert (Admin)", lifeCycleStages, emailProvider, user.Email); } catch (Exception ex) { Log.Error("Unable to send web visit email to user: "******", email: " + user.Email, ex); } } emailResults.Add( new KeyValuePair <IEnumerable <string>, string>( relatedVisits.Select(c => c.VisitReference).ToList(), "Success")); } catch (Exception ex) { Log.Error("Unable to send web visit email to user: "******"Failed")); } } } if (emailResults.Any()) { _webAnalyticsService.UpdateWebVisitNotifications(new UpdateWebVisitNotificationsRequest { VisitReferences = emailResults }); } Log.Informational("Completed processing Real-Time Alerts"); }