Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

fix(api): format email quantities

Acho Arnold Ewin9bac0f7a22a8
diff --git a/api/pkg/emails/factory.go b/api/pkg/emails/factory.go
index 992f4864..63d85f5e 100644
--- a/api/pkg/emails/factory.go
+++ b/api/pkg/emails/factory.go
@@ -3,6 +3,7 @@ package emails
import (
"fmt"
"net/http"
+ "strconv"
"github.com/nyaruka/phonenumbers"
)
@@ -21,6 +22,15 @@ func (factory *factory) formatBool(value bool) string {
return "No"
}
+func (factory *factory) formatQuantity(value uint) string {
+ formatted := strconv.FormatUint(uint64(value), 10)
+ for index := len(formatted) - 3; index > 0; index -= 3 {
+ formatted = formatted[:index] + "," + formatted[index:]
+ }
+
+ return formatted
+}
+
func (factory *factory) formatHTTPResponseCode(code *int) string {
responseCode := "-"
if code != nil {
diff --git a/api/pkg/emails/hermes_user_email_factory.go b/api/pkg/emails/hermes_user_email_factory.go
index cae4a3b9..174b0f42 100644
--- a/api/pkg/emails/hermes_user_email_factory.go
+++ b/api/pkg/emails/hermes_user_email_factory.go
@@ -73,8 +73,8 @@ func (factory *hermesUserEmailFactory) UsageLimitExceeded(user *entities.User, u
email := hermes.Email{
Body: hermes.Body{
Intros: []string{
- fmt.Sprintf("You've reached your limit of %d messages on the %s plan, so new messages will not be processed until your usage resets.", user.SubscriptionName.Limit(), user.SubscriptionName),
- fmt.Sprintf("Between %s and %s you sent %d messages and received %d, for a total of %d.", formatBillingDate(usage.StartTimestamp, user.Location()), formatBillingDate(usage.EndTimestamp, user.Location()), usage.SentMessages, usage.ReceivedMessages, usage.TotalMessages()),
+ fmt.Sprintf("You've reached your limit of %s messages on the %s plan, so new messages will not be processed until your usage resets.", factory.formatQuantity(user.SubscriptionName.Limit()), user.SubscriptionName),
+ fmt.Sprintf("Between %s and %s you sent %s messages and received %s, for a total of %s.", formatBillingDate(usage.StartTimestamp, user.Location()), formatBillingDate(usage.EndTimestamp, user.Location()), factory.formatQuantity(usage.SentMessages), factory.formatQuantity(usage.ReceivedMessages), factory.formatQuantity(usage.TotalMessages())),
},
Actions: []hermes.Action{
{
@@ -120,7 +120,7 @@ func (factory *hermesUserEmailFactory) UsageLimitAlert(user *entities.User, usag
Body: hermes.Body{
Intros: []string{
fmt.Sprintf("This is a friendly heads-up that you've used %d%% of your monthly SMS limit on the %s plan.", percent, user.SubscriptionName),
- fmt.Sprintf("Between %s and %s you sent %d messages and received %d, for a total of %d out of your %d message limit.", formatBillingDate(usage.StartTimestamp, user.Location()), formatBillingDate(usage.EndTimestamp, user.Location()), usage.SentMessages, usage.ReceivedMessages, usage.TotalMessages(), user.SubscriptionName.Limit()),
+ fmt.Sprintf("Between %s and %s you sent %s messages and received %s, for a total of %s out of your %s message limit.", formatBillingDate(usage.StartTimestamp, user.Location()), formatBillingDate(usage.EndTimestamp, user.Location()), factory.formatQuantity(usage.SentMessages), factory.formatQuantity(usage.ReceivedMessages), factory.formatQuantity(usage.TotalMessages()), factory.formatQuantity(user.SubscriptionName.Limit())),
},
Actions: []hermes.Action{
{
diff --git a/api/pkg/emails/hermes_user_email_factory_test.go b/api/pkg/emails/hermes_user_email_factory_test.go
index df322e92..e10e3f48 100644
--- a/api/pkg/emails/hermes_user_email_factory_test.go
+++ b/api/pkg/emails/hermes_user_email_factory_test.go
@@ -51,10 +51,11 @@ func TestUsageLimitExceeded_IncludesBreakdownAndBillingPeriod(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "[email protected]", email.ToEmail)
assert.Equal(t, "⚠️ You have exceeded your plan limit", email.Subject)
- assert.Contains(t, email.Text, "limit of 5000 messages")
+ assert.Contains(t, email.Text, "limit of 5,000 messages")
assert.Contains(t, email.Text, "Between 19 June 2026 and 18 July 2026")
- assert.Contains(t, email.Text, "you sent 3000 messages and received 2000")
- assert.Contains(t, email.Text, "for a total of 5000")
+ assert.Contains(t, email.Text, "you sent 3,000 messages and received 2,000")
+ assert.Contains(t, email.Text, "for a total of 5,000")
+ assert.Contains(t, email.HTML, "you sent 3,000 messages and received 2,000")
}
func TestUsageLimitAlert_IncludesPercentBreakdownAndLimit(t *testing.T) {
@@ -62,11 +63,11 @@ func TestUsageLimitAlert_IncludesPercentBreakdownAndLimit(t *testing.T) {
user := &entities.User{
Timezone: "UTC",
- SubscriptionName: entities.SubscriptionNameProMonthly,
+ SubscriptionName: entities.SubscriptionName20KMonthly,
}
usage := &entities.BillingUsage{
- SentMessages: 2500,
- ReceivedMessages: 1500,
+ SentMessages: 11_357,
+ ReceivedMessages: 6_643,
StartTimestamp: time.Date(2026, 6, 19, 0, 0, 0, 0, time.UTC),
EndTimestamp: time.Date(2026, 7, 18, 23, 59, 59, 0, time.UTC),
}
@@ -75,9 +76,10 @@ func TestUsageLimitAlert_IncludesPercentBreakdownAndLimit(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "[email protected]", email.ToEmail)
- assert.Equal(t, "⚠️ 80% Usage Limit Alert", email.Subject)
- assert.Contains(t, email.Text, "used 80% of your monthly SMS limit")
+ assert.Equal(t, "⚠️ 90% Usage Limit Alert", email.Subject)
+ assert.Contains(t, email.Text, "used 90% of your monthly SMS limit")
assert.Contains(t, email.Text, "Between 19 June 2026 and 18 July 2026")
- assert.Contains(t, email.Text, "you sent 2500 messages and received 1500")
- assert.Contains(t, email.Text, "for a total of 4000 out of your 5000 message limit")
+ assert.Contains(t, email.Text, "you sent 11,357 messages and received 6,643")
+ assert.Contains(t, email.Text, "for a total of 18,000 out of your 20,000 message limit")
+ assert.Contains(t, email.HTML, "for a total of 18,000 out of your 20,000 message limit")
}