Example #1
0
        public async Task <Result> Create(int projectId, CreateDataCollectorRequestDto createDto)
        {
            var phoneNumberExists = await _nyssContext.DataCollectors
                                    .Where(dc => dc.Project.State == ProjectState.Open)
                                    .AnyAsync(dc => dc.PhoneNumber == createDto.PhoneNumber);

            if (phoneNumberExists)
            {
                return(Error(ResultKey.DataCollector.PhoneNumberAlreadyExists).Cast <int>());
            }

            var project = await _nyssContext.Projects
                          .Include(p => p.NationalSociety)
                          .SingleAsync(p => p.Id == projectId);

            if (project.State != ProjectState.Open)
            {
                return(Error(ResultKey.DataCollector.ProjectIsClosed));
            }

            var nationalSocietyId = project.NationalSociety.Id;

            var supervisor = await _nyssContext.UserNationalSocieties
                             .FilterAvailableUsers()
                             .Where(u => u.User.Id == createDto.SupervisorId && u.User.Role == Role.Supervisor && u.NationalSocietyId == nationalSocietyId)
                             .Select(u => (SupervisorUser)u.User)
                             .SingleAsync();

            var village = await _nyssContext.Villages
                          .SingleAsync(v => v.Id == createDto.VillageId && v.District.Region.NationalSociety.Id == nationalSocietyId);

            var zone = createDto.ZoneId != null
                ? await _nyssContext.Zones.SingleAsync(z => z.Id == createDto.ZoneId.Value)
                : null;

            var dataCollector = new DataCollector
            {
                Name                  = createDto.Name,
                DisplayName           = createDto.DisplayName,
                PhoneNumber           = createDto.PhoneNumber,
                AdditionalPhoneNumber = createDto.AdditionalPhoneNumber,
                BirthGroupDecade      = createDto.BirthGroupDecade,
                Sex = createDto.Sex,
                DataCollectorType = createDto.DataCollectorType,
                Location          = CreatePoint(createDto.Latitude, createDto.Longitude),
                Village           = village,
                Supervisor        = supervisor,
                Project           = project,
                Zone             = zone,
                CreatedAt        = _dateTimeProvider.UtcNow,
                IsInTrainingMode = true
            };

            await _nyssContext.AddAsync(dataCollector);

            await _nyssContext.SaveChangesAsync();

            return(Success(ResultKey.DataCollector.CreateSuccess));
        }
Example #2
0
        public async Task CreateDataCollector_WhenPhoneNumberAlreadyExists_ShouldReturnError()
        {
            // Arrange
            var dataCollector = new CreateDataCollectorRequestDto
            {
                PhoneNumber  = DataCollectorPhoneNumber1,
                SupervisorId = SupervisorId,
                VillageId    = _nyssContextMock.Villages.ToList()[0].Id,
                Latitude     = 15,
                Longitude    = 45
            };

            // Act
            var result = await _dataCollectorService.Create(ProjectId, dataCollector);

            // Assert
            result.IsSuccess.ShouldBeFalse();
            result.Message.Key.ShouldBe(ResultKey.DataCollector.PhoneNumberAlreadyExists);
        }
Example #3
0
        public async Task CreateDataCollector_WhenSuccess_ShouldReturnSuccess(DataCollectorType type, string displayName)
        {
            // Arrange
            var dataCollector = new CreateDataCollectorRequestDto
            {
                DataCollectorType = type,
                PhoneNumber       = "+4712344567",
                DisplayName       = displayName,
                SupervisorId      = SupervisorId,
                VillageId         = _nyssContextMock.Villages.ToList()[0].Id,
                Latitude          = 15,
                Longitude         = 45
            };

            // Act
            await _dataCollectorService.Create(ProjectId, dataCollector);

            // Assert
            await _nyssContextMock.Received(1).AddAsync(Arg.Any <DataCollector>());
        }
Example #4
0
 public async Task <Result> Create(int projectId, [FromBody] CreateDataCollectorRequestDto createDataCollectorDto) =>
 await _dataCollectorService.Create(projectId, createDataCollectorDto);