feat: add logging and audit trail to vendor service operations
This commit is contained in:
parent
b2b1067e00
commit
6296fc623c
|
|
@ -3,6 +3,7 @@ package dto
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -52,3 +53,18 @@ type VendorResponse struct {
|
||||||
IsActive bool `json:"is_active"`
|
IsActive bool `json:"is_active"`
|
||||||
Client pkgdto.IdNameResponse `json:"client"`
|
Client pkgdto.IdNameResponse `json:"client"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EntityToVendorResponse(e entities.MVendorEntity) VendorResponse {
|
||||||
|
return VendorResponse{
|
||||||
|
ID: e.ID.String(),
|
||||||
|
SearchKey: e.SearchKey,
|
||||||
|
Name: e.Name,
|
||||||
|
Address: e.Address,
|
||||||
|
ContactPerson: e.ContactPerson,
|
||||||
|
IsActive: e.IsActive,
|
||||||
|
Client: pkgdto.IdNameResponse{
|
||||||
|
ID: e.ClientID.String(),
|
||||||
|
Name: e.Client.Name,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,11 @@ import (
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/mvendor/query"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/mvendor/query"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/mvendor/repository"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/mvendor/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/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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -26,13 +28,15 @@ type vendorService struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
vendorRepo repository.VendorRepository
|
vendorRepo repository.VendorRepository
|
||||||
sequenceService sequenceservice.SequenceService
|
sequenceService sequenceservice.SequenceService
|
||||||
|
log *logrus.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVendorService(vendorRepo repository.VendorRepository, sequenceService sequenceservice.SequenceService, db *gorm.DB) VendorService {
|
func NewVendorService(vendorRepo repository.VendorRepository, sequenceService sequenceservice.SequenceService, db *gorm.DB, log *logrus.Logger) VendorService {
|
||||||
return &vendorService{
|
return &vendorService{
|
||||||
vendorRepo: vendorRepo,
|
vendorRepo: vendorRepo,
|
||||||
sequenceService: sequenceService,
|
sequenceService: sequenceService,
|
||||||
db: db,
|
db: db,
|
||||||
|
log: log,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,6 +48,8 @@ func (s *vendorService) Create(ctx context.Context, req dto.VendorCreateRequest)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
userID := utils.GetUserID(ctx)
|
||||||
|
|
||||||
suffix := utils.GetInitials(req.Name)
|
suffix := utils.GetInitials(req.Name)
|
||||||
|
|
||||||
seqConfig := pkgdto.SequenceConfig{
|
seqConfig := pkgdto.SequenceConfig{
|
||||||
|
|
@ -64,6 +70,7 @@ func (s *vendorService) Create(ctx context.Context, req dto.VendorCreateRequest)
|
||||||
Address: req.Address,
|
Address: req.Address,
|
||||||
ContactPerson: req.ContactPerson,
|
ContactPerson: req.ContactPerson,
|
||||||
IsActive: req.IsActive,
|
IsActive: req.IsActive,
|
||||||
|
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||||
}
|
}
|
||||||
clientUUID, err := uuid.Parse(req.ClientID)
|
clientUUID, err := uuid.Parse(req.ClientID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -76,8 +83,16 @@ func (s *vendorService) Create(ctx context.Context, req dto.VendorCreateRequest)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return dto.VendorResponse{}, err
|
return dto.VendorResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": userID,
|
||||||
|
"action": "create",
|
||||||
|
"entity": "vendor",
|
||||||
|
"entity_id": created.ID.String(),
|
||||||
|
}).Info("Vendor created")
|
||||||
|
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
return entityToVendorResponse(created), nil
|
return dto.EntityToVendorResponse(created), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *vendorService) GetById(ctx context.Context, vendorId string) (dto.VendorResponse, error) {
|
func (s *vendorService) GetById(ctx context.Context, vendorId string) (dto.VendorResponse, error) {
|
||||||
|
|
@ -85,7 +100,7 @@ func (s *vendorService) GetById(ctx context.Context, vendorId string) (dto.Vendo
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.VendorResponse{}, err
|
return dto.VendorResponse{}, err
|
||||||
}
|
}
|
||||||
return entityToVendorResponse(vendor), nil
|
return dto.EntityToVendorResponse(vendor), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *vendorService) GetAll(ctx context.Context, filter query.VendorFilter) ([]dto.VendorResponse, int64, error) {
|
func (s *vendorService) GetAll(ctx context.Context, filter query.VendorFilter) ([]dto.VendorResponse, int64, error) {
|
||||||
|
|
@ -95,7 +110,7 @@ func (s *vendorService) GetAll(ctx context.Context, filter query.VendorFilter) (
|
||||||
}
|
}
|
||||||
responses := make([]dto.VendorResponse, len(vendors))
|
responses := make([]dto.VendorResponse, len(vendors))
|
||||||
for i, v := range vendors {
|
for i, v := range vendors {
|
||||||
responses[i] = entityToVendorResponse(v)
|
responses[i] = dto.EntityToVendorResponse(v)
|
||||||
}
|
}
|
||||||
return responses, total, nil
|
return responses, total, nil
|
||||||
}
|
}
|
||||||
|
|
@ -107,6 +122,9 @@ func (s *vendorService) Update(ctx context.Context, req dto.VendorUpdateRequest,
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
userID := utils.GetUserID(ctx)
|
||||||
|
|
||||||
vendor, err := s.vendorRepo.GetById(ctx, tx, vendorId)
|
vendor, err := s.vendorRepo.GetById(ctx, tx, vendorId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|
@ -130,13 +148,23 @@ func (s *vendorService) Update(ctx context.Context, req dto.VendorUpdateRequest,
|
||||||
}
|
}
|
||||||
vendor.ClientID = clientUUID
|
vendor.ClientID = clientUUID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vendor.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||||
updated, err := s.vendorRepo.Update(ctx, tx, vendor)
|
updated, err := s.vendorRepo.Update(ctx, tx, vendor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return dto.VendorResponse{}, err
|
return dto.VendorResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": userID,
|
||||||
|
"action": "update",
|
||||||
|
"entity": "vendor",
|
||||||
|
"entity_id": vendorId,
|
||||||
|
}).Info("Vendor updated")
|
||||||
|
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
return entityToVendorResponse(updated), nil
|
return dto.EntityToVendorResponse(updated), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *vendorService) Delete(ctx context.Context, vendorId string) error {
|
func (s *vendorService) Delete(ctx context.Context, vendorId string) error {
|
||||||
|
|
@ -146,26 +174,35 @@ func (s *vendorService) Delete(ctx context.Context, vendorId string) error {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
userID := utils.GetUserID(ctx)
|
||||||
|
|
||||||
|
vendor, err := s.vendorRepo.GetById(ctx, tx, vendorId)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
vendor.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||||
|
_, err = s.vendorRepo.Update(ctx, tx, vendor)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if err := s.vendorRepo.Delete(ctx, tx, vendorId); err != nil {
|
if err := s.vendorRepo.Delete(ctx, tx, vendorId); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": userID,
|
||||||
|
"action": "delete",
|
||||||
|
"entity": "vendor",
|
||||||
|
"entity_id": vendorId,
|
||||||
|
}).Info("Vendor deleted")
|
||||||
|
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func entityToVendorResponse(e entities.MVendorEntity) dto.VendorResponse {
|
|
||||||
|
|
||||||
return dto.VendorResponse{
|
|
||||||
ID: e.ID.String(),
|
|
||||||
SearchKey: e.SearchKey,
|
|
||||||
Name: e.Name,
|
|
||||||
Address: e.Address,
|
|
||||||
ContactPerson: e.ContactPerson,
|
|
||||||
IsActive: e.IsActive,
|
|
||||||
Client: pkgdto.IdNameResponse{
|
|
||||||
ID: e.ClientID.String(),
|
|
||||||
Name: e.Client.Name,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -183,7 +183,7 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
permissionsServ := permissionsService.NewPermissionsService(permissionsRepository, db)
|
permissionsServ := permissionsService.NewPermissionsService(permissionsRepository, db)
|
||||||
categoryServ := categoryService.NewCategoryService(categoryRepository, db)
|
categoryServ := categoryService.NewCategoryService(categoryRepository, db)
|
||||||
uomServ := uomService.NewUomService(uomRepository, sequenceServ, db, log)
|
uomServ := uomService.NewUomService(uomRepository, sequenceServ, db, log)
|
||||||
mvendorServ := mvendorService.NewVendorService(mvendorRepository, sequenceServ, db)
|
mvendorServ := mvendorService.NewVendorService(mvendorRepository, sequenceServ, db, log)
|
||||||
warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db, log)
|
warehouseServ := warehouseService.NewWarehouseService(warehouseRepository, db, log)
|
||||||
zonaServ := zonaService.NewZonaService(zonaRepository, db, log)
|
zonaServ := zonaService.NewZonaService(zonaRepository, db, log)
|
||||||
aisleServ := aisleService.NewAisleService(aisleRepository, db)
|
aisleServ := aisleService.NewAisleService(aisleRepository, db)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue