Skip to content

Commit

Permalink
Fix downloads being overwritten when having multiple items
Browse files Browse the repository at this point in the history
  • Loading branch information
mraerino committed Oct 3, 2019
1 parent 80355c4 commit 27a744e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
64 changes: 63 additions & 1 deletion api/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestOrderCreate(t *testing.T) {
Claims: map[string]string{
"email": test.Data.testUser.Email,
},
Percentage: 15,
Percentage: 15,
ProductTypes: []string{"Book"},
},
},
Expand Down Expand Up @@ -249,6 +249,68 @@ func TestOrderCreate(t *testing.T) {
assert.Equal(t, uint64(15), discountItem.Percentage)
assert.Equal(t, uint64(0), discountItem.Fixed)
})

t.Run("MultipleItemsWithDownloads", func(t *testing.T) {
test := NewRouteTest(t)

site := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/i/believe/i/can/fly":
fmt.Fprint(w, productMetaFrame(
`{
"sku": "123-i-can-fly-456",
"downloads": [{"title": "First Download", "url": "/assets/first-download"}],
"prices": [{"currency": "USD", "amount": "3.00"}]
}`,
))
return
case "/its/not/about/the/money":
fmt.Fprintf(w, productMetaFrame(
`{
"sku": "not-about-the-money",
"downloads": [{"title": "Second Download", "url": "/assets/second-download"}],
"prices": [{"currency": "USD", "amount": "5.00"}]
}`,
))
return
}
w.WriteHeader(http.StatusNotFound)
}))
defer site.Close()
test.Config.SiteURL = site.URL

body := strings.NewReader(`{
"email": "[email protected]",
"shipping_address": {
"name": "Test User",
"address1": "Branengebranen",
"city": "Berlin", "country": "Germany", "zip": "94107"
},
"line_items": [
{"path": "/i/believe/i/can/fly", "quantity": 1},
{"path": "/its/not/about/the/money", "quantity": 1}
]
}`)
token := test.Data.testUserToken
recorder := test.TestEndpoint(http.MethodPost, "/orders", body, token)

order := &models.Order{}
extractPayload(t, http.StatusCreated, recorder, order)
assert.Len(t, order.Downloads, 2)
for _, dl := range order.Downloads {
fmt.Printf("dl: %+v\n", dl)
switch dl.Sku {
case "123-i-can-fly-456":
assert.Equal(t, "First Download", dl.Title)
assert.Equal(t, "/assets/first-download", dl.URL)
case "not-about-the-money":
assert.Equal(t, "Second Download", dl.Title)
assert.Equal(t, "/assets/second-download", dl.URL)
default:
t.Errorf("Unknown download item: %+v", dl)
}
}
})
}

func TestOrderCreateNewUser(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions models/line_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (i *LineItem) Process(config *conf.Configuration, userClaims map[string]int
i.AddonItems[index].Price = lowestPrice.cents
}

order.Downloads = i.MissingDownloads(order, meta)
order.Downloads = append(order.Downloads, i.MissingDownloads(order, meta)...)

return i.calculatePrice(userClaims, meta.Prices, order.Currency)
}
Expand Down Expand Up @@ -338,22 +338,22 @@ func (i *LineItem) FetchMeta(siteURL string) (*LineItemMetadata, error) {
// MissingDownloads returns all downloads that are not yet listed in the order
func (i *LineItem) MissingDownloads(order *Order, meta *LineItemMetadata) []Download {
downloads := []Download{}
for _, download := range meta.Downloads {
for _, metaDownload := range meta.Downloads {
alreadyCreated := false
for _, d := range order.Downloads {
if d.URL == download.URL {
if d.URL == metaDownload.URL {
alreadyCreated = true
break
}
}
if alreadyCreated {
continue
}
download.ID = uuid.NewRandom().String()
download.OrderID = order.ID
download.Title = i.Title
download.Sku = i.Sku
downloads = append(downloads, download)
orderDownload := metaDownload
orderDownload.ID = uuid.NewRandom().String()
orderDownload.OrderID = order.ID
orderDownload.Sku = i.Sku
downloads = append(downloads, orderDownload)
}
return downloads
}
Expand Down

0 comments on commit 27a744e

Please sign in to comment.