98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
package mappers
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/audit"
|
|
"RenewCMS/internal/infrastructure/persistence/models"
|
|
"encoding/json"
|
|
)
|
|
|
|
const (
|
|
emptyParams = "{}"
|
|
emptyChanges = "[]"
|
|
)
|
|
|
|
func AuditEventToModel(e audit.Event) (models.AuditEvent, error) {
|
|
params, err := encodeJSON(e.Params, emptyParams)
|
|
if err != nil {
|
|
return models.AuditEvent{}, err
|
|
}
|
|
changes, err := encodeJSON(e.Changes, emptyChanges)
|
|
if err != nil {
|
|
return models.AuditEvent{}, err
|
|
}
|
|
return models.AuditEvent{
|
|
ID: e.ID,
|
|
CreatedAt: e.CreatedAt.UTC(),
|
|
Action: e.Action,
|
|
Category: string(e.Category),
|
|
ActorType: string(e.Actor.Type),
|
|
ActorID: e.Actor.ID,
|
|
ActorName: e.Actor.Name,
|
|
TargetType: e.Target.Type,
|
|
TargetID: e.Target.ID,
|
|
TargetName: e.Target.Name,
|
|
Outcome: string(e.Outcome),
|
|
FailureReason: e.FailureReason,
|
|
Security: e.Security,
|
|
Source: string(e.Source),
|
|
IP: e.IP,
|
|
UserAgent: e.UserAgent,
|
|
Params: params,
|
|
Changes: changes,
|
|
}, nil
|
|
}
|
|
|
|
func AuditEventToDomain(m models.AuditEvent) (audit.Event, error) {
|
|
params := map[string]any{}
|
|
if err := json.Unmarshal([]byte(m.Params), ¶ms); err != nil {
|
|
return audit.Event{}, err
|
|
}
|
|
changes := []audit.Change{}
|
|
if err := json.Unmarshal([]byte(m.Changes), &changes); err != nil {
|
|
return audit.Event{}, err
|
|
}
|
|
return audit.Event{
|
|
ID: m.ID,
|
|
CreatedAt: m.CreatedAt.UTC(),
|
|
Action: m.Action,
|
|
Category: audit.Category(m.Category),
|
|
Actor: audit.Actor{Type: audit.ActorType(m.ActorType), ID: m.ActorID, Name: m.ActorName},
|
|
Target: audit.Target{Type: m.TargetType, ID: m.TargetID, Name: m.TargetName},
|
|
Outcome: audit.Outcome(m.Outcome),
|
|
FailureReason: m.FailureReason,
|
|
Security: m.Security,
|
|
Source: audit.Source(m.Source),
|
|
IP: m.IP,
|
|
UserAgent: m.UserAgent,
|
|
Params: params,
|
|
Changes: changes,
|
|
}, nil
|
|
}
|
|
|
|
func encodeJSON(value any, empty string) (string, error) {
|
|
encoded, err := json.Marshal(value)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if string(encoded) == "null" {
|
|
return empty, nil
|
|
}
|
|
return string(encoded), nil
|
|
}
|
|
|
|
func AuditSettingsToDomain(m models.AuditSettings) audit.Settings {
|
|
return audit.Settings{
|
|
RetentionDays: m.RetentionDays,
|
|
LastCleanupAt: m.LastCleanupAt,
|
|
LastCleanupRemoved: m.LastCleanupRemoved,
|
|
}
|
|
}
|
|
|
|
func AuditSettingsToModel(s audit.Settings) models.AuditSettings {
|
|
return models.AuditSettings{
|
|
RetentionDays: s.RetentionDays,
|
|
LastCleanupAt: s.LastCleanupAt,
|
|
LastCleanupRemoved: s.LastCleanupRemoved,
|
|
}
|
|
}
|