Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
api endpoint available
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDunko committed Oct 20, 2019
1 parent d57b7d2 commit 9b7d8a8
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 6 deletions.
23 changes: 23 additions & 0 deletions private/functions_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ function find_requests_by_status() {
return $result;
}

function find_requests_by_availability_and_user($userkp) {
global $db;

// done in different function
// $sql = "SELECT kp_user FROM users ";
// $sql .= "WHERE name_user = '" . $username . "'";
// $result = mysqli_query($db, $sql);
// $userArray = mysqli_fetch_assoc($result);
// $userKp = $userArray['kp_user'];

$sql = "SELECT * FROM requests ";
$sql .= "WHERE responsible = " . $userkp;
$sql .= " AND ( status = 2";
$sql .= " OR status = 3 )";
$result = mysqli_query($db, $sql);
// $requestArray = mysqli_fetch_assoc($result);
if($result->num_rows == 0) {
return false;
} else {
return $result;
}
}

function find_request_by_kp($key) {
global $db;
$sql = "SELECT * FROM requests ";
Expand Down
22 changes: 20 additions & 2 deletions private/functions_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,26 @@ function find_user_by_nameuser($name_user) {
$sql .= "WHERE name_user = '" . $name_user . "' ";
// $sql .= "AND flg_active = 1";
$result = mysqli_query($db, $sql);
$request = mysqli_fetch_assoc($result);
return $request;
if($result->num_rows == 0) {
return false;
} else {
$request = mysqli_fetch_assoc($result);
return $request;
}
}

function find_userkp_by_nameuser($name_user) {
global $db;
$sql = "SELECT kp_user FROM users ";
$sql .= "WHERE name_user = '" . $name_user . "' ";
// $sql .= "AND flg_active = 1";
$result = mysqli_query($db, $sql);
if($result->num_rows == 0) {
return false;
} else {
$request = mysqli_fetch_assoc($result);
return $request;
}
}

function find_user_by_apikey($apikey) {
Expand Down
6 changes: 3 additions & 3 deletions private/meta.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

define("REQX_VERSION", '1.6.0');
define("REQX_RELEASENO", '101');
define("REQX_RELEASEDATE", '2019-10-19');
define("REQX_VERSION", '1.6.1');
define("REQX_RELEASENO", '102');
define("REQX_RELEASEDATE", '2019-10-20');
73 changes: 73 additions & 0 deletions public/api/available.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php require_once('../../private/initialize.php');

header("Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
// header("WWW-Authenticate: Basic realm=\"My Realm\"");

$token = substr($_SERVER['REMOTE_USER'], 7);

$user = find_user_by_apikey($token);

// TODO: sanitize $token
if(!$user) {
http_response_code(401);
echo json_encode(array(
"success" => false,
"message" => "Valid access token required to process the request"
));
exit();
}

if(is_blank($_GET['responsible'])) {
http_response_code(400);
echo json_encode(array(
"success" => false,
"message" => "Username required to process the request"
));
exit();
}

// get user kp
$user = find_userkp_by_nameuser($_GET['responsible']);
if (!$user) {
http_response_code(404);
echo json_encode(array(
"success" => false,
"message" => "Username not found"
));
exit();
}
$userKp = $user['kp_user'];

// return available tickets for a user
$request_set = find_requests_by_availability_and_user($userKp);
if(!$request_set) {
http_response_code(200);
echo json_encode(array(
"success" => true,
"tickets" => array()
));
exit();
}

while($request = mysqli_fetch_assoc($request_set)) {
$tickets[] = array(
"id" => $request['kp_request'],
"description" => $request['description'],
"category" => find_selectiontext_by_kp(h($request['category'])),
"priority" => find_selectiontext_by_kp(h($request['priority'])),
"source" => find_userabbr_by_kp(h($request['source'])),
"status" => find_selectiontext_by_kp(h($request['status'])),
"responsible" => find_userabbr_by_kp(h($request['responsible']))
);
}

http_response_code(200);
$response = array(
"success" => true,
"tickets" => $tickets
);

echo json_encode($response);
3 changes: 2 additions & 1 deletion public/api/tickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
if(!$user) {
http_response_code(401);
echo json_encode(array(
"message" => "Valid access token missing"
"success" => false,
"message" => "Valid access token required to process the request"
));
exit();
}
Expand Down

0 comments on commit 9b7d8a8

Please sign in to comment.