public async Task <IHttpActionResult> Create([FromBody] CreatePersonnelDutyDto
                                                     personnelDuty)
        {
            if (personnelDuty == null)
            {
                return(BadRequest());
            }
            //custom validations
            var validator = new CreatePersonnelDutyDtoValidator();
            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.Create(personnelDuty
                                                                , 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());
        }
Пример #2
0
        public async Task <CustomResult <string> > Create(CreatePersonnelDutyDto dto
                                                          , string username)
        {
            var user = await _authService.FindUserByUsernameAsync(username);

            if (user != null)
            {
                var personnel = _personnelRepository.Get(q => q.Code == user.Username)
                                .SingleOrDefault();
                if (personnel == null)
                {
                    return(new CustomResult <string>
                    {
                        Message = "person is not available"
                    });
                }

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

                int      personnelDutyId = 0;
                DateTime from            = DateTime.Now;
                DateTime to = DateTime.Now;
                switch (dto.DutyDuration)
                {
                case RequestDuration.Daily:
                    if (dto.DailyDuty != null)
                    {
                        var dailyDuty = new PersonnelDailyDuty
                        {
                            PersonnelId        = personnel.Id,
                            DutyId             = dto.DutyId,
                            SubmittedDate      = DateTime.Now,
                            DutyDuration       = dto.DutyDuration,
                            RequestDescription = dto.RequestDescription,
                            FromDate           = dto.DailyDuty.FromDate,
                            ToDate             = dto.DailyDuty.ToDate
                        };
                        _personnelDailyDutyRepository.Insert(dailyDuty);
                        personnelDutyId = dailyDuty.Id;
                        from            = dailyDuty.FromDate;
                        to = dailyDuty.ToDate;
                    }
                    else
                    {
                        PersonnelDutyDetailsError(dto.DutyDuration, "create");
                    }
                    break;

                case RequestDuration.Hourly:
                    if (dto.HourlyDuty != null)
                    {
                        var hourlyDuty = new PersonnelHourlyDuty
                        {
                            PersonnelId        = personnel.Id,
                            DutyId             = dto.DutyId,
                            SubmittedDate      = DateTime.Now,
                            DutyDuration       = dto.DutyDuration,
                            RequestDescription = dto.RequestDescription,
                            Date     = dto.HourlyDuty.Date,
                            FromTime = dto.HourlyDuty.FromTime,
                            ToTime   = dto.HourlyDuty.ToTime
                        };
                        _personnelHourlyDutyRepository.Insert(hourlyDuty);
                        personnelDutyId = hourlyDuty.Id;
                        from            = hourlyDuty.Date.Add(hourlyDuty.FromTime);
                        to = hourlyDuty.Date.Add(hourlyDuty.ToTime);
                    }
                    else
                    {
                        PersonnelDutyDetailsError(dto.DutyDuration, "create");
                    }
                    break;

                default:
                    break;
                }

                //Send Request Message to Approval Proc
                var notificationReceiverId = await _requestMessageHandlerService
                                             .HandleDutyRequest(personnelDutyId, dto.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
                });
            }
            return(new CustomResult <string>
            {
                Message = "user not found"
            });
        }