mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 07:15:43 +00:00
feat(BE): price-product-supplier
This commit is contained in:
@@ -111,8 +111,25 @@ func (s *nonstockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*enti
|
||||
return nil, err
|
||||
}
|
||||
|
||||
supplierIDs := utils.UniqueUintSlice(req.SupplierIDs)
|
||||
if len(supplierIDs) > 0 {
|
||||
var (
|
||||
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))
|
||||
}
|
||||
seen[supplier.SupplierID] = struct{}{}
|
||||
supplierIDs = append(supplierIDs, supplier.SupplierID)
|
||||
supplierLinks = append(supplierLinks, entity.NonstockSupplier{
|
||||
SupplierId: supplier.SupplierID,
|
||||
Price: supplier.Price,
|
||||
})
|
||||
}
|
||||
supplierList, supplierErr := s.Repository.GetSuppliersByIDs(ctx, supplierIDs)
|
||||
if supplierErr != nil {
|
||||
s.Log.Errorf("Failed to validate suppliers: %+v", supplierErr)
|
||||
@@ -155,7 +172,7 @@ func (s *nonstockService) CreateOne(c *fiber.Ctx, req *validation.Create) (*enti
|
||||
return err
|
||||
}
|
||||
|
||||
return s.Repository.SyncSuppliersDiff(ctx, tx, createBody.Id, supplierIDs)
|
||||
return s.Repository.SyncSuppliersDiff(ctx, tx, createBody.Id, supplierLinks)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -193,15 +210,27 @@ func (s nonstockService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint
|
||||
updateBody["uom_id"] = *req.UomID
|
||||
}
|
||||
|
||||
var supplierIDs []uint
|
||||
var supplierLinks []entity.NonstockSupplier
|
||||
var supplierUpdate bool
|
||||
if req.SupplierIDs != nil {
|
||||
if req.Suppliers != nil {
|
||||
supplierUpdate = true
|
||||
supplierIDs = utils.UniqueUintSlice(*req.SupplierIDs)
|
||||
if len(supplierIDs) > 0 {
|
||||
var supplierList []entity.Supplier
|
||||
var supplierErr error
|
||||
supplierList, supplierErr = s.Repository.GetSuppliersByIDs(ctx, supplierIDs)
|
||||
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))
|
||||
}
|
||||
seen[supplier.SupplierID] = struct{}{}
|
||||
supplierIDs = append(supplierIDs, supplier.SupplierID)
|
||||
supplierLinks = append(supplierLinks, entity.NonstockSupplier{
|
||||
SupplierId: supplier.SupplierID,
|
||||
Price: supplier.Price,
|
||||
})
|
||||
}
|
||||
|
||||
supplierList, supplierErr := s.Repository.GetSuppliersByIDs(ctx, supplierIDs)
|
||||
if supplierErr != nil {
|
||||
s.Log.Errorf("Failed to validate suppliers: %+v", supplierErr)
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate suppliers")
|
||||
@@ -253,11 +282,7 @@ func (s nonstockService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint
|
||||
}
|
||||
|
||||
if supplierUpdate {
|
||||
var ids []uint
|
||||
if len(supplierIDs) > 0 {
|
||||
ids = supplierIDs
|
||||
}
|
||||
if err := s.Repository.SyncSuppliersDiff(ctx, tx, id, ids); err != nil {
|
||||
if err := s.Repository.SyncSuppliersDiff(ctx, tx, id, supplierLinks); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user