From f61d9118a09f89de185f289f55b1ec85ec796aab Mon Sep 17 00:00:00 2001 From: marcusDenslow Date: Tue, 30 Jun 2026 20:02:17 +0200 Subject: [PATCH] feat(slack): show mark status buttons on all labeled orders --- pkg/notify/slack.go | 29 ++++++++++++++++------------ pkg/notify/slack_test.go | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 pkg/notify/slack_test.go diff --git a/pkg/notify/slack.go b/pkg/notify/slack.go index 264eb79..2cfbb0d 100644 --- a/pkg/notify/slack.go +++ b/pkg/notify/slack.go @@ -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{ diff --git a/pkg/notify/slack_test.go b/pkg/notify/slack_test.go new file mode 100644 index 0000000..97b44db --- /dev/null +++ b/pkg/notify/slack_test.go @@ -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) + } + }) + } +}