mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
fix(BE): remove supplier price in master nonstock
This commit is contained in:
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user