Пример #1
0
// return target State
func (sm *StateMachine) stateFactory(key string) (*State, error) {
	blueprint, ok := sm.BluePrints[key]
	if !ok {
		e := "StateMachineError: " + key + " is not a valid state."
		log.Println("Error:", e)
		return nil, errors.New(e)
	}

	return &State{
		CreatedAt:      utils.TimeNow(),
		LastModifiedAt: utils.TimeNow(),
		Type:           blueprint.Type,
		Key:            blueprint.Key,
		Description:    blueprint.Description,
	}, nil
}
Пример #2
0
func NewVersion() *Version {
	return &Version{
		Current:        0,
		Previous:       0,
		LastModifiedAt: utils.TimeNow(),
	}
}
Пример #3
0
// 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

}
Пример #4
0
// FreezeCustomer associates the current version of the customer with the order
// Changes on customer after freeze are no longer considered for this order.
func (order *Order) FreezeCustomer() error {
	if order.IsFrozenCustomer() {
		return errors.New("Customer version has already been frozen! Use UnfreezeCustomer() is necessary")
	}
	if !order.HasCustomer() {
		return errors.New("No customer is associated to this order yet!")
	}
	customer, err := order.GetCustomer(nil)
	if err != nil {
		return err
	}
	order.CustomerFreeze = &Freeze{
		Version: customer.GetVersion().Current,
		Time:    utils.TimeNow(),
	}
	return nil
}
Пример #5
0
func (order *Order) SetModified() error {
	order.LastModifiedAt = utils.TimeNow()
	return order.Upsert()
}
Пример #6
0
func (order *Order) SetCompleted() error {
	order.CompletedAt = utils.TimeNow()
	return order.Upsert()
}
Пример #7
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)
}
Пример #8
0
func (customer *Customer) SetModified() error {
	customer.LastModifiedAt = utils.TimeNow()
	return customer.Upsert()
}
Пример #9
0
func NewEvent() *Event {
	return &Event{
		Info:      &Info{},
		Timestamp: utils.TimeNow(),
	}
}
Пример #10
0
func (st *State) SetModified() {
	st.LastModifiedAt = utils.TimeNow()
}
Пример #11
0
func (v *Version) Increment() {
	v.Previous = v.Current
	v.Current = v.Current + 1
	v.LastModifiedAt = utils.TimeNow()
}