Ingest counter sales from the POS terminals, over MQTT and HTTP
A till holds every bill in its own SQLite database and keeps it for seven days after we acknowledge it, marking one synced only when its id comes back in an ack. Everything here follows from that. Silence is not acceptance, so a failing ingest publishes nothing at all and the terminal simply sends again. A duplicate is a success, because at-least-once delivery means a lost ack legitimately re-delivers bills we already hold, and calling those failures would strand a day of takings on the till. Deduplication is a unique index on the terminal's UUID plus an advisory lock held for the transaction. Bills land in pos_orders / pos_order_items rather than orders: a counter bill carries a cashier, a terminal, a rounding adjustment, promos, loyalty movement and a payment split that orders has nowhere to put, and forcing one into the other loses whatever does not fit. Stock is *not* split — a counter sale writes the same productstocks rows an app order does, through helpers extracted from createOrderTx so the rule that prevents overselling has one implementation rather than two. GetRevenueSummary and GetSalesSummary were extended to union the new table in; any new report has to remember the same. Terminal health goes to Redis under a 90-second TTL, sharing the instance the express backend uses. A heartbeat is a fact with an expiry date: a till that loses power stops refreshing and ages off the board by itself, where a Postgres row would need ~288k writes a day and a reaper. Proven end to end against the live estate before commit: a bill over HTTP and one over the real Mosquitto broker, the same bill three times producing one row and one stock movement, and a heartbeat arriving on the health endpoint. All probe data was removed afterwards. Four things that only surfaced against real data. An unset jsonb column failed the very first bill. Product SKUs are unusable as barcodes — 6,245 products share 93 SKUs and "1" covers 5,794 of them — against the till's unique index, so barcodes fall back to the product id. A taxpercent of -1 exists and would have put negative GST in a filed slab. And a product with id 0 exists, which can never be billed and is now skipped. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,16 +9,21 @@ import (
|
||||
)
|
||||
|
||||
type Facade struct {
|
||||
UserController *controllers.UserController
|
||||
ProductController *controllers.ProductController
|
||||
OrderController *controllers.OrderController
|
||||
DeliveriesController *controllers.DeliveriesController
|
||||
UtilsController *controllers.UtilsController
|
||||
TenantController *controllers.TenantController
|
||||
PartnerController *controllers.PartnerController
|
||||
UserController *controllers.UserController
|
||||
ProductController *controllers.ProductController
|
||||
OrderController *controllers.OrderController
|
||||
DeliveriesController *controllers.DeliveriesController
|
||||
UtilsController *controllers.UtilsController
|
||||
TenantController *controllers.TenantController
|
||||
PartnerController *controllers.PartnerController
|
||||
CustomerController *controllers.CustomerController
|
||||
StockRequestController *controllers.StockRequestController
|
||||
CatalogueController *controllers.CatalogueController
|
||||
PosController *controllers.PosController
|
||||
|
||||
// Held so the NATS consumer can reach the ingest without going through
|
||||
// HTTP. Unexported: everything else should use the controller.
|
||||
posService services.PosService
|
||||
}
|
||||
|
||||
// NewFacade wires up modules against the main (nearledb) connection.
|
||||
@@ -79,16 +84,33 @@ func NewFacade(db *gorm.DB, catalogueDB *gorm.DB) *Facade {
|
||||
stockRequestService := services.NewStockRequestService(stockRequestRepo, productService)
|
||||
stockRequestController := controllers.NewStockRequestController(stockRequestService)
|
||||
|
||||
// POS Module — ingest from the in-store terminals.
|
||||
//
|
||||
// Presence has no *gorm.DB: terminal health lives in Redis under a TTL, so
|
||||
// a till that loses power ages out of the board by itself instead of
|
||||
// leaving a Postgres row claiming it is online.
|
||||
posRepo := repositories.NewPosRepository(db)
|
||||
posPresence := repositories.NewPosPresenceRepository()
|
||||
posService := services.NewPosService(posRepo, posPresence)
|
||||
posController := controllers.NewPosController(posService)
|
||||
|
||||
return &Facade{
|
||||
UserController: userController,
|
||||
ProductController: productController,
|
||||
OrderController: orderController,
|
||||
DeliveriesController: deliveriesController,
|
||||
UtilsController: utilsController,
|
||||
TenantController: tenantController,
|
||||
UserController: userController,
|
||||
ProductController: productController,
|
||||
OrderController: orderController,
|
||||
DeliveriesController: deliveriesController,
|
||||
UtilsController: utilsController,
|
||||
TenantController: tenantController,
|
||||
PartnerController: partnerController,
|
||||
CustomerController: customerController,
|
||||
StockRequestController: stockRequestController,
|
||||
CatalogueController: catalogueController,
|
||||
PosController: posController,
|
||||
posService: posService,
|
||||
}
|
||||
}
|
||||
|
||||
// PosService exposes the ingest to callers outside the HTTP layer — the NATS
|
||||
// consumer runs the same code path a POST does, so a bill arriving over MQTT
|
||||
// and one arriving over HTTP cannot diverge.
|
||||
func (f *Facade) PosService() services.PosService { return f.posService }
|
||||
|
||||
Reference in New Issue
Block a user