invites: move code gen to function

code to generate an invite code w/ a non-integer first character was
reused a bunch, so it's now function GenerateInviteCode().
This commit is contained in:
Harvey Tindall
2023-10-11 11:30:28 +01:00
parent 7c861e5763
commit 77f6b1042e
4 changed files with 23 additions and 47 deletions

View File

@@ -1,11 +1,9 @@
package main
import (
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/lithammer/shortuuid/v3"
"github.com/timshannon/badgerhold/v4"
)
@@ -151,13 +149,7 @@ func (app *appContext) EnableReferralForProfile(gc *gin.Context) {
}
// Generate new code for referral template
inv.Code = shortuuid.New()
// make sure code doesn't begin with number
_, err := strconv.Atoi(string(inv.Code[0]))
for err == nil {
inv.Code = shortuuid.New()
_, err = strconv.Atoi(string(inv.Code[0]))
}
inv.Code = GenerateInviteCode()
inv.Created = time.Now()
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
inv.IsReferral = true