public async Task <IHttpActionResult> Update([FromBody] UpdatePersonnelDutyDto
                                                     personnelDuty)
        {
            if (personnelDuty == null)
            {
                return(BadRequest());
            }
            //custom validations
            var validator = new UpdatePersonnelDutyDtoValidator();
            var results   = validator.Validate(personnelDuty);

            if (!results.IsValid)
            {
                foreach (var failure in results.Errors)
                {
                    ModelState.AddModelError(failure.PropertyName, failure.ErrorMessage);
                }
            }
            if (!ModelState.IsValid)
            {
                string errorMessage = new ModelStateError(_logger).OutputMessage(ModelState);
                return(BadRequest(errorMessage));
            }
            try
            {
                var result = await _personnelDutyService.Update(personnelDuty);

                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());
        }
Пример #2
0
        private async Task DeleteAndInsertAgain(UpdatePersonnelDutyDto dto)
        {
            var personnel = _personnelRepository.GetById(dto.PersonnelId);

            using (var scope = new TransactionScope())
            {
                _personnelDutyRepository.Delete(dto.Id, DeleteState.Permanent);
                await Create(new CreatePersonnelDutyDto
                {
                    DutyId             = dto.DutyId,
                    DutyDuration       = dto.DutyDuration,
                    FileUploadPath     = dto.FileUploadPath,
                    RequestDescription = dto.RequestDescription
                }, personnel.Code);

                scope.Complete();
            }
        }
Пример #3
0
        public async Task <CustomResult <string> > Update(UpdatePersonnelDutyDto dto)
        {
            var personnelDuty = _personnelDutyRepository.GetById(dto.Id);

            if (personnelDuty != null)
            {
                dto.PersonnelId = personnelDuty.PersonnelId;
                var personnel = _personnelRepository.GetById(dto.PersonnelId);
                if (personnel == null)
                {
                    return(new CustomResult <string>
                    {
                        Message = "person is not available"
                    });
                }

                var user = await _authService.FindUserByUsernameAsync(personnel.Code);

                if (user == null)
                {
                    return(new CustomResult <string>
                    {
                        Message = "user for person not found"
                    });
                }

                if (AlreadyRequested(personnel.Id, dto.DailyDuty, dto.HourlyDuty, personnelDuty.Id))
                {
                    return(new CustomResult <string>
                    {
                        Message = "cannot add multiple duties in the time period"
                    });
                }

                if (personnelDuty.RequestAction == RequestAction.Unknown)
                {
                    DateTime from = DateTime.Now;
                    DateTime to   = DateTime.Now;
                    switch (dto.DutyDuration)
                    {
                    case RequestDuration.Daily:
                        if (personnelDuty.DutyDuration == dto.DutyDuration)     //update the same type
                        {
                            var dailyDuty = personnelDuty as PersonnelDailyDuty;
                            dailyDuty.PersonnelId        = dto.PersonnelId;
                            dailyDuty.DutyId             = dto.DutyId;
                            dailyDuty.SubmittedDate      = DateTime.Now;
                            dailyDuty.DutyDuration       = dto.DutyDuration;
                            dailyDuty.RequestDescription = dto.RequestDescription;
                            dailyDuty.FromDate           = dto.DailyDuty.FromDate;
                            dailyDuty.ToDate             = dto.DailyDuty.ToDate;

                            from = dailyDuty.FromDate;
                            to   = dailyDuty.ToDate;
                        }
                        else     //update entity type -> remove and insert again
                        {
                            try
                            {
                                await DeleteAndInsertAgain(dto);
                            }
                            catch (TransactionAbortedException ex)
                            {
                                _logger.LogRunTimeError(ex, ex.Message
                                                        + " Either update or delete operation failed.");
                                throw;
                            }
                            catch (Exception ex)
                            {
                                _logger.LogRunTimeError(ex, ex.Message);
                                throw;
                            }
                        }
                        break;

                    case RequestDuration.Hourly:
                        if (personnelDuty.DutyDuration == dto.DutyDuration)     //update the same type
                        {
                            var hourlyDuty = personnelDuty as PersonnelHourlyDuty;
                            hourlyDuty.PersonnelId        = dto.PersonnelId;
                            hourlyDuty.DutyId             = dto.DutyId;
                            hourlyDuty.SubmittedDate      = DateTime.Now;
                            hourlyDuty.DutyDuration       = dto.DutyDuration;
                            hourlyDuty.RequestDescription = dto.RequestDescription;
                            hourlyDuty.Date     = dto.HourlyDuty.Date;
                            hourlyDuty.FromTime = dto.HourlyDuty.FromTime;
                            hourlyDuty.ToTime   = dto.HourlyDuty.ToTime;

                            from = hourlyDuty.Date.Add(hourlyDuty.FromTime);
                            to   = hourlyDuty.Date.Add(hourlyDuty.ToTime);
                        }
                        else     //update entity type -> remove and insert again
                        {
                            try
                            {
                                await DeleteAndInsertAgain(dto);
                            }
                            catch (TransactionAbortedException ex)
                            {
                                _logger.LogRunTimeError(ex, ex.Message
                                                        + " Either update or delete operation failed.");
                                throw;
                            }
                            catch (Exception ex)
                            {
                                _logger.LogRunTimeError(ex, ex.Message);
                                throw;
                            }
                        }
                        break;

                    default:
                        break;
                    }

                    //Send Request Message to Approval Proc
                    //remove prev
                    _messageService.RemoveRequest(RequestType.Duty, personnelDuty.Id);
                    //insert new
                    var notificationReceiverId = await _requestMessageHandlerService
                                                 .HandleDutyRequest(personnelDuty.Id, personnelDuty.DutyDuration
                                                                    , from, to, user.Username);

                    if (notificationReceiverId == null)
                    {
                        return(new CustomResult <string>
                        {
                            Message = "approval procedure not found"
                        });
                    }

                    return(new CustomResult <string>
                    {
                        IsValid = true,
                        ReturnId = notificationReceiverId
                    });
                }
                else
                {
                    string requestAction = personnelDuty.RequestAction == RequestAction.Accept
                        ? "confirmed" : personnelDuty.RequestAction == RequestAction.PartialAccept
                        ? "waiting for confirmation" : "rejected";
                    return(new CustomResult <string>
                    {
                        IsValid = false,
                        Message = "cannot update the duty info, status: "
                                  + requestAction
                    });
                }
            }
            else
            {
                try
                {
                    throw new LogicalException();
                }
                catch (LogicalException ex)
                {
                    _logger.LogLogicalError
                        (ex, "PersonnelDuty entity with the id: '{0}', is not available." +
                        " update operation failed.", dto.Id);
                    throw;
                }
            }
        }