feat: integrate logging and audit trail in client service operations
This commit is contained in:
parent
cd60dc109f
commit
6c757e2065
|
|
@ -7,6 +7,9 @@ import (
|
|||
"github.com/Caknoooo/go-gin-clean-starter/modules/client/dto"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/client/query"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/client/repository"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
|
@ -24,6 +27,7 @@ type ClientService interface {
|
|||
type clientService struct {
|
||||
db *gorm.DB
|
||||
clientRepo repository.ClientRepository
|
||||
log *logrus.Logger
|
||||
}
|
||||
|
||||
func (s *clientService) GetAll(ctx context.Context, filter query.ClientFilter) ([]dto.ClientResponse, int64, error) {
|
||||
|
|
@ -63,13 +67,15 @@ func (s *clientService) Create(ctx context.Context, req dto.ClientCreateRequest)
|
|||
}
|
||||
}()
|
||||
|
||||
userID := utils.GetUserID(ctx)
|
||||
client := entities.M_Client{
|
||||
Name: req.Name,
|
||||
PIC: req.PIC,
|
||||
Phone: req.Phone,
|
||||
Email: req.Email,
|
||||
Address: req.Address,
|
||||
Logo: req.Logo, // []byte, sudah benar untuk simpan file
|
||||
Logo: req.Logo,
|
||||
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||
}
|
||||
|
||||
createdClient, err := s.clientRepo.Create(ctx, tx, client)
|
||||
|
|
@ -83,11 +89,12 @@ func (s *clientService) Create(ctx context.Context, req dto.ClientCreateRequest)
|
|||
return dto.ClientResponse{}, err
|
||||
}
|
||||
|
||||
// Mapping entity ke response (Logo bisa dikonversi ke base64 jika perlu)
|
||||
// logoBase64 := ""
|
||||
// if len(createdClient.Logo) > 0 {
|
||||
// logoBase64 = base64.StdEncoding.EncodeToString(createdClient.Logo)
|
||||
// }
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": userID,
|
||||
"action": "create",
|
||||
"entity": "client",
|
||||
"entity_id": createdClient.ID.String(),
|
||||
}).Info("Client created")
|
||||
|
||||
result, err := s.GetById(ctx, createdClient.ID.String())
|
||||
if err != nil {
|
||||
|
|
@ -177,6 +184,8 @@ func (s *clientService) Update(ctx context.Context, req dto.ClientUpdateRequest,
|
|||
if req.Logo != nil {
|
||||
client.Logo = *req.Logo
|
||||
}
|
||||
userID := utils.GetUserID(ctx)
|
||||
client.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||
updatedClient, err := s.clientRepo.Update(ctx, tx, client)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
|
@ -187,10 +196,13 @@ func (s *clientService) Update(ctx context.Context, req dto.ClientUpdateRequest,
|
|||
tx.Rollback()
|
||||
return dto.ClientResponse{}, err
|
||||
}
|
||||
// logoBase64 := ""
|
||||
// if len(updatedClient.Logo) > 0 {
|
||||
// logoBase64 = base64.StdEncoding.EncodeToString(updatedClient.Logo)
|
||||
// }
|
||||
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": userID,
|
||||
"action": "update",
|
||||
"entity": "client",
|
||||
"entity_id": clientId,
|
||||
}).Info("Client updated")
|
||||
|
||||
result, err := s.GetById(ctx, updatedClient.ID.String())
|
||||
if err != nil {
|
||||
|
|
@ -206,7 +218,18 @@ func (s *clientService) Delete(ctx context.Context, clientId string) error {
|
|||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
err := s.clientRepo.Delete(ctx, tx, clientId)
|
||||
client, err := s.clientRepo.GetById(ctx, tx, clientId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
userID := utils.GetUserID(ctx)
|
||||
client.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||
if _, err := s.clientRepo.Update(ctx, tx, client); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
err = s.clientRepo.Delete(ctx, tx, clientId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
|
|
@ -216,6 +239,13 @@ func (s *clientService) Delete(ctx context.Context, clientId string) error {
|
|||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
s.log.WithFields(logrus.Fields{
|
||||
"user_id": userID,
|
||||
"action": "delete",
|
||||
"entity": "client",
|
||||
"entity_id": clientId,
|
||||
}).Info("Client deleted")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -237,9 +267,10 @@ func (s *clientService) RemoveMenusFromClient(ctx context.Context, clientId stri
|
|||
return s.clientRepo.RemoveMenusFromClient(ctx, s.db, clientId, menuIds)
|
||||
}
|
||||
|
||||
func NewClientService(clientRepo repository.ClientRepository, db *gorm.DB) ClientService {
|
||||
func NewClientService(clientRepo repository.ClientRepository, db *gorm.DB, log *logrus.Logger) ClientService {
|
||||
return &clientService{
|
||||
clientRepo: clientRepo,
|
||||
db: db,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ func RegisterDependencies(injector *do.Injector) {
|
|||
roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db, log)
|
||||
menuSvc := menuService.NewMenuService(menuRepository, jwtService, db, log)
|
||||
maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db)
|
||||
clientServ := clientService.NewClientService(clientRepository, db)
|
||||
clientServ := clientService.NewClientService(clientRepository, db, log)
|
||||
permissionsServ := permissionsService.NewPermissionsService(permissionsRepository, db)
|
||||
categoryServ := categoryService.NewCategoryService(categoryRepository, db)
|
||||
uomServ := uomService.NewUomService(uomRepository, sequenceServ, db, log)
|
||||
|
|
|
|||
Loading…
Reference in New Issue