예제 #1
0
        public async Task <Maybe <Details.TrainingDetails> > Handle(Details.Query request, CancellationToken cancellationToken)
        {
            var now = _clock.GetCurrentInstant();

            var maybeTraining = await _trainingRepo.GetById(request.TrainingId);

            if (maybeTraining.HasNoValue)
            {
                return(Maybe <Details.TrainingDetails> .None);
            }

            var training = maybeTraining.Value;
            var trainer  = await _trainerProvider.GetTrainerDetails(training.CoordinatorID);

            if (trainer.HasNoValue)
            {
                throw new ApplicationException($"inconsistent data in DB: cannot find trainer with Id={training.CoordinatorID}");
            }

            if (now < training.StartDateTime.ToInstant())
            {
                return(Maybe <Details.TrainingDetails> .From(await BuildFutureTrainingDetails(training, trainer.Value, now)));
            }
            else if (training.EndDateTime.ToInstant() < now)
            {
                return(Maybe <Details.TrainingDetails> .From(await BuildPastTrainingDetails(training, trainer.Value)));
            }
            else
            {
                return(Maybe <Details.TrainingDetails> .From(await BuildCurrentTrainingDetails(training, trainer.Value)));
            }
        }
예제 #2
0
        public async Task <Result <GetEnrollmentDetails.Details, Error> > Handle(GetEnrollmentDetails.QueryByEnrollmentId request, CancellationToken cancellationToken)
        {
            var id = EnrollmentAggregate.EnrollmentId.With(request.EnrollmentID);

            var events = await _eventStore.LoadEventsAsync <EnrollmentAggregate, EnrollmentAggregate.EnrollmentId>(id, cancellationToken);

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

            if (enrollment.IsNew)
            {
                return(Result.Failure <GetEnrollmentDetails.Details, Error>(new Error.ResourceNotFound()));
            }

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

            var trainers = await _trainerProvider.GetTrainerDetails(preferredTrainings.Select(x => x.CoordinatorID).ToArray());

            var latestCampaign = (await _campaignRepository.GetAll())
                                 .OrderByDescending(x => x.StartDateTime, OffsetDateTime.Comparer.Instant)
                                 .FirstOrDefault();
            var now = _clock.GetCurrentInstant();

            var details = await BuildDetails(enrollment, events, preferredTrainings, trainers, now, latestCampaign);

            return(Result.Success <GetEnrollmentDetails.Details, Error>(details));
        }
예제 #3
0
        public async Task <IReadOnlyCollection <TrainingSummary> > Handle(Recruitment.Trainings.Index.Query request, CancellationToken cancellationToken)
        {
            var query = _repository.Query();

            if (request.CampaignIds.Any())
            {
                query = query.Where(x => request.CampaignIds.Contains(x.Campaign.Id));
            }
            if (!string.IsNullOrWhiteSpace(request.City))
            {
                query = query.Where(x => request.City.Contains(x.City));
            }
            if (request.CoordinatorId.HasValue)
            {
                query = query.Where(x => x.CoordinatorID == request.CoordinatorId);
            }

            var now        = _clock.GetCurrentInstant();
            var trainerIds = await query.Select(x => x.CoordinatorID).ToListAsync();

            var trainers = (await _trainerProvider.GetTrainerDetails(trainerIds)).ToDictionary(x => x.Guid);

            var results = await query
                          .Select(x => new TrainingSummary()
            {
                ID              = x.ID,
                Address         = x.Address,
                City            = x.City,
                StartDateTime   = x.StartDateTime,
                EndDateTime     = x.EndDateTime,
                CoordinatorID   = x.CoordinatorID,
                CoordinatorName = trainers.ContainsKey(x.CoordinatorID) ? trainers[x.CoordinatorID].Name : string.Empty,
                Timing          = x.CalculateTiming(now)
            }).ToListAsync() as IEnumerable <TrainingSummary>;

            if (request.From.HasValue)
            {
                results = results.Where(x => x.StartDateTime.ToInstant() >= request.From.Value);
            }
            if (request.To.HasValue)
            {
                results = results.Where(x => x.EndDateTime.ToInstant() <= request.To.Value);
            }

            return(results
                   .OrderBy(x => x.StartDateTime.ToInstant())
                   .ToArray());
        }
예제 #4
0
        public async Task <Result <Campaign, Error> > Handle(Query request, CancellationToken cancellationToken)
        {
            var campaign = await _repository.GetById(request.CampaignID);

            if (campaign == null)
            {
                return(Result.Failure <Campaign, Error>(new Error.ResourceNotFound($"Nie znaleziono kampanii o ID={request.CampaignID}")));
            }

            var now = _clock.GetCurrentInstant();

            var trainerIDs = campaign.Trainings.Select(x => x.CoordinatorID).Distinct().ToArray();
            var trainers   = await _trainerProvider.GetTrainerDetails(trainerIDs);

            var isrecruitmentFormOpen = new Interval(campaign.StartDateTime.ToInstant(), campaign.EndDateTime.ToInstant()).Contains(now);
            var canScheduleTraining   = (now < campaign.StartDateTime.ToInstant()) &&
                                        (await _authorizationAnalyzer.Authorize(new ScheduleTraining.Command())).Succeeded;

            return(Result.Success <Campaign, Error>(new Campaign()
            {
                ID = campaign.Id,
                Name = campaign.Name,
                StartDateTime = campaign.StartDateTime,
                EndDateTime = campaign.EndDateTime,
                Trainings = campaign.Trainings
                            .OrderBy(x => x.StartDateTime, OffsetDateTime.Comparer.Instant)
                            .Select(x => new TrainingSummary()
                {
                    ID = x.ID,
                    Address = x.Address,
                    City = x.City,
                    CoordinatorID = x.CoordinatorID,
                    CoordinatorName = trainers.Single(y => y.Guid == x.CoordinatorID).Name,
                    StartDateTime = x.StartDateTime,
                    EndDateTime = x.EndDateTime,
                    Timing = x.CalculateTiming(now)
                })
                            .ToArray(),
                IsRecruitmentFormOpen = isrecruitmentFormOpen,
                CanScheduleTraining = canScheduleTraining
            }));
        }