Skip to content

Commit

Permalink
feat: ChallengeSubscriptionValidate activity
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jun 28, 2020
1 parent 1877337 commit 8a6a25f
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 356 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ linters:
#- gocognit
- goconst
- gocritic
- gocyclo
#- gocyclo
- gofmt
- goimports
- golint
Expand Down
1 change: 1 addition & 0 deletions api/pwdb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ message Activity {
UserSetPreferences = 3;
UserDeleteAccount = 4;
SeasonChallengeBuy = 5;
ChallengeSubscriptionValidate = 6;
}
}

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.

44 changes: 40 additions & 4 deletions go/pkg/pwapi/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package pwapi

import (
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"pathwar.land/pathwar/v2/go/internal/testutil"
"pathwar.land/pathwar/v2/go/pkg/pwdb"
"pathwar.land/pathwar/v2/go/pkg/pwinit"
)

func TestActivity(t *testing.T) {
Expand Down Expand Up @@ -55,13 +57,22 @@ func TestActivity(t *testing.T) {
// FIXME: call UserSetPreferences

// buy challenge
var theChallenge *pwdb.SeasonChallenge
var subscription *SeasonChallengeBuy_Output
{
solo := testingSoloSeason(t, svc)
activeTeam := session.User.ActiveTeamMember.Team
challenges, err := svc.SeasonChallengeList(ctx, &SeasonChallengeList_Input{solo.ID})
require.NoError(t, err)

subscription, err := svc.SeasonChallengeBuy(ctx, &SeasonChallengeBuy_Input{SeasonChallengeID: challenges.Items[0].ID, TeamID: activeTeam.ID})
for _, challenge := range challenges.Items {
if len(challenge.Flavor.Instances) > 0 {
theChallenge = challenge
break
}
}
require.NotNil(t, theChallenge)
subscription, err = svc.SeasonChallengeBuy(ctx, &SeasonChallengeBuy_Input{SeasonChallengeID: theChallenge.ID, TeamID: activeTeam.ID})
require.NoError(t, err)

activities = testingActivities(t, svc)
Expand All @@ -73,7 +84,32 @@ func TestActivity(t *testing.T) {
assert.Equal(t, activity.TeamID, session.User.ActiveTeamMember.Team.ID)
assert.Equal(t, activity.Season.Name, "Solo Mode")
assert.Equal(t, activity.ChallengeSubscriptionID, subscription.ChallengeSubscription.ID)
//assert.Equal(t, activity.SeasonChallengeID)
assert.Equal(t, activity.SeasonChallengeID, subscription.ChallengeSubscription.SeasonChallenge.ID)
}

// validate challenge
{
var configData pwinit.InitConfig
err := json.Unmarshal(theChallenge.Flavor.Instances[0].GetInstanceConfig(), &configData)
require.NoError(t, err)
input := ChallengeSubscriptionValidate_Input{
ChallengeSubscriptionID: subscription.ChallengeSubscription.ID,
Passphrases: configData.Passphrases,
}
_, err = svc.ChallengeSubscriptionValidate(ctx, &input)
require.NoError(t, err)

activities = testingActivities(t, svc)
assert.Len(t, activities.Items, 4)
activity := activities.Items[3]
assert.Equal(t, activity.Kind, pwdb.Activity_ChallengeSubscriptionValidate)
assert.Equal(t, activity.AuthorID, session.User.ID)
assert.Equal(t, activity.ChallengeSubscriptionID, subscription.ChallengeSubscription.ID)
assert.Equal(t, activity.SeasonChallengeID, subscription.ChallengeSubscription.SeasonChallenge.ID)
assert.Equal(t, activity.ChallengeFlavorID, subscription.ChallengeSubscription.SeasonChallenge.Flavor.ID)
assert.Equal(t, activity.Season.Name, "Solo Mode")
assert.Equal(t, activity.TeamID, session.User.ActiveTeamMember.Team.ID)
//fmt.Println(godev.PrettyJSON(activity))
}

// delete account
Expand All @@ -82,8 +118,8 @@ func TestActivity(t *testing.T) {
assert.NoError(t, err)

activities = testingActivities(t, svc)
assert.Len(t, activities.Items, 4)
activity := activities.Items[3]
assert.Len(t, activities.Items, 5)
activity := activities.Items[4]
//fmt.Println(godev.PrettyJSON(activity))
assert.Equal(t, activity.Kind, pwdb.Activity_UserDeleteAccount)
assert.Equal(t, activity.AuthorID, session.User.ID)
Expand Down
95 changes: 56 additions & 39 deletions go/pkg/pwapi/api_challenge-subscription-validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"time"

"github.com/jinzhu/gorm"
"pathwar.land/pathwar/v2/go/pkg/errcode"
"pathwar.land/pathwar/v2/go/pkg/pwdb"
"pathwar.land/pathwar/v2/go/pkg/pwinit"
Expand Down Expand Up @@ -115,35 +116,56 @@ func (svc *service) ChallengeSubscriptionValidate(ctx context.Context, in *Chall
AuthorComment: in.Comment,
Status: pwdb.ChallengeValidation_NeedReview,
}
err = svc.db.Create(&validation).Error
if err != nil {
return nil, errcode.ErrCreateChallengeValidation.Wrap(err)
}

// load freshly inserted entry
err = svc.db.
Preload("Author").
Preload("ChallengeSubscription").
Preload("ChallengeSubscription.SeasonChallenge").
Preload("ChallengeSubscription.Validations").
Preload("ChallengeSubscription.Team").
First(&validation, validation.ID).
Error
if err != nil {
return nil, errcode.ErrGetChallengeValidation.Wrap(err)
}
// update DB
err = svc.db.Transaction(func(tx *gorm.DB) error {
err = tx.Create(&validation).Error
if err != nil {
return errcode.ErrCreateChallengeValidation.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
// update challenge subscription
now := time.Now()
err = tx.
Model(&subscription).
Updates(pwdb.ChallengeSubscription{
Status: pwdb.ChallengeSubscription_Closed,
ClosedAt: &now,
CloserID: userID,
}).Error
if err != nil {
return errcode.ErrUpdateChallengeSubscription.Wrap(err)
}

// mark used instances as needing a redump
usedInstanceIDs := make([]int64, len(usedInstances))
i := 0
for id := range usedInstances {
usedInstanceIDs[i] = id
i++
}
err = tx.
Model(&instances[0]).
Where("id IN (?)", usedInstanceIDs).
Update(pwdb.ChallengeInstance{Status: pwdb.ChallengeInstance_NeedRedump}).
Error
if err != nil {
return errcode.ErrAgentUpdateState.Wrap(err)
}

activity := pwdb.Activity{
Kind: pwdb.Activity_ChallengeSubscriptionValidate,
AuthorID: userID,
ChallengeSubscriptionID: subscription.ID,
TeamID: subscription.TeamID,
SeasonChallengeID: subscription.SeasonChallengeID,
ChallengeFlavorID: subscription.SeasonChallenge.FlavorID,
SeasonID: subscription.SeasonChallenge.SeasonID,
}
return tx.Create(&activity).Error
})
if err != nil {
return nil, errcode.ErrUpdateChallengeSubscription.Wrap(err)
return nil, err
}

// load updated challenge subscription with validations
Expand All @@ -155,23 +177,18 @@ func (svc *service) ChallengeSubscriptionValidate(ctx context.Context, in *Chall
if err != nil {
return nil, pwdb.GormToErrcode(err)
}

// mark used instances as needing a redump
usedInstanceIDs := make([]int64, len(usedInstances))
i := 0
for id := range usedInstances {
usedInstanceIDs[i] = id
i++
}
// load freshly inserted entry
err = svc.db.
Model(&instances[0]).
Where("id IN (?)", usedInstanceIDs).
Update(pwdb.ChallengeInstance{Status: pwdb.ChallengeInstance_NeedRedump}).
Preload("Author").
Preload("ChallengeSubscription").
Preload("ChallengeSubscription.SeasonChallenge").
Preload("ChallengeSubscription.Validations").
Preload("ChallengeSubscription.Team").
First(&validation, validation.ID).
Error
if err != nil {
return nil, errcode.ErrAgentUpdateState.Wrap(err)
return nil, errcode.ErrGetChallengeValidation.Wrap(err)
}

ret := ChallengeSubscriptionValidate_Output{
ChallengeValidation: &validation,
ChallengeSubscription: &subscription,
Expand Down
Loading

0 comments on commit 8a6a25f

Please sign in to comment.