/// <summary> /// Adds new response for report /// </summary> /// <param name="responseToCreate">Data of new response</param> /// <param name="userId">Identifier of currently authorized user</param> /// <returns>Service result with operation status and object to return (Updated details of the report)</returns> public async Task <IServiceResult <ReportDetailsToReturnDto> > SendResponse(ReportResponseToCreateDto responseToCreate, int userId) { try { UserEntity user = await _userManager.Users.Where(x => x.Id == userId).Include(ur => ur.UserRoles).ThenInclude(r => r.Role).FirstOrDefaultAsync(); string userRole = user.UserRoles.Select(x => x.Role).FirstOrDefault().Name; ReportEntity reportToUpdate = await _repository.GetReportById(responseToCreate.ReportId, userRole, userId); if (reportToUpdate != null) { ResponseEntity response = new ResponseEntity { Date = DateTime.Now, Message = responseToCreate.Message, UserId = userId }; reportToUpdate.Responses.Add(response); reportToUpdate.ModificationEntries.Add(HistoryEntriesGenerator.GetNewResponseEntry(user.UserName, reportToUpdate.Heading)); _repository.SaveChanges(); await SendEmployeeRespondedEmail(reportToUpdate.UserId, reportToUpdate.Heading); ReportDetailsToReturnDto reportToReturn = _reportMapper.Map(reportToUpdate); return(new ServiceResult <ReportDetailsToReturnDto>(ResultType.Correct, reportToReturn)); } return(new ServiceResult <ReportDetailsToReturnDto>(ResultType.Failed, new List <string> { "Błąd podczas aktualizacji zgłoszenia" })); } catch (Exception) { return(new ServiceResult <ReportDetailsToReturnDto>(ResultType.Error, new List <string> { "Błąd podczas aktualizacji zgłoszenia" })); } }
/// <summary> /// Updates status of report /// </summary> /// <param name="statusToUpdate">New status and report identifier</param> /// <param name="userId">Identifier of currently authorized user</param> /// <returns>Service result with operation status and object to return (Updated modifiation entries and status)</returns> public async Task <IServiceResult <ReportStatusUpdateToReturnDto> > UpdateStatus(ReportStatusUpdateToCreateDto statusToUpdate, int userId) { try { UserEntity user = await _userManager.Users.Where(x => x.Id == userId).Include(ur => ur.UserRoles).ThenInclude(r => r.Role).FirstOrDefaultAsync(); string userRole = user.UserRoles.Select(x => x.Role).FirstOrDefault().Name; ReportEntity reportToUpdate = await _repository.GetReportById(statusToUpdate.ReportId, userRole, userId); if (reportToUpdate != null) { reportToUpdate.Status = (StatusEnum)Enum.Parse(typeof(StatusEnum), statusToUpdate.NewStatus.ToString()); string statusName = ConvertToStatusName(reportToUpdate.Status); reportToUpdate.ModificationEntries.Add(HistoryEntriesGenerator.GetStatusUpdatedEntry(user.UserName, statusName)); _repository.SaveChanges(); await SendStatusChangedEmail(reportToUpdate.UserId, reportToUpdate.Heading, reportToUpdate.Status); ReportStatusUpdateToReturnDto statusUpdated = new ReportStatusUpdateToReturnDto { ModificationEntries = _reportMapper.Map(reportToUpdate.ModificationEntries).ToList(), Status = statusToUpdate.NewStatus }; return(new ServiceResult <ReportStatusUpdateToReturnDto>(ResultType.Correct, statusUpdated)); } return(new ServiceResult <ReportStatusUpdateToReturnDto>(ResultType.Error, new List <string> { "Błąd podczas aktualizacji zgłoszenia" })); } catch (Exception) { return(new ServiceResult <ReportStatusUpdateToReturnDto>(ResultType.Error, new List <string> { "Błąd podczas aktualizacji zgłoszenia" })); } }