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
|
rateDate?: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ConvertCurrencyForDisplayInput = {
|
||||||
|
amount: number
|
||||||
|
sourceCurrency: string
|
||||||
|
displayCurrency: string
|
||||||
|
}
|
||||||
|
|
||||||
export const m2mTypeDefs = `#graphql
|
export const m2mTypeDefs = `#graphql
|
||||||
input LockCurrencyPriceInput {
|
input LockCurrencyPriceInput {
|
||||||
sourceAmount: Float!
|
sourceAmount: Float!
|
||||||
@@ -24,6 +30,24 @@ export const m2mTypeDefs = `#graphql
|
|||||||
rateDate: String
|
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 {
|
type LockedCurrencyPrice {
|
||||||
sourceAmount: Float!
|
sourceAmount: Float!
|
||||||
sourceCurrency: String!
|
sourceCurrency: String!
|
||||||
@@ -41,6 +65,7 @@ export const m2mTypeDefs = `#graphql
|
|||||||
|
|
||||||
type Query {
|
type Query {
|
||||||
health: String!
|
health: String!
|
||||||
|
convertCurrencyForDisplay(input: ConvertCurrencyForDisplayInput!): DisplayCurrencyConversion!
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mutation {
|
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 = {
|
export const m2mResolvers = {
|
||||||
Query: {
|
Query: {
|
||||||
health: () => 'ok',
|
health: () => 'ok',
|
||||||
|
convertCurrencyForDisplay,
|
||||||
},
|
},
|
||||||
Mutation: {
|
Mutation: {
|
||||||
lockCurrencyPrice,
|
lockCurrencyPrice,
|
||||||
|
|||||||
Reference in New Issue
Block a user