/// <summary> /// Updates an employee using the API /// </summary> /// <param name="employeeId">Id token of the employee to update</param> /// <param name="employee">The employee metadata object</param> /// <param name="token">The current active auth token</param> /// <returns>Void</returns> public async Task <bool> UpdateEmployeeAsync(EntityId employeeId, MetaObject employee, string token) { Logger.Debug("Updating Employee [{0}]", employeeId); var createEmployeeUri = $"/API/2.0/Data/EmployeeManagement/Employee/Default/{employeeId.Id}?token={token}"; // ensure required fields are present if (employee.ContainsKey("FirstName") && string.IsNullOrEmpty(employee["FirstName"] as string)) { employee["FirstName"] = "-"; } if (employee.ContainsKey("LastName") && string.IsNullOrEmpty(employee["LastName"] as string)) { employee["LastName"] = "-"; } if (employee.ContainsKey("EmployeeNum") && string.IsNullOrEmpty(employee["EmployeeNum"] as string)) { var empData = JsonConvert.SerializeObject(employee); throw new DataException($"EmployeeNum is missing and is required:\n{empData}"); } var employeeData = JsonConvert.SerializeObject(employee); var createEmployeeContent = new StringContent(employeeData, Encoding.UTF8, "application/json"); var resp = await Http.PostAsync(createEmployeeUri, createEmployeeContent); if (!resp.IsSuccessStatusCode) { throw new DataException($"Error updating employee using API: ({resp.StatusCode}): {resp.ReasonPhrase}\n{createEmployeeUri}\n{employeeData}"); } var content = await resp.Content.ReadAsStringAsync(); return(true); }
/// <summary> /// Creates a new employee based on the supplied employee object /// </summary> /// <param name="employee">Objective describing the employee to create.</param> /// <param name="token">The active AuthToken.</param> /// <returns>String employee id</returns> public async Task <string> CreateEmployeeAsync(MetaObject employee, string token) { Logger.Debug("Creating employee"); var createEmployeeUri = $"/API/2.0/Data/EmployeeManagement/Employee/Default?token={token}"; // ensure required fields are present if (!employee.ContainsKey("FirstName") || string.IsNullOrEmpty(employee["FirstName"] as string)) { employee["FirstName"] = "-"; } if (!employee.ContainsKey("LastName") || string.IsNullOrEmpty(employee["LastName"] as string)) { employee["LastName"] = "-"; } if (!employee.ContainsKey("EmployeeNum") || string.IsNullOrEmpty(employee["EmployeeNum"] as string)) { var empData = JsonConvert.SerializeObject(employee); throw new DataException($"EmployeeNum is missing and is required:\n{empData}"); } var employeeData = JsonConvert.SerializeObject(employee); var createEmployeeContent = new StringContent(employeeData, Encoding.UTF8, "application/json"); var resp = await Http.PostAsync(createEmployeeUri, createEmployeeContent); if (!resp.IsSuccessStatusCode) { throw new DataException($"Error creating employee using API: ({resp.StatusCode}): {resp.ReasonPhrase}\n{employeeData}"); } var content = await resp.Content.ReadAsStringAsync(); var createEmployeeResult = JObject.Parse(content); return(createEmployeeResult["id"].Value <string>()); }