Ejemplo n.º 1
0
        public async Task <Result <Nothing, Error> > Handle(RecordAcceptedTrainingInvitation.Command request, CancellationToken cancellationToken)
        {
            var user = await _userAccessor.GetUser();

            var enrollmentId        = EnrollmentId.With(request.EnrollmentId);
            var enrollmentReadModel = _enrollmentRepo.Query()
                                      .SingleOrDefault(x => x.Id == enrollmentId);

            if (enrollmentReadModel == null)
            {
                return(Result.Failure <Nothing, Error>(new Error.ResourceNotFound()));
            }

            var preferredTrainings = await _trainingRepository.GetByIds(enrollmentReadModel.PreferredTrainings.Select(x => x.ID).ToArray());

            var result = await _aggregateStore.Update <EnrollmentAggregate, EnrollmentId, Result <Nothing, Error> >(
                enrollmentId, CommandId.New,
                (aggregate) => aggregate.RecordCandidateAcceptedTrainingInvitation(request, user, preferredTrainings, _clock.GetCurrentInstant()),
                cancellationToken);

            if (result.Unwrap().IsSuccess)
            {
                var selectedTraining = preferredTrainings.Single(x => x.ID == request.SelectedTrainingID);
                _backgroundJobClient.Schedule(() => _engine.Execute(
                                                  new SendTrainingReminder.Command()
                {
                    EnrollmentId = request.EnrollmentId, TrainingId = request.SelectedTrainingID
                }),
                                              selectedTraining.StartDateTime.Minus(NodaTime.Duration.FromHours(24)).ToDateTimeOffset());
            }

            return(result.Unwrap());
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Validates whether given GUID matches this aggregate's ID
 /// </summary>
 /// <param name="guid"></param>
 private void ValidateIdMatchOrThrow(Guid guid)
 {
     if (EnrollmentId.With(guid) != this.Id)
     {
         throw new AggregateMismatchException($"ID mismatch in {nameof(EnrollmentAggregate)}; expected {Id.GetGuid()}, got {guid}");
     }
 }
Ejemplo n.º 3
0
 public async Task <Enrollment?> FindByIdAsync(
     EnrollmentId id,
     CancellationToken cancellationToken = default)
 {
     return(await _context.Enrollments
            .Where(e => e.Id == id)
            .SingleOrDefaultAsync(cancellationToken)
            .ConfigureAwait(false));
 }
        public async Task <Result <Nothing, Error> > Handle(RecordContact.Command request, CancellationToken cancellationToken)
        {
            var user = await _userAccessor.GetUser();

            var result = await _aggregateStore.Update <EnrollmentAggregate, EnrollmentId, Result <Nothing, Error> >(
                EnrollmentId.With(request.EnrollmentId), CommandId.New,
                (aggregate) => aggregate.RecordContact(request, user),
                cancellationToken);

            return(result.Unwrap());
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <Enrollment?> > GetById(
            EnrollmentId id,
            CancellationToken cancellationToken)
        {
            var enrollment = await _sender
                             .Send(new GetEnrollmentByIdQuery(id), cancellationToken)
                             .ConfigureAwait(false);

            return(enrollment is null
                ? NotFound()
                : Ok(enrollment));
        }
        public async Task <Result <Nothing, Error> > Handle(RecordRefusedTrainingInvitation.Command request, CancellationToken cancellationToken)
        {
            var user = await _userAccessor.GetUser();

            var enrollment = await _aggregateStore.LoadAsync <EnrollmentAggregate, EnrollmentId>(EnrollmentId.With(request.EnrollmentId), cancellationToken);

            var preferredTrainings = await _trainingRepository.GetByIds(enrollment.PreferredTrainingIds);

            var result = await _aggregateStore.Update <EnrollmentAggregate, EnrollmentId, Result <Nothing, Error> >(
                EnrollmentId.With(request.EnrollmentId), CommandId.New,
                (aggregate) => aggregate.RecordCandidateRefusedTrainingInvitation(request, user, preferredTrainings, _clock.GetCurrentInstant()),
                cancellationToken);

            return(result.Unwrap());
        }
Ejemplo n.º 7
0
        public async Task <Result <Nothing, Error> > Handle(RecordResignation.Command request, CancellationToken cancellationToken)
        {
            var user = await _userAccessor.GetUser();

            var authResult = await _authService.AuthorizeAsync(await _userAccessor.GetClaimsPrincipal(), EnrollmentId.With(request.EnrollmentId), AuthorizationPolicies.OwningCandidateOrCoordinator);

            if (authResult.Succeeded == false)
            {
                return(Result.Failure <Nothing, Error>(new Error.AuthorizationFailed()));
            }

            var result = await _aggregateStore.Update <EnrollmentAggregate, EnrollmentId, Result <Nothing, Error> >(
                EnrollmentId.With(request.EnrollmentId), CommandId.New,
                (aggregate) => aggregate.RecordResignation(request, user, _clock.GetCurrentInstant()),
                cancellationToken);

            return(result.Unwrap());
        }
        public async Task <Result <Nothing, Error> > Handle(RecordTrainingResults.Command request, CancellationToken cancellationToken)
        {
            var user = await _userAccessor.GetUser();

            var selectedTraining = await _trainingRepository.GetById(request.TrainingId);

            if (selectedTraining.HasNoValue)
            {
                return(Result.Failure <Nothing, Error>(new Error.ResourceNotFound($"Training with ID={request.TrainingId} not found")));
            }

            var enrollment = await _aggregateStore.LoadAsync <EnrollmentAggregate, EnrollmentId>(EnrollmentId.With(request.EnrollmentId), cancellationToken);

            var preferredTrainings = await _trainingRepository.GetByIds(enrollment.PreferredTrainingIds);

            var result = await _aggregateStore.Update <EnrollmentAggregate, EnrollmentId, Result <Nothing, Error> >(
                EnrollmentId.With(request.EnrollmentId), CommandId.New,
                (aggregate) => aggregate.RecordTrainingResults(request, user, preferredTrainings, selectedTraining.Value, _clock.GetCurrentInstant()),
                cancellationToken);

            return(result.Unwrap());
        }
        public bool Validate()
        {
            ErrorSummary = string.Empty;

            Validator.AddRule(
                nameof(FacilityHandedTo),
                () => RuleResult.Assert(
                    !string.IsNullOrWhiteSpace(FacilityHandedTo),
                    $"{nameof(FacilityHandedTo)} is required"
                    )
                );

            Validator.AddRule(
                nameof(HandedTo),
                () => RuleResult.Assert(
                    !string.IsNullOrWhiteSpace(HandedTo),
                    $"{nameof(HandedTo)} is required"
                    )
                );


            Validator.AddRule(
                nameof(EnrollmentId),
                () => RuleResult.Assert(
                    !string.IsNullOrWhiteSpace(EnrollmentId) &&
                    EnrollmentId.Trim().Length == 10 && isNumeric(EnrollmentId),
                    $"CCC {nameof(EnrollmentId)} is invalid"
                    )
                );

            Validator.AddRule(
                nameof(DateEnrolled),
                () => RuleResult.Assert(
                    !(DateEnrolled.Date > DateTime.Today),
                    $"{nameof(DateEnrolled)} cannot be in future"
                    )
                );

            if (HasArtStartDate)
            {
                Validator.AddRule(
                    nameof(ARTStartDate),
                    () => RuleResult.Assert(
                        !(ARTStartDate.Date > DateTime.Today),
                        $"{nameof(ARTStartDate)} cannot be in future"
                        )
                    );
            }

            if (null != ParentViewModel.Client)
            {
                if (!ParentViewModel.Client.DateEnrolled.IsNullOrEmpty())
                {
                    Validator.AddRule(
                        nameof(DateEnrolled),
                        () => RuleResult.Assert(
                            !(DateEnrolled.Date < ParentViewModel.Client.DateEnrolled.Value.Date),
                            $"{nameof(DateEnrolled)} cannot be before Registration Date"
                            )
                        );

                    if (HasArtStartDate)
                    {
                        Validator.AddRule(
                            nameof(ARTStartDate),
                            () => RuleResult.Assert(
                                !(ARTStartDate.Date < ParentViewModel.Client.DateEnrolled.Value.Date),
                                $"{nameof(ARTStartDate)} cannot be before Registration Date"
                                )
                            );
                    }
                }
            }



            var result = Validator.ValidateAll();

            Errors = result.AsObservableDictionary();
            if (null != Errors && Errors.Count > 0)
            {
                ErrorSummary = Errors.First().Value;
            }

            return(result.IsValid);
        }
Ejemplo n.º 10
0
 public EnrollmentAggregate(EnrollmentId id) : base(id)
 {
 }
Ejemplo n.º 11
0
 public GetEnrollmentByIdQuery(EnrollmentId id)
 {
     Id = id;
 }