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>
123 lines
3.8 KiB
Go
123 lines
3.8 KiB
Go
// Publishes a bill over the real broker and waits for the ack, standing in for
|
|
// a till until one is available to test with.
|
|
//
|
|
// go run ./scratch/mqttpub
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
const (
|
|
locationID = "1135"
|
|
terminalID = "T4A9"
|
|
orderID = "99999999-8888-4777-8666-555555555555" // distinct from the HTTP test
|
|
)
|
|
|
|
func main() {
|
|
_ = godotenv.Load()
|
|
|
|
broker := os.Getenv("MQTT_URL")
|
|
if broker == "" {
|
|
broker = "tcp://66.116.225.226:1883"
|
|
}
|
|
|
|
opts := mqtt.NewClientOptions().
|
|
AddBroker(broker).
|
|
SetClientID("pos-e2e-probe").
|
|
SetUsername(os.Getenv("MQTT_USER")).
|
|
SetPassword(os.Getenv("MQTT_PASSWORD")).
|
|
SetCleanSession(true)
|
|
|
|
client := mqtt.NewClient(opts)
|
|
if t := client.Connect(); t.Wait() && t.Error() != nil {
|
|
log.Fatal("connect:", t.Error())
|
|
}
|
|
defer client.Disconnect(500)
|
|
fmt.Println("connected to", broker)
|
|
|
|
acks := make(chan []byte, 1)
|
|
ackTopic := fmt.Sprintf("nearle/pos/%s/%s/ack", locationID, terminalID)
|
|
if t := client.Subscribe(ackTopic, 1, func(_ mqtt.Client, m mqtt.Message) {
|
|
acks <- m.Payload()
|
|
}); t.Wait() && t.Error() != nil {
|
|
log.Fatal("subscribe:", t.Error())
|
|
}
|
|
fmt.Println("listening on", ackTopic)
|
|
|
|
batch := map[string]any{
|
|
"schema": 1,
|
|
"batch_id": "batch-mqtt-0001",
|
|
"store_id": locationID,
|
|
"terminal_id": terminalID,
|
|
"sent_at": time.Now().UTC().Format(time.RFC3339),
|
|
"orders": []map[string]any{{
|
|
"id": orderID,
|
|
"invoice_number": "INV-2608-T4A9-00002",
|
|
"created_at": time.Now().UTC().Format(time.RFC3339),
|
|
"terminal_id": terminalID,
|
|
"cashier": "Divya",
|
|
"customer": map[string]any{"id": "c-2", "mobile": "9840099999", "name": "Ravi"},
|
|
"subtotal": 60.0,
|
|
"discount": 0.0,
|
|
"promos": []any{},
|
|
"tax": 4.44,
|
|
"tax_breakdown": map[string]float64{"0.08": 4.44},
|
|
"round_off": 0.0,
|
|
"total": 60.0,
|
|
"points_earned": 0,
|
|
"points_redeemed": 0,
|
|
"payments": []map[string]any{{"method": "upi", "amount": 60.0, "reference": "TESTUPI"}},
|
|
"items": []map[string]any{{
|
|
"product_id": "6988", "barcode": "6988", "name": "Mysore Banana",
|
|
"quantity": 1, "unit_price": 60.0, "discount": 0.0,
|
|
"gst_rate": 0.08, "tax": 4.44, "line_total": 60.0,
|
|
}},
|
|
}},
|
|
}
|
|
|
|
body, _ := json.Marshal(batch)
|
|
orderTopic := fmt.Sprintf("nearle/pos/%s/%s/order", locationID, terminalID)
|
|
if t := client.Publish(orderTopic, 1, false, body); t.Wait() && t.Error() != nil {
|
|
log.Fatal("publish:", t.Error())
|
|
}
|
|
fmt.Println("published a bill to", orderTopic)
|
|
|
|
// The same 20 seconds a real till waits before giving up and re-sending.
|
|
select {
|
|
case payload := <-acks:
|
|
fmt.Println("\nACK RECEIVED:")
|
|
var pretty map[string]any
|
|
_ = json.Unmarshal(payload, &pretty)
|
|
out, _ := json.MarshalIndent(pretty, " ", " ")
|
|
fmt.Println(" " + string(out))
|
|
case <-time.After(20 * time.Second):
|
|
fmt.Println("\nNO ACK within 20s — a real till would keep the bill and send it again")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// A heartbeat too, so the Redis presence path is exercised.
|
|
health, _ := json.Marshal(map[string]any{
|
|
"schema": 1, "status": "online",
|
|
"terminal_id": terminalID, "location_id": locationID,
|
|
"store_name": "Ragul stores Selvapuram", "app_version": "1.1.0",
|
|
"pending_bills": 0, "pending_registrations": 0,
|
|
"today_bills": 2, "today_amount": 170.0,
|
|
"printer_reachable": false,
|
|
"reported_at": time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
healthTopic := fmt.Sprintf("nearle/pos/%s/%s/health", locationID, terminalID)
|
|
if t := client.Publish(healthTopic, 1, false, health); t.Wait() && t.Error() != nil {
|
|
log.Fatal("publish health:", t.Error())
|
|
}
|
|
fmt.Println("\npublished a heartbeat to", healthTopic)
|
|
time.Sleep(2 * time.Second)
|
|
}
|