RezinaLMS-BackEnd/internal/web_server/handlers/activity_log_helper.go
Yared Yemane c742cb01c3 feat: comprehensive activity logging and fix question type definition persistence
Add activity log coverage across mutating APIs with shared recordActivity helper and expanded action/resource constants. Fix question type definition logs to use correct QUESTION_TYPE_DEFINITION actions. Persist question_type_definition_id inside CreateQuestion transaction.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-20 02:50:35 -07:00

35 lines
1.2 KiB
Go

package handlers
import (
"context"
"encoding/json"
"Yimaru-Backend/internal/domain"
"github.com/gofiber/fiber/v2"
)
func activityActorFromCtx(c *fiber.Ctx) (actorID *int64, actorRole *string, ip *string, ua *string) {
ipStr := c.IP()
uaStr := c.Get("User-Agent")
ip = &ipStr
ua = &uaStr
if uid, ok := c.Locals("user_id").(int64); ok && uid > 0 {
actorID = &uid
}
if role, ok := c.Locals("role").(domain.Role); ok {
roleStr := string(role)
actorRole = &roleStr
}
return actorID, actorRole, ip, ua
}
func (h *Handler) recordActivity(c *fiber.Ctx, action domain.ActivityAction, resourceType domain.ResourceType, resourceID *int64, message string, metadata json.RawMessage) {
actorID, actorRole, ip, ua := activityActorFromCtx(c)
go h.activityLogSvc.RecordAction(context.Background(), actorID, actorRole, action, resourceType, resourceID, message, metadata, ip, ua)
}
func (h *Handler) recordActivityWithActor(actorID *int64, actorRole *string, ip, ua *string, action domain.ActivityAction, resourceType domain.ResourceType, resourceID *int64, message string, metadata json.RawMessage) {
go h.activityLogSvc.RecordAction(context.Background(), actorID, actorRole, action, resourceType, resourceID, message, metadata, ip, ua)
}