big ole refactor
This commit is contained in:
@@ -2,19 +2,18 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type updater[T any] struct {
|
||||
tx bun.Tx
|
||||
q *bun.UpdateQuery
|
||||
model *T
|
||||
columns []string
|
||||
auditCallback AuditCallback
|
||||
auditRequest *http.Request
|
||||
tx bun.Tx
|
||||
q *bun.UpdateQuery
|
||||
model *T
|
||||
columns []string
|
||||
audit *AuditMeta
|
||||
auditInfo *AuditInfo
|
||||
}
|
||||
|
||||
// Update creates an updater for a model
|
||||
@@ -69,11 +68,10 @@ func (u *updater[T]) Set(query string, args ...any) *updater[T] {
|
||||
}
|
||||
|
||||
// WithAudit enables audit logging for this update operation
|
||||
// The callback will be invoked after successful update with auto-generated audit info
|
||||
// If the callback returns an error, the transaction will be rolled back
|
||||
func (u *updater[T]) WithAudit(r *http.Request, callback AuditCallback) *updater[T] {
|
||||
u.auditRequest = r
|
||||
u.auditCallback = callback
|
||||
// If the provided *AuditInfo is nil, will use reflection to automatically work out the details
|
||||
func (u *updater[T]) WithAudit(meta *AuditMeta, info *AuditInfo) *updater[T] {
|
||||
u.audit = meta
|
||||
u.auditInfo = info
|
||||
return u
|
||||
}
|
||||
|
||||
@@ -82,7 +80,7 @@ func (u *updater[T]) WithAudit(r *http.Request, callback AuditCallback) *updater
|
||||
func (u *updater[T]) Exec(ctx context.Context) error {
|
||||
// Build audit details BEFORE update (captures changed fields)
|
||||
var details map[string]any
|
||||
if u.auditCallback != nil && len(u.columns) > 0 {
|
||||
if u.audit != nil && len(u.columns) > 0 {
|
||||
details = extractChangedFields(u.model, u.columns)
|
||||
}
|
||||
|
||||
@@ -93,21 +91,22 @@ func (u *updater[T]) Exec(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// Handle audit logging if enabled
|
||||
if u.auditCallback != nil && u.auditRequest != nil {
|
||||
tableName := extractTableName[T]()
|
||||
resourceType := extractResourceType(tableName)
|
||||
action := buildAction(resourceType, "update")
|
||||
if u.audit != nil {
|
||||
if u.auditInfo == nil {
|
||||
tableName := extractTableName[T]()
|
||||
resourceType := extractResourceType(tableName)
|
||||
action := buildAction(resourceType, "update")
|
||||
|
||||
info := &AuditInfo{
|
||||
Action: action,
|
||||
ResourceType: resourceType,
|
||||
ResourceID: extractPrimaryKey(u.model),
|
||||
Details: details, // Changed fields only
|
||||
u.auditInfo = &AuditInfo{
|
||||
Action: action,
|
||||
ResourceType: resourceType,
|
||||
ResourceID: extractPrimaryKey(u.model),
|
||||
Details: details, // Changed fields only
|
||||
}
|
||||
}
|
||||
|
||||
// Call audit callback - if it fails, return error to trigger rollback
|
||||
if err := u.auditCallback(ctx, u.tx, info, u.auditRequest); err != nil {
|
||||
return errors.Wrap(err, "audit.callback")
|
||||
err = LogSuccess(ctx, u.tx, u.audit, u.auditInfo)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "LogSuccess")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user