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.
- Loading branch information
Showing
12 changed files
with
1,011 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package pwapi | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"pathwar.land/go/pkg/errcode" | ||
"pathwar.land/go/pkg/pwdb" | ||
) | ||
|
||
func (svc *service) CouponValidate(ctx context.Context, in *CouponValidate_Input) (*CouponValidate_Output, error) { | ||
// validation | ||
if in == nil || in.Hash == "" || in.TeamID == 0 { | ||
return nil, errcode.ErrMissingInput | ||
} | ||
|
||
userID, err := userIDFromContext(ctx, svc.db) | ||
if err != nil { | ||
return nil, fmt.Errorf("get userid from context: %w", err) | ||
} | ||
|
||
// check if user belongs to team | ||
// FIXME: or is admin | ||
var team pwdb.Team | ||
err = svc.db. | ||
Joins("JOIN team_member ON team_member.team_id = team.id AND team_member.user_id = ?", userID). | ||
Preload("Members"). | ||
First(&team, in.TeamID). | ||
Error | ||
if err != nil { | ||
return nil, errcode.TODO.Wrap(err) | ||
} | ||
|
||
// fetch coupon | ||
var coupon pwdb.Coupon | ||
err = svc.db. | ||
Where(pwdb.Coupon{Hash: in.Hash}). | ||
First(&coupon). | ||
Error | ||
if err != nil { | ||
return nil, errcode.TODO.Wrap(err) | ||
} | ||
|
||
var validations int32 | ||
err = svc.db. | ||
Model(&pwdb.CouponValidation{}). | ||
Where(&pwdb.CouponValidation{CouponID: coupon.ID}). | ||
Count(&validations). | ||
Error | ||
if err != nil { | ||
return nil, errcode.TODO.Wrap(err) | ||
} | ||
if validations >= coupon.MaxValidationCount { | ||
return nil, errcode.TODO.Wrap(err) | ||
} | ||
|
||
// FIXME: validate team | ||
// FIXME: already validated by this team | ||
// FIXME: inacitve user/team | ||
|
||
// create validation | ||
validation := pwdb.CouponValidation{ | ||
Comment: "xxx", | ||
AuthorID: userID, | ||
TeamID: team.ID, | ||
CouponID: coupon.ID, | ||
} | ||
err = svc.db.Create(&validation).Error | ||
if err != nil { | ||
return nil, errcode.TODO.Wrap(err) | ||
} | ||
|
||
// FIXME: increment team.cash | ||
|
||
// load it again with preload | ||
err = svc.db. | ||
Preload("Team"). | ||
Preload("Author"). | ||
Preload("Coupon"). | ||
First(&validation, validation.ID). | ||
Error | ||
if err != nil { | ||
return nil, errcode.TODO.Wrap(err) | ||
} | ||
|
||
ret := CouponValidate_Output{ | ||
CouponValidation: &validation, | ||
} | ||
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,57 @@ | ||
package pwapi | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"pathwar.land/go/internal/testutil" | ||
"pathwar.land/go/pkg/errcode" | ||
) | ||
|
||
func TestEngine_CouponValidate(t *testing.T) { | ||
svc, cleanup := TestingService(t, ServiceOpts{Logger: testutil.Logger(t)}) | ||
defer cleanup() | ||
ctx := testingSetContextToken(context.Background(), t) | ||
|
||
// fetch user session | ||
session, err := svc.UserGetSession(ctx, nil) | ||
assert.NoError(t, err) | ||
activeTeam := session.User.ActiveTeamMember.Team | ||
|
||
var tests = []struct { | ||
name string | ||
input *CouponValidate_Input | ||
expectedErr error | ||
}{ | ||
{"nil", nil, errcode.ErrMissingInput}, | ||
{"empty", &CouponValidate_Input{}, errcode.ErrMissingInput}, | ||
// more invalid arguments | ||
// test-coupon-1, invalid season | ||
// {"invalid team ID", &CouponValidate_Input{CouponID: coupons.Items[0].ID, TeamID: 42}, ErrInvalidInput}, | ||
// {"valid 1", &CouponValidate_Input{CouponID: coupons.Items[0].ID, TeamID: activeTeam.ID}, nil}, | ||
// {"valid 2 (duplicate)", &CouponValidate_Input{CouponID: coupons.Items[0].ID, TeamID: activeTeam.ID}, errcode.TODO}, | ||
// FIXME: check for a team and a coupon in different seasons | ||
// FIXME: check for a team from another user | ||
// FIXME: check for a coupon in draft mode | ||
{"test-coupon-1", &CouponValidate_Input{Hash: "test-coupon-1", TeamID: activeTeam.ID}, nil}, | ||
{"test-coupon-1-again", &CouponValidate_Input{Hash: "test-coupon-1", TeamID: activeTeam.ID}, errcode.TODO}, | ||
//{"test-coupon-2", &CouponValidate_Input{Hash: "test-coupon-2", TeamID: activeTeam.ID}, nil}, | ||
{"test-coupon-3-invalid-season", &CouponValidate_Input{Hash: "test-coupon-3", TeamID: activeTeam.ID}, errcode.TODO}, | ||
{"test-coupon-4", &CouponValidate_Input{Hash: "test-coupon-4", TeamID: activeTeam.ID}, nil}, | ||
} | ||
|
||
for _, test := range tests { | ||
ret, err := svc.CouponValidate(ctx, test.input) | ||
testSameErrcodes(t, test.name, test.expectedErr, err) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
validation := ret.CouponValidation | ||
assert.Equalf(t, test.input.Hash, validation.Coupon.Hash, test.name) | ||
assert.Equalf(t, test.input.TeamID, validation.Team.ID, test.name) | ||
assert.Equalf(t, validation.Team.SeasonID, validation.Coupon.SeasonID, test.name) | ||
//fmt.Println(godev.PrettyJSON(ret)) | ||
} | ||
} |
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.