feat: enhance product service with logging and audit trail functionality

This commit is contained in:
Habib Fatkhul Rohman 2025-12-03 13:57:47 +07:00
parent 8d852521c4
commit b2b1067e00
2 changed files with 51 additions and 14 deletions

View File

@ -11,8 +11,11 @@ import (
"github.com/Caknoooo/go-gin-clean-starter/modules/product/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/product/repository"
sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service"
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
@ -35,8 +38,8 @@ type productService struct {
inventoryTransactionRepo invtransactionrepo.InventoryTransactionRepository
inventoryStorageRepo invstoragerepo.InventoryStorageRepository
categoryRepo categoryrepo.CategoryRepository
sequenceService sequenceservice.SequenceService // tambahkan ini
sequenceService sequenceservice.SequenceService
log *logrus.Logger
}
// GetCrossReferencesByProductAndClient implements ProductService.
@ -119,7 +122,7 @@ func (s *productService) RemoveCrossReference(ctx context.Context, productId str
}
func NewProductService(productRepo repository.ProductRepository, db *gorm.DB, inventoryTransactionRepo invtransactionrepo.InventoryTransactionRepository,
inventoryStorageRepo invstoragerepo.InventoryStorageRepository, categoryRepo categoryrepo.CategoryRepository, sequenceService sequenceservice.SequenceService) ProductService {
inventoryStorageRepo invstoragerepo.InventoryStorageRepository, categoryRepo categoryrepo.CategoryRepository, sequenceService sequenceservice.SequenceService, log *logrus.Logger) ProductService {
return &productService{
productRepo: productRepo,
db: db,
@ -127,6 +130,7 @@ func NewProductService(productRepo repository.ProductRepository, db *gorm.DB, in
inventoryStorageRepo: inventoryStorageRepo,
categoryRepo: categoryRepo,
sequenceService: sequenceService,
log: log,
}
}
@ -185,6 +189,8 @@ func (s *productService) Create(ctx context.Context, req dto.ProductCreateReques
CategoryID: &categoryUUID,
UomID: &uomUUID,
ClientID: clientUUID,
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
// DimLength: req.DimLength,
// DimWidth: req.DimWidth,
// DimHeight: req.DimHeight,
@ -221,6 +227,12 @@ func (s *productService) Create(ctx context.Context, req dto.ProductCreateReques
tx.Rollback()
return dto.ProductResponse{}, err
}
s.log.WithFields(logrus.Fields{
"user_id": utils.GetUserID(ctx),
"action": "create",
"entity": "product",
"entity_id": created.ID.String(),
}).Info("Product created")
return s.GetById(ctx, created.ID.String())
}
@ -362,6 +374,8 @@ func (s *productService) Update(ctx context.Context, req dto.ProductUpdateReques
id := parseUUID(*req.UomToUomID)
product.UomToUomID = &id
}
product.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
updated, err := s.productRepo.Update(ctx, tx, product)
if err != nil {
tx.Rollback()
@ -372,6 +386,12 @@ func (s *productService) Update(ctx context.Context, req dto.ProductUpdateReques
tx.Rollback()
return dto.ProductResponse{}, err
}
s.log.WithFields(logrus.Fields{
"user_id": utils.GetUserID(ctx),
"action": "delete",
"entity": "product",
"entity_id": productId,
}).Info("Product Updated")
return s.GetById(ctx, updated.ID.String())
}
@ -382,7 +402,18 @@ func (s *productService) Delete(ctx context.Context, productId string) error {
tx.Rollback()
}
}()
err := s.productRepo.Delete(ctx, tx, productId)
product, err := s.productRepo.GetById(ctx, tx, productId)
if err != nil {
tx.Rollback()
return err
}
product.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
if _, err := s.productRepo.Update(ctx, tx, product); err != nil {
tx.Rollback()
return err
}
err = s.productRepo.Delete(ctx, tx, productId)
if err != nil {
tx.Rollback()
return err
@ -392,6 +423,12 @@ func (s *productService) Delete(ctx context.Context, productId string) error {
tx.Rollback()
return err
}
s.log.WithFields(logrus.Fields{
"user_id": utils.GetUserID(ctx),
"action": "delete",
"entity": "product",
"entity_id": productId,
}).Info("Product Deleted")
return nil
}

View File

@ -175,7 +175,7 @@ func RegisterDependencies(injector *do.Injector) {
// Service
sequenceServ := sequenceService.NewSequenceService(sequenceRepository, db)
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, clientRepository, refreshTokenRepository, jwtService, db)
productService := productService.NewProductService(productRepository, db, inventoryTransactionRepository, inventoryStorageRepository, categoryRepository, sequenceServ)
productService := productService.NewProductService(productRepository, db, inventoryTransactionRepository, inventoryStorageRepository, categoryRepository, sequenceServ, log)
roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db, log)
menuSvc := menuService.NewMenuService(menuRepository, jwtService, db)
maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db)