Add remove_user endpoint

This commit is contained in:
sas.fajri
2026-05-08 10:58:09 +07:00
parent 1957038c10
commit 98ad7346fa

View File

@@ -176,6 +176,103 @@ class User extends MY_Controller
));
}
public function remove_user()
{
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
return;
}
$username = $this->sanitize_username($this->get_input('username'));
if ($username === '') {
$this->sys_error("username wajib diisi");
return;
}
$userQuery = $this->db_dashboard->query(
"SELECT User_ID
FROM cpone_dashboard.dashboard_user
WHERE User_Username = ?
LIMIT 1",
array($username)
);
if (!$userQuery || $userQuery->num_rows() === 0) {
$this->log_activity("REMOVE_USER", $username, $this->sys_input, "N", "user tidak ditemukan");
$this->sys_error("user tidak ditemukan");
return;
}
$userID = intval($userQuery->row()->User_ID);
$projectRows = $this->db_dashboard->query(
"SELECT UserProj_McuID
FROM cpone_dashboard.dashboard_user_project
WHERE UserProj_UserID = ?
AND UserProj_IsActive = 'Y'",
array($userID)
);
if (!$projectRows) {
$this->log_activity("REMOVE_USER", $username, $this->sys_input, "N", "gagal ambil project user");
$this->sys_error_db("gagal ambil project user", $this->db_dashboard);
return;
}
$mcuIDs = array();
foreach ($projectRows->result_array() as $row) {
$mcuID = isset($row['UserProj_McuID']) ? intval($row['UserProj_McuID']) : 0;
if ($mcuID > 0) {
$mcuIDs[] = $mcuID;
}
}
$this->db_dashboard->trans_begin();
foreach ($mcuIDs as $mcuID) {
$removeProjectQuery = $this->db_dashboard->query(
"CALL sp_remove_user_project(?, ?)",
array($username, $mcuID)
);
$this->clean_mysqli_connection($this->db_dashboard->conn_id);
if (!$removeProjectQuery) {
$error = $this->db_dashboard->error();
$this->db_dashboard->trans_rollback();
$this->log_activity("REMOVE_USER", $username, $this->sys_input, "N", $error['message']);
$this->sys_error_db("gagal remove project user", $this->db_dashboard);
return;
}
}
$updateUserQuery = $this->db_dashboard->query(
"UPDATE cpone_dashboard.dashboard_user
SET User_IsActive = 'N',
User_UpdatedAt = NOW()
WHERE User_ID = ?",
array($userID)
);
if (!$updateUserQuery) {
$this->db_dashboard->trans_rollback();
$this->log_activity("REMOVE_USER", $username, $this->sys_input, "N", "gagal nonaktifkan user");
$this->sys_error_db("gagal nonaktifkan user", $this->db_dashboard);
return;
}
if ($this->db_dashboard->trans_status() === FALSE) {
$this->db_dashboard->trans_rollback();
$this->log_activity("REMOVE_USER", $username, $this->sys_input, "N", "transaction failed");
$this->sys_error("gagal remove user");
return;
}
$this->db_dashboard->trans_commit();
$this->log_activity("REMOVE_USER", $username, $this->sys_input, "Y", "success");
$this->sys_ok(array(
"message" => "user berhasil dinonaktifkan",
"username" => $username,
"removed_project_count" => count($mcuIDs)
));
}
public function search_project()
{
if (!$this->isLogin) {