-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb9c7bb
commit d62e88d
Showing
4 changed files
with
81 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,69 @@ | ||
package notification | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"fmt" | ||
"strings" | ||
"log" | ||
"text/template" | ||
|
||
"github.com/ahobsonsayers/twitchets/twickets" | ||
) | ||
|
||
var ( | ||
//go:embed template/message.tmpl.md | ||
messageTemplateFS embed.FS | ||
messageTemplate *template.Template | ||
) | ||
|
||
func init() { | ||
var err error | ||
messageTemplate, err = template.ParseFS(messageTemplateFS, "message.tmpl.md") | ||
if err != nil { | ||
log.Fatalf("failed to read notification message template: %v", err) | ||
} | ||
} | ||
|
||
type Client interface { | ||
SendTicketNotification(twickets.Ticket) error | ||
} | ||
|
||
func notificationMessage(ticket twickets.Ticket, includeLink bool) string { // nolint: revive | ||
var builder strings.Builder | ||
|
||
writeLine(&builder, "%s, %s", ticket.Event.Venue.Name, ticket.Event.Venue.Location.Name) | ||
writeLine(&builder, "%s %s", ticket.Event.Date.Format("Monday 2 January 2006"), ticket.Event.Time.Format("3:04pm")) | ||
writeLine(&builder, "%d ticket(s)", ticket.TicketQuantity) | ||
|
||
writeLine(&builder, "") | ||
type MessageTemplateData struct { | ||
Venue string | ||
Location string | ||
Date string | ||
Time string | ||
NumTickets int | ||
TotalTicketPrice string | ||
TotalPrice string | ||
OriginalTicketPrice string | ||
OriginalTotalPrice string | ||
Discount float64 | ||
Link string | ||
} | ||
|
||
writeLine(&builder, "Ticket Price: %s", ticket.TotalTicketPrice().String()) | ||
writeLine(&builder, "Total Price: %s", ticket.TotalPrice().String()) | ||
if ticket.Discount() < 0 { | ||
writeLine(&builder, "Discount: None") | ||
} else { | ||
writeLine(&builder, "Discount: %s", ticket.DiscountString()) | ||
func renderMessage(ticket twickets.Ticket, includeLink bool) (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"), | ||
NumTickets: ticket.TicketQuantity, | ||
TotalTicketPrice: ticket.TotalTicketPrice().String(), | ||
TotalPrice: ticket.TotalPrice().String(), | ||
OriginalTicketPrice: ticket.OriginalTicketPrice().String(), | ||
OriginalTotalPrice: ticket.OriginalTotalPrice.String(), | ||
Discount: ticket.Discount(), | ||
} | ||
|
||
writeLine(&builder, "") | ||
|
||
writeLine(&builder, "Original Ticket Price: %s", ticket.OriginalTicketPrice().String()) | ||
writeLine(&builder, "Original Total Price: %s", ticket.OriginalTotalPrice.String()) | ||
|
||
if includeLink { | ||
writeLine(&builder, "") | ||
writeLine(&builder, "Buy: %s", ticket.Link()) | ||
templateData.Link = ticket.Link() | ||
} | ||
|
||
return builder.String() | ||
} | ||
var buffer bytes.Buffer | ||
err := messageTemplate.Execute(&buffer, templateData) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to render notification message template:, %w", err) | ||
} | ||
|
||
func writeLine(builder *strings.Builder, format string, args ...any) { | ||
_, _ = builder.WriteString( | ||
fmt.Sprintf(format, args...) + "\n", | ||
) | ||
return buffer.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{{ .Venue }}, {{ .Location }} | ||
{{ .Date }} {{ .Time }} | ||
|
||
Num Tickets: {{ .NumTickets }} ticket(s) | ||
Ticket Price: {{ .TotalTicketPrice }} | ||
Total Price: {{ .TotalPrice }} | ||
{{ if lt .Discount 0.0 }} | ||
Discount: None | ||
{{ else }} | ||
Discount: {{ .Discount }}% | ||
{{ end }} | ||
|
||
Original Ticket Price: {{ .OriginalTicketPrice }} | ||
Original Total Price: {{ .OriginalTotalPrice }} | ||
{{ if .Link != "" }} | ||
Buy: {{ .Link }} | ||
{{ end }} |