feat(slack): show mark status buttons on all labeled orders

This commit is contained in:
2026-06-30 20:02:17 +02:00
parent d7f2189394
commit f61d9118a0
2 changed files with 58 additions and 12 deletions

View File

@@ -253,25 +253,30 @@ func SlackPostLabelPurchased(orderID uint, labelURL string, costCents int) {
headLine := fmt.Sprintf(":white_check_mark: Labeled - %s `%s` (%s). <%s|Download label PDF>", order.Carrier, order.TrackingNumber, dollars(costCents), labelURL)
blocks := []map[string]any{
{
"type": "section",
"text": map[string]any{"type": "mrkdwn", "text": headLine},
},
}
if order.TrackingNumber != "" {
blocks = append(blocks, markStatusActionBlock(order.ID))
}
_, _ = postSlackMessage(token, map[string]any{
"channel": channel,
"thread_ts": *order.SlackThreadTS,
"text": headLine,
"blocks": blocks,
"blocks": labelPurchaseBlocks(&order, headLine),
})
}
// labelPurchaseBlocks assembles the Block Kit payload for the post-label thread
// reply: a headline section plus the manual mark-status buttons for any labeled
// order. The buttons are gtated on a present tracking number (carrier-agnostic)
func labelPurchaseBlocks(order *models.Order, headline string) []map[string]any {
blocks := []map[string]any{
{
"type": "section",
"text": map[string]any{"type": "mrkdwn", "text": headline},
},
}
if order.TrackingNumber != "" {
blocks = append(blocks, markStatusActionBlock(order.ID))
}
return blocks
}
func markStatusActionBlock(orderID uint) map[string]any {
id := fmt.Sprintf("%d", orderID)
return map[string]any{

41
pkg/notify/slack_test.go Normal file
View File

@@ -0,0 +1,41 @@
package notify
import (
"testing"
"terminalShop/pkg/models"
)
// hadActionsBlock reports whether the Block Kit slice contains an "actions"
// block, the row that carries the mark-status buttons.
func hasActionsBlock(blocks []map[string]any) bool {
for _, b := range blocks {
if b["type"] == "actions" {
return true
}
}
return false
}
func TestLabelPurchasedBlocksButtonGating(t *testing.T) {
cases := []struct {
name string
order models.Order
wantButtons bool
}{
{"shippo us order with tracking shows buttons", models.Order{ID: 7, Carrier: "USPS", TrackingNumber: "9400111899223817"}, true},
{"bring orders with tracking shows buttons", models.Order{ID: 8, Carrier: "BRING", TrackingNumber: "CONS123456"}, true},
{"order without tracking shows no buttons", models.Order{ID: 9, Carrier: "USPS", TrackingNumber: ""}, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
blocks := labelPurchaseBlocks(&tc.order, "headline")
if got := hasActionsBlock(blocks); got != tc.wantButtons {
t.Fatalf("hasActionsBlock = %v, want %v", got, tc.wantButtons)
}
if len(blocks) == 0 || blocks[0]["type"] != "section" {
t.Fatalf("expected first block to be the headline section, got %v", blocks)
}
})
}
}