mirror of
https://github.com/foomo/shop.git
synced 2025-10-16 12:35:39 +00:00
customer: NewCustomer has optional email address, update pkg/errors (#19)
If the email address is set on NewCustomer, it must be a valid one. updating pkg/errors keeps the error pkg in sync with other projects.
This commit is contained in:
parent
e9c774b013
commit
5e17e81a7f
@ -89,8 +89,9 @@ type CustomerCustomProvider interface {
|
||||
//------------------------------------------------------------------
|
||||
|
||||
// NewCustomer creates a new customer in the database and returns it.
|
||||
// addrkey must be unique for a customer
|
||||
// customerProvider may be nil at this point.
|
||||
// addrkey must be unique for a customer.
|
||||
// customerProvider is required.
|
||||
// mailContact can be nil, if not nil a valid email address is required.
|
||||
func NewCustomer(addrkey string, addrkeyHash string, externalID string, mailContact *address.Contact, customProvider CustomerCustomProvider) (*Customer, error) {
|
||||
var mErr *multierror.Error
|
||||
|
||||
@ -103,10 +104,8 @@ func NewCustomer(addrkey string, addrkeyHash string, externalID string, mailCont
|
||||
if externalID == "" {
|
||||
mErr = multierror.Append(mErr, errors.New("required externalID is empty"))
|
||||
}
|
||||
if mailContact == nil {
|
||||
mErr = multierror.Append(mErr, errors.New("required mailContact is empty"))
|
||||
} else {
|
||||
if mailContact.Value == "" {
|
||||
if mailContact != nil {
|
||||
if !strings.ContainsRune(mailContact.Value, '@') {
|
||||
mErr = multierror.Append(mErr, errors.New("required email address in mailContact.Value is empty"))
|
||||
}
|
||||
if !mailContact.IsMail() {
|
||||
@ -121,7 +120,19 @@ func NewCustomer(addrkey string, addrkeyHash string, externalID string, mailCont
|
||||
return nil, mErr.ErrorOrNil()
|
||||
}
|
||||
|
||||
email := lc(mailContact.Value)
|
||||
var person *address.Person
|
||||
var email string
|
||||
if mailContact != nil {
|
||||
email = lc(mailContact.Value)
|
||||
person = &address.Person{
|
||||
Contacts: map[string]*address.Contact{
|
||||
mailContact.ID: mailContact,
|
||||
},
|
||||
DefaultContacts: map[address.ContactType]string{
|
||||
address.ContactTypeEmail: mailContact.ID,
|
||||
},
|
||||
}
|
||||
}
|
||||
customer := &Customer{
|
||||
Version: version.NewVersion(),
|
||||
Id: unique.GetNewID(),
|
||||
@ -132,28 +143,20 @@ func NewCustomer(addrkey string, addrkeyHash string, externalID string, mailCont
|
||||
IsGuest: false,
|
||||
CreatedAt: utils.TimeNow(),
|
||||
LastModifiedAt: utils.TimeNow(),
|
||||
Person: &address.Person{
|
||||
Contacts: map[string]*address.Contact{
|
||||
mailContact.ID: mailContact,
|
||||
},
|
||||
DefaultContacts: map[address.ContactType]string{
|
||||
address.ContactTypeEmail: mailContact.ID,
|
||||
},
|
||||
},
|
||||
Localization: &Localization{},
|
||||
Custom: customProvider.NewCustomerCustom(),
|
||||
Person: person,
|
||||
Localization: &Localization{},
|
||||
Custom: customProvider.NewCustomerCustom(),
|
||||
}
|
||||
|
||||
// initial version should be 1
|
||||
customer.Version.Increment()
|
||||
|
||||
// persist customer in database
|
||||
errInsert := customer.insert()
|
||||
if errInsert != nil {
|
||||
if mgo.IsDup(errInsert) {
|
||||
if err := customer.insert(); err != nil {
|
||||
if mgo.IsDup(err) {
|
||||
return nil, shop_error.ErrorDuplicateKey
|
||||
}
|
||||
return nil, errInsert
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// retrieve customer again from database,
|
||||
|
||||
@ -278,7 +278,7 @@ func TestNewCustomer(t *testing.T) {
|
||||
customProvider: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: "5 errors occurred:\n\t* required addrkey is empty\n\t* required addrkeyHash is empty\n\t* required externalID is empty\n\t* required mailContact is empty\n\t* custom provider not set\n\n",
|
||||
wantErr: "4 errors occurred:\n\t* required addrkey is empty\n\t* required addrkeyHash is empty\n\t* required externalID is empty\n\t* custom provider not set\n\n",
|
||||
},
|
||||
{
|
||||
name: "all fields empty except mail contact",
|
||||
|
||||
2
go.mod
2
go.mod
@ -10,7 +10,7 @@ require (
|
||||
github.com/mitchellh/mapstructure v1.3.0
|
||||
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
|
||||
github.com/pkg/errors v0.8.1
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/prometheus/client_golang v1.6.0
|
||||
github.com/satori/go.uuid v1.2.0
|
||||
github.com/sergi/go-diff v1.1.0
|
||||
|
||||
2
go.sum
2
go.sum
@ -66,6 +66,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
|
||||
|
||||
Loading…
Reference in New Issue
Block a user