Add audit to replace, update set
This commit is contained in:
@@ -2,32 +2,66 @@ package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"git.gsuntres.com/general/commons"
|
||||
)
|
||||
|
||||
// UpdateSet search documents using filter and updates the first it finds using the $set operator.
|
||||
func (c *MongoClient) UpdateSet(ctx context.Context, database, name string, filter, data bson.M) (bool, error) {
|
||||
func (c *MongoClient) UpdateSet(ctx context.Context, database, name string, filter, original bson.M) (bool, error) {
|
||||
collection := c.GetCollection(database, name)
|
||||
|
||||
|
||||
data := commons.BsonClone(original)
|
||||
|
||||
prepareForUpdateSet(data)
|
||||
|
||||
if err := c.DiscriminatorCheckAndApplyToFilter(ctx, name, filter); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
f := Mongofy(&Query{ Filter: filter })
|
||||
|
||||
if err := c.DiscriminatorOmitInData(name, data); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
var found bson.M
|
||||
if c.WithAudit {
|
||||
if err := collection.FindOne(ctx, f).Decode(&found); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
update := bson.M{ "$set": data }
|
||||
updateResult, err := collection.UpdateOne(ctx, filter, update)
|
||||
updateResult, err := collection.UpdateOne(ctx, f, update)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
changed := updateResult.ModifiedCount != 0
|
||||
|
||||
ignoreAudit := slices.Contains(c.IgnoreAudit, name)
|
||||
|
||||
if changed && c.WithAudit && !ignoreAudit {
|
||||
var after bson.M
|
||||
if err := collection.FindOne(ctx, f).Decode(&after); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
contx := commons.ContextSerialize(ctx, c.ContextFields)
|
||||
audit := &AuditResult {
|
||||
Entity: name,
|
||||
Op: OpUpdate,
|
||||
Data: original,
|
||||
Before: found,
|
||||
After: after,
|
||||
Context: contx,
|
||||
}
|
||||
|
||||
(*c.OnAudit)(audit)
|
||||
}
|
||||
|
||||
return changed, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user