Add soft deletes via status column

- Add status ENUM('active','disabled','deleted') to 8 tables
- Change all physical DELETE to UPDATE status='deleted'
- All SELECT queries filter by status='active'
- toggleActive now alternates between active/disabled status
- Exceptions: monitoring_history, rate_limits, sessions keep physical purge
- audit_logs remains immutable
This commit is contained in:
2026-06-07 17:26:29 -04:00
parent 921d98bf60
commit 1741545318
6 changed files with 138 additions and 60 deletions

View File

@@ -18,13 +18,18 @@ class Team
public function findAll(?int $userId = null): array
{
$where = $userId ? ' WHERE t.created_by = ?' : '';
$params = $userId ? [$userId] : [];
$where = ' WHERE t.status = \'active\'';
$params = [];
if ($userId) {
$where .= ' AND t.created_by = ?';
$params[] = $userId;
}
return $this->db->fetchAll(
"SELECT t.*,
(SELECT COUNT(*) FROM team_user WHERE team_id = t.id) AS member_count,
(SELECT COUNT(*) FROM team_server WHERE team_id = t.id) AS server_count
(SELECT COUNT(*) FROM team_user WHERE team_id = t.id AND status = 'active') AS member_count,
(SELECT COUNT(*) FROM team_server WHERE team_id = t.id AND status = 'active') AS server_count
FROM teams t{$where}
ORDER BY t.name ASC",
$params
@@ -40,9 +45,9 @@ class Team
{
$team = $this->db->fetch(
'SELECT t.*,
(SELECT COUNT(*) FROM team_user WHERE team_id = t.id) AS member_count,
(SELECT COUNT(*) FROM team_server WHERE team_id = t.id) AS server_count
FROM teams t WHERE t.id = ?',
(SELECT COUNT(*) FROM team_user WHERE team_id = t.id AND status = \'active\') AS member_count,
(SELECT COUNT(*) FROM team_server WHERE team_id = t.id AND status = \'active\') AS server_count
FROM teams t WHERE t.id = ? AND t.status = \'active\'',
[$id]
);
return $team ?: null;
@@ -64,7 +69,10 @@ class Team
public function delete(int $id): bool
{
return $this->db->delete('teams', 'id = ?', [$id]) > 0;
return $this->db->update('teams', [
'status' => 'deleted',
'updated_at' => date('Y-m-d H:i:s'),
], 'id = ?', [$id]) > 0;
}
public function getMembers(int $teamId): array
@@ -73,7 +81,7 @@ class Team
'SELECT tu.*, u.username, u.email
FROM team_user tu
JOIN users u ON tu.user_id = u.id
WHERE tu.team_id = ?
WHERE tu.team_id = ? AND tu.status = \'active\' AND u.status = \'active\'
ORDER BY u.username ASC',
[$teamId]
);
@@ -82,7 +90,7 @@ class Team
public function addMember(int $teamId, int $userId): bool
{
$existing = $this->db->fetch(
'SELECT id FROM team_user WHERE team_id = ? AND user_id = ?',
'SELECT id FROM team_user WHERE team_id = ? AND user_id = ? AND status = \'active\'',
[$teamId, $userId]
);
if ($existing) {
@@ -98,8 +106,9 @@ class Team
public function removeMember(int $teamId, int $userId): bool
{
return $this->db->delete(
return $this->db->update(
'team_user',
['status' => 'deleted'],
'team_id = ? AND user_id = ?',
[$teamId, $userId]
) > 0;
@@ -111,7 +120,7 @@ class Team
'SELECT ts.*, s.name AS server_name, s.ip_address
FROM team_server ts
JOIN servers s ON ts.server_id = s.id
WHERE ts.team_id = ?
WHERE ts.team_id = ? AND ts.status = \'active\' AND s.status = \'active\'
ORDER BY s.name ASC',
[$teamId]
);
@@ -124,7 +133,7 @@ class Team
}
$existing = $this->db->fetch(
'SELECT id FROM team_server WHERE team_id = ? AND server_id = ?',
'SELECT id FROM team_server WHERE team_id = ? AND server_id = ? AND status = \'active\'',
[$teamId, $serverId]
);
if ($existing) {
@@ -141,8 +150,9 @@ class Team
public function removeServer(int $teamId, int $serverId): bool
{
return $this->db->delete(
return $this->db->update(
'team_server',
['status' => 'deleted'],
'team_id = ? AND server_id = ?',
[$teamId, $serverId]
) > 0;
@@ -157,7 +167,7 @@ class Team
return $this->db->update(
'team_server',
['role' => $role],
'team_id = ? AND server_id = ?',
'team_id = ? AND server_id = ? AND status = \'active\'',
[$teamId, $serverId]
) > 0;
}