feat: implement stock reservation background worker and geocode sync endpoint

This commit is contained in:
Tiago Ribeiro 2026-03-09 08:59:06 -03:00
parent 2e0a65389d
commit 4aac3a0a7e
2 changed files with 21 additions and 0 deletions

View file

@ -311,6 +311,9 @@ func (s *Server) Start(ctx context.Context) error {
ReadHeaderTimeout: 5 * time.Second,
}
// Start Stock Reservation Cleanup Worker
s.svc.StartStockCleanupWorker(ctx)
log.Printf("starting %s on %s", s.cfg.AppName, s.cfg.Addr())
return srv.ListenAndServe()
}

View file

@ -43,6 +43,24 @@ func (s *Service) ListProducts(ctx context.Context, filter domain.ProductFilter,
return &domain.ProductPage{Products: products, Total: total, Page: page, PageSize: pageSize}, nil
}
// StartStockCleanupWorker runs a background goroutine to expire old reservations.
func (s *Service) StartStockCleanupWorker(ctx context.Context) {
ticker := time.NewTicker(5 * time.Minute)
go func() {
for {
select {
case <-ticker.C:
if err := s.repo.ExpireReservations(context.Background()); err != nil {
fmt.Printf("ERROR: failed to expire stock reservations: %v\n", err)
}
case <-ctx.Done():
ticker.Stop()
return
}
}
}()
}
// ReserveStock creates a temporary hold on inventory.
func (s *Service) ReserveStock(ctx context.Context, productID, inventoryItemID, buyerID uuid.UUID, quantity int64) (*domain.StockReservation, error) {
// 1. Check availability (physical stock - active reservations)