Add audit to replace, update set
This commit is contained in:
23
replace.go
23
replace.go
@@ -2,14 +2,19 @@ package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
|
||||
"git.gsuntres.com/general/commons"
|
||||
)
|
||||
|
||||
func (c *MongoClient) Replace(ctx context.Context, database, name string, id string, data bson.M) (bson.M, error) {
|
||||
func (c *MongoClient) Replace(ctx context.Context, database, name string, id string, original bson.M) (bson.M, error) {
|
||||
collection := c.GetCollection(database, name)
|
||||
|
||||
data := commons.BsonClone(original)
|
||||
|
||||
filter := map[string]any { "_id": id }
|
||||
|
||||
var found bson.M
|
||||
@@ -31,6 +36,22 @@ func (c *MongoClient) Replace(ctx context.Context, database, name string, id str
|
||||
|
||||
PostReplace(updateResult, data, id)
|
||||
|
||||
ignoreAudit := slices.Contains(c.IgnoreAudit, name)
|
||||
|
||||
if c.WithAudit && !ignoreAudit {
|
||||
contx := commons.ContextSerialize(ctx, c.ContextFields)
|
||||
audit := &AuditResult {
|
||||
Entity: name,
|
||||
Op: OpUpdate,
|
||||
Data: original,
|
||||
Before: found,
|
||||
After: data,
|
||||
Context: contx,
|
||||
}
|
||||
|
||||
(*c.OnAudit)(audit)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user