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