public async Task <IHttpActionResult> Create([FromBody] CreatePersonnelShiftReplacementDto personnelShiftReplacement) { if (personnelShiftReplacement == null) { return(BadRequest()); } if (!ModelState.IsValid) { string errorMessage = new ModelStateError(_logger).OutputMessage(ModelState); return(BadRequest(errorMessage)); } try { var result = await _personnelShiftReplacementService .Create(personnelShiftReplacement, User.Identity.Name); 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> > Create(CreatePersonnelShiftReplacementDto dto , string username) { var user = await _authService.FindUserByUsernameAsync(username); if (user != null) { #region check if both personnel are available and have the same work unit var personnel = _personnelRepository.Get(q => q.Code == user.Username , 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" }); } #endregion var replacementDate = dto.ReplacementDate; if (AlreadyRequested(personnel.Id, replacementDate)) { 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 var personnelShiftReplacement = new PersonnelShiftReplacement { PersonnelId = personnel.Id, ReplacedPersonnelId = dto.ReplacedPersonnelId, WorkingHourId = dto.WorkingHourId, ReplacedWorkingHourId = dto.ReplacedWorkingHourId, RequestedDate = DateTime.Now, ReplacementDate = replacementDate }; _personnelShiftReplacementRepository.Insert(personnelShiftReplacement); int personnelShiftReplacementId = personnelShiftReplacement.Id; //Send Request Message to Approval Proc var notificationReceiverId = await _requestMessageHandlerService .HandleShiftReplacementRequest(personnelShiftReplacementId, user.Username); if (notificationReceiverId == null) { return(new CustomResult <string> { Message = "shift replacement procedure not found" }); } return(new CustomResult <string> { IsValid = true, ReturnId = notificationReceiverId }); } return(new CustomResult <string> { Message = "user not found" }); }