Skip to content

Commit

Permalink
fix: merge api challenge subscription close into api challenge subscr…
Browse files Browse the repository at this point in the history
…iption validate call
  • Loading branch information
Z-a-r-a-k-i committed Jun 11, 2020
1 parent 86b841a commit b3b1e32
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 959 deletions.
2 changes: 2 additions & 0 deletions AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions api/pwapi.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ service Service {
rpc SeasonChallengeGet(SeasonChallengeGet.Input) returns (SeasonChallengeGet.Output) { option (google.api.http) = {get: "/season-challenge"}; };
rpc SeasonChallengeBuy(SeasonChallengeBuy.Input) returns (SeasonChallengeBuy.Output) { option (google.api.http) = {post: "/season-challenge/buy"; body: "*"}; };
rpc ChallengeSubscriptionValidate(ChallengeSubscriptionValidate.Input) returns (ChallengeSubscriptionValidate.Output) { option (google.api.http) = {post: "/challenge-subscription/validate"; body: "*"}; };
rpc ChallengeSubscriptionClose(ChallengeSubscriptionClose.Input) returns (ChallengeSubscriptionClose.Output) { option (google.api.http) = {post: "/challenge-subscription/close"; body: "*"}; };

//
// Organization
Expand Down Expand Up @@ -233,15 +232,7 @@ message ChallengeSubscriptionValidate {
}
message Output {
pathwar.db.ChallengeValidation challenge_validation = 1;
}
}

message ChallengeSubscriptionClose {
message Input {
int64 challenge_subscription_id = 1 [(gogoproto.customname) = "ChallengeSubscriptionID"];
}
message Output {
pathwar.db.ChallengeSubscription challenge_subscription = 1;
pathwar.db.ChallengeSubscription challenge_subscription = 2;
}
}

Expand Down
2 changes: 1 addition & 1 deletion docs/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 0 additions & 63 deletions go/pkg/pwapi/api_challenge-subscription-close.go

This file was deleted.

91 changes: 0 additions & 91 deletions go/pkg/pwapi/api_challenge-subscription-close_test.go

This file was deleted.

37 changes: 31 additions & 6 deletions go/pkg/pwapi/api_challenge-subscription-validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"time"

"pathwar.land/pathwar/v2/go/pkg/errcode"
"pathwar.land/pathwar/v2/go/pkg/pwdb"
Expand All @@ -24,26 +25,26 @@ func (svc *service) ChallengeSubscriptionValidate(ctx context.Context, in *Chall

// check input challenge subscription
// FIXME: or is admin
var challengeSubscription pwdb.ChallengeSubscription
var subscription pwdb.ChallengeSubscription
err = svc.db.
Preload("Team", "team.deletion_status = ?", pwdb.DeletionStatus_Active).
Preload("SeasonChallenge").
Preload("SeasonChallenge.Flavor").
Preload("SeasonChallenge.Flavor.Instances").
Joins("JOIN team ON team.id = challenge_subscription.team_id").
Joins("JOIN team_member ON team_member.team_id = team.id AND team_member.user_id = ?", userID).
First(&challengeSubscription, in.ChallengeSubscriptionID).
First(&subscription, in.ChallengeSubscriptionID).
Error
if err != nil {
return nil, errcode.ErrGetChallengeSubscription.Wrap(err)
}

// check if challenge subscription is still open
if challengeSubscription.Status != pwdb.ChallengeSubscription_Active {
if subscription.Status != pwdb.ChallengeSubscription_Active {
return nil, errcode.ErrChallengeInactiveValidation.Wrap(errors.New("challenge is disabled"))
}

instances := challengeSubscription.SeasonChallenge.Flavor.Instances
instances := subscription.SeasonChallenge.Flavor.Instances
if len(instances) == 0 {
return nil, errcode.ErrChallengeInactiveValidation.Wrap(errors.New("challenge has no instances"))
}
Expand Down Expand Up @@ -119,7 +120,7 @@ func (svc *service) ChallengeSubscriptionValidate(ctx context.Context, in *Chall
return nil, errcode.ErrCreateChallengeValidation.Wrap(err)
}

// load and return the freshly inserted entry
// load freshly inserted entry
err = svc.db.
Preload("Author").
Preload("ChallengeSubscription").
Expand All @@ -132,6 +133,29 @@ func (svc *service) ChallengeSubscriptionValidate(ctx context.Context, in *Chall
return nil, errcode.ErrGetChallengeValidation.Wrap(err)
}

// update challenge subscription
now := time.Now()
err = svc.db.
Model(&subscription).
Updates(pwdb.ChallengeSubscription{
Status: pwdb.ChallengeSubscription_Closed,
ClosedAt: &now,
CloserID: userID,
}).Error
if err != nil {
return nil, errcode.ErrUpdateChallengeSubscription.Wrap(err)
}

// load updated challenge subscription with validations
err = svc.db.
Preload("Validations").
Where(pwdb.ChallengeSubscription{ID: subscription.ID}).
First(&subscription).
Error
if err != nil {
return nil, pwdb.GormToErrcode(err)
}

// mark used instances as needing a redump
usedInstanceIDs := make([]int64, len(usedInstances))
i := 0
Expand All @@ -149,7 +173,8 @@ func (svc *service) ChallengeSubscriptionValidate(ctx context.Context, in *Chall
}

ret := ChallengeSubscriptionValidate_Output{
ChallengeValidation: &validation,
ChallengeValidation: &validation,
ChallengeSubscription: &subscription,
}
return &ret, nil
}
7 changes: 7 additions & 0 deletions go/pkg/pwapi/api_challenge-subscription-validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestSvc_ChallengeSubscriptionValidate(t *testing.T) {
SeasonChallengeID: challenges.Items[9].ID,
TeamID: activeTeam.ID,
})
require.NoError(t, err)

var tests = []struct {
name string
Expand Down Expand Up @@ -72,6 +73,12 @@ func TestSvc_ChallengeSubscriptionValidate(t *testing.T) {
assert.Equalf(t, test.input.Comment, ret.ChallengeValidation.AuthorComment, test.name)
assert.Equalf(t, test.expectedPassphraseIndices, ret.ChallengeValidation.Passphrases, test.name)
assert.NotEmptyf(t, ret.ChallengeValidation.ChallengeSubscription.Validations, test.name)
assert.NotNilf(t, ret.ChallengeSubscription.ClosedAt, test.name)
assert.Equalf(t, session.User.ID, ret.ChallengeSubscription.CloserID, test.name)
assert.Equalf(t, pwdb.ChallengeSubscription_Closed, ret.ChallengeSubscription.Status, test.name)
assert.Equalf(t, activeTeam.ID, ret.ChallengeSubscription.Team.ID, test.name)
assert.Equalf(t, test.input.ChallengeSubscriptionID, ret.ChallengeSubscription.ID, test.name)
assert.NotEmptyf(t, ret.ChallengeSubscription.Validations, test.name)

{
challenge, err := svc.SeasonChallengeGet(ctx, &SeasonChallengeGet_Input{SeasonChallengeID: ret.ChallengeValidation.ChallengeSubscription.SeasonChallenge.ID})
Expand Down
Loading

0 comments on commit b3b1e32

Please sign in to comment.