Compare commits
No commits in common. "4ea8e32a8e545883da7f7f62077ae155e35adc9f" and "8eb19c57bab93d906fff7c4aea5327816625043c" have entirely different histories.
4ea8e32a8e
...
8eb19c57ba
@ -1,7 +1,7 @@
|
|||||||
server:
|
server:
|
||||||
base-url:
|
base-url:
|
||||||
local-url:
|
local-url:
|
||||||
self-order-url: http://localhost:5173
|
self-order-url: http://localhost:5174
|
||||||
port: 4000
|
port: 4000
|
||||||
|
|
||||||
jwt:
|
jwt:
|
||||||
@ -58,4 +58,4 @@ fonnte:
|
|||||||
|
|
||||||
fcm:
|
fcm:
|
||||||
credentials_file: "infra/firebase-service-account.json"
|
credentials_file: "infra/firebase-service-account.json"
|
||||||
project_id: "apskel-pos-v2"
|
project_id: "your-firebase-project-id"
|
||||||
@ -409,7 +409,6 @@ func (h *SelfOrderHandler) validateCreateOrderRequest(req *contract.SelfOrderCre
|
|||||||
return fmt.Errorf("session_id is required")
|
return fmt.Errorf("session_id is required")
|
||||||
}
|
}
|
||||||
if len(req.OrderItems) == 0 {
|
if len(req.OrderItems) == 0 {
|
||||||
|
|
||||||
return fmt.Errorf("at least one order item is required")
|
return fmt.Errorf("at least one order item is required")
|
||||||
}
|
}
|
||||||
for i, item := range req.OrderItems {
|
for i, item := range req.OrderItems {
|
||||||
|
|||||||
@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type InventoryRepository interface {
|
type InventoryRepository interface {
|
||||||
@ -279,12 +278,7 @@ func (r *InventoryRepositoryImpl) UpdateReorderLevel(ctx context.Context, id uui
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *InventoryRepositoryImpl) BulkCreate(ctx context.Context, inventoryItems []*entities.Inventory) error {
|
func (r *InventoryRepositoryImpl) BulkCreate(ctx context.Context, inventoryItems []*entities.Inventory) error {
|
||||||
return r.db.WithContext(ctx).
|
return r.db.WithContext(ctx).CreateInBatches(inventoryItems, 100).Error
|
||||||
Clauses(clause.OnConflict{
|
|
||||||
Columns: []clause.Column{{Name: "outlet_id"}, {Name: "product_id"}},
|
|
||||||
DoNothing: true,
|
|
||||||
}).
|
|
||||||
CreateInBatches(inventoryItems, 100).Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *InventoryRepositoryImpl) BulkUpdate(ctx context.Context, inventoryItems []*entities.Inventory) error {
|
func (r *InventoryRepositoryImpl) BulkUpdate(ctx context.Context, inventoryItems []*entities.Inventory) error {
|
||||||
@ -307,25 +301,21 @@ func (r *InventoryRepositoryImpl) BulkAdjustQuantity(ctx context.Context, adjust
|
|||||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||||
for productID, delta := range adjustments {
|
for productID, delta := range adjustments {
|
||||||
var inventory entities.Inventory
|
var inventory entities.Inventory
|
||||||
err := tx.Set("gorm:query_option", "FOR UPDATE").
|
if err := tx.Where("product_id = ? AND outlet_id = ?", productID, outletID).First(&inventory).Error; err != nil {
|
||||||
Where("product_id = ? AND outlet_id = ?", productID, outletID).
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
First(&inventory).Error
|
// Inventory doesn't exist, create it with initial quantity
|
||||||
if err != nil {
|
inventory = entities.Inventory{
|
||||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
ProductID: productID,
|
||||||
|
OutletID: outletID,
|
||||||
|
Quantity: 0,
|
||||||
|
ReorderLevel: 0,
|
||||||
|
}
|
||||||
|
if err := tx.Create(&inventory).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to create inventory record for product %s: %w", productID, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Use FirstOrCreate to handle race conditions — avoids duplicate key
|
|
||||||
// if another transaction already inserted this row concurrently.
|
|
||||||
inventory = entities.Inventory{
|
|
||||||
ProductID: productID,
|
|
||||||
OutletID: outletID,
|
|
||||||
Quantity: 0,
|
|
||||||
ReorderLevel: 0,
|
|
||||||
}
|
|
||||||
if err := tx.Where(entities.Inventory{ProductID: productID, OutletID: outletID}).
|
|
||||||
FirstOrCreate(&inventory).Error; err != nil {
|
|
||||||
return fmt.Errorf("failed to create inventory record for product %s: %w", productID, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inventory.UpdateQuantity(delta)
|
inventory.UpdateQuantity(delta)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user