feat: enhance product service with logging and audit trail functionality
This commit is contained in:
parent
8d852521c4
commit
b2b1067e00
|
|
@ -11,8 +11,11 @@ import (
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/product/query"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/product/query"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/product/repository"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/product/repository"
|
||||||
sequenceservice "github.com/Caknoooo/go-gin-clean-starter/modules/sequence/service"
|
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"
|
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/google/uuid"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -35,8 +38,8 @@ type productService struct {
|
||||||
inventoryTransactionRepo invtransactionrepo.InventoryTransactionRepository
|
inventoryTransactionRepo invtransactionrepo.InventoryTransactionRepository
|
||||||
inventoryStorageRepo invstoragerepo.InventoryStorageRepository
|
inventoryStorageRepo invstoragerepo.InventoryStorageRepository
|
||||||
categoryRepo categoryrepo.CategoryRepository
|
categoryRepo categoryrepo.CategoryRepository
|
||||||
sequenceService sequenceservice.SequenceService // tambahkan ini
|
sequenceService sequenceservice.SequenceService
|
||||||
|
log *logrus.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCrossReferencesByProductAndClient implements ProductService.
|
// 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,
|
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{
|
return &productService{
|
||||||
productRepo: productRepo,
|
productRepo: productRepo,
|
||||||
db: db,
|
db: db,
|
||||||
|
|
@ -127,6 +130,7 @@ func NewProductService(productRepo repository.ProductRepository, db *gorm.DB, in
|
||||||
inventoryStorageRepo: inventoryStorageRepo,
|
inventoryStorageRepo: inventoryStorageRepo,
|
||||||
categoryRepo: categoryRepo,
|
categoryRepo: categoryRepo,
|
||||||
sequenceService: sequenceService,
|
sequenceService: sequenceService,
|
||||||
|
log: log,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -176,15 +180,17 @@ func (s *productService) Create(ctx context.Context, req dto.ProductCreateReques
|
||||||
return dto.ProductResponse{}, err
|
return dto.ProductResponse{}, err
|
||||||
}
|
}
|
||||||
product := entities.MProductEntity{
|
product := entities.MProductEntity{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
RefNumber: refNumber,
|
RefNumber: refNumber,
|
||||||
SKU: req.SKU,
|
SKU: req.SKU,
|
||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
Status: req.Status,
|
Status: req.Status,
|
||||||
IsReturnable: req.IsReturnable,
|
IsReturnable: req.IsReturnable,
|
||||||
CategoryID: &categoryUUID,
|
CategoryID: &categoryUUID,
|
||||||
UomID: &uomUUID,
|
UomID: &uomUUID,
|
||||||
ClientID: clientUUID,
|
ClientID: clientUUID,
|
||||||
|
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||||
|
|
||||||
// DimLength: req.DimLength,
|
// DimLength: req.DimLength,
|
||||||
// DimWidth: req.DimWidth,
|
// DimWidth: req.DimWidth,
|
||||||
// DimHeight: req.DimHeight,
|
// DimHeight: req.DimHeight,
|
||||||
|
|
@ -221,6 +227,12 @@ func (s *productService) Create(ctx context.Context, req dto.ProductCreateReques
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return dto.ProductResponse{}, err
|
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())
|
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)
|
id := parseUUID(*req.UomToUomID)
|
||||||
product.UomToUomID = &id
|
product.UomToUomID = &id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
product.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||||
updated, err := s.productRepo.Update(ctx, tx, product)
|
updated, err := s.productRepo.Update(ctx, tx, product)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|
@ -372,6 +386,12 @@ func (s *productService) Update(ctx context.Context, req dto.ProductUpdateReques
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return dto.ProductResponse{}, err
|
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())
|
return s.GetById(ctx, updated.ID.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -382,7 +402,18 @@ func (s *productService) Delete(ctx context.Context, productId string) error {
|
||||||
tx.Rollback()
|
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 {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
|
|
@ -392,6 +423,12 @@ func (s *productService) Delete(ctx context.Context, productId string) error {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
s.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": utils.GetUserID(ctx),
|
||||||
|
"action": "delete",
|
||||||
|
"entity": "product",
|
||||||
|
"entity_id": productId,
|
||||||
|
}).Info("Product Deleted")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -175,7 +175,7 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
// Service
|
// Service
|
||||||
sequenceServ := sequenceService.NewSequenceService(sequenceRepository, db)
|
sequenceServ := sequenceService.NewSequenceService(sequenceRepository, db)
|
||||||
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, clientRepository, refreshTokenRepository, jwtService, 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)
|
roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db, log)
|
||||||
menuSvc := menuService.NewMenuService(menuRepository, jwtService, db)
|
menuSvc := menuService.NewMenuService(menuRepository, jwtService, db)
|
||||||
maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db)
|
maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue