Skip to content

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

feat: add formatEntityName utility for human-readable entitlement messages

Acho Arnoldceb3b35f1d45
diff --git a/api/pkg/services/entitlement_service.go b/api/pkg/services/entitlement_service.go
index 62c280ab..2cdae129 100644
--- a/api/pkg/services/entitlement_service.go
+++ b/api/pkg/services/entitlement_service.go
@@ -3,6 +3,8 @@ package services
import (
"context"
"fmt"
+ "strings"
+ "unicode"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/repositories"
@@ -86,11 +88,49 @@ func (service *EntitlementService) Check(
return &EntitlementCheckResult{
Allowed: false,
Message: fmt.Sprintf(
- "Upgrade to a paid plan to create more than %d send schedule. Visit https://httpsms.com/pricing for details.",
+ "Upgrade to a paid plan to create more than %d %s. Visit https://httpsms.com/pricing for details.",
limit,
+ formatEntityName(entityName, true),
),
}, nil
}
return &EntitlementCheckResult{Allowed: true}, nil
}
+
+// formatEntityName converts a PascalCase entity name to lowercase words and optionally pluralizes it.
+// e.g. "MessageSendSchedule" → "message send schedules" (plural) or "message send schedule" (singular)
+func formatEntityName(name string, plural bool) string {
+ var words []string
+ start := 0
+ for i := 1; i < len(name); i++ {
+ if unicode.IsUpper(rune(name[i])) {
+ words = append(words, strings.ToLower(name[start:i]))
+ start = i
+ }
+ }
+ words = append(words, strings.ToLower(name[start:]))
+
+ if plural && len(words) > 0 {
+ last := words[len(words)-1]
+ switch {
+ case strings.HasSuffix(last, "s"), strings.HasSuffix(last, "x"), strings.HasSuffix(last, "z"),
+ strings.HasSuffix(last, "sh"), strings.HasSuffix(last, "ch"):
+ words[len(words)-1] = last + "es"
+ case strings.HasSuffix(last, "y") && len(last) > 1 && !isVowel(last[len(last)-2]):
+ words[len(words)-1] = last[:len(last)-1] + "ies"
+ default:
+ words[len(words)-1] = last + "s"
+ }
+ }
+
+ return strings.Join(words, " ")
+}
+
+func isVowel(c byte) bool {
+ switch c {
+ case 'a', 'e', 'i', 'o', 'u':
+ return true
+ }
+ return false
+}