コード例 #1
0
ファイル: watchlist.go プロジェクト: foomo/shop
func (cw *CustomerWatchLists) EditList(listId string, name string, public bool, recipient string, targetDate string, description string) (*WatchList, error) {
	for _, list := range cw.Lists {
		if list.Id == listId {
			if list.Name != "" {
				list.Name = name
			}
			if list.Recipient != "" {
				list.Recipient = recipient
			}
			if list.TargetDate != "" {
				list.TargetDate = targetDate
			}
			if list.Description != "" {
				list.Description = description
			}
			if public {
				if list.PublicURIHash == "" {
					list.PublicURIHash = unique.GetNewID()
				}
			} else {
				list.PublicURIHash = ""
			}
			err := cw.Upsert()
			if err != nil {
				return nil, err
			}
			return list, nil
		}
	}
	return nil, errors.New("List with Id " + listId + " not found")
}
コード例 #2
0
ファイル: order.go プロジェクト: 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

}
コード例 #3
0
ファイル: watchlist.go プロジェクト: foomo/shop
func (cw *CustomerWatchLists) AddList(watchlistType string, name string, public bool, recipient string, targetDate string, description string) (*WatchList, error) {
	watchList := &WatchList{
		Id:            unique.GetNewID(),
		Type:          watchlistType,
		Name:          name,
		CreatedAt:     utils.GetDateYYYY_MM_DD(),
		Recipient:     recipient,
		TargetDate:    targetDate,
		Description:   description,
		PublicURIHash: "",
		Items:         []*WatchListItem{},
	}
	if public {
		watchList.PublicURIHash = unique.GetNewID()
	} else {
		watchList.PublicURIHash = ""
	}
	cw.Lists = append(cw.Lists, watchList)
	err := cw.Upsert()
	if err != nil {
		return nil, err
	}
	return watchList, nil
}
コード例 #4
0
ファイル: customer.go プロジェクト: foomo/shop
// 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()
}
コード例 #5
0
ファイル: customer.go プロジェクト: foomo/shop
// 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)
}
コード例 #6
0
ファイル: watchlist_test.go プロジェクト: foomo/shop
func TestWatchListsManipulate(t *testing.T) {
	test_utils.DropAllCollections()
	customerID := unique.GetNewID()
	_, err := NewCustomerWatchListsFromCustomerID(customerID)
	if err != nil {
		t.Fatal(err)
	}
	cw, err := GetCustomerWatchListsByCustomerID(customerID)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	// Create List
	listA, err := cw.AddList("TypeX", "ListA", true, "me", utils.GetDateYYYY_MM_DD(), "My awesome list")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	// Add item to list
	err = cw.ListAddItem(listA.Id, "item1", 2)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	// Increase Quantity of item
	err = cw.ListAddItem(listA.Id, "item1", 3)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	// Add another item
	err = cw.ListAddItem(listA.Id, "item2", 2)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	item, err := cw.GetItem(listA.Id, "item1")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.Quantity != 5 {
		utils.PrintJSON(cw)
		t.Fatal("Wrong Quantity, expected 5")
	}
	// Reduce Quantity of item by 1
	err = cw.ListRemoveItem(listA.Id, "item2", 1)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	item, err = cw.GetItem(listA.Id, "item2")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.Quantity != 1 {
		t.Fatal("Wrong Quantity, expected 1")
	}
	// Remove last of item2
	err = cw.ListRemoveItem(listA.Id, "item2", 1)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}

	if len(listA.Items) != 1 {
		utils.PrintJSON(cw)
		t.Fatal("Expected 1 item in ListA")
	}
	newDescription := "new description"
	newName := "newName"
	// Edit list
	_, err = cw.EditList(listA.Id, newName, false, "", "", newDescription)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if listA.Name != newName || listA.Description != newDescription || listA.PublicURIHash != "" {
		utils.PrintJSON(cw)
		t.Fatal("EditList failed")
	}
	// Set item fulfilled
	cw.ListSetItemFulfilled(listA.Id, "item1", 2)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	item, err = cw.GetItem(listA.Id, "item1")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.QtyFulfilled != 2 {
		utils.PrintJSON(cw)
		t.Fatal("Expected QtyFulfilled == 2")
	}

	// Create second CustomerWatchLists and merge
	sessionID := unique.GetNewID()
	_, err = NewCustomerWatchListsFromSessionID(sessionID)
	if err != nil {
		t.Fatal(err)
	}
	cw2, err := GetCustomerWatchListsBySessionID(sessionID)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	listB, err := cw2.AddList("TypeX", "ListB", false, "recipient string", "targetDate string", "watchlist from session")
	if err != nil {
		t.Fatal(err)
	}
	err = cw2.ListAddItem(listB.Id, "item1b", 2)
	if err != nil {
		utils.PrintJSON(cw2)
		t.Fatal(err)
	}
	err = cw2.ListAddItem(listB.Id, "item1", 2)
	if err != nil {
		utils.PrintJSON(cw2)
		t.Fatal(err)
	}

	// Merge ListB from cw2 into ListA from cw
	err = MergeLists(cw2, listB.Id, cw, listA.Id)
	if err != nil {
		t.Fatal(err)
	}
	item, err = cw.GetItem(listA.Id, "item1")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.Quantity != 7 {
		utils.PrintJSON(cw)
		t.Fatal("Expected Quantity == 7")
	}
	item, err = cw2.GetItem(listB.Id, "item1b")
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}
	if item.Quantity != 2 {
		utils.PrintJSON(cw)
		t.Fatal("Expected Quantity == 2")
	}
	utils.PrintJSON(cw)

	// Test Getter
	cw, err = GetCustomerWatchListsByCustomerID(customerID)
	if err != nil {
		utils.PrintJSON(cw)
		t.Fatal(err)
	}

	// Test for non existant Id
	_, err = GetCustomerWatchListsByCustomerID("InvalidID")
	if err == nil {
		t.Fatal("Expected error not found", err)
	}

	watchList, err := cw2.EditList(listB.Id, "", true, "", "", "")
	if err != nil {
		t.Fatal("Edit List failed", err)
	}

	cw, err = GetCustomerWatchListsByURIHash(watchList.PublicURIHash)
	if err != nil {
		t.Fatal(err)
	}
	watchList, err = cw.GetListByURIHash(watchList.PublicURIHash)
	if err != nil {
		t.Fatal(err)
	}
	utils.PrintJSON(watchList)
}