code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/parties_create_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 PartiesCreateTheFollowingReferralCode(referralEngine *referral.Engine, teamsEngine *teams.Engine, table *godog.Table) error {
    30  	ctx := context.Background()
    31  
    32  	for _, r := range parseCreateReferralCodeTable(table) {
    33  		row := newCreateReferralCodeRow(r)
    34  		err := referralEngine.CreateReferralSet(ctx, row.Party(), row.Code())
    35  		if err := checkExpectedError(row, err, nil); err != nil {
    36  			return err
    37  		}
    38  
    39  		if row.IsTeam() {
    40  			team := &commandspb.CreateReferralSet_Team{
    41  				Name:      row.Team(),
    42  				Closed:    row.Closed(),
    43  				AllowList: row.AllowList(),
    44  			}
    45  
    46  			err = teamsEngine.CreateTeam(ctx, row.Party(), types.TeamID(row.Team()), team)
    47  			if err != nil {
    48  				return err
    49  			}
    50  		}
    51  	}
    52  	return nil
    53  }
    54  
    55  func parseCreateReferralCodeTable(table *godog.Table) []RowWrapper {
    56  	return StrictParseTable(table, []string{
    57  		"party",
    58  		"code",
    59  	}, []string{
    60  		"is_team",
    61  		"team",
    62  		"error",
    63  		"reference",
    64  		"closed",
    65  		"allow list",
    66  	})
    67  }
    68  
    69  type createReferralCodeRow struct {
    70  	row RowWrapper
    71  }
    72  
    73  func newCreateReferralCodeRow(r RowWrapper) createReferralCodeRow {
    74  	row := createReferralCodeRow{
    75  		row: r,
    76  	}
    77  	return row
    78  }
    79  
    80  func (r createReferralCodeRow) Party() types.PartyID {
    81  	return types.PartyID(r.row.MustStr("party"))
    82  }
    83  
    84  func (r createReferralCodeRow) Code() types.ReferralSetID {
    85  	return types.ReferralSetID(r.row.MustStr("code"))
    86  }
    87  
    88  func (r createReferralCodeRow) Error() string {
    89  	return r.row.Str("error")
    90  }
    91  
    92  func (r createReferralCodeRow) ExpectError() bool {
    93  	return r.row.HasColumn("error")
    94  }
    95  
    96  func (r createReferralCodeRow) Reference() string {
    97  	return r.row.MustStr("reference")
    98  }
    99  
   100  func (r createReferralCodeRow) 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 createReferralCodeRow) Team() string {
   108  	return r.row.Str("team")
   109  }
   110  
   111  func (r createReferralCodeRow) AllowList() []string {
   112  	if !r.row.HasColumn("allow list") {
   113  		return nil
   114  	}
   115  	return r.row.MustStrSlice("allow list", ",")
   116  }
   117  
   118  func (r createReferralCodeRow) Closed() bool {
   119  	if !r.row.HasColumn("closed") {
   120  		return false
   121  	}
   122  	return r.row.MustBool("closed")
   123  }