public async Task <ActionResult <User> > GetUser(string coachId) { var currentUserId = User.Identity.Name; User user; try { if (User.IsInRole(Role.Admin)) { user = await _coachService.GetUserFromAdminAsync(coachId); } else if (User.IsInRole(Role.Coach)) { user = await _coachService.GetUserFromCoachAsync(currentUserId, coachId); } else if (User.IsInRole(Role.Builder)) { user = await _coachService.GetUserFromBuilderAsync(currentUserId, coachId); } else { return(Forbid("You must be part of the Buildup program")); } } catch (UnauthorizedAccessException e) { return(Forbid($"You are not allowed to get user's info: {e.Message}")); } catch (Exception e) { return(BadRequest($"Can't get the user: {e.Message}")); } if (user == null) { return(NotFound()); } return(Ok(user)); }
// Sending proof public async Task <string> SendReturningAsync(string currentUserId, string projectId, BuildOnReturningSubmitModel buildOnReturningSubmitModel) { // First we need basics checks Builder builder = await _buildersService.GetBuilderFromAdminAsync(currentUserId); if (builder == null) { throw new UnauthorizedAccessException("You are not a builder"); } Coach coachForBuilder = await _buildersService.GetCoachForBuilderFromAdminAsync(builder.Id); var project = await _projectsService.GetProjectAsync(builder.Id); if (project == null) { throw new Exception("The project doesn't exist"); } if (coachForBuilder == null) { throw new Exception("This builder don't have a coach..."); } if (project.Id != projectId) { throw new UnauthorizedAccessException("The project doesn't belong to you"); } User userForCoach = await _coachsService.GetUserFromAdminAsync(coachForBuilder.Id); if (userForCoach == null) { throw new Exception("The coach doesn't have any user"); } // Then we register the returning string fileId = null; if (buildOnReturningSubmitModel.File != null && buildOnReturningSubmitModel.File.Length >= 1) { var filename = $"{projectId}_{buildOnReturningSubmitModel.FileName}"; fileId = await _filesService.UploadFile(filename, buildOnReturningSubmitModel.File, false); } BuildOnReturning returning = new BuildOnReturning() { ProjectId = projectId, BuildOnStepId = buildOnReturningSubmitModel.BuildOnStepId, Type = buildOnReturningSubmitModel.Type, Status = BuildOnReturningStatus.Waiting, FileName = buildOnReturningSubmitModel.FileName, FileId = fileId, Comment = buildOnReturningSubmitModel.Comment }; await _buildOnReturnings.InsertOneAsync(returning); // Now we need to notify the coach await _notificationService.NotifyBuildOnReturningSubmited(coachForBuilder.Id, userForCoach.Email); return(returning.Id); }