diff --git a/callbacks.go b/callbacks.go index 791298f..3eff39a 100644 --- a/callbacks.go +++ b/callbacks.go @@ -11,6 +11,7 @@ import ( type LoggablePlugin interface { SetUser(user string) *gorm.DB + SetFrom(from string) *gorm.DB GetRecords(objectId string) ([]*ChangeLog, error) } @@ -48,6 +49,13 @@ func (r *loggablePlugin) SetUser(user string) *gorm.DB { return db } +func (r *loggablePlugin) SetFrom(from string) *gorm.DB { + r.mu.Lock() + db := r.db.Set("loggable:from", from) + r.mu.Unlock() + return db +} + func (r *loggablePlugin) addRecord(scope *gorm.Scope, action string) error { var jsonObject JSONB j, err := json.Marshal(scope.Value) @@ -62,14 +70,19 @@ func (r *loggablePlugin) addRecord(scope *gorm.Scope, action string) error { if !ok { user = "" } + from, ok := scope.DB().Get("loggable:from") + if !ok { + from = "" + } cl := ChangeLog{ - ID: uuid.NewV4().String(), - ChangedBy: user.(string), - Action: action, - ObjectID: scope.PrimaryKeyValue().(string), - ObjectType: scope.GetModelStruct().ModelType.Name(), - Object: jsonObject, + ID: uuid.NewV4().String(), + ChangedBy: user.(string), + ChangedFrom: from.(string), + Action: action, + ObjectID: scope.PrimaryKeyValue().(string), + ObjectType: scope.GetModelStruct().ModelType.Name(), + Object: jsonObject, } err = scope.DB().Create(&cl).Error if err != nil { @@ -91,11 +104,13 @@ func (r *loggablePlugin) addCreated(scope *gorm.Scope) { r.addRecord(scope, "create") } } + func (r *loggablePlugin) addUpdated(scope *gorm.Scope) { if isLoggable(scope) { r.addRecord(scope, "update") } } + func (r *loggablePlugin) addDeleted(scope *gorm.Scope) { if isLoggable(scope) { r.addRecord(scope, "delete") diff --git a/loggable.go b/loggable.go index 74ff57f..93acb19 100644 --- a/loggable.go +++ b/loggable.go @@ -8,18 +8,20 @@ import ( ) type ChangeLog struct { - ID string `gorm:"type:uuid;primary_key;"` - CreatedAt time.Time `sql:"DEFAULT:current_timestamp"` - ChangedBy string `gorm:"index"` - Action string - ObjectID string `gorm:"index"` - ObjectType string `gorm:"index"` - Object JSONB `sql:"type:JSONB"` + ID string `gorm:"type:uuid;primary_key;"` + CreatedAt time.Time `sql:"DEFAULT:current_timestamp"` + ChangedBy string `gorm:"index"` + ChangedFrom string `gorm:"index"` + Action string + ObjectID string `gorm:"index"` + ObjectType string `gorm:"index"` + Object JSONB `sql:"type:JSONB"` } type loggableInterface interface { stubMethod() error } + type LoggableModel struct { }