async Task RemoveIfExist(StudyParticipant studyParticipant) { if (studyParticipant != null) { await RemoveIfExist(studyParticipant.StudyId, studyParticipant.UserId, studyParticipant.RoleName); } }
protected StudyParticipantDto ConvertToDto(StudyParticipant studyParticipant, UserDto user) { var dto = _mapper.Map <StudyParticipantDto>(studyParticipant); dto.FullName = user.FullName; dto.UserName = user.UserName; dto.EmailAddress = user.EmailAddress; return(dto); }
async Task <StudyParticipantDto> AddAzureUserAsync(int studyId, ParticipantLookupDto user, string role) { UserDto addedUser; if (_userService.IsMockUser()) //If mock user, he can only add him self { addedUser = await _userService.GetCurrentUserAsync(); await _userService.EnsureExists(addedUser); } else { var newUserFromAzure = await _azureADUsersService.GetUserAsync(user.ObjectId); if (newUserFromAzure == null) { throw new NotFoundException($"AD User with id {user.ObjectId} not found!"); } addedUser = await _userService.EnsureExists(new UserDto(user.ObjectId, newUserFromAzure.UserPrincipalName, newUserFromAzure.DisplayName, newUserFromAzure.Mail)); } var studyFromDb = await GetStudyForParticipantOperation(studyId, role); if (RoleAllreadyExistsForUser(studyFromDb, addedUser.Id, role)) { throw new ArgumentException($"Role {role} allready granted for user {user.DatabaseId.Value} on study {studyFromDb.Id}"); } StudyParticipant createdStudyParticipant = null; try { createdStudyParticipant = new StudyParticipant { UserId = addedUser.Id, StudyId = studyFromDb.Id, RoleName = role }; studyFromDb.StudyParticipants = new List <StudyParticipant> { createdStudyParticipant }; await _db.SaveChangesAsync(); return(ConvertToDto(createdStudyParticipant, addedUser)); } catch (Exception) { await RemoveIfExist(createdStudyParticipant); throw; } }
public static void AddParticipant(Study study, int userId, string role) { if (study.StudyParticipants == null) { study.StudyParticipants = new List <StudyParticipant>(); } var newParticipant = new StudyParticipant() { UserId = userId, RoleName = role, CreatedBy = "seed", Created = DateTime.UtcNow }; study.StudyParticipants.Add(newParticipant); }
public static void Map(this StudyParticipant dbStudyParticipant, StudyParticipant studyParticipant) { dbStudyParticipant.ParticipantId = studyParticipant.ParticipantId; dbStudyParticipant.StudyId = studyParticipant.StudyId; dbStudyParticipant.SiteId = studyParticipant.SiteId; dbStudyParticipant.GroupId = studyParticipant.GroupId; dbStudyParticipant.StartDate = studyParticipant.StartDate; dbStudyParticipant.EndDate = studyParticipant.EndDate; dbStudyParticipant.Abandoned = studyParticipant.Abandoned; dbStudyParticipant.DataCollectionTurn = studyParticipant.DataCollectionTurn; dbStudyParticipant.PlaceOfConsent = studyParticipant.PlaceOfConsent; dbStudyParticipant.RegionOfResidence = studyParticipant.RegionOfResidence; dbStudyParticipant.PlaceOfResidence = studyParticipant.PlaceOfResidence; dbStudyParticipant.MaritalStatus = studyParticipant.MaritalStatus; dbStudyParticipant.NumberOfChildren = studyParticipant.NumberOfChildren; dbStudyParticipant.DateOfCurrentJob = studyParticipant.DateOfCurrentJob; dbStudyParticipant.JobType = studyParticipant.JobType; dbStudyParticipant.Education = studyParticipant.Education; }
async Task <StudyParticipantDto> AddDbUserAsync(int studyId, int userId, string role) { var studyFromDb = await GetStudyForParticipantOperation(studyId, role); if (RoleAllreadyExistsForUser(studyFromDb, userId, role)) { throw new ArgumentException($"Role {role} allready granted for user {userId} on study {studyFromDb.Id}"); } var userFromDb = await _userService.GetByDbIdAsync(userId); if (userFromDb == null) { throw NotFoundException.CreateForEntity("User", userId); } StudyParticipant createdStudyParticipant = null; try { createdStudyParticipant = new StudyParticipant { StudyId = studyFromDb.Id, UserId = userId, RoleName = role }; await _db.StudyParticipants.AddAsync(createdStudyParticipant); await _db.SaveChangesAsync(); return(ConvertToDto(createdStudyParticipant, userFromDb)); } catch (Exception) { await RemoveIfExist(createdStudyParticipant); throw; } }