Skip to content

Commit a189cf1

Browse files
committed
Fix downloads being overwritten when having multiple items
1 parent 80355c4 commit a189cf1

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

api/order_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func TestOrderCreate(t *testing.T) {
217217
Claims: map[string]string{
218218
"email": test.Data.testUser.Email,
219219
},
220-
Percentage: 15,
220+
Percentage: 15,
221221
ProductTypes: []string{"Book"},
222222
},
223223
},
@@ -249,6 +249,68 @@ func TestOrderCreate(t *testing.T) {
249249
assert.Equal(t, uint64(15), discountItem.Percentage)
250250
assert.Equal(t, uint64(0), discountItem.Fixed)
251251
})
252+
253+
t.Run("MultipleItemsWithDownloads", func(t *testing.T) {
254+
test := NewRouteTest(t)
255+
256+
site := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
257+
switch r.URL.Path {
258+
case "/i/believe/i/can/fly":
259+
fmt.Fprint(w, productMetaFrame(
260+
`{
261+
"sku": "123-i-can-fly-456",
262+
"downloads": [{"title": "First Download", "url": "/assets/first-download"}],
263+
"prices": [{"currency": "USD", "amount": "3.00"}]
264+
}`,
265+
))
266+
return
267+
case "/its/not/about/the/money":
268+
fmt.Fprintf(w, productMetaFrame(
269+
`{
270+
"sku": "not-about-the-money",
271+
"downloads": [{"title": "Second Download", "url": "/assets/second-download"}],
272+
"prices": [{"currency": "USD", "amount": "5.00"}]
273+
}`,
274+
))
275+
return
276+
}
277+
w.WriteHeader(http.StatusNotFound)
278+
}))
279+
defer site.Close()
280+
test.Config.SiteURL = site.URL
281+
282+
body := strings.NewReader(`{
283+
"email": "info@example.com",
284+
"shipping_address": {
285+
"name": "Test User",
286+
"address1": "Branengebranen",
287+
"city": "Berlin", "country": "Germany", "zip": "94107"
288+
},
289+
"line_items": [
290+
{"path": "/i/believe/i/can/fly", "quantity": 1},
291+
{"path": "/its/not/about/the/money", "quantity": 1}
292+
]
293+
}`)
294+
token := test.Data.testUserToken
295+
recorder := test.TestEndpoint(http.MethodPost, "/orders", body, token)
296+
297+
order := &models.Order{}
298+
extractPayload(t, http.StatusCreated, recorder, order)
299+
assert.Len(t, order.Downloads, 2)
300+
for _, dl := range order.Downloads {
301+
fmt.Printf("dl: %+v\n", dl)
302+
switch dl.Sku {
303+
case "123-i-can-fly-456":
304+
assert.Equal(t, "First Download", dl.Title)
305+
assert.Equal(t, "/assets/first-download", dl.URL)
306+
case "not-about-the-money":
307+
assert.Equal(t, "Second Download", dl.Title)
308+
assert.Equal(t, "/assets/second-download", dl.URL)
309+
default:
310+
t.Errorf("Unknown download item: %+v", dl)
311+
}
312+
}
313+
})
252314
}
253315

254316
func TestOrderCreateNewUser(t *testing.T) {

models/line_item.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (i *LineItem) Process(config *conf.Configuration, userClaims map[string]int
285285
i.AddonItems[index].Price = lowestPrice.cents
286286
}
287287

288-
order.Downloads = i.MissingDownloads(order, meta)
288+
order.Downloads = append(order.Downloads, i.MissingDownloads(order, meta)...)
289289

290290
return i.calculatePrice(userClaims, meta.Prices, order.Currency)
291291
}
@@ -338,22 +338,25 @@ func (i *LineItem) FetchMeta(siteURL string) (*LineItemMetadata, error) {
338338
// MissingDownloads returns all downloads that are not yet listed in the order
339339
func (i *LineItem) MissingDownloads(order *Order, meta *LineItemMetadata) []Download {
340340
downloads := []Download{}
341-
for _, download := range meta.Downloads {
341+
for _, metaDownload := range meta.Downloads {
342342
alreadyCreated := false
343343
for _, d := range order.Downloads {
344-
if d.URL == download.URL {
344+
if d.URL == metaDownload.URL {
345345
alreadyCreated = true
346346
break
347347
}
348348
}
349349
if alreadyCreated {
350350
continue
351351
}
352-
download.ID = uuid.NewRandom().String()
353-
download.OrderID = order.ID
354-
download.Title = i.Title
355-
download.Sku = i.Sku
356-
downloads = append(downloads, download)
352+
orderDownload := metaDownload
353+
orderDownload.ID = uuid.NewRandom().String()
354+
orderDownload.OrderID = order.ID
355+
orderDownload.Sku = i.Sku
356+
if orderDownload.Title == "" {
357+
orderDownload.Title = i.Title
358+
}
359+
downloads = append(downloads, orderDownload)
357360
}
358361
return downloads
359362
}

0 commit comments

Comments
 (0)