audit

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2025 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	ActionType   ActionType `json:"actionType,omitempty"`
	CreatedAt    *time.Time `json:"createdAt,omitempty"`
	UpdatedAt    *time.Time `json:"updatedAt,omitempty"`
	UpdatedBy    *string    `json:"updatedBy,omitempty"`
	UpdateReason *string    `json:"updateReason,omitempty"`
}

Action base fields used in actions

func ActionEntityToAction

func ActionEntityToAction(entity *ActionEntity, model *gorm.Model) (action Action)

ActionEntityToAction action entity to action When we create a model with audit action like

 type XXXAction struct {
	   ... // custom business fields
    audit.Action
}

And the corresponding DB XXXActionEntity

 type XXXActionEntity struct {
    gorm.Model
	   ... // custom business fields
    audit.ActionEntity
}

When we want to map from the XXXEntity to XXXModel just do as below

func XXXActionEntityToXXXAction(e *XXXActionEntity) * XXXAction {
   return &XXXAction{
       ... // mapping custom business fields
       ActionEntity: ActionEntityToAction(&e.Entity, &e.Model)
   }
}

func (Action) After

func (r Action) After(l Action) bool

After check one action is created after another

func (Action) Before

func (r Action) Before(l Action) bool

Before check one action is created before another

type ActionEntity

type ActionEntity struct {
	UpdatedBy    *string
	UpdateReason *string `gorm:"column:update_reason"`
	ActionType   ActionType
}

ActionEntity base fields used in actions tables

type ActionType

type ActionType string

ActionType action type

const (
	Create ActionType = "Create"
	Update ActionType = "Update"
	Delete ActionType = "Delete"
)

CRUD actions

func ActionCreate

func ActionCreate() *ActionType

ActionCreate ...

func ActionDelete

func ActionDelete() *ActionType

ActionDelete ...

func ActionUpdate

func ActionUpdate() *ActionType

ActionUpdate ...

func NewActionType

func NewActionType(v ActionType) *ActionType

NewActionType create a new ActionType ref

type ChangeRequest

type ChangeRequest struct {
	ID uint `json:"-"`
	Request
}

ChangeRequest request to change a model

func (*ChangeRequest) SetID

func (r *ChangeRequest) SetID(id uint)

SetID set the model ID

type DeleteRequest

type DeleteRequest struct {
	ChangeRequest
}

DeleteRequest request to delete a model

type Entity

type Entity struct {
	LastUpdatedBy *string     `gorm:"-"`
	UpdateReason  *string     `gorm:"-"`
	ActionType    *ActionType `gorm:"-"`
}

Entity base fields used in audit

func RequestToEntity

func RequestToEntity(req *Request) (entity Entity)

RequestToEntity request to entity

type IChangeRequest

type IChangeRequest interface {
	SetID(uint)
}

IChangeRequest general change request interface

type Model

type Model struct {
	CreatedAt    *time.Time `json:"createdAt,omitempty"`
	UpdatedAt    *time.Time `json:"updatedAt,omitempty"`
	UpdatedBy    *string    `json:"updatedBy,omitempty"`
	UpdateReason *string    `json:"updateReason,omitempty"`
}

Model basic response fields

func EntityToModel

func EntityToModel(entity *Entity, m *gorm.Model) (model Model)

EntityToModel base entity to base model When we create a model with audit like

 type XXXModel struct {
	   ... // custom business fields
	   Actions []*audit.Action
    audit.Model
}

And the corresponding DB XXXEntity

 type XXXModel struct {
    gorm.Model
	   ... // custom business fields
	   Actions []*audit.ActionEntity
    audit.Entity
}

When we want to map from the XXXEntity to XXXModel just do as below

func XXXEntityToXXXModel(e *XXXEntity) * XXXModel {
   return &XXXModel{
       ... // mapping custom business fields
       Model: EntityToModel(&e.Entity, &e.Model)
   }
}

type Request

type Request struct {
	// usually an email address, but can be something else
	// for email see the limit here https://stackoverflow.com/a/574698
	// for other like names or ids 254 is also enough
	RequestedBy *string `json:"requestedBy,omitempty"  binding:"required,printascii,max=254"`
	Reason      *string `json:"reason,omitempty"  binding:"required,printascii,max=512"`
}

Request base request

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL