feat: complete SOP execution workflow
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/auth"
|
||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/model"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -14,5 +15,21 @@ func Record(db *gorm.DB, principal auth.Principal, action, resource string, reso
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Create(&model.AuditLog{TenantID: principal.TenantID, UserID: principal.UserID, Action: action, Resource: resource, ResourceID: resourceID, Payload: datatypes.JSON(data)}).Error
|
||||
fields := []zap.Field{
|
||||
zap.Uint64("tenant_id", principal.TenantID),
|
||||
zap.Uint64("user_id", principal.UserID),
|
||||
zap.String("action", action),
|
||||
zap.String("resource", resource),
|
||||
zap.Uint64("resource_id", resourceID),
|
||||
}
|
||||
if err := db.Create(&model.AuditLog{TenantID: principal.TenantID, UserID: principal.UserID, Action: action, Resource: resource, ResourceID: resourceID, Payload: datatypes.JSON(data)}).Error; err != nil {
|
||||
zap.L().Error("persist business event", append(fields, zap.Error(err))...)
|
||||
return err
|
||||
}
|
||||
logBusinessEvent(fields)
|
||||
return nil
|
||||
}
|
||||
|
||||
func logBusinessEvent(fields []zap.Field) {
|
||||
zap.L().Info("business event", fields...)
|
||||
}
|
||||
|
||||
34
internal/audit/audit_test.go
Normal file
34
internal/audit/audit_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"go.uber.org/zap/zaptest/observer"
|
||||
)
|
||||
|
||||
func TestLogBusinessEventIncludesRequiredFields(t *testing.T) {
|
||||
core, logs := observer.New(zapcore.InfoLevel)
|
||||
undo := zap.ReplaceGlobals(zap.New(core))
|
||||
defer undo()
|
||||
|
||||
logBusinessEvent([]zap.Field{
|
||||
zap.Uint64("tenant_id", 1),
|
||||
zap.Uint64("user_id", 2),
|
||||
zap.String("action", "publish"),
|
||||
zap.String("resource", "sop"),
|
||||
zap.Uint64("resource_id", 3),
|
||||
})
|
||||
|
||||
entries := logs.All()
|
||||
if len(entries) != 1 || entries[0].Message != "business event" {
|
||||
t.Fatalf("entries = %+v, want one business event", entries)
|
||||
}
|
||||
fields := entries[0].ContextMap()
|
||||
for _, key := range []string{"tenant_id", "user_id", "action", "resource", "resource_id"} {
|
||||
if _, ok := fields[key]; !ok {
|
||||
t.Fatalf("required field %q is missing from business event", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user