Skip to content

Commit

Permalink
Add ticket type to notification message
Browse files Browse the repository at this point in the history
  • Loading branch information
ahobsonsayers committed Sep 23, 2024
1 parent 887aa71 commit 91758fb
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 18 deletions.
10 changes: 10 additions & 0 deletions test/assets/message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Test Venue, Test Location
Monday 1 January 0001 12:00am

2 ticket(s) - Standing
Ticket Price: £1.50
Total Price: £3.00
Discount: 25.00%

Original Ticket Price: £2.00
Original Total Price: £4.00
8 changes: 6 additions & 2 deletions test/testutils/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func ProjectDirectory(t *testing.T) string {

// ProjectDirectoryJoinPath returns a path joined to the project directory.
// See `ProjectDirectory` for more information
func ProjectDirectoryJoin(t *testing.T, path string) string {
return filepath.Join(ProjectDirectory(t), path)
func ProjectDirectoryJoin(t *testing.T, pathElems ...string) string {
pathElems = append(
[]string{ProjectDirectory(t)},
pathElems...,
)
return filepath.Join(pathElems...)
}
2 changes: 1 addition & 1 deletion twickets/notification/gotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type GotifyClient struct {
var _ Client = GotifyClient{}

func (g GotifyClient) SendTicketNotification(ticket twickets.Ticket) error {
notificationMessage, err := renderMessage(ticket, true)
notificationMessage, err := RenderMessage(ticket)
if err != nil {
return err
}
Expand Down
15 changes: 7 additions & 8 deletions twickets/notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ type Client interface {
}

type MessageTemplateData struct {
Venue string
Location string
Date string
Time string
Venue string
Location string
TicketType string // Standing, Stalls etc.
NumTickets int
TotalTicketPrice string
TotalPrice string
Expand All @@ -42,22 +43,20 @@ type MessageTemplateData struct {
Link string
}

func renderMessage(ticket twickets.Ticket, includeLink bool) (string, error) { // nolint: revive
func RenderMessage(ticket twickets.Ticket) (string, error) {
templateData := MessageTemplateData{
Venue: ticket.Event.Venue.Name,
Location: ticket.Event.Venue.Location.Name,
Date: ticket.Event.Date.Format("Monday 2 January 2006"),
Time: ticket.Event.Time.Format("3:04pm"),
Venue: ticket.Event.Venue.Name,
Location: ticket.Event.Venue.Location.Name,
TicketType: ticket.TicketType,
NumTickets: ticket.TicketQuantity,
TotalTicketPrice: ticket.TotalTicketPrice().String(),
TotalPrice: ticket.TotalPrice().String(),
OriginalTicketPrice: ticket.OriginalTicketPrice().String(),
OriginalTotalPrice: ticket.OriginalTotalPrice.String(),
Discount: ticket.Discount(),
}
if includeLink {
templateData.Link = ticket.Link()
}

var buffer bytes.Buffer
err := messageTemplate.Execute(&buffer, templateData)
Expand Down
24 changes: 23 additions & 1 deletion twickets/notification/notification_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package notification_test

import "github.com/ahobsonsayers/twitchets/twickets"
import (
"os"
"testing"

"github.com/ahobsonsayers/twitchets/test/testutils"
"github.com/ahobsonsayers/twitchets/twickets"
"github.com/ahobsonsayers/twitchets/twickets/notification"
"github.com/stretchr/testify/require"
)

func testNotificationTicket() twickets.Ticket {
return twickets.Ticket{
Expand All @@ -14,6 +22,7 @@ func testNotificationTicket() twickets.Ticket {
},
},
},
TicketType: "Standing",
TicketQuantity: 2,
TicketsPrice: twickets.Price{
Currency: twickets.CurrencyGBP,
Expand All @@ -29,3 +38,16 @@ func testNotificationTicket() twickets.Ticket {
},
}
}

func TestRenderMessage(t *testing.T) {
expectedMessagePath := testutils.ProjectDirectoryJoin(t, "test", "assets", "message.md")
expectedMessageBytes, err := os.ReadFile(expectedMessagePath)
require.NoError(t, err)
expectedMessage := string(expectedMessageBytes)

tickets := testNotificationTicket()
actualMessage, err := notification.RenderMessage(tickets)
require.NoError(t, err)

require.Equal(t, expectedMessage, actualMessage)
}
2 changes: 1 addition & 1 deletion twickets/notification/ntfy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type NtfyClient struct {
var _ Client = NtfyClient{}

func (c NtfyClient) SendTicketNotification(ticket twickets.Ticket) error {
notificationMessage, err := renderMessage(ticket, false)
notificationMessage, err := RenderMessage(ticket)
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions twickets/notification/template/message.tmpl.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ .Venue }}, {{ .Location }}
{{ .Date }} {{ .Time }}

Number: {{ .NumTickets }} ticket(s)
{{ .NumTickets }} ticket(s) - {{ .TicketType }}
Ticket Price: {{ .TotalTicketPrice }}
Total Price: {{ .TotalPrice }}
{{ if lt .Discount 0.0 -}}
Expand All @@ -12,7 +12,3 @@ Discount: {{ printf "%.2f" .Discount }}%

Original Ticket Price: {{ .OriginalTicketPrice }}
Original Total Price: {{ .OriginalTotalPrice }}

{{ if ne .Link "" -}}
Buy: {{ .Link }}
{{- end }}

0 comments on commit 91758fb

Please sign in to comment.