-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1863 from Shopify/sle-c/convert-money-type
[Fix] Convert received Money.amount from string to number
- Loading branch information
Showing
9 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/shopify-api': patch | ||
--- | ||
|
||
Convert all Money.amount returned from the Billing GraphQL API from string to number type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import {ActiveSubscriptionLineItem} from './types'; | ||
|
||
/** | ||
* Converts string amounts to numbers in Money type objects | ||
*/ | ||
export function convertMoneyAmount(data: any) { | ||
if (!data) return data; | ||
|
||
convertAppUsagePricingMoney(data); | ||
convertAppRecurringPricingMoney(data); | ||
convertAppDiscountMoney(data); | ||
|
||
return data; | ||
} | ||
|
||
export function convertAppRecurringPricingMoney(data: any): void { | ||
if (!data) return; | ||
|
||
if (data.price?.amount && typeof data.price.amount === 'string') { | ||
data.price.amount = parseFloat(data.price.amount); | ||
} | ||
} | ||
|
||
export function convertAppDiscountMoney(data: any): void { | ||
if (!data) return; | ||
|
||
if ( | ||
data.discount?.priceAfterDiscount?.amount && | ||
typeof data.discount.priceAfterDiscount.amount === 'string' | ||
) { | ||
data.discount.priceAfterDiscount.amount = parseFloat( | ||
data.discount.priceAfterDiscount.amount, | ||
); | ||
} | ||
|
||
if ( | ||
data.discount?.value?.amount?.amount && | ||
typeof data.discount.value.amount.amount === 'string' | ||
) { | ||
data.discount.value.amount.amount = parseFloat( | ||
data.discount.value.amount.amount, | ||
); | ||
} | ||
} | ||
|
||
export function convertAppUsagePricingMoney(data: any): void { | ||
if (!data) return; | ||
|
||
if (data.balanceUsed?.amount && typeof data.balanceUsed.amount === 'string') { | ||
data.balanceUsed.amount = parseFloat(data.balanceUsed.amount); | ||
} | ||
|
||
if ( | ||
data.cappedAmount?.amount && | ||
typeof data.cappedAmount.amount === 'string' | ||
) { | ||
data.cappedAmount.amount = parseFloat(data.cappedAmount.amount); | ||
} | ||
} | ||
|
||
/** | ||
* Converts Money amounts in line items | ||
*/ | ||
export function convertLineItems(lineItems: ActiveSubscriptionLineItem[]) { | ||
return lineItems.map((item) => { | ||
if (item.plan?.pricingDetails) { | ||
item.plan.pricingDetails = convertMoneyAmount(item.plan.pricingDetails); | ||
} | ||
return item; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters