Example #1
0
File: order.go Project: foomo/shop
// NewOrderWithCustomId creates a new Order in the database and returns it.
// With orderIdFunc, an optional method can be specified to generate the orderId. If nil, a default algorithm is used.
func NewOrderWithCustomId(customProvider OrderCustomProvider, orderIdFunc func() (string, error)) (*Order, error) {
	var orderId string
	if orderIdFunc != nil {
		var err error
		orderId, err = orderIdFunc()
		if err != nil {
			return nil, err
		}
	} else {
		orderId = unique.GetNewID()
	}
	order := &Order{
		State:          DefaultStateMachine.GetInitialState(),
		Flags:          &Flags{},
		CartId:         unique.GetNewID(),
		Id:             orderId,
		Version:        version.NewVersion(),
		CreatedAt:      utils.TimeNow(),
		LastModifiedAt: utils.TimeNow(),
		CustomerFreeze: &Freeze{},
		CustomerData:   &CustomerData{},
		OrderType:      OrderTypeOrder,
		Positions:      []*Position{},
		Payment:        &payment.Payment{},
		PriceInfo:      &OrderPriceInfo{},
		Shipping:       &shipping.ShippingProperties{},
	}

	if customProvider != nil {
		order.Custom = customProvider.NewOrderCustom()
	}

	// Store order in database
	err := order.insert()
	// Retrieve order again from. (Otherwise upserts on order would fail because of missing mongo ObjectID)
	order, err = GetOrderById(order.Id, customProvider)
	return order, err

}
Example #2
0
// CreateCustomerCredentials
func CreateCustomerCredentials(email, password string) error {
	if password == "" {
		log.Println("WARNING: Empty password is reserved for guest customer (and will not grant access).")
	}
	available, err := CheckLoginAvailable(lc(email))
	if err != nil {
		return err
	}
	if !available {
		return errors.New(lc(email) + " is already taken!")
	}
	crypto, err := crypto.HashPassword(password)
	if err != nil {
		return err
	}
	credentials := &CustomerCredentials{
		Version: version.NewVersion(),
		Email:   lc(email),
		Crypto:  crypto,
	}
	p := GetCredentialsPersistor()
	return p.GetCollection().Insert(credentials)

}
Example #3
0
// NewCustomer creates a new Customer in the database and returns it.
// Email must be unique for a customer. customerProvider may be nil at this point.
func NewCustomer(email, password string, customProvider CustomerCustomProvider) (*Customer, error) {
	log.Println("=== Creating new customer ", email)
	isGuest := password == ""
	if email == "" {
		return nil, errors.New(shop_error.ErrorRequiredFieldMissing)
	}
	//var err error
	// We only create credentials if a customer is available.
	// A guest customer gets a new entry in the customer db for each order!
	// if !isGuest {
	// 	// Check is desired Email is available
	// 	available, err := CheckLoginAvailable(email)
	// 	if err != nil {
	// 		return nil, err
	// 	}
	// 	if !available {
	// 		return nil, errors.New(shop_error.ErrorNotFound + " Login " + email + " is already taken!")
	// 	}

	// 	// These credentials are not used at the moment
	// 	// err = CreateCustomerCredentials(email, password)
	// 	// if err != nil {
	// 	// 	return nil, err
	// 	// }
	// }

	customer := &Customer{
		Flags:          &Flags{},
		Version:        version.NewVersion(),
		Id:             unique.GetNewID(),
		Email:          utils.IteString(isGuest, "", lc(email)), // If Customer is a guest, we do not set the email address. This field should be unique in the database (and would not be if the guest ordered twice).
		CreatedAt:      utils.TimeNow(),
		LastModifiedAt: utils.TimeNow(),
		Person: &address.Person{
			Contacts: &address.Contacts{
				Email: email,
			},
		},
		Localization: &Localization{},
		Tracking:     &Tracking{},
	}

	trackingId, err := crypto.CreateHash(customer.GetID())
	if err != nil {
		return nil, err
	}
	customer.Tracking.TrackingID = "tid" + trackingId

	if isGuest {
		customer.IsGuest = true
	}

	if customProvider != nil {
		customer.Custom = customProvider.NewCustomerCustom()
	}
	// Store order in database
	err = customer.insert()
	if err != nil {
		log.Println("Could not insert customer", email)
		return nil, err
	}
	// Retrieve customer again from. (Otherwise upserts on customer would fail because of missing mongo ObjectID)
	return GetCustomerById(customer.Id, customProvider)
}