@@ -3,6 +3,8 @@ package services
33import (
44 "context"
55 "fmt"
6+ "strings"
7+ "unicode"
68
79 "github.com/NdoleStudio/httpsms/pkg/entities"
810 "github.com/NdoleStudio/httpsms/pkg/repositories"
@@ -86,11 +88,49 @@ func (service *EntitlementService) Check(
8688 return & EntitlementCheckResult {
8789 Allowed : false ,
8890 Message : fmt .Sprintf (
89- "Upgrade to a paid plan to create more than %d send schedule . Visit https://httpsms.com/pricing for details." ,
91+ "Upgrade to a paid plan to create more than %d %s . Visit https://httpsms.com/pricing for details." ,
9092 limit ,
93+ formatEntityName (entityName , true ),
9194 ),
9295 }, nil
9396 }
9497
9598 return & EntitlementCheckResult {Allowed : true }, nil
9699}
100+
101+ // formatEntityName converts a PascalCase entity name to lowercase words and optionally pluralizes it.
102+ // e.g. "MessageSendSchedule" → "message send schedules" (plural) or "message send schedule" (singular)
103+ func formatEntityName (name string , plural bool ) string {
104+ var words []string
105+ start := 0
106+ for i := 1 ; i < len (name ); i ++ {
107+ if unicode .IsUpper (rune (name [i ])) {
108+ words = append (words , strings .ToLower (name [start :i ]))
109+ start = i
110+ }
111+ }
112+ words = append (words , strings .ToLower (name [start :]))
113+
114+ if plural && len (words ) > 0 {
115+ last := words [len (words )- 1 ]
116+ switch {
117+ case strings .HasSuffix (last , "s" ), strings .HasSuffix (last , "x" ), strings .HasSuffix (last , "z" ),
118+ strings .HasSuffix (last , "sh" ), strings .HasSuffix (last , "ch" ):
119+ words [len (words )- 1 ] = last + "es"
120+ case strings .HasSuffix (last , "y" ) && len (last ) > 1 && ! isVowel (last [len (last )- 2 ]):
121+ words [len (words )- 1 ] = last [:len (last )- 1 ] + "ies"
122+ default :
123+ words [len (words )- 1 ] = last + "s"
124+ }
125+ }
126+
127+ return strings .Join (words , " " )
128+ }
129+
130+ func isVowel (c byte ) bool {
131+ switch c {
132+ case 'a' , 'e' , 'i' , 'o' , 'u' :
133+ return true
134+ }
135+ return false
136+ }
0 commit comments