// AddAddress adds a new address to the customers profile and returns its unique id func (customer *Customer) AddAddress(addr *address.Address) (string, error) { err := CheckRequiredAddressFields(addr) if err != nil { log.Println("Error", err) return "", err } // Create a unique id for this address addr.Id = unique.GetNewID() // Prevent nil pointer in case we get an incomplete address if addr.Person == nil { addr.Person = &address.Person{ Contacts: &address.Contacts{}, } } else if addr.Person.Contacts == nil { addr.Person.Contacts = &address.Contacts{} } // If Person of Customer is still empty and this is the first address // added to the customer, Person of Address is adopted for Customer if len(customer.Addresses) == 0 && customer.Person.LastName == "" { *customer.Person = *addr.Person } customer.Addresses = append(customer.Addresses, addr) // If this is the first added Address, it's set as billing address if addr.Type == address.AddressDefaultBilling || len(customer.Addresses) == 1 { err := customer.SetDefaultBillingAddress(addr.Id) if err != nil { return addr.Id, err } } // If this is the first added Address, it's set as shipping address if addr.Type == address.AddressDefaultShipping { err := customer.SetDefaultShippingAddress(addr.Id) if err != nil { return addr.Id, err } } return addr.Id, customer.Upsert() }