Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(voucher): turn create command to only admin #144

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/engine/command/calculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const (
)

type Calculator struct {
clientMgr *client.Mgr
clientMgr client.Manager
}

func NewCalculator(clientMgr *client.Mgr) Calculator {
func NewCalculator(clientMgr client.Manager) Calculator {
return Calculator{
clientMgr: clientMgr,
}
Expand Down
1 change: 1 addition & 0 deletions internal/engine/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (cmd *Command) FailedResult(message string, a ...interface{}) CommandResult
Color: cmd.Color,
Title: fmt.Sprintf("%v %v", cmd.Help, cmd.Emoji),
Message: fmt.Sprintf(message, a...),
Error: message,
Successful: false,
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/command/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const (
)

type Market struct {
clientMgr *client.Mgr
clientMgr client.Manager
priceCache cache.Cache[string, entity.Price]
}

func NewMarket(clientMgr *client.Mgr, priceCache cache.Cache[string, entity.Price]) Market {
func NewMarket(clientMgr client.Manager, priceCache cache.Cache[string, entity.Price]) Market {
return Market{
clientMgr: clientMgr,
priceCache: priceCache,
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/command/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const (

type Network struct {
ctx context.Context
clientMgr *client.Mgr
clientMgr client.Manager
}

func NewNetwork(ctx context.Context, clientMgr *client.Mgr) Network {
func NewNetwork(ctx context.Context, clientMgr client.Manager) Network {
return Network{
ctx: ctx,
clientMgr: clientMgr,
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/command/phoenix/phoenix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const (
type Phoenix struct {
wallet wallet.IWallet
db repository.Database
clientMgr *client.Mgr
clientMgr client.Manager
faucetAmount uint
}

func NewPhoenix(wallet wallet.IWallet, faucetAmount uint, clientMgr *client.Mgr, db repository.Database) Phoenix {
func NewPhoenix(wallet wallet.IWallet, faucetAmount uint, clientMgr client.Manager, db repository.Database) Phoenix {
return Phoenix{
wallet: wallet,
clientMgr: clientMgr,
Expand Down
7 changes: 3 additions & 4 deletions internal/engine/command/voucher/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ import (
"time"

amt "github.com/pactus-project/pactus/types/amount"
"github.com/pagu-project/Pagu/pkg/log"

"github.com/pagu-project/Pagu/internal/engine/command"
"github.com/pagu-project/Pagu/internal/entity"
"github.com/pagu-project/Pagu/pkg/log"
)

func (v *Voucher) claimHandler(cmd command.Command, _ entity.AppID, callerID string, args ...string) command.CommandResult {
code := args[0]
if len(code) != 8 {
return cmd.ErrorResult(errors.New("voucher code is not valid"))
return cmd.ErrorResult(errors.New("voucher code is not valid, length must be 8"))
}

voucher, err := v.db.GetVoucherByCode(code)
if err != nil {
return cmd.ErrorResult(errors.New("voucher code is not valid"))
return cmd.ErrorResult(errors.New("voucher code is not valid, no voucher found"))
}

now := time.Now().Month()
Expand Down
4 changes: 3 additions & 1 deletion internal/engine/command/voucher/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
)

func (v *Voucher) createHandler(cmd command.Command, _ entity.AppID, _ string, args ...string) command.CommandResult {
//! Admin only check
if cmd.User.Role != entity.Admin {
return cmd.ErrorResult(errors.New("only users with Admin role can create a new voucher"))
}

code := utils.RandomString(8, utils.CapitalAlphanumerical)
for _, err := v.db.GetVoucherByCode(code); err == nil; {
Expand Down
10 changes: 0 additions & 10 deletions internal/engine/command/voucher/create_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/engine/command/voucher/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const (
type Voucher struct {
db repository.Database
wallet wallet.IWallet
clientManager *client.Mgr
clientManager client.Manager
}

func NewVoucher(db repository.Database, wallet wallet.IWallet, cli *client.Mgr) Voucher {
func NewVoucher(db repository.Database, wallet wallet.IWallet, cli client.Manager) Voucher {
return Voucher{
db: db,
wallet: wallet,
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/command/wallet.middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func (h *MiddlewareHandler) WalletBalance(_ *Command, _ entity.AppID, _ string, _ ...string) error {
if h.wallet.Balance() < 5 {
return errors.New("empty pagu wallet balance")
return errors.New("the Pagu Wallet balance is less than 5 PAC")
}

return nil
Expand Down
30 changes: 13 additions & 17 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ type BotEngine struct {
ctx context.Context //nolint
cancel context.CancelFunc

clientMgr *client2.Mgr
phoenixClientMgr *client2.Mgr
rootCmd command.Command
clientMgr client2.Manager
rootCmd command.Command

blockchainCmd calculator.Calculator
networkCmd network.Network
Expand Down Expand Up @@ -94,7 +93,7 @@ func NewBotEngine(cfg *config.Config) (*BotEngine, error) {
return newBotEngine(ctx, cancel, db, cm, wal, cfg.Phoenix.FaucetAmount, cfg.BotName), nil
}

func newBotEngine(ctx context.Context, cnl context.CancelFunc, db repository.Database, cm *client2.Mgr, wallet wallet.IWallet, phoenixFaucetAmount uint, botName string) *BotEngine {
func newBotEngine(ctx context.Context, cnl context.CancelFunc, db repository.Database, cm client2.Manager, wallet wallet.IWallet, phoenixFaucetAmount uint, botName string) *BotEngine {
rootCmd := command.Command{
Emoji: "🤖",
Name: "pagu",
Expand All @@ -118,17 +117,16 @@ func newBotEngine(ctx context.Context, cnl context.CancelFunc, db repository.Dat
marketCmd := market.NewMarket(cm, priceCache)

return &BotEngine{
ctx: ctx,
cancel: cnl,
clientMgr: cm,
rootCmd: rootCmd,
networkCmd: netCmd,
blockchainCmd: bcCmd,
phoenixCmd: ptCmd,
phoenixClientMgr: cm,
zealyCmd: zealyCmd,
voucherCmd: voucherCmd,
marketCmd: marketCmd,
ctx: ctx,
cancel: cnl,
clientMgr: cm,
rootCmd: rootCmd,
networkCmd: netCmd,
blockchainCmd: bcCmd,
phoenixCmd: ptCmd,
zealyCmd: zealyCmd,
voucherCmd: voucherCmd,
marketCmd: marketCmd,
}
}

Expand Down Expand Up @@ -244,12 +242,10 @@ func (be *BotEngine) Stop() {

be.cancel()
be.clientMgr.Stop()
be.phoenixClientMgr.Stop()
}

func (be *BotEngine) Start() {
log.Info("Starting the Bot Engine")

be.clientMgr.Start()
be.phoenixClientMgr.Start()
}
9 changes: 9 additions & 0 deletions internal/entity/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ import (
"gorm.io/gorm"
)

type Role int

const (
Admin Role = 0
Mod Role = 1
BasicUser Role = 2
)

type User struct {
ID uint `gorm:"primaryKey;unique"`
ApplicationID AppID
CallerID string
Role Role

CreatedAt time.Time
UpdatedAt time.Time
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/client_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Mgr struct {
clients []IClient
}

func NewClientMgr(ctx context.Context) *Mgr {
func NewClientMgr(ctx context.Context) Manager {
return &Mgr{
clients: make([]IClient, 0),
valMap: make(map[string]*pactus.PeerInfo),
Expand Down
20 changes: 20 additions & 0 deletions pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,23 @@ type IClient interface {
GetFee(context.Context, int64) (int64, error)
Close() error
}

type Manager interface {
Start()
Stop()
updateValMap()
AddClient(c IClient)
getLocalClient() IClient
GetRandomClient() IClient
GetBlockchainInfo() (*pactus.GetBlockchainInfoResponse, error)
GetBlockchainHeight() (uint32, error)
GetLastBlockTime() (uint32, uint32)
GetNetworkInfo() (*pactus.GetNetworkInfoResponse, error)
GetPeerInfo(address string) (*pactus.PeerInfo, error)
GetValidatorInfo(address string) (*pactus.GetValidatorResponse, error)
GetValidatorInfoByNumber(num int32) (*pactus.GetValidatorResponse, error)
GetTransactionData(txID string) (*pactus.GetTransactionResponse, error)
GetBalance(addr string) (int64, error)
GetFee(amt int64) (int64, error)
GetCirculatingSupply() (int64, error)
}
Loading
Loading