-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(voucher): add status command (#146)
Co-authored-by: mj <[email protected]>
- Loading branch information
1 parent
4e0f796
commit 76d25bf
Showing
10 changed files
with
273 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package voucher | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/pagu-project/Pagu/internal/engine/command" | ||
"github.com/pagu-project/Pagu/internal/entity" | ||
) | ||
|
||
func (v *Voucher) statusHandler(cmd command.Command, _ entity.AppID, _ string, args ...string) command.CommandResult { | ||
if len(args) > 0 { | ||
code := args[0] | ||
return v.codeStatus(cmd, code) | ||
} | ||
|
||
return v.vouchersStatus(cmd) | ||
} | ||
|
||
func (v *Voucher) codeStatus(cmd command.Command, code string) command.CommandResult { | ||
voucher, err := v.db.GetVoucherByCode(code) | ||
if err != nil { | ||
return cmd.ErrorResult(errors.New("voucher code is not valid, no voucher found")) | ||
} | ||
|
||
isClaimed := "NO" | ||
txLink := "" | ||
if len(voucher.TxHash) > 0 { | ||
isClaimed = "YES" | ||
txLink = fmt.Sprintf("https://pacviewer.com/transaction/%s", voucher.TxHash) | ||
} | ||
|
||
return cmd.SuccessfulResult("Code: %s\nAmount: %d PAC\n"+ | ||
"Expire At: %s\nRecipient: %s\nDescription: %s\nClaimed: %v\nTx Link: %s"+ | ||
"\n", | ||
voucher.Code, | ||
voucher.Amount, | ||
voucher.CreatedAt.AddDate(0, int(voucher.ValidMonths), 0).Format("02/01/2006, 15:04:05"), | ||
voucher.Recipient, | ||
voucher.Desc, | ||
isClaimed, | ||
txLink) | ||
} | ||
|
||
func (v *Voucher) vouchersStatus(cmd command.Command) command.CommandResult { | ||
vouchers, err := v.db.ListVoucher() | ||
if err != nil { | ||
return cmd.ErrorResult(err) | ||
} | ||
|
||
total := 0 | ||
totalAmount := 0 | ||
totalClaimedAmount := 0 | ||
totalClaimed := 0 | ||
totalExpired := 0 | ||
|
||
for _, vch := range vouchers { | ||
total++ | ||
totalAmount += int(vch.Amount) | ||
|
||
if len(vch.TxHash) > 0 { | ||
totalClaimed++ | ||
totalClaimedAmount += int(vch.Amount) | ||
} | ||
if time.Until(vch.CreatedAt.AddDate(0, int(vch.ValidMonths), 0)) <= 0 { | ||
totalExpired++ | ||
} | ||
} | ||
|
||
return cmd.SuccessfulResult("Total Codes: %d\nTotal Amount: %d PAC\n\n\n"+ | ||
"Claimed: %d\nTotal Claimed Amount: %d\nTotal Expired: %d"+ | ||
"\n", | ||
total, | ||
totalAmount, | ||
totalClaimed, | ||
totalClaimedAmount, | ||
totalExpired) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package voucher | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pagu-project/Pagu/internal/engine/command" | ||
"github.com/pagu-project/Pagu/internal/entity" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func TestStatusNormal(t *testing.T) { | ||
voucher, db, _, _ := setup(t) | ||
|
||
t.Run("one code status normal", func(t *testing.T) { | ||
now := time.Now() | ||
validMonths := uint8(2) | ||
|
||
db.EXPECT().GetVoucherByCode("12345678").Return( | ||
entity.Voucher{ | ||
ID: 1, | ||
Code: "12345678", | ||
Desc: "some_desc", | ||
Recipient: "some_recipient", | ||
ValidMonths: validMonths, | ||
Amount: uint(100), | ||
TxHash: "some_transaction_hash", | ||
ClaimedBy: 0, | ||
Model: gorm.Model{ | ||
CreatedAt: now, | ||
}, | ||
}, nil, | ||
).AnyTimes() | ||
|
||
expTime := now.AddDate(0, int(validMonths), 0).Format("02/01/2006, 15:04:05") | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.statusHandler(cmd, entity.AppIdDiscord, "", "12345678") | ||
assert.True(t, result.Successful) | ||
assert.Equal(t, result.Message, fmt.Sprintf("Code: 12345678\nAmount: 100 PAC\n"+ | ||
"Expire At: %s\nRecipient: some_recipient\nDescription: some_desc\nClaimed: YES\nTx Link: https://pacviewer.com/transaction/some_transaction_hash"+ | ||
"\n", expTime)) | ||
}) | ||
|
||
t.Run("wrong code", func(t *testing.T) { | ||
db.EXPECT().GetVoucherByCode("invalid_code").Return( | ||
entity.Voucher{}, errors.New(""), | ||
).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.statusHandler(cmd, entity.AppIdDiscord, "", "invalid_code") | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "An error occurred: voucher code is not valid, no voucher found") | ||
}) | ||
|
||
t.Run("list vouchers status normal", func(t *testing.T) { | ||
now := time.Now() | ||
validMonths := uint8(2) | ||
|
||
db.EXPECT().ListVoucher().Return( | ||
[]*entity.Voucher{ | ||
{ | ||
ID: 1, | ||
Code: "code1", | ||
ValidMonths: validMonths, | ||
Amount: uint(100), | ||
TxHash: "some_transaction_hash", | ||
Model: gorm.Model{ | ||
CreatedAt: now, | ||
}, | ||
}, | ||
{ | ||
ID: 2, | ||
Code: "code2", | ||
ValidMonths: validMonths, | ||
Amount: uint(100), | ||
Model: gorm.Model{ | ||
CreatedAt: now, | ||
}, | ||
}, | ||
{ | ||
ID: 3, | ||
Code: "code3", | ||
ValidMonths: validMonths, | ||
Amount: uint(100), | ||
Model: gorm.Model{ | ||
CreatedAt: now.AddDate(0, -3, 0), | ||
}, | ||
}, | ||
}, nil, | ||
).AnyTimes() | ||
|
||
cmd := command.Command{ | ||
User: &entity.User{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
result := voucher.statusHandler(cmd, entity.AppIdDiscord, "") | ||
assert.True(t, result.Successful) | ||
assert.Equal(t, result.Message, "Total Codes: 3\nTotal Amount: 300 PAC\n\n\n"+ | ||
"Claimed: 1\nTotal Claimed Amount: 100\nTotal Expired: 1"+ | ||
"\n") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,8 @@ | ||
package repository | ||
|
||
import "github.com/pagu-project/Pagu/internal/entity" | ||
|
||
type Database interface { | ||
AddFaucet(f *entity.PhoenixFaucet) error | ||
CanGetFaucet(user *entity.User) bool | ||
AddUser(u *entity.User) error | ||
HasUser(id string) bool | ||
GetUserInApp(appID entity.AppID, callerID string) (*entity.User, error) | ||
AddVoucher(v *entity.Voucher) error | ||
GetVoucherByCode(code string) (entity.Voucher, error) | ||
ClaimVoucher(id uint, txHash string, claimer uint) error | ||
GetZealyUser(id string) (*entity.ZealyUser, error) | ||
AddZealyUser(u *entity.ZealyUser) error | ||
UpdateZealyUser(id string, txHash string) error | ||
GetAllZealyUser() ([]*entity.ZealyUser, error) | ||
IUser | ||
IVoucher | ||
IFaucet | ||
IZealy | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters