public async Task <IHttpActionResult> Update([FromBody] UpdatePersonnelShiftReplacementDto personnelShiftReplacement) { if (personnelShiftReplacement == null) { return(BadRequest()); } if (!ModelState.IsValid) { string errorMessage = new ModelStateError(_logger).OutputMessage(ModelState); return(BadRequest(errorMessage)); } try { var result = await _personnelShiftReplacementService.Update(personnelShiftReplacement); if (result.ReturnId != null) { await _notificationService.NotifyUpdates(result.ReturnId); } if (!result.IsValid) { return(BadRequest(result.Message)); } } catch (LogicalException ex) { return(BadRequest(ex.Message)); } catch { return(BadRequest(AppSettings.INTERNAL_SERVER_ERROR_MESSAGE)); } return(Ok()); }
public async Task <CustomResult <string> > Update(UpdatePersonnelShiftReplacementDto dto) { var personnelShiftReplacement = _personnelShiftReplacementRepository.GetById(dto.Id); if (personnelShiftReplacement != null) { dto.PersonnelId = personnelShiftReplacement.PersonnelId; #region check if both personnel are available and have the same work unit var personnel = _personnelRepository.Get(q => q.Id == dto.PersonnelId , includeProperties: "Position").SingleOrDefault(); if (personnel == null) { return(new CustomResult <string> { Message = "person info is not available" }); } var replacedPersonnel = _personnelRepository.Get(q => q.Id == dto.ReplacedPersonnelId , includeProperties: "Position").SingleOrDefault(); if (replacedPersonnel == null) { return(new CustomResult <string> { Message = "replaced person info is not available" }); } if (personnel.Position.WorkUnitId != replacedPersonnel.Position.WorkUnitId) { return(new CustomResult <string> { Message = "work unit for both personnel should be the same" }); } var user = await _authService.FindUserByUsernameAsync(personnel.Code); #endregion var replacementDate = dto.ReplacementDate; if (AlreadyRequested(personnel.Id, replacementDate, personnelShiftReplacement.Id)) { return(new CustomResult <string> { Message = "cannot add multiple replacement in the same day" }); } #region check if replacement date is valid for any of the selected personnel var assignmentAvailable = _personnelShiftService.IsAssignmentAvailable(dto.ShiftId, replacementDate); if (!assignmentAvailable) { return(new CustomResult <string> { Message = "there is no shift for the personnel in the requested date" }); } #endregion //ok if (personnelShiftReplacement.RequestAction == RequestAction.Unknown) { personnelShiftReplacement.PersonnelId = dto.PersonnelId; personnelShiftReplacement.ReplacedPersonnelId = dto.ReplacedPersonnelId; personnelShiftReplacement.WorkingHourId = dto.WorkingHourId; personnelShiftReplacement.ReplacedWorkingHourId = dto.ReplacedWorkingHourId; personnelShiftReplacement.ReplacementDate = replacementDate; _personnelShiftReplacementRepository.Update(personnelShiftReplacement); //Send Request Message to Approval Proc //remove prev _messageService.RemoveRequest(RequestType.ShiftReplacement, personnelShiftReplacement.Id); //insert new var notificationReceiverId = await _requestMessageHandlerService .HandleShiftReplacementRequest(personnelShiftReplacement.Id, user.Username); if (notificationReceiverId == null) { return(new CustomResult <string> { Message = "shift replacement procedure not found" }); } return(new CustomResult <string> { IsValid = true, ReturnId = notificationReceiverId }); } else { string requestAction = personnelShiftReplacement.RequestAction == RequestAction.Accept ? "confirmed" : personnelShiftReplacement.RequestAction == RequestAction.PartialAccept ? "waiting for confirmation" : "rejected"; return(new CustomResult <string> { IsValid = false, Message = "cannot update the shift replacement info, status:" + requestAction }); } } else { try { throw new LogicalException(); } catch (LogicalException ex) { _logger.LogLogicalError (ex, "PersonnelShiftReplacement entity with the id: '{0}', is not available." + " update operation failed.", dto.Id); throw; } } }