diff --git a/app/Config/Routes.php b/app/Config/Routes.php index da0b9c9..b09ef55 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -173,10 +173,15 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) { //report $routes->get('reports/policy-excel', 'PolicyReportController::downloadExcel'); + + //audit + $routes->get('audit/history', 'MasterController::getHistory'); }); + + $routes->get("api/policy/PolicyFilePath", "PolicyController::PolicyFilePath"); diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php index 7137b8a..028f7c1 100644 --- a/app/Controllers/MasterController.php +++ b/app/Controllers/MasterController.php @@ -370,6 +370,76 @@ class MasterController extends ResourceController + public function getHistory() + { + $db = \Config\Database::connect(); + + // ----------------------------- + // REQUEST VALIDATION + // ----------------------------- + $tableName = $this->request->getGet('table_name'); + $pk = $this->request->getGet('pk'); + + if (!$tableName || !$pk) { + return $this->response->setJSON([ + 'status' => 'failed', + 'message' => 'table_name and pk are required' + ]); + } + + // ----------------------------- + // QUERY + // ----------------------------- + $history = $db->table('partner_audit_history pah') + ->select(" + pah.column_name, + pah.old_val, + pah.new_val, + pah.changed_on, + ps.name AS changed_by + ") + ->join('partner_staff ps', 'ps.id = pah.user_id', 'left') + ->where('pah.table_name', $tableName) + ->where('pah.pk', $pk) + ->orderBy('pah.changed_on', 'DESC') + ->get() + ->getResultArray(); + + if (empty($history)) { + return $this->response->setJSON([ + 'status' => 'success', + 'data' => [], + 'message' => 'No audit history found' + ]); + } + + // ----------------------------- + // FORMAT RESPONSE (READABLE) + // ----------------------------- + $result = []; + + foreach ($history as $row) { + $result[] = [ + 'column_name' => $row['column_name'], + 'message' => "{$row['column_name']} data changed from " + . ($row['old_val'] ?? 'NULL') + . " to " + . ($row['new_val'] ?? 'NULL') + . " by " + . ($row['changed_by'] ?? 'System'), + 'changed_on' => $row['changed_on'], + 'changed_by' => $row['changed_by'] + ]; + } + + return $this->response->setJSON([ + 'status' => 'success', + 'data' => $result + ]); + } + + +