Exemplo n.º 1
0
 public async Task <HttpResponseMessage> Create(HttpRequestMessage request, RequestViewModel requestViewModel)
 {
     return(await CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         if (!ModelState.IsValid)
         {
             response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
         }
         var dateNow = DateTime.Now.Date;
         if (requestViewModel.StartDate < dateNow)
         {
             return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_CREATE_REQUEST_IN_PAST_NOT_MSG);
         }
         else
         {
             requestViewModel.AppUser = new Models.AppUserViewModel();
             requestViewModel.CreatedDate = DateTime.Now;
             requestViewModel.UserId = AppUserManager.FindByNameAsync(User.Identity.Name).Result.Id;
             requestViewModel.AppUser.GroupId = AppUserManager.FindByNameAsync(User.Identity.Name).Result.GroupId ?? 0;
             requestViewModel.Status = true;
             //requestViewModel.ChangeStatusById = __groupService.GetGroupLeadIdByGroup(requestViewModel.AppUser.GroupId.Value);
             Request newRequest = new Request();
             newRequest.UpdateRequest(requestViewModel);
             //Check Request Day Off
             var model = _requestService.GetAllRequest(newRequest);
             if (model == 0)
             {
                 if (_requestService.checkUnitEntitleday(newRequest))
                 {
                     if (_requestService.Add(newRequest))
                     {
                         var responseData = Mapper.Map <Request, RequestViewModel>(newRequest);
                         var group = __groupService.GetGroupById(requestViewModel.AppUser.GroupId.ToString());
                         responseData.GroupName = group.Name;
                         var dataDelegation = _configDelegationService.GetDelegationByUserId(newRequest.UserId);
                         if (dataDelegation.StartDate <= newRequest.CreatedDate.Value.Date && dataDelegation.EndDate >= newRequest.CreatedDate.Value.Date)
                         {
                             responseData.CheckConfigDelegateDefault = true;
                             responseData.AssignConfigDelegate = dataDelegation.AssignTo;
                             _configDelegationService.ChangeStatusAfterAddRequest(dataDelegation.AssignTo, newRequest);
                         }
                         else
                         {
                             if (newRequest.CreatedDate != null)
                             {
                                 if (group.DelegateId != null && group.StartDate <= newRequest.CreatedDate.Value.Date && group.EndDate >= newRequest.CreatedDate.Value.Date)
                                 {
                                     responseData.CheckGroupDelegateDefault = true;
                                     responseData.AssignGroupDelegate = group.DelegateId;
                                     _requestService.CheckDelegateDefault(requestViewModel.AppUser.GroupId.ToString(), newRequest.ID, newRequest.ChangeStatusById);
                                 }
                             }
                         }
                         return request.CreateResponse(HttpStatusCode.Created, responseData);
                     }
                 }
                 else
                 {
                     return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_ENTITLEDAY);
                 }
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_CREATE_REQUEST_NOT_MSG);
             }
             if (model == 1)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 2)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME_MORNING);
             }
             if (model == 3)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME_AFTERNOON);
             }
             if (model == 4)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME_LATECOMING);
             }
             if (model == 5)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME_EARLYLEAVING);
             }
             if (model == 6)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 7)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_MORNING);
             }
             if (model == 8)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_MORNING_LATECOMING);
             }
             if (model == 9)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 10)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_AFTERNOONLEAVE);
             }
             if (model == 11)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_AFTERNOONLEAVE_EARLYLEAVING);
             }
             if (model == 12)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 13)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_MORNING_LATECOMING);
             }
             if (model == 14)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_LATECOMING_EARLYLEAVING);
             }
             if (model == 15)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_FULLTIME);
             }
             if (model == 16)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_EARLYLEAVING_AFTERNOONLEAVE);
             }
             if (model == 17)
             {
                 return request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.ERROR_REQUEST_EARLYLEAVING);
             }
         }
         return response;
     }));
 }