Convert display prices on current rates
Build and deploy Docker image / build (push) Successful in 1m25s
Build and deploy Docker image / build (push) Successful in 1m25s
This commit is contained in:
+1
-1
Submodule graphql-contracts updated: 1526ffc3b9...d8f6796b68
@@ -15,6 +15,12 @@ type LockCurrencyPriceInput = {
|
||||
rateDate?: string | null
|
||||
}
|
||||
|
||||
type ConvertCurrencyForDisplayInput = {
|
||||
amount: number
|
||||
sourceCurrency: string
|
||||
displayCurrency: string
|
||||
}
|
||||
|
||||
export const m2mTypeDefs = `#graphql
|
||||
input LockCurrencyPriceInput {
|
||||
sourceAmount: Float!
|
||||
@@ -24,6 +30,24 @@ export const m2mTypeDefs = `#graphql
|
||||
rateDate: String
|
||||
}
|
||||
|
||||
input ConvertCurrencyForDisplayInput {
|
||||
amount: Float!
|
||||
sourceCurrency: String!
|
||||
displayCurrency: String!
|
||||
}
|
||||
|
||||
type DisplayCurrencyConversion {
|
||||
sourceAmount: Float!
|
||||
sourceCurrency: String!
|
||||
displayAmount: Float!
|
||||
displayCurrency: String!
|
||||
exchangeRate: Float!
|
||||
rateSnapshotUuid: String
|
||||
rateEffectiveDate: String!
|
||||
rateQuotedAt: String!
|
||||
provider: String!
|
||||
}
|
||||
|
||||
type LockedCurrencyPrice {
|
||||
sourceAmount: Float!
|
||||
sourceCurrency: String!
|
||||
@@ -41,6 +65,7 @@ export const m2mTypeDefs = `#graphql
|
||||
|
||||
type Query {
|
||||
health: String!
|
||||
convertCurrencyForDisplay(input: ConvertCurrencyForDisplayInput!): DisplayCurrencyConversion!
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
@@ -118,9 +143,49 @@ async function lockCurrencyPrice(
|
||||
}
|
||||
}
|
||||
|
||||
async function convertCurrencyForDisplay(
|
||||
_: unknown,
|
||||
args: { input: ConvertCurrencyForDisplayInput },
|
||||
) {
|
||||
const sourceAmount = Number(args.input.amount)
|
||||
if (!Number.isFinite(sourceAmount) || sourceAmount < 0) {
|
||||
throw new GraphQLError('amount must be a non-negative finite number')
|
||||
}
|
||||
const sourceCurrency = currencyCode(args.input.sourceCurrency)
|
||||
const displayCurrency = currencyCode(args.input.displayCurrency)
|
||||
if (sourceCurrency === displayCurrency) {
|
||||
const quotedAt = new Date()
|
||||
return {
|
||||
sourceAmount,
|
||||
sourceCurrency,
|
||||
displayAmount: sourceAmount,
|
||||
displayCurrency,
|
||||
exchangeRate: 1,
|
||||
rateSnapshotUuid: null,
|
||||
rateEffectiveDate: quotedAt.toISOString().slice(0, 10),
|
||||
rateQuotedAt: quotedAt.toISOString(),
|
||||
provider: 'identity',
|
||||
}
|
||||
}
|
||||
const snapshot = await latestCurrencyRatesSnapshot()
|
||||
const exchangeRate = convertMoney(1, sourceCurrency, displayCurrency, snapshot)
|
||||
return {
|
||||
sourceAmount,
|
||||
sourceCurrency,
|
||||
displayAmount: sourceAmount * exchangeRate,
|
||||
displayCurrency,
|
||||
exchangeRate,
|
||||
rateSnapshotUuid: snapshot.uuid,
|
||||
rateEffectiveDate: snapshot.effectiveDate.toISOString().slice(0, 10),
|
||||
rateQuotedAt: snapshot.updatedAt.toISOString(),
|
||||
provider: snapshot.provider,
|
||||
}
|
||||
}
|
||||
|
||||
export const m2mResolvers = {
|
||||
Query: {
|
||||
health: () => 'ok',
|
||||
convertCurrencyForDisplay,
|
||||
},
|
||||
Mutation: {
|
||||
lockCurrencyPrice,
|
||||
|
||||
Reference in New Issue
Block a user