[server] Apply change limit only for user edit
This commit is contained in:
@@ -243,7 +243,7 @@ func (h *AdminHandler) UpdateReferral(c *gin.Context) {
|
||||
}
|
||||
go h.DiscordController.NotifyAdminAction(
|
||||
fmt.Sprintf("Admin (%d) updating referral code for %d to %s", auth.GetUserID(c.Request.Header), request.UserID, request.Code))
|
||||
err := h.StorageBonusCtl.UpdateReferralCode(c, request.UserID, request.Code)
|
||||
err := h.StorageBonusCtl.UpdateReferralCode(c, request.UserID, request.Code, true)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("Failed to disable 2FA")
|
||||
handler.Error(c, stacktrace.Propagate(err, ""))
|
||||
|
||||
@@ -32,7 +32,7 @@ func (h StorageBonusHandler) UpdateReferralCode(context *gin.Context) {
|
||||
return
|
||||
}
|
||||
userID := auth.GetUserID(context.Request.Header)
|
||||
err := h.Controller.UpdateReferralCode(context, userID, request.Code)
|
||||
err := h.Controller.UpdateReferralCode(context, userID, request.Code, false)
|
||||
if err != nil {
|
||||
handler.Error(context, stacktrace.Propagate(err, ""))
|
||||
return
|
||||
|
||||
@@ -133,7 +133,7 @@ func (c *Controller) GetOrCreateReferralCode(ctx *gin.Context, userID int64) (*s
|
||||
return referralCode, nil
|
||||
}
|
||||
|
||||
func (c *Controller) UpdateReferralCode(ctx *gin.Context, userID int64, code string) error {
|
||||
func (c *Controller) UpdateReferralCode(ctx *gin.Context, userID int64, code string, isAdminEdit bool) error {
|
||||
code = strings.ToUpper(code)
|
||||
if !random.IsAlphanumeric(code) {
|
||||
return stacktrace.Propagate(ente.NewBadRequestWithMessage("code is not alphanumeric"), "")
|
||||
@@ -141,7 +141,7 @@ func (c *Controller) UpdateReferralCode(ctx *gin.Context, userID int64, code str
|
||||
if len(code) < 4 || len(code) > 8 {
|
||||
return stacktrace.Propagate(ente.NewBadRequestWithMessage("code length should be between 4 and 8"), "")
|
||||
}
|
||||
err := c.StorageBonus.AddNewCode(ctx, userID, code)
|
||||
err := c.StorageBonus.AddNewCode(ctx, userID, code, isAdminEdit)
|
||||
if err != nil {
|
||||
return stacktrace.Propagate(err, "failed to update referral code")
|
||||
}
|
||||
|
||||
@@ -39,14 +39,14 @@ func (r *Repository) InsertCode(ctx context.Context, userID int64, code string)
|
||||
// AddNewCode and mark the old one as inactive for a given userID.
|
||||
// Note: This method is not being used in the initial MVP as we don't allow user to change the storagebonus
|
||||
// code
|
||||
func (r *Repository) AddNewCode(ctx context.Context, userID int64, code string) error {
|
||||
func (r *Repository) AddNewCode(ctx context.Context, userID int64, code string, isAdminEdit bool) error {
|
||||
// check current referral code count
|
||||
var count int
|
||||
err := r.DB.QueryRowContext(ctx, "SELECT COALESCE(COUNT(*),0) FROM referral_codes WHERE user_id = $1", userID).Scan(&count)
|
||||
if err != nil {
|
||||
return stacktrace.Propagate(err, "failed to get storagebonus code count for user %d", userID)
|
||||
}
|
||||
if count > maxReferralChangeAllowed {
|
||||
if !isAdminEdit && count > maxReferralChangeAllowed {
|
||||
return stacktrace.Propagate(&ente.ApiError{
|
||||
Code: "REFERRAL_CHANGE_LIMIT_REACHED",
|
||||
Message: fmt.Sprintf("max referral code change limit %d reached", maxReferralChangeAllowed),
|
||||
|
||||
Reference in New Issue
Block a user