github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/resident_country.go (about)

     1  /*
     2   * Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package identity
    19  
    20  import (
    21  	"github.com/mysteriumnetwork/node/core/location/locationstate"
    22  
    23  	"github.com/pkg/errors"
    24  
    25  	"github.com/mysteriumnetwork/node/config"
    26  	"github.com/mysteriumnetwork/node/eventbus"
    27  )
    28  
    29  // AppTopicResidentCountry resident country event topic
    30  const AppTopicResidentCountry = "resident-country"
    31  
    32  type locationProvider interface {
    33  	GetOrigin() locationstate.Location
    34  }
    35  
    36  // ResidentCountry for saving and loading resident country
    37  // resident country is used by legal to pay VAT
    38  type ResidentCountry struct {
    39  	eventBus         eventbus.EventBus
    40  	locationResolver locationProvider
    41  }
    42  
    43  // NewResidentCountry constructor
    44  func NewResidentCountry(eventBus eventbus.EventBus, locationResolver locationProvider) *ResidentCountry {
    45  	return &ResidentCountry{eventBus: eventBus, locationResolver: locationResolver}
    46  }
    47  
    48  // Save country code and fire AppTopicResidentCountry
    49  func (rc *ResidentCountry) Save(identity, countryCode string) error {
    50  	if len(countryCode) == 0 || len(identity) == 0 {
    51  		return errors.New("identity and countryCode are required")
    52  	}
    53  	config.Current.SetUser(config.FlagResidentCountry.Name, countryCode)
    54  	if err := config.Current.SaveUserConfig(); err != nil {
    55  		return err
    56  	}
    57  	rc.publishResidentCountry(identity)
    58  	return nil
    59  }
    60  
    61  // Get get stored resident country
    62  func (rc *ResidentCountry) Get() string {
    63  	stored := config.Current.GetString(config.FlagResidentCountry.Name)
    64  	if len(stored) == 0 {
    65  		return rc.locationResolver.GetOrigin().Country
    66  	}
    67  	return stored
    68  }
    69  
    70  func (rc *ResidentCountry) publishResidentCountry(identity string) {
    71  	country := rc.Get()
    72  	event := ResidentCountryEvent{
    73  		ID:      identity,
    74  		Country: country,
    75  	}
    76  	rc.eventBus.Publish(AppTopicResidentCountry, event)
    77  }