forked from pathwar/pathwar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ChallengeSubscriptionClose + related small fixes & refactors
- Loading branch information
Showing
14 changed files
with
1,099 additions
and
523 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,62 @@ | ||
package pwengine | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"pathwar.land/go/pkg/pwdb" | ||
) | ||
|
||
func (e *engine) ChallengeSubscriptionClose(ctx context.Context, in *ChallengeSubscriptionCloseInput) (*ChallengeSubscriptionCloseOutput, error) { | ||
{ // validation | ||
if in.ChallengeSubscriptionID == 0 { | ||
return nil, ErrMissingArgument | ||
} | ||
// validation | ||
if in == nil || in.ChallengeSubscriptionID == 0 { | ||
return nil, ErrMissingArgument | ||
} | ||
|
||
userID, err := userIDFromContext(ctx, e.db) | ||
if err != nil { | ||
return nil, fmt.Errorf("get userid from context: %w", err) | ||
} | ||
|
||
// fetch subscription | ||
var subscription pwdb.ChallengeSubscription | ||
err = e.db. | ||
Preload("Team"). | ||
Preload("SeasonChallenge"). | ||
Preload("Validations"). | ||
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). | ||
Where(pwdb.ChallengeSubscription{ | ||
Status: pwdb.ChallengeSubscription_Active, | ||
}). | ||
First(&subscription, in.ChallengeSubscriptionID). | ||
Error | ||
if err != nil { | ||
return nil, ErrInvalidArgument // fmt.Errorf("fetch challenge subscription: %w", err) | ||
} | ||
|
||
ret := ChallengeSubscriptionCloseOutput{} | ||
// check for required validations | ||
// FIXME: check for required validation, not just for amount of validations | ||
if len(subscription.Validations) == 0 { | ||
return nil, ErrMissingRequiredValidation | ||
} | ||
|
||
// FIXME: add more validations | ||
|
||
// update challenge subscription | ||
now := time.Now() | ||
err = e.db. | ||
Model(&subscription). | ||
Updates(pwdb.ChallengeSubscription{ | ||
Status: pwdb.ChallengeSubscription_Closed, | ||
ClosedAt: &now, | ||
CloserID: userID, | ||
}).Error | ||
if err != nil { | ||
return nil, fmt.Errorf("update challenge subscription: %w", err) | ||
} | ||
|
||
ret := ChallengeSubscriptionCloseOutput{ChallengeSubscription: &subscription} | ||
return &ret, nil | ||
} |
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,95 @@ | ||
package pwengine | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"pathwar.land/go/internal/testutil" | ||
"pathwar.land/go/pkg/pwdb" | ||
) | ||
|
||
func TestEngine_ChallengeSubscriptionClose(t *testing.T) { | ||
engine, cleanup := TestingEngine(t, Opts{Logger: testutil.Logger(t)}) | ||
defer cleanup() | ||
ctx := testingSetContextToken(context.Background(), t) | ||
|
||
solo := testingSoloSeason(t, engine) | ||
|
||
// fetch challenges | ||
challenges, err := engine.SeasonChallengeList(ctx, &SeasonChallengeListInput{solo.ID}) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
// fetch user session | ||
session, err := engine.UserGetSession(ctx, nil) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
activeTeam := session.User.ActiveTeamMember.Team | ||
|
||
// buy two challenges | ||
subscription1, err := engine.SeasonChallengeBuy(ctx, &SeasonChallengeBuyInput{ | ||
SeasonChallengeID: challenges.Items[0].ID, | ||
TeamID: activeTeam.ID, | ||
}) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
subscription2, err := engine.SeasonChallengeBuy(ctx, &SeasonChallengeBuyInput{ | ||
SeasonChallengeID: challenges.Items[1].ID, | ||
TeamID: activeTeam.ID, | ||
}) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
// validate second challenge | ||
_, err = engine.ChallengeSubscriptionValidate(ctx, &ChallengeSubscriptionValidateInput{ | ||
ChallengeSubscriptionID: subscription2.ChallengeSubscription.ID, | ||
Passphrase: "secret", | ||
}) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
var tests = []struct { | ||
name string | ||
input *ChallengeSubscriptionCloseInput | ||
expectedErr error | ||
}{ | ||
{"nil", nil, ErrMissingArgument}, | ||
{"empty", &ChallengeSubscriptionCloseInput{}, ErrMissingArgument}, | ||
{"subscription1", &ChallengeSubscriptionCloseInput{ChallengeSubscriptionID: subscription1.ChallengeSubscription.ID}, ErrMissingRequiredValidation}, | ||
{"subscription2", &ChallengeSubscriptionCloseInput{ChallengeSubscriptionID: subscription2.ChallengeSubscription.ID}, nil}, | ||
{"subscription2", &ChallengeSubscriptionCloseInput{ChallengeSubscriptionID: subscription2.ChallengeSubscription.ID}, ErrInvalidArgument}, | ||
} | ||
for _, test := range tests { | ||
ret, err := engine.ChallengeSubscriptionClose(ctx, test.input) | ||
if test.expectedErr != err { | ||
t.Errorf("%s: Expected %v, got %v.", test.name, test.expectedErr, err) | ||
} | ||
if err != nil { | ||
continue | ||
} | ||
|
||
if ret.ChallengeSubscription.ClosedAt == nil { | ||
t.Errorf("%s: Expected ClosedAt != nil.", test.name) | ||
} | ||
if ret.ChallengeSubscription.CloserID != session.User.ID { | ||
t.Errorf("%s: Expected %d, got %d.", test.name, session.User.ID, ret.ChallengeSubscription.CloserID) | ||
} | ||
if ret.ChallengeSubscription.Status != pwdb.ChallengeSubscription_Closed { | ||
t.Errorf("%s: Expected %v, got %v.", test.name, pwdb.ChallengeSubscription_Closed, ret.ChallengeSubscription.Status) | ||
} | ||
if ret.ChallengeSubscription.Team.ID != activeTeam.ID { | ||
t.Errorf("%s: Expected %d, got %d.", test.name, activeTeam.ID, ret.ChallengeSubscription.Team.ID) | ||
} | ||
if test.input.ChallengeSubscriptionID != ret.ChallengeSubscription.ID { | ||
t.Errorf("%s: Expected %d, got %d.", test.name, test.input.ChallengeSubscriptionID, ret.ChallengeSubscription.ID) | ||
} | ||
if len(ret.ChallengeSubscription.Validations) == 0 { | ||
t.Errorf("%s: should have at least one validation", test.name) | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.