code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/parties_apply_the_following_referral_codes.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package steps 17 18 import ( 19 "context" 20 21 "code.vegaprotocol.io/vega/core/referral" 22 "code.vegaprotocol.io/vega/core/teams" 23 "code.vegaprotocol.io/vega/core/types" 24 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 25 26 "github.com/cucumber/godog" 27 ) 28 29 func PartiesApplyTheFollowingReferralCode(referralEngine *referral.Engine, teamsEngine *teams.Engine, table *godog.Table) error { 30 ctx := context.Background() 31 32 for _, r := range parseApplyReferralCodeTable(table) { 33 row := newApplyReferralCodeRow(r) 34 err := referralEngine.ApplyReferralCode(ctx, row.Party(), row.Code()) 35 if checkErr := checkExpectedError(row, err, nil); checkErr != nil { 36 if !row.IsTeam() { 37 return checkErr 38 } 39 err = checkErr 40 } 41 // If we have team details, submit a new team 42 if row.IsTeam() { 43 team := &commandspb.JoinTeam{ 44 Id: row.Team(), 45 } 46 if joinErr := teamsEngine.JoinTeam(ctx, row.Party(), team); joinErr != nil { 47 err = checkExpectedError(row, joinErr, nil) 48 } 49 } 50 if err != nil { 51 return err 52 } 53 } 54 return nil 55 } 56 57 func parseApplyReferralCodeTable(table *godog.Table) []RowWrapper { 58 return StrictParseTable(table, []string{ 59 "party", 60 "code", 61 }, []string{ 62 "error", 63 "reference", 64 "is_team", 65 "team", 66 }) 67 } 68 69 type applyReferralCodeRow struct { 70 row RowWrapper 71 } 72 73 func newApplyReferralCodeRow(r RowWrapper) applyReferralCodeRow { 74 row := applyReferralCodeRow{ 75 row: r, 76 } 77 return row 78 } 79 80 func (r applyReferralCodeRow) Party() types.PartyID { 81 return types.PartyID(r.row.MustStr("party")) 82 } 83 84 func (r applyReferralCodeRow) Code() types.ReferralSetID { 85 return types.ReferralSetID(r.row.MustStr("code")) 86 } 87 88 func (r applyReferralCodeRow) Error() string { 89 return r.row.Str("error") 90 } 91 92 func (r applyReferralCodeRow) ExpectError() bool { 93 return r.row.HasColumn("error") 94 } 95 96 func (r applyReferralCodeRow) Reference() string { 97 return r.row.MustStr("reference") 98 } 99 100 func (r applyReferralCodeRow) IsTeam() bool { 101 if !r.row.HasColumn("is_team") { 102 return false 103 } 104 return r.row.Bool("is_team") 105 } 106 107 func (r applyReferralCodeRow) Team() string { 108 return r.row.Str("team") 109 }