From fe002c9602792ef43a8cc81dd65ad7fd1fa30d52 Mon Sep 17 00:00:00 2001 From: "Hafizh A. Y" Date: Thu, 15 Jan 2026 15:49:15 +0700 Subject: [PATCH] fix(BE): remove supplier price in master nonstock --- ...ove_price_from_nonstock_suppliers.down.sql | 3 ++ ...emove_price_from_nonstock_suppliers.up.sql | 3 ++ internal/entities/nonstock_supplier.go | 1 - .../master/nonstocks/dto/nonstock.dto.go | 2 - .../repositories/nonstock.repository.go | 16 ++----- .../nonstocks/services/nonstock.service.go | 44 +++++++++---------- .../validations/nonstock.validation.go | 21 ++++----- .../suppliers/dto/supplier_nonstock.dto.go | 2 - 8 files changed, 38 insertions(+), 54 deletions(-) create mode 100644 internal/database/migrations/20260115082849_remove_price_from_nonstock_suppliers.down.sql create mode 100644 internal/database/migrations/20260115082849_remove_price_from_nonstock_suppliers.up.sql diff --git a/internal/database/migrations/20260115082849_remove_price_from_nonstock_suppliers.down.sql b/internal/database/migrations/20260115082849_remove_price_from_nonstock_suppliers.down.sql new file mode 100644 index 00000000..503f592d --- /dev/null +++ b/internal/database/migrations/20260115082849_remove_price_from_nonstock_suppliers.down.sql @@ -0,0 +1,3 @@ +-- Rollback: add price back to nonstock_suppliers +ALTER TABLE nonstock_suppliers + ADD COLUMN IF NOT EXISTS price NUMERIC(15, 3) NOT NULL DEFAULT 0; diff --git a/internal/database/migrations/20260115082849_remove_price_from_nonstock_suppliers.up.sql b/internal/database/migrations/20260115082849_remove_price_from_nonstock_suppliers.up.sql new file mode 100644 index 00000000..07fdd009 --- /dev/null +++ b/internal/database/migrations/20260115082849_remove_price_from_nonstock_suppliers.up.sql @@ -0,0 +1,3 @@ +-- Migration: remove price from nonstock_suppliers +ALTER TABLE nonstock_suppliers + DROP COLUMN IF EXISTS price; diff --git a/internal/entities/nonstock_supplier.go b/internal/entities/nonstock_supplier.go index d666e3c8..2206390c 100644 --- a/internal/entities/nonstock_supplier.go +++ b/internal/entities/nonstock_supplier.go @@ -5,7 +5,6 @@ import "time" type NonstockSupplier struct { NonstockId uint `gorm:"not null"` SupplierId uint `gorm:"not null"` - Price float64 `gorm:"type:numeric(15,3);not null;default:0"` CreatedAt time.Time `gorm:"autoCreateTime"` Nonstock Nonstock `gorm:"foreignKey:NonstockId;references:Id"` diff --git a/internal/modules/master/nonstocks/dto/nonstock.dto.go b/internal/modules/master/nonstocks/dto/nonstock.dto.go index fa102b9c..8182da21 100644 --- a/internal/modules/master/nonstocks/dto/nonstock.dto.go +++ b/internal/modules/master/nonstocks/dto/nonstock.dto.go @@ -37,7 +37,6 @@ type NonstockSupplierDTO struct { Name string `json:"name"` Alias string `json:"alias"` Category string `json:"category"` - Price float64 `json:"price"` } // === Mapper Functions === @@ -121,7 +120,6 @@ func toNonstockSupplierDTOs(relations []entity.NonstockSupplier) []NonstockSuppl Name: relation.Supplier.Name, Alias: relation.Supplier.Alias, Category: relation.Supplier.Category, - Price: relation.Price, }) } diff --git a/internal/modules/master/nonstocks/repositories/nonstock.repository.go b/internal/modules/master/nonstocks/repositories/nonstock.repository.go index 16260272..56ef39b8 100644 --- a/internal/modules/master/nonstocks/repositories/nonstock.repository.go +++ b/internal/modules/master/nonstocks/repositories/nonstock.repository.go @@ -61,30 +61,20 @@ func (r *NonstockRepositoryImpl) SyncSuppliersDiff(ctx context.Context, tx *gorm return err } - existingMap := make(map[uint]entity.NonstockSupplier, len(existing)) + existingMap := make(map[uint]struct{}, len(existing)) for _, rel := range existing { - existingMap[rel.SupplierId] = rel + existingMap[rel.SupplierId] = struct{}{} } incomingMap := make(map[uint]struct{}, len(suppliers)) for _, rel := range suppliers { incomingMap[rel.SupplierId] = struct{}{} - if existingRel, exists := existingMap[rel.SupplierId]; exists { - if existingRel.Price != rel.Price { - if err := db.WithContext(ctx). - Model(&entity.NonstockSupplier{}). - Where("nonstock_id = ? AND supplier_id = ?", nonstockID, rel.SupplierId). - Update("price", rel.Price). - Error; err != nil { - return err - } - } + if _, exists := existingMap[rel.SupplierId]; exists { continue } record := entity.NonstockSupplier{ NonstockId: nonstockID, SupplierId: rel.SupplierId, - Price: rel.Price, } if err := db.WithContext(ctx).Create(&record).Error; err != nil { return err diff --git a/internal/modules/master/nonstocks/services/nonstock.service.go b/internal/modules/master/nonstocks/services/nonstock.service.go index e1cc5495..1b6a409c 100644 --- a/internal/modules/master/nonstocks/services/nonstock.service.go +++ b/internal/modules/master/nonstocks/services/nonstock.service.go @@ -115,19 +115,18 @@ func (s *nonstockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*enti supplierLinks []entity.NonstockSupplier supplierIDs []uint ) - if len(req.Suppliers) > 0 { - seen := make(map[uint]struct{}, len(req.Suppliers)) - supplierLinks = make([]entity.NonstockSupplier, 0, len(req.Suppliers)) - supplierIDs = make([]uint, 0, len(req.Suppliers)) - for _, supplier := range req.Suppliers { - if _, exists := seen[supplier.SupplierID]; exists { - return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Duplicate supplier_id %d", supplier.SupplierID)) + if len(req.SupplierIDs) > 0 { + seen := make(map[uint]struct{}, len(req.SupplierIDs)) + supplierLinks = make([]entity.NonstockSupplier, 0, len(req.SupplierIDs)) + supplierIDs = make([]uint, 0, len(req.SupplierIDs)) + for _, supplierID := range req.SupplierIDs { + if _, exists := seen[supplierID]; exists { + return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Duplicate supplier_id %d", supplierID)) } - seen[supplier.SupplierID] = struct{}{} - supplierIDs = append(supplierIDs, supplier.SupplierID) + seen[supplierID] = struct{}{} + supplierIDs = append(supplierIDs, supplierID) supplierLinks = append(supplierLinks, entity.NonstockSupplier{ - SupplierId: supplier.SupplierID, - Price: supplier.Price, + SupplierId: supplierID, }) } supplierList, supplierErr := s.Repository.GetSuppliersByIDs(ctx, supplierIDs) @@ -212,21 +211,20 @@ func (s nonstockService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint var supplierLinks []entity.NonstockSupplier var supplierUpdate bool - if req.Suppliers != nil { + if req.SupplierIDs != nil { supplierUpdate = true - if len(*req.Suppliers) > 0 { - seen := make(map[uint]struct{}, len(*req.Suppliers)) - supplierLinks = make([]entity.NonstockSupplier, 0, len(*req.Suppliers)) - supplierIDs := make([]uint, 0, len(*req.Suppliers)) - for _, supplier := range *req.Suppliers { - if _, exists := seen[supplier.SupplierID]; exists { - return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Duplicate supplier_id %d", supplier.SupplierID)) + if len(*req.SupplierIDs) > 0 { + seen := make(map[uint]struct{}, len(*req.SupplierIDs)) + supplierLinks = make([]entity.NonstockSupplier, 0, len(*req.SupplierIDs)) + supplierIDs := make([]uint, 0, len(*req.SupplierIDs)) + for _, supplierID := range *req.SupplierIDs { + if _, exists := seen[supplierID]; exists { + return nil, fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Duplicate supplier_id %d", supplierID)) } - seen[supplier.SupplierID] = struct{}{} - supplierIDs = append(supplierIDs, supplier.SupplierID) + seen[supplierID] = struct{}{} + supplierIDs = append(supplierIDs, supplierID) supplierLinks = append(supplierLinks, entity.NonstockSupplier{ - SupplierId: supplier.SupplierID, - Price: supplier.Price, + SupplierId: supplierID, }) } diff --git a/internal/modules/master/nonstocks/validations/nonstock.validation.go b/internal/modules/master/nonstocks/validations/nonstock.validation.go index c5491991..6378ac18 100644 --- a/internal/modules/master/nonstocks/validations/nonstock.validation.go +++ b/internal/modules/master/nonstocks/validations/nonstock.validation.go @@ -1,22 +1,17 @@ package validation -type SupplierPrice struct { - SupplierID uint `json:"supplier_id" validate:"required,gt=0"` - Price float64 `json:"price" validate:"required,gte=0"` -} - type Create struct { - Name string `json:"name" validate:"required_strict,min=3,max=50"` - UomID uint `json:"uom_id" validate:"required,gt=0"` - Suppliers []SupplierPrice `json:"suppliers,omitempty" validate:"omitempty,dive"` - Flags []string `json:"flags" validate:"dive,max=50"` + Name string `json:"name" validate:"required_strict,min=3,max=50"` + UomID uint `json:"uom_id" validate:"required,gt=0"` + SupplierIDs []uint `json:"supplier_ids,omitempty" validate:"omitempty,dive,gt=0"` + Flags []string `json:"flags" validate:"dive,max=50"` } type Update struct { - Name *string `json:"name,omitempty" validate:"omitempty,min=3,max=50"` - UomID *uint `json:"uom_id,omitempty" validate:"omitempty,gt=0"` - Suppliers *[]SupplierPrice `json:"suppliers,omitempty" validate:"omitempty,dive"` - Flags *[]string `json:"flags,omitempty" validate:"omitempty,dive,max=50"` + Name *string `json:"name,omitempty" validate:"omitempty,min=3,max=50"` + UomID *uint `json:"uom_id,omitempty" validate:"omitempty,gt=0"` + SupplierIDs *[]uint `json:"supplier_ids,omitempty" validate:"omitempty,dive,gt=0"` + Flags *[]string `json:"flags,omitempty" validate:"omitempty,dive,max=50"` } type Query struct { diff --git a/internal/modules/master/suppliers/dto/supplier_nonstock.dto.go b/internal/modules/master/suppliers/dto/supplier_nonstock.dto.go index 8c5e0082..828063eb 100644 --- a/internal/modules/master/suppliers/dto/supplier_nonstock.dto.go +++ b/internal/modules/master/suppliers/dto/supplier_nonstock.dto.go @@ -10,7 +10,6 @@ import ( type SupplierNonstockDTO struct { Id uint `json:"id"` Name string `json:"name"` - Price float64 `json:"price"` Uom *uomDTO.UomRelationDTO `json:"uom,omitempty"` Flags []string `json:"flags"` } @@ -43,7 +42,6 @@ func toSupplierNonstockDTOs(relations []entity.NonstockSupplier) []SupplierNonst result = append(result, SupplierNonstockDTO{ Id: Nonstock.Id, Name: Nonstock.Name, - Price: relation.Price, Uom: uomRef, Flags: flags, })