public async Task <ActionResult <string> > SubmitReturning(string projectId, [FromBody] BuildOnReturningSubmitModel buildOnReturningSubmitModel) { var currentUserId = User.Identity.Name; string result; try { if (User.IsInRole(Role.Builder)) { result = await _buildOnsService.SendReturningAsync(currentUserId, projectId, buildOnReturningSubmitModel); } else { return(Forbid("You must be a builder to submit returnings")); } } catch (UnauthorizedAccessException e) { return(Forbid($"Can't submit the returning: {e.Message}")); } catch (Exception e) { return(BadRequest($"Can't submit the returning: {e.Message}")); } return(Ok(result)); }
// 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); }