flamingo.me/flamingo-commerce/v3@v3.11.0/cart/interfaces/controller/forms/basics.go (about)

     1  package forms
     2  
     3  import (
     4  	"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
     5  	"flamingo.me/flamingo-commerce/v3/customer/domain"
     6  )
     7  
     8  type (
     9  	// AddressForm defines the checkout address form data
    10  	AddressForm struct {
    11  		Vat              string `form:"vat"`
    12  		Firstname        string `form:"firstname" validate:"required" conform:"ucfirst,trim"`
    13  		Lastname         string `form:"lastname" validate:"required" conform:"ucfirst,trim"`
    14  		MiddleName       string `form:"middlename" conform:"ucfirst,trim"`
    15  		Title            string `form:"title" conform:"trim"`
    16  		Salutation       string `form:"salutation" conform:"trim"`
    17  		Street           string `form:"street" conform:"ucfirst,trim"`
    18  		StreetNr         string `form:"streetNr" conform:"trim"`
    19  		AddressLine1     string `form:"addressLine1" conform:"trim"`
    20  		AddressLine2     string `form:"addressLine2" conform:"trim"`
    21  		Company          string `form:"company" conform:"trim"`
    22  		PostCode         string `form:"postCode" conform:"trim"`
    23  		City             string `form:"city" conform:"ucfirst,trim"`
    24  		State            string `form:"state" conform:"ucfirst,trim"`
    25  		RegionCode       string `form:"regionCode" conform:"trim"`
    26  		Country          string `form:"country" conform:"ucfirst,trim"`
    27  		CountryCode      string `form:"countryCode" conform:"trim"`
    28  		PhoneAreaCode    string `form:"phoneAreaCode" conform:"trim"`
    29  		PhoneCountryCode string `form:"phoneCountryCode" conform:"trim"`
    30  		PhoneNumber      string `form:"phoneNumber" conform:"trim"`
    31  		Email            string `form:"email" validate:"required,email" conform:"trim,lowercase"`
    32  	}
    33  )
    34  
    35  // MapToDomainAddress - returns the cart Address Object
    36  func (a *AddressForm) MapToDomainAddress() cart.Address {
    37  	lines := make([]string, 2)
    38  	lines[0] = a.AddressLine1
    39  	lines[1] = a.AddressLine2
    40  
    41  	return cart.Address{
    42  		Vat:                    a.Vat,
    43  		Firstname:              a.Firstname,
    44  		Lastname:               a.Lastname,
    45  		MiddleName:             a.MiddleName,
    46  		Title:                  a.Title,
    47  		Salutation:             a.Salutation,
    48  		Street:                 a.Street,
    49  		StreetNr:               a.StreetNr,
    50  		AdditionalAddressLines: lines,
    51  		Company:                a.Company,
    52  		PostCode:               a.PostCode,
    53  		City:                   a.City,
    54  		State:                  a.State,
    55  		RegionCode:             a.RegionCode,
    56  		Country:                a.Country,
    57  		CountryCode:            a.CountryCode,
    58  		Email:                  a.Email,
    59  		TelephoneCountryCode:   a.PhoneCountryCode,
    60  		TelephoneAreaCode:      a.PhoneAreaCode,
    61  		TelephoneNumber:        a.PhoneNumber,
    62  		Telephone:              a.PhoneCountryCode + a.PhoneAreaCode + a.PhoneNumber,
    63  	}
    64  }
    65  
    66  // LoadFromCustomerAddress - fills the form from data in the address object (from customer module)
    67  func (a *AddressForm) LoadFromCustomerAddress(address domain.Address) {
    68  
    69  	if a.Email == "" || a.Email == "@" {
    70  		a.Email = address.Email
    71  	}
    72  	if a.Firstname == "" {
    73  		a.Firstname = address.Firstname
    74  	}
    75  	if a.Lastname == "" {
    76  		a.Lastname = address.Lastname
    77  	}
    78  	if a.CountryCode == "" {
    79  		a.CountryCode = address.CountryCode
    80  	}
    81  	if a.PhoneNumber == "" {
    82  		a.PhoneNumber = address.Telephone
    83  	}
    84  
    85  	if a.Street == "" && a.City == "" {
    86  		a.Street = address.Street
    87  		a.StreetNr = address.StreetNr
    88  		a.City = address.City
    89  	}
    90  
    91  }
    92  
    93  // LoadFromCartAddress - loads the form data from cart address
    94  func (a *AddressForm) LoadFromCartAddress(address cart.Address) {
    95  	if address.Firstname != "" {
    96  		a.Firstname = address.Firstname
    97  	}
    98  	if address.PostCode != "" {
    99  		a.PostCode = address.PostCode
   100  	}
   101  
   102  	if address.State != "" {
   103  		a.State = address.State
   104  	}
   105  
   106  	if len(address.AdditionalAddressLines) > 0 {
   107  		a.AddressLine1 = address.AdditionalAddressLines[0]
   108  	}
   109  	if len(address.AdditionalAddressLines) > 1 {
   110  		a.AddressLine2 = address.AdditionalAddressLines[1]
   111  	}
   112  
   113  	if address.Lastname != "" {
   114  		a.Lastname = address.Lastname
   115  	}
   116  
   117  	if address.Email != "" {
   118  		a.Email = address.Email
   119  	}
   120  
   121  	if address.Street != "" {
   122  		a.Street = address.Street
   123  	}
   124  
   125  	if address.StreetNr != "" {
   126  		a.StreetNr = address.StreetNr
   127  	}
   128  
   129  	if address.Title != "" {
   130  		a.Title = address.Title
   131  	}
   132  	if address.Salutation != "" {
   133  		a.Title = address.Salutation
   134  	}
   135  
   136  	if address.City != "" {
   137  		a.City = address.City
   138  	}
   139  
   140  	//nolint:staticcheck // deprecated since 10.08
   141  	if address.Telephone != "" {
   142  		a.PhoneNumber = address.Telephone
   143  	}
   144  
   145  	if address.TelephoneCountryCode != "" {
   146  		a.PhoneCountryCode = address.TelephoneCountryCode
   147  	}
   148  
   149  	if address.TelephoneAreaCode != "" {
   150  		a.PhoneAreaCode = address.TelephoneAreaCode
   151  	}
   152  
   153  	// Overrides if used new field
   154  	if address.TelephoneNumber != "" {
   155  		a.PhoneNumber = address.TelephoneNumber
   156  	}
   157  
   158  	if address.CountryCode != "" {
   159  		a.CountryCode = address.CountryCode
   160  	}
   161  
   162  	if address.Company != "" {
   163  		a.Company = address.Company
   164  	}
   165  }