feat: optimize background tasks and fix warehouse pricelist workflow
Optimize task retention from 5 minutes to 30 seconds to reduce polling overhead since toast notifications are shown only once. Add conditional warehouse pricelist creation via checkbox. Fix category storage in warehouse pricelists to properly load from lot table. Replace SSE with task polling for all long operations. Add comprehensive logging for debugging while minimizing noise from polling endpoints. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -175,14 +175,37 @@ func (s *Service) CreateForSourceWithProgress(createdBy, source string, sourceIt
|
||||
}
|
||||
|
||||
if len(sourceItems) > 0 {
|
||||
// For warehouse and other explicit source items - use only provided data
|
||||
// DO NOT load metadata settings (price_period_days, coefficient, etc.)
|
||||
|
||||
// Load categories from lot table
|
||||
lotNames := make([]string, 0, len(sourceItems))
|
||||
for _, srcItem := range sourceItems {
|
||||
if strings.TrimSpace(srcItem.LotName) != "" {
|
||||
lotNames = append(lotNames, strings.TrimSpace(srcItem.LotName))
|
||||
}
|
||||
}
|
||||
|
||||
categoryMap := make(map[string]*string)
|
||||
if len(lotNames) > 0 {
|
||||
var lots []models.Lot
|
||||
if err := s.db.Where("lot_name IN ?", lotNames).Find(&lots).Error; err == nil {
|
||||
for _, lot := range lots {
|
||||
categoryMap[lot.LotName] = lot.LotCategory
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
items = make([]models.PricelistItem, 0, len(sourceItems))
|
||||
for _, srcItem := range sourceItems {
|
||||
if strings.TrimSpace(srcItem.LotName) == "" || srcItem.Price <= 0 {
|
||||
lotName := strings.TrimSpace(srcItem.LotName)
|
||||
if lotName == "" || srcItem.Price <= 0 {
|
||||
continue
|
||||
}
|
||||
items = append(items, models.PricelistItem{
|
||||
PricelistID: pricelist.ID,
|
||||
LotName: strings.TrimSpace(srcItem.LotName),
|
||||
LotName: lotName,
|
||||
LotCategory: categoryMap[lotName],
|
||||
Price: srcItem.Price,
|
||||
PriceMethod: strings.TrimSpace(srcItem.PriceMethod),
|
||||
})
|
||||
@@ -194,6 +217,22 @@ func (s *Service) CreateForSourceWithProgress(createdBy, source string, sourceIt
|
||||
return nil, fmt.Errorf("getting lot metadata: %w", err)
|
||||
}
|
||||
|
||||
// Load categories from lot table for all metadata items
|
||||
lotNames := make([]string, 0, len(metadata))
|
||||
for _, m := range metadata {
|
||||
lotNames = append(lotNames, m.LotName)
|
||||
}
|
||||
|
||||
categoryMap := make(map[string]*string)
|
||||
if len(lotNames) > 0 {
|
||||
var lots []models.Lot
|
||||
if err := s.db.Where("lot_name IN ?", lotNames).Find(&lots).Error; err == nil {
|
||||
for _, lot := range lots {
|
||||
categoryMap[lot.LotName] = lot.LotCategory
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create pricelist items with all price settings
|
||||
items = make([]models.PricelistItem, 0, len(metadata))
|
||||
for _, m := range metadata {
|
||||
@@ -203,6 +242,7 @@ func (s *Service) CreateForSourceWithProgress(createdBy, source string, sourceIt
|
||||
items = append(items, models.PricelistItem{
|
||||
PricelistID: pricelist.ID,
|
||||
LotName: m.LotName,
|
||||
LotCategory: categoryMap[m.LotName],
|
||||
Price: *m.CurrentPrice,
|
||||
PriceMethod: string(m.PriceMethod),
|
||||
PricePeriodDays: m.PricePeriodDays,
|
||||
|
||||
Reference in New Issue
Block a user