public async Task <Result> Edit(EditDataCollectorRequestDto editDto) { var phoneNumberExists = await _nyssContext.DataCollectors .AnyAsync(dc => dc.PhoneNumber == editDto.PhoneNumber && dc.Id != editDto.Id); if (phoneNumberExists) { return(Error(ResultKey.DataCollector.PhoneNumberAlreadyExists).Cast <int>()); } var dataCollector = await _nyssContext.DataCollectors .Include(dc => dc.Project) .ThenInclude(x => x.NationalSociety) .Include(dc => dc.Supervisor) .Include(dc => dc.Village) .ThenInclude(v => v.District) .ThenInclude(d => d.Region) .Include(dc => dc.Zone) .SingleAsync(dc => dc.Id == editDto.Id); if (dataCollector.Project.State != ProjectState.Open) { return(Error(ResultKey.DataCollector.ProjectIsClosed)); } var nationalSocietyId = dataCollector.Project.NationalSociety.Id; var supervisor = await _nyssContext.UserNationalSocieties .FilterAvailableUsers() .Where(u => u.User.Id == editDto.SupervisorId && u.User.Role == Role.Supervisor && u.NationalSocietyId == nationalSocietyId) .Select(u => (SupervisorUser)u.User) .SingleAsync(); var village = await _nyssContext.Villages .SingleAsync(v => v.Id == editDto.VillageId && v.District.Region.NationalSociety.Id == nationalSocietyId); var zone = editDto.ZoneId != null ? await _nyssContext.Zones.SingleAsync(z => z.Id == editDto.ZoneId.Value) : null; dataCollector.Name = editDto.Name; dataCollector.DisplayName = editDto.DisplayName; dataCollector.PhoneNumber = editDto.PhoneNumber; dataCollector.AdditionalPhoneNumber = editDto.AdditionalPhoneNumber; dataCollector.BirthGroupDecade = editDto.BirthGroupDecade; dataCollector.Location = CreatePoint(editDto.Latitude, editDto.Longitude); dataCollector.Sex = editDto.Sex; dataCollector.Village = village; dataCollector.Supervisor = supervisor; dataCollector.Zone = zone; await _nyssContext.SaveChangesAsync(); return(SuccessMessage(ResultKey.DataCollector.EditSuccess)); }
public void EditDataCollector_WhenDataCollectorDoesNotExist_ShouldThrowException() { // Arrange var dataCollector = new EditDataCollectorRequestDto { Id = 3, PhoneNumber = DataCollectorPhoneNumber3, SupervisorId = SupervisorId, VillageId = _nyssContextMock.Villages.ToList()[0].Id, Latitude = 15, Longitude = 45 }; Should.ThrowAsync <Exception>(() => _dataCollectorService.Edit(dataCollector)); }
public async Task EditDataCollector_WhenSuccessful_ShouldReturnSuccess(DataCollectorType type, string displayName) { // Arrange var dataCollector = new EditDataCollectorRequestDto { DataCollectorType = type, Id = DataCollectorWithoutReportsId, DisplayName = displayName, PhoneNumber = DataCollectorPhoneNumber1, SupervisorId = SupervisorId, VillageId = _nyssContextMock.Villages.ToList()[0].Id, Latitude = 15, Longitude = 45 }; // Act var result = await _dataCollectorService.Edit(dataCollector); // Assert result.IsSuccess.ShouldBeTrue(); result.Message.Key.ShouldBe(ResultKey.DataCollector.EditSuccess); }
public async Task <Result> Edit([FromBody] EditDataCollectorRequestDto editDataCollectorDto) => await _dataCollectorService.Edit(editDataCollectorDto);