mirror of
https://github.com/marcusDenslow/terminalShop.git
synced 2026-10-25 10:02:21 +01:00
Compare commits
2 Commits
master
...
fix/slack-
| Author | SHA1 | Date | |
|---|---|---|---|
| f61d9118a0 | |||
| d7f2189394 |
@@ -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 strings.EqualFold(order.Carrier, "BRING") {
|
||||
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
41
pkg/notify/slack_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user