public async Task <UserPermission> GetAllUserPermission(UserPermissionEmployeeIdDetails employeeIdDetails)
        {
            var userPermission = FindByCondition(x => x.EmployeeId == employeeIdDetails.EmployeeId)
                                 .SingleOrDefault();

            return(await Task.FromResult(userPermission));
        }
        public async Task <List <int> > GetUserPermissionsByEmployeeId(UserPermissionEmployeeIdDetails userPermissionEmployeeIdDetails)
        {
            var userPermissions = FindByCondition(x => string.Equals(x.EmployeeId.Trim(), userPermissionEmployeeIdDetails.EmployeeId.Trim(), StringComparison.OrdinalIgnoreCase) && x.IsDeleted == false)
                                  .Select(x => x.Id)
                                  .ToList();

            return(await Task.FromResult(userPermissions));
        }
        public async Task <UserPermission> GetUserPermission(UserPermissionEmployeeIdDetails userPermissionEmployeeIdDetails)
        {
            var userPermission = FindByCondition(x => x.EmployeeId == userPermissionEmployeeIdDetails.EmployeeId.ToString() && x.IsDeleted == false)
                                 .Include(x => x.Role)
                                 .SingleOrDefault();

            return(await Task.FromResult(userPermission));
        }
        /// <summary>
        /// Deletes the userPermission.
        /// </summary>
        /// <param name="userPermissionEmployeeIdDetails">The userPermission identifier details.</param>
        /// <returns></returns>
        public async Task <CSSResponse> DeleteUserPermission(UserPermissionEmployeeIdDetails userPermissionEmployeeIdDetails)
        {
            var userPermission = await _repository.UserPermissions.GetUserPermission(userPermissionEmployeeIdDetails);

            if (userPermission == null)
            {
                return(new CSSResponse(HttpStatusCode.NotFound));
            }

            userPermission.IsDeleted = true;

            _repository.UserPermissions.UpdateUserPermission(userPermission);
            await _repository.SaveAsync();

            return(new CSSResponse(HttpStatusCode.NoContent));
        }
        /// <summary>
        /// Gets the userPermission.
        /// </summary>
        /// <param name="userPermissionEmployeeIdDetails">The userPermission identifier details.</param>
        /// <returns></returns>
        public async Task <CSSResponse> GetUserPermission(UserPermissionEmployeeIdDetails userPermissionEmployeeIdDetails)
        {
            var userPermission = await _repository.UserPermissions.GetUserPermission(userPermissionEmployeeIdDetails);

            if (userPermission == null)
            {
                // call the agent admins api to check if the user exists on agent admins
                HttpClient client = new HttpClient();
                var        apiUrl = _config.GetValue <string>("GlobalConfiguration:GatewayUrl");
                client.BaseAddress = new Uri(apiUrl);
                var urlParameters = $"agentadmins/employees/{userPermissionEmployeeIdDetails.EmployeeId}";

                // Add an Accept header for JSON format.
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = client.GetAsync(urlParameters).Result;

                if (response.IsSuccessStatusCode)
                {
                    string responseStream = await response.Content.ReadAsStringAsync();

                    UserPermissionAgent agentPermission = JsonConvert.DeserializeObject <UserPermissionAgent>(responseStream);
                    // map the response to new userPermission intended for agents
                    userPermission = new UserPermission {
                        EmployeeId = agentPermission.EmployeeId, Firstname = agentPermission.Firstname, Lastname = agentPermission.Lastname, Sso = agentPermission.Sso
                    };
                }
                else
                {
                    return(new CSSResponse(HttpStatusCode.NotFound));
                }
            }

            var mappedPermission = _mapper.Map <UserPermissionDTO>(userPermission);

            return(new CSSResponse(mappedPermission, HttpStatusCode.OK));
        }
        /// <summary>
        /// Updates the userPermission.
        /// </summary>
        /// <param name="userPermissionEmployeeIdDetails">The userPermission identifier details.</param>
        /// <param name="userPermissionDetails">The userPermission details.</param>
        /// <returns></returns>
        public async Task <CSSResponse> UpdateUserPermission(UserPermissionEmployeeIdDetails userPermissionEmployeeIdDetails, UpdateUserPermissionDTO userPermissionDetails)
        {
            UserPermission userPermission = await _repository.UserPermissions.GetUserPermission(userPermissionEmployeeIdDetails);

            if (userPermission == null)
            {
                return(new CSSResponse(HttpStatusCode.NotFound));
            }

            if (userPermission.Sso != userPermissionDetails.Sso)
            {
                var userPermissions = await _repository.UserPermissions.GetUserPermissionsBySso(new UserPermissionSsoDetails { Sso = userPermissionDetails.Sso });

                if (userPermissions?.Count > 0 && userPermissions.IndexOf(userPermission.Id) == -1)
                {
                    return(new CSSResponse($"User Permission with sso '{userPermissionDetails.Sso}' already exists.", HttpStatusCode.Conflict));
                }
            }

            if (userPermission.EmployeeId != userPermissionDetails.EmployeeId)
            {
                var userPermissions = await _repository.UserPermissions.GetUserPermissionsByEmployeeId(new UserPermissionEmployeeIdDetails { EmployeeId = userPermissionDetails.EmployeeId });

                if (userPermissions?.Count > 0 && userPermissions.IndexOf(userPermission.Id) == -1)
                {
                    return(new CSSResponse($"User Permission with employeeId '{userPermissionDetails.EmployeeId}' already exists.", HttpStatusCode.Conflict));
                }
            }


            var userPermissionRequest = _mapper.Map(userPermissionDetails, userPermission);

            _repository.UserPermissions.UpdateUserPermission(userPermissionRequest);

            await _repository.SaveAsync();

            return(new CSSResponse(HttpStatusCode.NoContent));
        }