From 9d513489055a774961b1442b559a45a04f44ad87 Mon Sep 17 00:00:00 2001 From: Sepehr Dehghani <55226923+sepehr-dh99@users.noreply.github.com> Date: Tue, 14 Jan 2025 20:57:42 +0330 Subject: [PATCH] fix: check voucher code expiry (#267) --- internal/engine/command/voucher/claim.go | 3 +-- internal/engine/command/voucher/create.go | 2 +- internal/engine/command/voucher/create_test.go | 8 ++++---- internal/engine/command/voucher/voucher.go | 8 ++++---- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/internal/engine/command/voucher/claim.go b/internal/engine/command/voucher/claim.go index d16bf77..1f13844 100644 --- a/internal/engine/command/voucher/claim.go +++ b/internal/engine/command/voucher/claim.go @@ -25,8 +25,7 @@ func (v *VoucherCmd) claimHandler( return cmd.ErrorResult(errors.New("voucher code is not valid, no voucher found")) } - now := time.Now().Month() - if voucher.CreatedAt.Month() >= (now + time.Month(voucher.ValidMonths)) { + if voucher.CreatedAt.AddDate(0, int(voucher.ValidMonths), 0).Before(time.Now()) { return cmd.ErrorResult(errors.New("voucher is expired")) } diff --git a/internal/engine/command/voucher/create.go b/internal/engine/command/voucher/create.go index 096bee5..ab650a3 100644 --- a/internal/engine/command/voucher/create.go +++ b/internal/engine/command/voucher/create.go @@ -26,7 +26,7 @@ type BulkRecorder struct { Description string `csv:"Description"` } -func (v *VoucherCmd) createOneHandler( +func (v *VoucherCmd) createHandler( caller *entity.User, cmd *command.Command, args map[string]string, diff --git a/internal/engine/command/voucher/create_test.go b/internal/engine/command/voucher/create_test.go index ad4169b..7335c18 100644 --- a/internal/engine/command/voucher/create_test.go +++ b/internal/engine/command/voucher/create_test.go @@ -21,7 +21,7 @@ func TestCreateOne(t *testing.T) { "valid-months": "1", } - result := td.voucherCmd.createOneHandler(caller, cmd, args) + result := td.voucherCmd.createHandler(caller, cmd, args) assert.False(t, result.Successful) assert.Contains(t, result.Message, "stake amount is more than 1000") }) @@ -32,7 +32,7 @@ func TestCreateOne(t *testing.T) { "valid-months": "1.1", } - result := td.voucherCmd.createOneHandler(caller, cmd, args) + result := td.voucherCmd.createHandler(caller, cmd, args) assert.False(t, result.Successful) }) @@ -42,7 +42,7 @@ func TestCreateOne(t *testing.T) { "valid-months": "1", } - result := td.voucherCmd.createOneHandler(caller, cmd, args) + result := td.voucherCmd.createHandler(caller, cmd, args) assert.True(t, result.Successful) assert.Contains(t, result.Message, "Voucher created successfully!") }) @@ -55,7 +55,7 @@ func TestCreateOne(t *testing.T) { "description": "Testnet node", } - result := td.voucherCmd.createOneHandler(caller, cmd, args) + result := td.voucherCmd.createHandler(caller, cmd, args) assert.True(t, result.Successful) assert.Contains(t, result.Message, "Voucher created successfully!") }) diff --git a/internal/engine/command/voucher/voucher.go b/internal/engine/command/voucher/voucher.go index 38e59cd..6e6494b 100644 --- a/internal/engine/command/voucher/voucher.go +++ b/internal/engine/command/voucher/voucher.go @@ -49,8 +49,8 @@ func (v *VoucherCmd) GetCommand() *command.Command { TargetFlag: command.TargetMaskMainnet, } - subCmdCreateOne := &command.Command{ - Name: "create-one", + subCmdCreate := &command.Command{ + Name: "create", Help: "Generate a single voucher code", Args: []*command.Args{ { @@ -81,7 +81,7 @@ func (v *VoucherCmd) GetCommand() *command.Command { SubCommands: nil, AppIDs: []entity.PlatformID{entity.PlatformIDDiscord}, Middlewares: []command.MiddlewareFunc{middlewareHandler.OnlyModerator}, - Handler: v.createOneHandler, + Handler: v.createHandler, TargetFlag: command.TargetMaskModerator, } @@ -138,7 +138,7 @@ func (v *VoucherCmd) GetCommand() *command.Command { } cmdVoucher.AddSubCommand(subCmdClaim) - cmdVoucher.AddSubCommand(subCmdCreateOne) + cmdVoucher.AddSubCommand(subCmdCreate) cmdVoucher.AddSubCommand(subCmdCreateBulk) cmdVoucher.AddSubCommand(subCmdStatus)