Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
update all usage tier management calls
Browse files Browse the repository at this point in the history
  • Loading branch information
postables committed Apr 14, 2020
1 parent b133421 commit 49f3b65
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
3 changes: 3 additions & 0 deletions api/v2/routes_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ func (api *API) upgradeAccount(c *gin.Context) {
api.LogError(c, err, eh.UserSearchError)(http.StatusBadRequest)
return
}
if usages.Tier == models.Unverified {
Fail(c, errors.New("unverified account upgrade process must be done via email verification"))
}
// prevent people from repeatedly calling this granting perpetual credits
if usages.Tier != models.Free {
Fail(c, errors.New("user account is already upgrade"))
Expand Down
4 changes: 2 additions & 2 deletions api/v2/routes_ens.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (api *API) ClaimENSName(c *gin.Context) {
return
}
// prevent processing if account is free tier
if usage.Tier == models.Free {
if usage.Tier == models.Free || usage.Tier == models.Unverified {
Fail(c, errors.New("free accounts not eligible for ens claim"), http.StatusBadRequest)
return
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func (api *API) UpdateContentHash(c *gin.Context) {
return
}
// prevent processing if account is free tier
if usage.Tier == models.Free {
if usage.Tier == models.Free || usage.Tier == models.Unverified {
Fail(c, errors.New("free accounts not eligible for ens claim"), http.StatusBadRequest)
return
}
Expand Down
2 changes: 1 addition & 1 deletion api/v2/routes_rtfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (api *API) addFile(c *gin.Context) {
// if the user is within the free tier, then we throttle on-demand encryption
// free accounts are limited to a file upload size of 275MB when performing
// on-demand encryption. Non free accounts do not have this limit
if userUsage.Tier == models.Free {
if userUsage.Tier == models.Free || userUsage.Tier == models.Unverified {
megabytesUint := datasize.MB.Bytes()
maxSize := megabytesUint * 275
if fileHandler.Size > int64(maxSize) {
Expand Down
16 changes: 11 additions & 5 deletions api/v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,24 @@ func (api *API) validateHoldTime(username, holdTime string) (int64, error) {
if err != nil {
return 0, err
}
if usageTier.Tier == models.Free && holdTimeInt > freeHoldTimeLimitInMonths {
return 0, errors.New("free accounts are limited to maximum hold times of 12 month")
} else if usageTier.Tier != models.Free && holdTimeInt > nonFreeHoldTimeLimitInMonths {
return 0, errors.New("non free accounts are limited to a maximum hold time of 24 months")
switch usageTier.Tier {
case models.Free, models.Unverified:
if holdTimeInt > freeHoldTimeLimitInMonths {
return 0, errors.New("free accounts are limited to maximum hold times of 12 month")

}
default:
if holdTimeInt > nonFreeHoldTimeLimitInMonths {
return 0, errors.New("non free accounts are limited to a maximum hold time of 24 months")
}
}
return holdTimeInt, nil
}

func (api *API) ensureLEMaxPinTime(upload *models.Upload, holdTime int64, tier models.DataUsageTier) error {
var limit time.Time
switch tier {
case models.Free:
case models.Free, models.Unverified:
limit = time.Now().AddDate(1, 0, 0)
case models.Paid, models.Partner, models.WhiteLabeled:
limit = time.Now().AddDate(2, 0, 0)
Expand Down
5 changes: 5 additions & 0 deletions api/v2/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func Test_Ensure_Two_Year_Max(t *testing.T) {
{"12-Months-paid", args{12, models.Paid}, false},
{"22-Months-paid", args{22, models.Paid}, false},
{"25-Months-paid", args{25, models.Paid}, true},
{"10-Months-unverified", args{10, models.Unverified}, false},
{"11-Months-unverified", args{11, models.Unverified}, false},
{"12-Months-unverified", args{12, models.Unverified}, true},
{"22-Months-unverified", args{22, models.Unverified}, true},
{"25-Months-unverified", args{25, models.Unverified}, true},
{"10-Months-free", args{10, models.Free}, false},
{"11-Months-free", args{11, models.Free}, false},
{"12-Months-free", args{12, models.Free}, true},
Expand Down
5 changes: 5 additions & 0 deletions cmd/temporal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ var commands = map[string]cmd.Cmd{
fmt.Println("failed to create user account", err)
os.Exit(1)
}
// update tier
if err := models.NewUsageManager(d.DB).UpdateTier(args["user"], models.Free); err != nil {
fmt.Println("failed to update user account tier", err)
os.Exit(1)
}
// add credits
if _, err := models.NewUserManager(d.DB).AddCredits(args["user"], 99999999); err != nil {
fmt.Println("failed to grant credits to user account", err)
Expand Down
4 changes: 2 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func CalculatePinCost(username, contentHash string, holdTimeInMonths int64, im r
return 0, err
}
// if they are free tier, they don't incur data charges
if usage.Tier == models.Free || usage.Tier == models.WhiteLabeled {
if usage.Tier == models.Free || usage.Tier == models.WhiteLabeled || usage.Tier == models.Unverified {
return 0, nil
}
// dynamic pricing based on their usage tier
Expand All @@ -49,7 +49,7 @@ func CalculateFileCost(username string, holdTimeInMonths, size int64, um *models
return 0, err
}
// if they are free tier, they don't incur data charges
if usage.Tier == models.Free || usage.Tier == models.WhiteLabeled {
if usage.Tier == models.Free || usage.Tier == models.WhiteLabeled || usage.Tier == models.Unverified {
return 0, nil
}
// dynamic pricing based on their usage tier
Expand Down

0 comments on commit 49f3b65

Please sign in to comment.