Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit ef6bdb0

Browse files
committed
Fix and test crypto listings in migration27
1 parent 0145adb commit ef6bdb0

File tree

2 files changed

+238
-104
lines changed

2 files changed

+238
-104
lines changed

repo/migrations/Migration027.go

Lines changed: 88 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,21 @@ func (Migration027) Up(repoPath, databasePassword string, testnetEnabled bool) e
211211
return err
212212
}
213213

214-
listingJSON := signedListingJSON["listing"]
214+
listingJSON, listingExists := signedListingJSON["listing"]
215+
if !listingExists {
216+
continue
217+
}
215218
listing := listingJSON.(map[string]interface{})
216219

217-
metadataJSON := listing["metadata"]
220+
metadataJSON, metadataExists := listing["metadata"]
221+
if !metadataExists {
222+
continue
223+
}
218224
metadata := metadataJSON.(map[string]interface{})
219-
itemJSON := listing["item"]
225+
itemJSON, itemExists := listing["item"]
226+
if !itemExists {
227+
continue
228+
}
220229
item := itemJSON.(map[string]interface{})
221230

222231
var (
@@ -238,24 +247,25 @@ func (Migration027) Up(repoPath, databasePassword string, testnetEnabled bool) e
238247
coupons = couponsJSON.([]interface{})
239248
}
240249

241-
pricingCurrencyJSON := metadata["pricingCurrency"]
242-
pricingCurrency := pricingCurrencyJSON.(string)
250+
pricingCurrencyJSON, pricingCurrencyExists := metadata["pricingCurrency"]
251+
if pricingCurrencyExists {
252+
pricingCurrency := pricingCurrencyJSON.(string)
253+
divisibility, ok := divisibilityMap[strings.ToUpper(pricingCurrency)]
254+
if !ok {
255+
divisibility = 2
256+
}
243257

244-
divisibility, ok := divisibilityMap[strings.ToUpper(pricingCurrency)]
245-
if !ok {
246-
divisibility = 2
247-
}
258+
item["priceCurrency"] = struct {
259+
Code string `json:"code"`
260+
Divisibility uint32 `json:"divisibility"`
261+
}{
262+
Code: pricingCurrency,
263+
Divisibility: uint32(divisibility),
264+
}
248265

249-
item["priceCurrency"] = struct {
250-
Code string `json:"code"`
251-
Divisibility uint32 `json:"divisibility"`
252-
}{
253-
Code: pricingCurrency,
254-
Divisibility: uint32(divisibility),
266+
delete(metadata, "pricingCurrency")
255267
}
256268

257-
delete(metadata, "pricingCurrency")
258-
259269
var modifier float64
260270
modifierJSON := metadata["priceModifier"]
261271
if modifierJSON != nil {
@@ -266,47 +276,29 @@ func (Migration027) Up(repoPath, databasePassword string, testnetEnabled bool) e
266276

267277
delete(metadata, "priceModifier")
268278

269-
priceJSON := item["price"]
270-
price := priceJSON.(float64)
271-
272-
item["bigPrice"] = strconv.Itoa(int(price))
279+
priceJSON, priceExists := item["price"]
280+
if priceExists {
281+
price := priceJSON.(float64)
273282

274-
delete(item, "price")
283+
item["bigPrice"] = strconv.Itoa(int(price))
275284

276-
var coinType string
277-
coinTypeJSON := metadata["coinType"]
278-
if coinTypeJSON != nil {
279-
coinType = coinTypeJSON.(string)
285+
delete(item, "price")
280286
}
281287

282-
metadata["cryptoCurrencyCode"] = coinType
283-
284-
delete(metadata, "coinType")
285-
286-
var coinDivisibility float64
287-
coinDivisibilityJSON := metadata["coinDivisibility"]
288-
if coinDivisibilityJSON != nil {
289-
coinDivisibility = coinDivisibilityJSON.(float64)
290-
}
291-
292-
metadata["cryptoDivisibility"] = uint32(coinDivisibility)
293-
294-
delete(metadata, "coinDivisibility")
295-
296288
for _, skuJSON := range skus {
297289
sku := skuJSON.(map[string]interface{})
298290

299-
quantityJSON, ok := sku["quantity"]
300-
if ok {
291+
quantityJSON, quantityExists := sku["quantity"]
292+
if quantityExists {
301293
quantity := quantityJSON.(float64)
302294

303295
sku["bigQuantity"] = strconv.Itoa(int(quantity))
304296

305297
delete(sku, "quantity")
306298
}
307299

308-
surchargeJSON, ok := sku["surcharge"]
309-
if ok {
300+
surchargeJSON, surchargeExists := sku["surcharge"]
301+
if surchargeExists {
310302
surcharge := surchargeJSON.(float64)
311303

312304
sku["bigSurcharge"] = strconv.Itoa(int(surcharge))
@@ -327,14 +319,16 @@ func (Migration027) Up(repoPath, databasePassword string, testnetEnabled bool) e
327319
service := serviceJSON.(map[string]interface{})
328320

329321
priceJSON := service["price"]
330-
price := priceJSON.(float64)
322+
price, priceExists := priceJSON.(float64)
331323

332-
service["bigPrice"] = strconv.Itoa(int(price))
324+
if priceExists {
325+
service["bigPrice"] = strconv.Itoa(int(price))
333326

334-
delete(service, "price")
327+
delete(service, "price")
328+
}
335329

336-
additionalItemPriceJSON, ok := service["additionalItemPrice"]
337-
if ok {
330+
additionalItemPriceJSON, additionalPriceExists := service["additionalItemPrice"]
331+
if additionalPriceExists {
338332
additionalItemPrice := additionalItemPriceJSON.(float64)
339333

340334
service["bigAdditionalItemPrice"] = strconv.Itoa(int(additionalItemPrice))
@@ -477,12 +471,21 @@ func (Migration027) Down(repoPath, databasePassword string, testnetEnabled bool)
477471
return err
478472
}
479473

480-
listingJSON := signedListingJSON["listing"]
474+
listingJSON, listingExists := signedListingJSON["listing"]
475+
if !listingExists {
476+
continue
477+
}
481478
listing := listingJSON.(map[string]interface{})
482479

483-
metadataJSON := listing["metadata"]
480+
metadataJSON, metadataExists := listing["metadata"]
481+
if !metadataExists {
482+
continue
483+
}
484484
metadata := metadataJSON.(map[string]interface{})
485-
itemJSON := listing["item"]
485+
itemJSON, itemExists := listing["item"]
486+
if !itemExists {
487+
continue
488+
}
486489
item := itemJSON.(map[string]interface{})
487490

488491
var (
@@ -504,15 +507,19 @@ func (Migration027) Down(repoPath, databasePassword string, testnetEnabled bool)
504507
coupons = couponsJSON.([]interface{})
505508
}
506509

507-
pricingCurrencyJSON := item["priceCurrency"]
508-
pricingCurrency := pricingCurrencyJSON.(map[string]interface{})
510+
pricingCurrencyJSON, pricingCurrencyExists := item["priceCurrency"]
511+
if pricingCurrencyExists {
512+
pricingCurrency := pricingCurrencyJSON.(map[string]interface{})
509513

510-
priceCurrencyCodeJSON := pricingCurrency["code"]
511-
priceCurrencyCode := priceCurrencyCodeJSON.(string)
514+
priceCurrencyCodeJSON, currencyCodeExists := pricingCurrency["code"]
515+
if currencyCodeExists {
516+
priceCurrencyCode := priceCurrencyCodeJSON.(string)
512517

513-
metadata["pricingCurrency"] = priceCurrencyCode
518+
metadata["pricingCurrency"] = priceCurrencyCode
514519

515-
delete(item, "priceCurrency")
520+
delete(item, "priceCurrency")
521+
}
522+
}
516523

517524
var modifier float64
518525
modifierJSON := item["priceModifier"]
@@ -524,39 +531,21 @@ func (Migration027) Down(repoPath, databasePassword string, testnetEnabled bool)
524531

525532
delete(item, "priceModifier")
526533

527-
priceJSON := item["bigPrice"]
528-
price := priceJSON.(string)
529-
530-
p, ok := new(big.Int).SetString(price, 10)
531-
if ok {
532-
item["price"] = p.Uint64()
533-
}
534-
delete(item, "bigPrice")
535-
536-
var coinType string
537-
coinTypeJSON := metadata["cryptoCurrencyCode"]
538-
if coinTypeJSON != nil {
539-
coinType = coinTypeJSON.(string)
540-
}
541-
542-
metadata["coinType"] = coinType
543-
544-
delete(metadata, "cryptoCurrencyCode")
534+
priceJSON, priceExists := item["bigPrice"]
535+
if priceExists {
536+
price := priceJSON.(string)
545537

546-
var coinDivisibility float64
547-
coinDivisibilityJSON := metadata["cryptoDivisibility"]
548-
if coinDivisibilityJSON != nil {
549-
coinDivisibility = coinDivisibilityJSON.(float64)
538+
p, ok := new(big.Int).SetString(price, 10)
539+
if ok {
540+
item["price"] = p.Uint64()
541+
}
542+
delete(item, "bigPrice")
550543
}
551544

552-
metadata["coinDivisibility"] = uint32(coinDivisibility)
553-
554-
delete(metadata, "cryptoDivisibility")
555-
556545
for _, skuJSON := range skus {
557546
sku := skuJSON.(map[string]interface{})
558-
quantityJSON, ok := sku["bigQuantity"]
559-
if ok {
547+
quantityJSON, quantityExists := sku["bigQuantity"]
548+
if quantityExists {
560549
quantity := quantityJSON.(string)
561550

562551
p, ok := new(big.Int).SetString(quantity, 10)
@@ -591,18 +580,20 @@ func (Migration027) Down(repoPath, databasePassword string, testnetEnabled bool)
591580
for x, serviceJSON := range services {
592581
service := serviceJSON.(map[string]interface{})
593582

594-
priceJSON := service["bigPrice"]
595-
price := priceJSON.(string)
583+
priceJSON, priceExists := service["bigPrice"]
584+
if priceExists {
585+
price := priceJSON.(string)
596586

597-
p, ok := new(big.Int).SetString(price, 10)
598-
if ok {
599-
service["price"] = p.Uint64()
600-
}
587+
p, ok := new(big.Int).SetString(price, 10)
588+
if ok {
589+
service["price"] = p.Uint64()
590+
}
601591

602-
delete(service, "bigPrice")
592+
delete(service, "bigPrice")
593+
}
603594

604-
additionalItemPriceJSON, ok := service["bigAdditionalItemPrice"]
605-
if ok {
595+
additionalItemPriceJSON, additionalPriceExists := service["bigAdditionalItemPrice"]
596+
if additionalPriceExists {
606597
additionalItemPrice := additionalItemPriceJSON.(string)
607598

608599
a, ok := new(big.Int).SetString(additionalItemPrice, 10)
@@ -622,8 +613,8 @@ func (Migration027) Down(repoPath, databasePassword string, testnetEnabled bool)
622613
for _, couponJSON := range coupons {
623614
coupon := couponJSON.(map[string]interface{})
624615

625-
priceDiscountJSON, ok := coupon["bigPriceDiscount"]
626-
if ok {
616+
priceDiscountJSON, priceDiscountExists := coupon["bigPriceDiscount"]
617+
if priceDiscountExists {
627618
priceDiscount := priceDiscountJSON.(string)
628619

629620
a, ok := new(big.Int).SetString(priceDiscount, 10)

0 commit comments

Comments
 (0)