public HttpResponseMessage AddConfigDelegation(HttpRequestMessage request, string userId, string groupId, ConfigDelegationModel delegationVm)
        {
            if (!ModelState.IsValid)
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
            else
            {
                //List<ExplanationRequest> lstExplanationRequest = new List<ExplanationRequest>();
                //var ChangeStatusById = AppUserManager.FindByNameAsync(User.Identity.Name).Result.Id;
                List <object> lstData        = new List <object>();
                var           delegationData = _configDelegationService.GetDataDelegationById(delegationVm.ID.ToString());
                delegationData.UpdateConfigDelegation(delegationVm);
                _configDelegationService.Update(delegationData);
                _configDelegationService.SaveChange();

                //Get request by user and group.Filter by StatusRequest is Pendding or Delegate in about time
                var model = _requestService.GetAllRequestByUser(userId, groupId);
                var data  = model.Where(x => (x.StatusRequest.Name.Equals(CommonConstants.StatusPending) ||
                                              x.StatusRequest.Name.Equals(CommonConstants.StatusDelegation)) &&
                                        x.CreatedDate.Value.Date >= delegationData.StartDate &&
                                        x.CreatedDate.Value.Date <= delegationData.EndDate);
                lstData.AddRange(data);
                _configDelegationService.ChangeStatusRequestConfigDelegate(delegationData.AssignTo, data.ToList());

                //Get explanation by userid and groupid.Filter by StatusRequest is Pending or Delegate
                var explanation     = _explanationRequestService.GetListExplanationByUser(userId, groupId);
                var dataExplanation = explanation.Where(a => (a.StatusRequest.Name.Equals(CommonConstants.StatusPending) ||
                                                              a.StatusRequest.Name.Equals(CommonConstants.StatusDelegation)) &&
                                                        a.CreatedDate.Value.Date >= delegationData.StartDate &&
                                                        a.CreatedDate.Value.Date <= delegationData.EndDate);
                lstData.AddRange(explanation);
                _configDelegationService.ChangeStatusExplanationRequestConfigDelegate(delegationData.AssignTo, dataExplanation.ToList());
                return(request.CreateResponse(HttpStatusCode.Created, lstData));
            }
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> Create(HttpRequestMessage request, AppUserViewModel applicationUserViewModel)
        {
            if (ModelState.IsValid)
            {
                if (User != null && MemoryCacheHelper.RemoveUserEditByAdmin(User.Identity.Name))
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, CommonConstants.Error_Edit_By_Admin));
                }
                if (applicationUserViewModel.Roles.Contains(CommonConstants.GroupLead) && applicationUserViewModel.GroupId != null)
                {
                    var groupleaderID = AppRoleManager.FindByNameAsync(CommonConstants.GroupLead).Result.Id;
                    var groupLeader   = AppUserManager.Users.Where(x => x.Roles.Any(r => r.RoleId.Equals(groupleaderID) && x.GroupId == applicationUserViewModel.GroupId)).FirstOrDefault();
                    if (groupLeader != null && !groupLeader.Equals(applicationUserViewModel))
                    {
                        return(request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.Error_Create_Exist_Group_Lead));
                    }
                }

                var username = AppUserManager.FindByNameAsync(applicationUserViewModel.UserName).Result;
                var email    = AppUserManager.FindByEmailAsync(applicationUserViewModel.Email).Result;
                //if (username != null && email != null)
                //{
                //    if (username.Status == false)
                //    {
                //        return request.CreateResponse(HttpStatusCode.OK, "Inaction");
                //    }
                //}
                if (username != null)
                {
                    if (username.Status == true)
                    {
                        return(request.CreateErrorResponse(HttpStatusCode.BadRequest, MessageSystem.MessageDuplicateUserName));
                    }
                }

                if (email != null)
                {
                    if (email.Status == true)
                    {
                        return(request.CreateErrorResponse(HttpStatusCode.BadRequest, MessageSystem.MessageDuplicateEmail));
                    }
                    else
                    {
                        return(request.CreateResponse(HttpStatusCode.OK, "Inaction"));
                    }
                }
                var newAppUser = new AppUser();
                newAppUser.UpdateUser(applicationUserViewModel);
                newAppUser.Status = true;
                try
                {
                    newAppUser.Id = Guid.NewGuid().ToString();
                    if (applicationUserViewModel.ListUserNo != null && applicationUserViewModel.ListUserNo.Count() > 0)
                    {
                        if (!ValidateUserNo(applicationUserViewModel.ListUserNo.Split('-')))
                        {
                            return(request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.MessageUserNoNotValid));
                        }
                        if (_fingermachineuserService.IsUserNoExist(applicationUserViewModel.ListUserNo.Split('-').ToList()))
                        {
                            return(request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.MessageUserNoExist));
                        }
                    }
                    var checkStartDateAndBirthDay = _userService.CheckStartDateAndBirthDay(applicationUserViewModel.BirthDay, applicationUserViewModel.StartWorkingDay);
                    if (!string.IsNullOrEmpty(checkStartDateAndBirthDay))
                    {
                        return(request.CreateErrorResponse(HttpStatusCode.BadRequest, checkStartDateAndBirthDay));
                    }
                    var result = await AppUserManager.CreateAsync(newAppUser, applicationUserViewModel.Password);

                    if (result.Succeeded)
                    {
                        var roles = applicationUserViewModel.Roles.ToArray();
                        await AppUserManager.AddToRolesAsync(newAppUser.Id, roles);

                        //Create entitle day of user new
                        _entitleDayAppUserService.CreateEntitleDayAppUser(newAppUser);
                        //Add user into table config delegation
                        var configDelegation = new ConfigDelegation();
                        configDelegation.UserId = newAppUser.Id;
                        _configDelegateionService.Add(configDelegation);
                        _configDelegateionService.SaveChange();

                        if (applicationUserViewModel.ListUserNo != null)
                        {
                            List <string> lstUserno = applicationUserViewModel.ListUserNo.Split('-').ToList();
                            if (lstUserno.Count() > 0)
                            {
                                if (_fingermachineuserService.IsUserNoExist(lstUserno))
                                {
                                    return(request.CreateResponse(HttpStatusCode.BadRequest, MessageSystem.MessageUserNoExist));
                                }
                                else
                                {
                                    foreach (var item in lstUserno)
                                    {
                                        _fingermachineuserService.Create(new FingerMachineUser()
                                        {
                                            ID = item, UserId = newAppUser.Id
                                        });
                                    }
                                    _unitOfWork.Commit();
                                }
                            }
                        }
                        return(request.CreateResponse(HttpStatusCode.OK, applicationUserViewModel));
                    }

                    else
                    {
                        return(request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Join(",", result.Errors)));
                    }
                }
                catch (NameDuplicatedException dex)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message));
                }
                catch (Exception ex)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
                }
            }
            else
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }