From 9ff322a6a16bb72cfce658e97850d037fde9c4d1 Mon Sep 17 00:00:00 2001 From: Hanan Askarim Date: Thu, 18 Jun 2026 13:58:13 +0700 Subject: [PATCH] add setting token --- application/controllers/tools/Wynacom.http | 17 +- application/controllers/tools/Wynacom.php | 150 +++++++++++++++++- .../2026-06-18_create_api_wynacom_setting.sql | 37 +++++ 3 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 scripts/sql/2026-06-18_create_api_wynacom_setting.sql diff --git a/application/controllers/tools/Wynacom.http b/application/controllers/tools/Wynacom.http index 8ad8c7b..72e0bfd 100644 --- a/application/controllers/tools/Wynacom.http +++ b/application/controllers/tools/Wynacom.http @@ -10,11 +10,25 @@ Content-Type: application/json ### Wynacom - check token POST https://{{host}}/one-api/tools/wynacom/check_token -Content-Type: application/jsons +Content-Type: application/json Authorization: Bearer {{accessToken}} {} +### Wynacom - get token setting +POST https://{{host}}/one-api/tools/wynacom/token_setting +Content-Type: application/json + +{} + +### Wynacom - save token setting exp 10 hari +POST https://{{host}}/one-api/tools/wynacom/save_token_setting +Content-Type: application/json + +{ + "token_expired_days": 3 +} + ### Wynacom - cek timestamp gabungan nonlab dan lab POST https://{{host}}/one-api/tools/wynacom/sampling Content-Type: application/json @@ -26,6 +40,7 @@ Content-Type: application/json ### Wynacom - cek union records nonlab dan lab POST https://{{host}}/one-api/tools/wynacom/sampling_timestamp Content-Type: application/json +Authorization: Bearer {{accessToken}} { "T_OrderHeaderLabNumber": "{{labNumber}}" diff --git a/application/controllers/tools/Wynacom.php b/application/controllers/tools/Wynacom.php index 87f57d1..91e2645 100644 --- a/application/controllers/tools/Wynacom.php +++ b/application/controllers/tools/Wynacom.php @@ -20,7 +20,9 @@ class Wynacom extends MY_Controller "POST /tools/wynacom/lab", "POST /tools/wynacom/sampling", "POST /tools/wynacom/generate_token", - "POST /tools/wynacom/check_token" + "POST /tools/wynacom/check_token", + "POST /tools/wynacom/token_setting", + "POST /tools/wynacom/save_token_setting" ) )); exit; @@ -84,17 +86,30 @@ class Wynacom extends MY_Controller public function generate_token() { try { + $active_token = $this->get_active_wynacom_token(); + if ($active_token) { + $this->sys_ok(array( + "access_token" => $active_token["Api_WynacomTokenAccessToken"], + "token_type" => "Bearer", + "expired_at" => $active_token["Api_WynacomTokenExpiredAt"], + "expires_in" => (int) $active_token["expires_in"] + )); + exit; + } + + $expired_days = $this->get_wynacom_token_expired_days(); $access_token = $this->create_access_token(); - $expired_at = date("Y-m-d H:i:s", strtotime("+3 days")); + $expired_at = date("Y-m-d H:i:s", strtotime("+{$expired_days} days")); $sql = "INSERT INTO api_wynacom_token ( Api_WynacomTokenAccessToken, Api_WynacomTokenExpiredAt, + Api_WynacomTokenIsActive, Api_WynacomTokenCreated, Api_WynacomTokenCreatedUserID, Api_WynacomTokenLastUpdated, Api_WynacomTokenLastUpdatedUserID - ) VALUES (?, ?, NOW(), 0, NOW(), 0)"; + ) VALUES (?, ?, 'Y', NOW(), 0, NOW(), 0)"; $qry = $this->db->query($sql, array( $access_token, $expired_at @@ -109,7 +124,8 @@ class Wynacom extends MY_Controller "access_token" => $access_token, "token_type" => "Bearer", "expired_at" => $expired_at, - "expires_in" => 259200 + "expired_days" => $expired_days, + "expires_in" => $expired_days * 86400 )); exit; } catch (Exception $exc) { @@ -135,9 +151,72 @@ class Wynacom extends MY_Controller } } + public function token_setting() + { + try { + $this->sys_ok($this->get_wynacom_token_setting()); + exit; + } catch (Exception $exc) { + $this->sys_error($exc->getMessage()); + exit; + } + } + + public function save_token_setting() + { + try { + $prm = is_array($this->sys_input) ? $this->sys_input : array(); + $expired_days = isset($prm["token_expired_days"]) ? (int) $prm["token_expired_days"] : 0; + + if ($expired_days < 1) { + throw new Exception("Parameter token_expired_days harus angka lebih besar dari 0"); + } + + if ($expired_days > 365) { + throw new Exception("Parameter token_expired_days maksimal 365 hari"); + } + + $sql = "INSERT INTO api_wynacom_setting ( + Api_WynacomSettingKey, + Api_WynacomSettingValue, + Api_WynacomSettingDescription, + Api_WynacomSettingIsActive, + Api_WynacomSettingCreatedUserID, + Api_WynacomSettingLastUpdatedUserID + ) VALUES ( + 'token_expired_days', + ?, + 'Masa berlaku token Wynacom dalam hari', + 'Y', + 0, + 0 + ) + ON DUPLICATE KEY UPDATE + Api_WynacomSettingValue = VALUES(Api_WynacomSettingValue), + Api_WynacomSettingDescription = VALUES(Api_WynacomSettingDescription), + Api_WynacomSettingIsActive = 'Y', + Api_WynacomSettingLastUpdated = NOW(), + Api_WynacomSettingLastUpdatedUserID = 0"; + $qry = $this->db->query($sql, array((string) $expired_days)); + + if (!$qry) { + $this->sys_error_db("Gagal menyimpan setting token Wynacom", $this->db); + exit; + } + + $this->sys_ok($this->get_wynacom_token_setting()); + exit; + } catch (Exception $exc) { + $this->sys_error($exc->getMessage()); + exit; + } + } + public function sampling_timestamp() { try { + $this->validate_wynacom_token($this->get_access_token_param()); + $order = $this->require_order_header(); $lab_number = $order["T_OrderHeaderLabNumber"]; @@ -217,6 +296,7 @@ class Wynacom extends MY_Controller $sql = "SELECT * FROM api_wynacom_token WHERE Api_WynacomTokenAccessToken = ? + AND Api_WynacomTokenIsActive = 'Y' AND Api_WynacomTokenExpiredAt >= NOW() AND ( Api_WynacomTokenDeleted IS NULL @@ -239,6 +319,68 @@ class Wynacom extends MY_Controller return $credential; } + private function get_active_wynacom_token() + { + $sql = "SELECT *, + TIMESTAMPDIFF(SECOND, NOW(), Api_WynacomTokenExpiredAt) AS expires_in + FROM api_wynacom_token + WHERE Api_WynacomTokenExpiredAt >= NOW() + AND Api_WynacomTokenIsActive = 'Y' + AND ( + Api_WynacomTokenDeleted IS NULL + OR Api_WynacomTokenDeleted = '0000-00-00 00:00:00' + ) + ORDER BY Api_WynacomTokenID DESC + LIMIT 1"; + $qry = $this->db->query($sql); + + if (!$qry) { + $this->sys_error_db("Gagal mengambil token aktif Wynacom", $this->db); + exit; + } + + $rows = $qry->result_array(); + return count($rows) > 0 ? $rows[0] : false; + } + + private function get_wynacom_token_expired_days() + { + $default_days = 3; + $sql = "SELECT Api_WynacomSettingValue + FROM api_wynacom_setting + WHERE Api_WynacomSettingKey = 'token_expired_days' + AND Api_WynacomSettingIsActive = 'Y' + ORDER BY Api_WynacomSettingID DESC + LIMIT 1"; + $qry = $this->db->query($sql); + + if (!$qry) { + return $default_days; + } + + $rows = $qry->result_array(); + if (count($rows) === 0) { + return $default_days; + } + + $expired_days = (int) $rows[0]["Api_WynacomSettingValue"]; + if ($expired_days < 1) { + return $default_days; + } + + return $expired_days; + } + + private function get_wynacom_token_setting() + { + $expired_days = $this->get_wynacom_token_expired_days(); + + return array( + "token_expired_days" => $expired_days, + "expires_in" => $expired_days * 86400 + ); + } + private function create_access_token() { if (function_exists("random_bytes")) { diff --git a/scripts/sql/2026-06-18_create_api_wynacom_setting.sql b/scripts/sql/2026-06-18_create_api_wynacom_setting.sql new file mode 100644 index 0000000..8355d59 --- /dev/null +++ b/scripts/sql/2026-06-18_create_api_wynacom_setting.sql @@ -0,0 +1,37 @@ +CREATE TABLE IF NOT EXISTS api_wynacom_setting ( + Api_WynacomSettingID INT NOT NULL AUTO_INCREMENT, + Api_WynacomSettingKey VARCHAR(100) NOT NULL, + Api_WynacomSettingValue VARCHAR(255) NOT NULL, + Api_WynacomSettingDescription VARCHAR(255) NULL, + Api_WynacomSettingIsActive CHAR(1) NOT NULL DEFAULT 'Y', + Api_WynacomSettingCreated DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + Api_WynacomSettingCreatedUserID INT NOT NULL DEFAULT 0, + Api_WynacomSettingLastUpdated DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + Api_WynacomSettingLastUpdatedUserID INT NOT NULL DEFAULT 0, + Api_WynacomSettingDeleted DATETIME NULL, + Api_WynacomSettingDeletedUserID INT NOT NULL DEFAULT 0, + PRIMARY KEY (Api_WynacomSettingID), + UNIQUE KEY uq_api_wynacom_setting_key (Api_WynacomSettingKey), + KEY idx_api_wynacom_setting_active (Api_WynacomSettingIsActive) +); + +INSERT INTO api_wynacom_setting ( + Api_WynacomSettingKey, + Api_WynacomSettingValue, + Api_WynacomSettingDescription, + Api_WynacomSettingIsActive, + Api_WynacomSettingCreatedUserID, + Api_WynacomSettingLastUpdatedUserID +) VALUES ( + 'token_expired_days', + '3', + 'Masa berlaku token Wynacom dalam hari', + 'Y', + 0, + 0 +) +ON DUPLICATE KEY UPDATE + Api_WynacomSettingValue = Api_WynacomSettingValue, + Api_WynacomSettingDescription = VALUES(Api_WynacomSettingDescription), + Api_WynacomSettingIsActive = 'Y', + Api_WynacomSettingLastUpdated = NOW();