Skip to content

Commit

Permalink
fix promocode criticals
Browse files Browse the repository at this point in the history
  • Loading branch information
coddmeistr committed Dec 11, 2024
1 parent 4618b33 commit 4178190
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
github.com/rabbitmq/amqp091-go v1.9.0
github.com/rs/cors v1.10.1
github.com/slntopp/nocloud-proto v0.0.0-20241211083453-89be9bac72b3
github.com/slntopp/nocloud-proto v0.0.0-20241211110413-7e286b620c82
github.com/spf13/viper v1.18.2
github.com/stoewer/go-strcase v1.3.0
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/slntopp/nocloud-proto v0.0.0-20241211083453-89be9bac72b3 h1:ivfNG8ZscFtFzqDtLEfzNXRHxw/j1UED25W9fVw9uMU=
github.com/slntopp/nocloud-proto v0.0.0-20241211083453-89be9bac72b3/go.mod h1:qPbslPB2J9Q7qm6H9Jaqf/Ysf61YlPL0DUFhIdAEikI=
github.com/slntopp/nocloud-proto v0.0.0-20241211110413-7e286b620c82 h1:a+9BOy9OGHKJSQMSkkQ+8+j8hU93KlGPKG0uySj1vsk=
github.com/slntopp/nocloud-proto v0.0.0-20241211110413-7e286b620c82/go.mod h1:qPbslPB2J9Q7qm6H9Jaqf/Ysf61YlPL0DUFhIdAEikI=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
Expand Down
21 changes: 10 additions & 11 deletions pkg/graph/promocodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (c *promocodesController) AddEntry(ctx context.Context, uuid string, entry
return err
}
promo = *applyCurrentStateWithUsed(&promo, entry)
if err = invalidStateError(promo.State); err != nil {
if err = invalidStateError(promo.Condition); err != nil {
_ = db.AbortTransaction(ctx, trID, &driver.AbortTransactionOptions{})
return err
}
Expand Down Expand Up @@ -689,18 +689,17 @@ func findEntry(uses []*pb.EntryResource, entry *pb.EntryResource) (int, bool) {
}

func applyCurrentState(promo *pb.Promocode) *pb.Promocode {
expired := time.Now().Unix() >= promo.GetDueDate()
expired := promo.GetDueDate() > 0 && time.Now().Unix() >= promo.GetDueDate()
taken := int64(len(promo.GetUses())) >= promo.GetLimit()

if taken {
promo.State = pb.PromocodeState_ALL_TAKEN
promo.Condition = pb.PromocodeCondition_ALL_TAKEN
}
if expired {
promo.State = pb.PromocodeState_EXPIRED
promo.Condition = pb.PromocodeCondition_EXPIRED
}

if !taken && !expired {
promo.State = pb.PromocodeState_USABLE
promo.Condition = pb.PromocodeCondition_USABLE
}

return promo
Expand All @@ -716,7 +715,7 @@ func applyCurrentStateWithUsed(promo *pb.Promocode, newEntry *pb.EntryResource)
if use.GetAccount() == newEntryAccount {
current++
if current == maxUsesPerUser {
promo.State = pb.PromocodeState_USED
promo.Condition = pb.PromocodeCondition_USED
break
}
}
Expand All @@ -725,13 +724,13 @@ func applyCurrentStateWithUsed(promo *pb.Promocode, newEntry *pb.EntryResource)
return promo
}

func invalidStateError(state pb.PromocodeState) error {
func invalidStateError(state pb.PromocodeCondition) error {
switch state {
case pb.PromocodeState_EXPIRED:
case pb.PromocodeCondition_EXPIRED:
return fmt.Errorf("promocode is expired")
case pb.PromocodeState_ALL_TAKEN:
case pb.PromocodeCondition_ALL_TAKEN:
return fmt.Errorf("no promocodes left")
case pb.PromocodeState_USED:
case pb.PromocodeCondition_USED:
return fmt.Errorf("already got maximum uses of this promocode")
}
return nil
Expand Down

0 comments on commit 4178190

Please sign in to comment.