Select Git revision
-
Christel Loftus authoredChristel Loftus authored
audit.go 12.51 KiB
package audit
import (
"encoding/json"
"fmt"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/number_utils"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/r3labs/diff/v2"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/reflection"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils"
)
type FieldChange struct {
From interface{} `json:"change_from"`
To interface{} `json:"change_to"`
}
func VerifyAuditEvents(original interface{}, new interface{}) error {
if original != nil {
structValue := reflect.ValueOf(original)
if structValue.Kind() != reflect.Struct {
return errors.Error("original object is not of type struct")
}
}
if new != nil {
structValue := reflect.ValueOf(new)
if structValue.Kind() != reflect.Struct {
return errors.Error("new object is not of type struct")
}
}
return nil
}
func GetChanges(original interface{}, new interface{}) (map[string]interface{}, error) {
// Clean audit events
original = cleanStruct(original)
new = cleanStruct(new)
changes := map[string]interface{}{}
changelog, err := diff.Diff(original, new)
if err != nil {
return changes, err
}
for _, change := range changelog {
if len(change.Path) == 1 {
// Root object change
field := ToSnakeCase(change.Path[0])
changes[field] = FieldChange{
From: change.From,
To: change.To,
}
} else if len(change.Path) == 2 {
// Child object changed
// ["Account", "ID"]
// 0 = Object
// 1 = field
ChildObjectChanges(changes, change.Path[0], change.Path[1], change.From, change.To)
} else if len(change.Path) >= 3 {