Skip to content

Commit 6b228fe

Browse files
authored
feature: recharge extra gift amount (#3296)
* feature: recharge extra gift amount * fix review
1 parent 62e25cb commit 6b228fe

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

controllers/account/controllers/account_controller.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ func (r *AccountReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
126126
}
127127
}()
128128
now := time.Now().UTC()
129-
account.Status.Balance += payment.Spec.Amount
129+
var gift = giveGift(payment.Spec.Amount)
130+
account.Status.Balance += payment.Spec.Amount + gift
130131
if err := r.Status().Update(ctx, account); err != nil {
131132
return ctrl.Result{}, fmt.Errorf("update account failed: %v", err)
132133
}
@@ -339,3 +340,22 @@ func (r *AccountReconciler) SetupWithManager(mgr ctrl.Manager) error {
339340
Watches(&source.Kind{Type: &accountv1.AccountBalance{}}, &handler.EnqueueRequestForObject{}).
340341
Complete(r)
341342
}
343+
344+
func giveGift(amount int64) int64 {
345+
var ratio int64
346+
switch {
347+
case amount < 299:
348+
return 0
349+
case amount < 599:
350+
ratio = 10
351+
case amount < 1999:
352+
ratio = 15
353+
case amount < 4999:
354+
ratio = 20
355+
case amount < 19999:
356+
ratio = 25
357+
default:
358+
ratio = 30
359+
}
360+
return amount * ratio / 100
361+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import "testing"
20+
21+
func Test_giveGift(t *testing.T) {
22+
type args struct {
23+
amount int64
24+
}
25+
tests := []struct {
26+
name string
27+
args args
28+
want int64
29+
}{
30+
// [1-298] -> 0%, [299-598] -> 10%, [599-1998] -> 15%, [1999-4998] -> 20%, [4999-19998] -> 25%, [19999+] -> 30%
31+
{name: "0% less than 299", args: args{amount: 100}, want: 0},
32+
{name: "10% between 299 and 599", args: args{amount: 299}, want: 29},
33+
{name: "10% between 299 and 599", args: args{amount: 300}, want: 30},
34+
{name: "15% between 599 and 1999", args: args{amount: 599}, want: 89},
35+
{name: "15% between 599 and 1999", args: args{amount: 600}, want: 90},
36+
{name: "20% between 1999 and 4999", args: args{amount: 1999}, want: 399},
37+
{name: "20% between 1999 and 4999", args: args{amount: 2000}, want: 400},
38+
{name: "25% between 4999 and 19999", args: args{amount: 4999}, want: 1249},
39+
{name: "25% between 4999 and 19999", args: args{amount: 5000}, want: 1250},
40+
{name: "30% more than 19999", args: args{amount: 19999}, want: 5999},
41+
{name: "30% more than 19999", args: args{amount: 20000}, want: 6000},
42+
{name: "30% more than 19999", args: args{amount: 99999}, want: 29999},
43+
}
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
if got := giveGift(tt.args.amount); got != tt.want {
47+
t.Errorf("giveGift() = %v, want %v", got, tt.want)
48+
}
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)