public ServiceBusMessageStates Consume(ApplicationStatusChanged applicationStatusChanged)
        {
            var application = _apprenticeshipApplicationReadRepository.Get(applicationStatusChanged.LegacyApplicationId, _strictEtlValidation);

            if (application == null)
            {
                _logService.Warn(_strictEtlValidation, string.Format(ApplicationNotFoundMessageFormat, applicationStatusChanged.LegacyApplicationId));
                return(ServiceBusMessageStates.Complete);
            }

            var applicationStatusAlerts = _applicationStatusAlertRepository.GetForApplication(application.EntityId);
            var applicationStatusAlert  = applicationStatusAlerts.FirstOrDefault(asa => asa.BatchId == null);

            if (applicationStatusAlert == null)
            {
                applicationStatusAlert = new ApplicationStatusAlert
                {
                    CandidateId          = application.CandidateId,
                    ApplicationId        = application.EntityId,
                    VacancyId            = application.Vacancy.Id,
                    Title                = application.Vacancy.Title,
                    EmployerName         = application.Vacancy.EmployerName,
                    Status               = application.Status,
                    UnsuccessfulReason   = application.UnsuccessfulReason,
                    DateApplied          = application.DateApplied ?? new DateTime(),
                    UnSuccessfulDateTime = application.UnsuccessfulDateTime ?? new DateTime()
                };
            }

            applicationStatusAlert.Status             = applicationStatusChanged.ApplicationStatus;
            applicationStatusAlert.UnsuccessfulReason = applicationStatusChanged.UnsuccessfulReason;

            _applicationStatusAlertRepository.Save(applicationStatusAlert);

            return(ServiceBusMessageStates.Complete);
        }
예제 #2
0
        public void SendDailyDigests(Guid batchId)
        {
            var candidatesExpiringDrafts          = _expiringDraftRepository.GetCandidatesDailyDigest();
            var candidatesApplicationStatusAlerts = _applicationStatusAlertRepository.GetCandidatesDailyDigest();

            var candidateIds = candidatesExpiringDrafts.Keys
                               .Union(candidatesApplicationStatusAlerts.Keys);

            foreach (var candidateId in candidateIds)
            {
                var candidate = _candidateReadRepository.Get(candidateId);
                var user      = _userReadRepository.Get(candidateId);

                if (candidate == null || user == null)
                {
                    _logService.Warn("Could not find a valid user or candidate for id: {0}. User is null: {1}, Candidate is null: {2}", candidateId, user == null, candidate == null);
                    return;
                }

                List <ExpiringApprenticeshipApplicationDraft> candidateExpiringDraftsDailyDigest;
                var candidateHasExpiringDrafts = candidatesExpiringDrafts.TryGetValue(candidateId, out candidateExpiringDraftsDailyDigest);

                List <ApplicationStatusAlert> candidateApplicationStatusAlertsDailyDigest;
                var candidateHasApplicationStatusAlerts = candidatesApplicationStatusAlerts.TryGetValue(candidateId, out candidateApplicationStatusAlertsDailyDigest);

                if (candidate.AllowsCommunication() && user.IsActive())
                {
                    var communicationRequest = CommunicationRequestFactory.GetDailyDigestCommunicationRequest(candidate, candidateExpiringDraftsDailyDigest, candidateApplicationStatusAlertsDailyDigest);

                    _serviceBus.PublishMessage(communicationRequest);

                    if (candidateHasExpiringDrafts)
                    {
                        // Update candidates expiring drafts to sent
                        candidateExpiringDraftsDailyDigest.ToList().ForEach(dd =>
                        {
                            dd.BatchId = batchId;
                            _expiringDraftRepository.Save(dd);
                        });
                    }

                    if (candidateHasApplicationStatusAlerts)
                    {
                        // Update candidates application status alerts to sent
                        candidateApplicationStatusAlertsDailyDigest.ToList().ForEach(dd =>
                        {
                            dd.BatchId = batchId;
                            _applicationStatusAlertRepository.Save(dd);
                        });
                    }
                }
                else
                {
                    if (candidateHasExpiringDrafts)
                    {
                        // Soft delete candidates expiring drafts by setting batch id to empty
                        candidateExpiringDraftsDailyDigest.ToList().ForEach(dd =>
                        {
                            dd.BatchId = Guid.Empty;
                            _expiringDraftRepository.Save(dd);
                        });
                    }

                    if (candidateHasApplicationStatusAlerts)
                    {
                        // Soft delete candidates saved application status alerts by setting batch id to empty
                        candidateApplicationStatusAlertsDailyDigest.ToList().ForEach(dd =>
                        {
                            dd.BatchId = Guid.Empty;
                            _applicationStatusAlertRepository.Save(dd);
                        });
                    }
                }
            }
        }