diff --git a/modules/mvendor/dto/vendor_dto.go b/modules/mvendor/dto/vendor_dto.go index d417ca4..16f8b6f 100644 --- a/modules/mvendor/dto/vendor_dto.go +++ b/modules/mvendor/dto/vendor_dto.go @@ -3,6 +3,7 @@ package dto import ( "errors" + "github.com/Caknoooo/go-gin-clean-starter/database/entities" pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" ) @@ -52,3 +53,18 @@ type VendorResponse struct { IsActive bool `json:"is_active"` 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, + }, + } +} diff --git a/modules/mvendor/service/vendor_service.go b/modules/mvendor/service/vendor_service.go index 93bb536..8c29829 100644 --- a/modules/mvendor/service/vendor_service.go +++ b/modules/mvendor/service/vendor_service.go @@ -8,9 +8,11 @@ import ( "github.com/Caknoooo/go-gin-clean-starter/modules/mvendor/query" "github.com/Caknoooo/go-gin-clean-starter/modules/mvendor/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" ) @@ -26,13 +28,15 @@ type vendorService struct { db *gorm.DB vendorRepo repository.VendorRepository 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{ vendorRepo: vendorRepo, sequenceService: sequenceService, 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) seqConfig := pkgdto.SequenceConfig{ @@ -59,11 +65,12 @@ func (s *vendorService) Create(ctx context.Context, req dto.VendorCreateRequest) } vendor := entities.MVendorEntity{ - SearchKey: searchKey, - Name: req.Name, - Address: req.Address, - ContactPerson: req.ContactPerson, - IsActive: req.IsActive, + SearchKey: searchKey, + Name: req.Name, + Address: req.Address, + ContactPerson: req.ContactPerson, + IsActive: req.IsActive, + FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE), } clientUUID, err := uuid.Parse(req.ClientID) if err != nil { @@ -76,8 +83,16 @@ func (s *vendorService) Create(ctx context.Context, req dto.VendorCreateRequest) tx.Rollback() 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() - return entityToVendorResponse(created), nil + return dto.EntityToVendorResponse(created), nil } 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 { 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) { @@ -95,7 +110,7 @@ func (s *vendorService) GetAll(ctx context.Context, filter query.VendorFilter) ( } responses := make([]dto.VendorResponse, len(vendors)) for i, v := range vendors { - responses[i] = entityToVendorResponse(v) + responses[i] = dto.EntityToVendorResponse(v) } return responses, total, nil } @@ -107,6 +122,9 @@ func (s *vendorService) Update(ctx context.Context, req dto.VendorUpdateRequest, tx.Rollback() } }() + + userID := utils.GetUserID(ctx) + vendor, err := s.vendorRepo.GetById(ctx, tx, vendorId) if err != nil { tx.Rollback() @@ -130,13 +148,23 @@ func (s *vendorService) Update(ctx context.Context, req dto.VendorUpdateRequest, } vendor.ClientID = clientUUID } + + vendor.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE) updated, err := s.vendorRepo.Update(ctx, tx, vendor) if err != nil { tx.Rollback() return dto.VendorResponse{}, err } + + s.log.WithFields(logrus.Fields{ + "user_id": userID, + "action": "update", + "entity": "vendor", + "entity_id": vendorId, + }).Info("Vendor updated") + tx.Commit() - return entityToVendorResponse(updated), nil + return dto.EntityToVendorResponse(updated), nil } 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() } }() + + 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 { tx.Rollback() return err } + + s.log.WithFields(logrus.Fields{ + "user_id": userID, + "action": "delete", + "entity": "vendor", + "entity_id": vendorId, + }).Info("Vendor deleted") + tx.Commit() 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, - }, - } -} diff --git a/providers/core.go b/providers/core.go index 3cf812d..b2bdb7b 100644 --- a/providers/core.go +++ b/providers/core.go @@ -183,7 +183,7 @@ func RegisterDependencies(injector *do.Injector) { permissionsServ := permissionsService.NewPermissionsService(permissionsRepository, db) categoryServ := categoryService.NewCategoryService(categoryRepository, db) 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) zonaServ := zonaService.NewZonaService(zonaRepository, db, log) aisleServ := aisleService.NewAisleService(aisleRepository, db)