mirror of
https://github.com/JGH0/Todo-App-Backend.git
synced 2026-06-03 13:28:47 +02:00
added loging
This commit is contained in:
@@ -21,7 +21,7 @@ class ActivityLogController extends BaseController
|
||||
public function index()
|
||||
{
|
||||
$userId = $this->getUserId();
|
||||
$limit = $this->request->getVar('limit') ?? 50;
|
||||
$limit = (int)($this->request->getVar('limit') ?? 50);
|
||||
$logs = $this->activityLogModel->getByUser($userId, $limit);
|
||||
|
||||
return $this->successResponse($logs, 'Activity logs retrieved successfully');
|
||||
|
||||
@@ -62,6 +62,23 @@ class TodoController extends BaseController
|
||||
];
|
||||
|
||||
$this->todoModel->insert($data);
|
||||
|
||||
// Manually log the activity
|
||||
try {
|
||||
$activityLogModel = new \App\Models\ActivityLogModel();
|
||||
$activityLogModel->logActivity([
|
||||
'user_id' => $userId,
|
||||
'action' => 'todo_created',
|
||||
'entity_type' => 'todo',
|
||||
'entity_id' => $data['id'],
|
||||
'details' => json_encode(['action' => 'created', 'title' => $data['title']]),
|
||||
'ip_address' => $this->request->getIPAddress(),
|
||||
'user_agent' => $this->request->getUserAgent()->getAgentString(),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
log_message('error', 'Failed to log activity: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$todo = $this->todoModel->getByUserWithCategories($userId, $data['id']);
|
||||
|
||||
return $this->successResponse($todo, 'Todo created successfully', 201);
|
||||
@@ -105,6 +122,23 @@ class TodoController extends BaseController
|
||||
}
|
||||
|
||||
$this->todoModel->update($id, $updateData);
|
||||
|
||||
// Manually log the activity
|
||||
try {
|
||||
$activityLogModel = new \App\Models\ActivityLogModel();
|
||||
$activityLogModel->logActivity([
|
||||
'user_id' => $userId,
|
||||
'action' => 'todo_updated',
|
||||
'entity_type' => 'todo',
|
||||
'entity_id' => $id,
|
||||
'details' => json_encode(['action' => 'updated', 'title' => $todo['title'] ?? 'Unknown']),
|
||||
'ip_address' => $this->request->getIPAddress(),
|
||||
'user_agent' => $this->request->getUserAgent()->getAgentString(),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
log_message('error', 'Failed to log activity: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$todo = $this->todoModel->getByUserWithCategories($userId, $id);
|
||||
|
||||
return $this->successResponse($todo, 'Todo updated successfully');
|
||||
@@ -125,6 +159,22 @@ class TodoController extends BaseController
|
||||
|
||||
$this->todoModel->delete($id);
|
||||
|
||||
// Manually log the activity
|
||||
try {
|
||||
$activityLogModel = new \App\Models\ActivityLogModel();
|
||||
$activityLogModel->logActivity([
|
||||
'user_id' => $userId,
|
||||
'action' => 'todo_deleted',
|
||||
'entity_type' => 'todo',
|
||||
'entity_id' => $id,
|
||||
'details' => json_encode(['action' => 'deleted', 'title' => $todo['title'] ?? 'Unknown']),
|
||||
'ip_address' => $this->request->getIPAddress(),
|
||||
'user_agent' => $this->request->getUserAgent()->getAgentString(),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
log_message('error', 'Failed to log activity: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return $this->successResponse(null, 'Todo deleted successfully');
|
||||
}
|
||||
|
||||
|
||||
@@ -34,9 +34,6 @@ class ActivityLogModel extends Model
|
||||
// Log an activity
|
||||
public function logActivity($data)
|
||||
{
|
||||
// Disable events to prevent any recursive logging
|
||||
$this->skipEvents();
|
||||
|
||||
if (!isset($data['id'])) {
|
||||
$data['id'] = $this->generateUuid();
|
||||
}
|
||||
@@ -44,12 +41,9 @@ class ActivityLogModel extends Model
|
||||
$data['created_at'] = date('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
$result = $this->insert($data);
|
||||
|
||||
// Re-enable events
|
||||
$this->skipEvents(false);
|
||||
|
||||
return $result;
|
||||
// Use builder directly to avoid triggering events
|
||||
$builder = $this->db->table($this->table);
|
||||
return $builder->insert($data);
|
||||
}
|
||||
|
||||
// Get logs by user
|
||||
|
||||
@@ -6,8 +6,6 @@ use CodeIgniter\Model;
|
||||
|
||||
class CategoryModel extends Model
|
||||
{
|
||||
use LoggableTrait;
|
||||
|
||||
protected $table = 'categories';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = false;
|
||||
@@ -30,9 +28,4 @@ class CategoryModel extends Model
|
||||
'user_id' => 'required',
|
||||
'name' => 'required|max_length[255]',
|
||||
];
|
||||
|
||||
protected function getEntityType(): string
|
||||
{
|
||||
return 'category';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ trait LoggableTrait
|
||||
{
|
||||
try {
|
||||
$request = \Config\Services::request();
|
||||
return $request->getUserAgent()->toString();
|
||||
return $request->getUserAgent()->getAgentString();
|
||||
} catch (\Exception $e) {
|
||||
return 'CLI/Script';
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ use CodeIgniter\Model;
|
||||
|
||||
class ProjectModel extends Model
|
||||
{
|
||||
use LoggableTrait;
|
||||
|
||||
protected $table = 'projects';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = false;
|
||||
@@ -30,9 +28,4 @@ class ProjectModel extends Model
|
||||
'user_id' => 'required',
|
||||
'name' => 'required|max_length[255]',
|
||||
];
|
||||
|
||||
protected function getEntityType(): string
|
||||
{
|
||||
return 'project';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ use CodeIgniter\Model;
|
||||
|
||||
class RecurringTaskModel extends Model
|
||||
{
|
||||
use LoggableTrait;
|
||||
|
||||
protected $table = 'recurring_tasks';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = false;
|
||||
@@ -35,11 +33,6 @@ class RecurringTaskModel extends Model
|
||||
'schedule' => 'required|in_list[daily,weekly,monthly,custom]',
|
||||
];
|
||||
|
||||
protected function getEntityType(): string
|
||||
{
|
||||
return 'recurring_task';
|
||||
}
|
||||
|
||||
// Get recurring tasks with categories
|
||||
public function getWithCategories($taskId = null)
|
||||
{
|
||||
|
||||
@@ -6,8 +6,6 @@ use CodeIgniter\Model;
|
||||
|
||||
class TodoModel extends Model
|
||||
{
|
||||
use LoggableTrait;
|
||||
|
||||
protected $table = 'todos';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = false;
|
||||
@@ -39,11 +37,6 @@ class TodoModel extends Model
|
||||
'status' => 'permit_empty|in_list[open,in_progress,completed,archived]',
|
||||
];
|
||||
|
||||
protected function getEntityType(): string
|
||||
{
|
||||
return 'todo';
|
||||
}
|
||||
|
||||
// Get todos with categories
|
||||
public function getWithCategories($todoId = null)
|
||||
{
|
||||
|
||||
@@ -6,8 +6,6 @@ use CodeIgniter\Model;
|
||||
|
||||
class UserModel extends Model
|
||||
{
|
||||
use LoggableTrait;
|
||||
|
||||
protected $table = 'users';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = false;
|
||||
@@ -40,9 +38,4 @@ class UserModel extends Model
|
||||
'is_unique' => 'This email is already registered',
|
||||
],
|
||||
];
|
||||
|
||||
protected function getEntityType(): string
|
||||
{
|
||||
return 'user';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user