Beispiel #1
0
 private void EnsureHasEndDate <TEvent>(Apprenticeship apprenticeship) where TEvent : class
 {
     if (apprenticeship.EndDate == null)
     {
         throw new InvalidOperationException(
                   $"Cannot publish {typeof(TEvent)} event for apprenticeship {apprenticeship.Id} because it does not have a end date");
     }
 }
Beispiel #2
0
 public Task PublishApprenticeshipResumed(Commitment commitment, Apprenticeship apprenticeship)
 {
     return(PublishWithLog <ApprenticeshipResumedEvent>(apprenticeship, ev =>
     {
         ev.ApprenticeshipId = apprenticeship.Id;
         ev.ResumedOn = _currentDateTime.Now;
     }, GetLogMessage(commitment, apprenticeship)));
 }
Beispiel #3
0
 private void EnsureHasApprenticeship <TEvent>(Apprenticeship apprenticeship) where TEvent : class
 {
     if (apprenticeship == null)
     {
         throw new InvalidOperationException(
                   $"Cannot publish {typeof(TEvent)} event for apprenticeship because the apprenticeship is null");
     }
 }
Beispiel #4
0
 public Task PublishApprenticeshipPaused(Commitment commitment, Apprenticeship apprenticeship)
 {
     return(PublishWithLog <ApprenticeshipPausedEvent>(ApprenticePreChecks.HasPauseDate, apprenticeship, ev =>
     {
         ev.ApprenticeshipId = apprenticeship.Id;
         ev.PausedOn = apprenticeship.PauseDate.Value;
     }, GetLogMessage(commitment, apprenticeship)));
 }
Beispiel #5
0
 public Task PublishApprenticeshipStopped(Commitment commitment, Apprenticeship apprenticeship)
 {
     return(PublishWithLog <ApprenticeshipStoppedEvent>(ApprenticePreChecks.HasStopDate, apprenticeship, ev =>
     {
         ev.AppliedOn = _currentDateTime.Now;
         ev.ApprenticeshipId = apprenticeship.Id;
         ev.StopDate = apprenticeship.StopDate.Value;
     }, GetLogMessage(commitment, apprenticeship)));
 }
Beispiel #6
0
        public Task PublishApprenticeshipUlnUpdatedEvent(Apprenticeship apprenticeship)
        {
            if (apprenticeship != null)
            {
                var @event = new ApprenticeshipUlnUpdatedEvent(apprenticeship.Id, apprenticeship.ULN, _currentDateTime.Now);
                return(PublishWithLog(@event, $"ApprenticeshipId:{apprenticeship.Id}, updated"));
            }

            return(Task.CompletedTask);
        }
Beispiel #7
0
 private PriceEpisode[] GetPriceEpisodes(Apprenticeship apprenticeship)
 {
     return(apprenticeship
            .PriceHistory
            .Select(x => new PriceEpisode
     {
         FromDate = x.FromDate,
         ToDate = x.ToDate,
         Cost = x.Cost
     }).ToArray());
 }
Beispiel #8
0
 public Task PublishApprenticeshipDeleted(Commitment commitment, Apprenticeship apprenticeship)
 {
     return(PublishWithLog <DraftApprenticeshipDeletedEvent>(ApprenticePreChecks.NotRequired, apprenticeship, ev =>
     {
         ev.DraftApprenticeshipId = apprenticeship.Id;
         ev.CohortId = commitment.Id;
         ev.DeletedOn = DateTime.UtcNow;
         ev.ReservationId = apprenticeship.ReservationId;
         ev.Uln = apprenticeship.ULN;
     }, GetLogMessage(commitment, apprenticeship)));
 }
Beispiel #9
0
 public Task PublishApprenticeshipUpdatedApproved(Commitment commitment, Apprenticeship apprenticeship)
 {
     return(PublishWithLog <ApprenticeshipUpdatedApprovedEvent>(ApprenticePreChecks.HasStartAndEndDate, apprenticeship, ev =>
     {
         ev.ApprenticeshipId = apprenticeship.Id;
         ev.ApprovedOn = _currentDateTime.Now;
         ev.StartDate = apprenticeship.StartDate.Value;
         ev.EndDate = apprenticeship.EndDate.Value;
         ev.PriceEpisodes = GetPriceEpisodes(apprenticeship);
         ev.TrainingType = (CommitmentsV2.Types.ProgrammeType)apprenticeship.TrainingType;
         ev.TrainingCode = apprenticeship.TrainingCode;
         ev.Uln = apprenticeship.ULN;
     }, GetLogMessage(commitment, apprenticeship)));
 }
Beispiel #10
0
        /// <summary>
        ///     publish the specified message and log whether the publish was successful or not.
        /// </summary>
        /// <typeparam name="TEvent"></typeparam>
        /// <param name="checks">Specified properties will be checked for null value before calling the message constructor</param>
        /// <param name="messageConstructor">Sets values on an instance of the message</param>
        /// <param name="apprenticeship"></param>
        /// <param name="message">A log message that will be recorded with the success or failure message</param>
        /// <returns></returns>
        private async Task PublishWithLog <TEvent>(ApprenticePreChecks checks, Apprenticeship apprenticeship, Action <TEvent> messageConstructor, string message) where TEvent : class
        {
            var logMessage = $"Publish {typeof(TEvent).Name} message. {message}";

            try
            {
                DoPreChecks <TEvent>(checks, apprenticeship);
                await _endpointInstance.Publish <TEvent>(messageConstructor);

                _logger.Info($"{logMessage} successful");
            }
            catch (Exception e)
            {
                _logger.Error(e, $"{logMessage} failed");
                throw;
            }
        }
Beispiel #11
0
        private void DoPreChecks <TEvent>(ApprenticePreChecks checks, Apprenticeship apprenticeship) where TEvent : class
        {
            void DoCheckIf(ApprenticePreChecks qualifyingFlag, Action check)
            {
                if ((checks & qualifyingFlag) == qualifyingFlag)
                {
                    check();
                }
            }

            if (checks == ApprenticePreChecks.NotRequired)
            {
                return;
            }

            EnsureHasApprenticeship <TEvent>(apprenticeship);

            DoCheckIf(ApprenticePreChecks.HasStartDate, () => EnsureHasStartDate <TEvent>(apprenticeship));
            DoCheckIf(ApprenticePreChecks.HasEndDate, () => EnsureHasEndDate <TEvent>(apprenticeship));
            DoCheckIf(ApprenticePreChecks.HasStopDate, () => EnsureHasStopDate <TEvent>(apprenticeship));
            DoCheckIf(ApprenticePreChecks.HasPauseDate, () => EnsureHasPauseDate <TEvent>(apprenticeship));
        }
Beispiel #12
0
 private string GetLogMessage(Commitment commitment, Apprenticeship apprenticeship)
 {
     return($"Provider:{commitment.ProviderId} Commitment:{commitment.Id} Apprenticeship:{apprenticeship.Id}");
 }
Beispiel #13
0
 private Task PublishWithLog <TEvent>(Apprenticeship apprentice, Action <TEvent> messageConstructor, string message) where TEvent : class
 {
     return(PublishWithLog(ApprenticePreChecks.NotRequired, apprentice, messageConstructor, message));
 }
 public static bool IsWaitingToStart(this Apprenticeship apprenticeship, ICurrentDateTime currentDateTime)
 {
     return(apprenticeship.StartDate.Value > new DateTime(currentDateTime.Now.Year, currentDateTime.Now.Month, 1));
 }