public void FromResponseDataTest()
        {
            var departmentRequest = new DepartmentRequest
            {
                Id = 2,
                DisplayOrder = 2,
                Module = DepartmentModule.LiveChat,
                ParentDepartmentId = 2,
                Title = "Title",
                Type = DepartmentType.Private,
                UserGroups = new List<int>(),
                UserVisibilityCustom = false
            };

            var department = new Department
                {
                    Id = 2,
                    DisplayOrder = 2,
                    Module = DepartmentModule.LiveChat,
                    ParentDepartmentId = 2,
                    Title = "Title",
                    Type = DepartmentType.Private,
                    UserGroups = new List<int>(),
                    UserVisibilityCustom = false
                };

            var departmentRequestResult = DepartmentRequest.FromResponseData(department);

            AssertUtility.ObjectsEqual(departmentRequest, departmentRequestResult);
        }
        /// <summary>
        /// Update the department identified by its internal identifier
        /// </summary>
        /// <param name="dept">Data to update the department. Department Id and Title must be supplied</param>
        /// <returns>Department data representing the updated department</returns>
		public Department UpdateDepartment(DepartmentRequest dept)
		{
			string apiMethod = String.Format("{0}/{1}", ApiBaseMethods.Departments, dept.Id);

            RequestBodyBuilder parameters = PopulateRequestParameters(dept, RequestTypes.Update);

			DepartmentCollection depts = Connector.ExecutePut<DepartmentCollection>(apiMethod, parameters.ToString());

			if (depts != null && depts.Count > 0)
			{
				return depts[0];
			}

			return null;
		}
        public void EnsureValidDataTest()
        {
            var departmentRequest = new DepartmentRequest
            {
                DisplayOrder = 2,
                Module = DepartmentModule.LiveChat,
                ParentDepartmentId = 2,
                Title = "Title",
                Type = DepartmentType.Private,
                UserGroups = new List<int>(),
                UserVisibilityCustom = false
            };

            Assert.DoesNotThrow(() => departmentRequest.EnsureValidData(RequestTypes.Create));
            Assert.DoesNotThrow(() => departmentRequest.EnsureValidData(RequestTypes.Update));
        }
        public void CreateDepartment()
        {
            var departmentRequest = new DepartmentRequest { Title = "Title", DisplayOrder = 2, Type = DepartmentType.Public };
            var departments = new DepartmentCollection { new Department { Title = "Title", DisplayOrder = 2, Type = DepartmentType.Public} };

            var parameters = new RequestBodyBuilder();
            parameters.AppendRequestData("title", departmentRequest.Title);
            parameters.AppendRequestData("type", EnumUtility.ToApiString(departmentRequest.Type));
            parameters.AppendRequestData("module", EnumUtility.ToApiString(departmentRequest.Module));
            parameters.AppendRequestData("displayorder", departmentRequest.DisplayOrder);
            parameters.AppendRequestData("uservisibilitycustom", 0);

            _kayakoApiRequest.Setup(x => x.ExecutePost<DepartmentCollection>(ApiBaseMethods.Departments, parameters.ToString())).Returns(departments);

            var departmentCreated = _departmentController.CreateDepartment(departmentRequest);

            _kayakoApiRequest.Verify(x => x.ExecutePost<DepartmentCollection>(ApiBaseMethods.Departments, parameters.ToString()), Times.Once());
            Assert.That(departmentCreated, Is.EqualTo(departments.First()));
        }
 public static Department ToResponseData(DepartmentRequest requestData)
 {
     return(DepartmentRequest.ToResponseType <DepartmentRequest, Department>(requestData));
 }
 public static DepartmentRequest FromResponseData(Department responseData)
 {
     return(DepartmentRequest.FromResponseType <Department, DepartmentRequest>(responseData));
 }
 public static Department ToResponseData(DepartmentRequest requestData) => ToResponseType <DepartmentRequest, Department>(requestData);
		public static Department ToResponseData(DepartmentRequest requestData)
		{
			return DepartmentRequest.ToResponseType<DepartmentRequest, Department>(requestData);
		}
        /// <summary>
        /// Creates a new department
        /// </summary>
        /// <param name="dept">Department data to create a new department with. Department Title, Module and Type must be supplied</param>
        /// <returns>Department data representing the department created</returns>
		public Department CreateDepartment(DepartmentRequest dept)
		{
            RequestBodyBuilder parameters = PopulateRequestParameters(dept, RequestTypes.Create);

			DepartmentCollection depts = Connector.ExecutePost<DepartmentCollection>(ApiBaseMethods.Departments, parameters.ToString());

			if (depts != null && depts.Count > 0)
			{
				return depts[0];
			}

			return null;
		}
		private static RequestBodyBuilder PopulateRequestParameters(DepartmentRequest dept, RequestTypes requestType)
        {
			dept.EnsureValidData(requestType);

            RequestBodyBuilder parameters = new RequestBodyBuilder();

            if (!String.IsNullOrEmpty(dept.Title))
            {
                parameters.AppendRequestData("title", dept.Title);
            }

			parameters.AppendRequestData("type", EnumUtility.ToApiString(dept.Type));

			if (requestType == RequestTypes.Create)
			{
				parameters.AppendRequestData("module", EnumUtility.ToApiString(dept.Module));
			}

            if (dept.DisplayOrder > 0)
            {
                parameters.AppendRequestData("displayorder", dept.DisplayOrder);
            }

            if (dept.ParentDepartmentId > 0)
            {
                parameters.AppendRequestData("parentdepartmentid", dept.ParentDepartmentId);
            }

            if(dept.UserVisibilityCustom)
            {
                parameters.AppendRequestData("uservisibilitycustom", 1);
            }
            else
            {
                parameters.AppendRequestData("uservisibilitycustom", 0);
            }

            if (dept.UserGroups != null && dept.UserGroups.Count > 0)
            {
                parameters.AppendRequestDataArray<int>("usergroupid[]", dept.UserGroups);
            }

            return parameters;
        }