GWM : audit api
This commit is contained in:
parent
78359fc9b0
commit
8bcac5d697
@ -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");
|
||||
|
||||
|
||||
@ -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
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user