Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: consistent artwork money fields resolver #6367

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lib/stitching/vortex/__tests__/pricingContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,17 @@ describe("PricingContext type", () => {
{
size_score: 600,
price_cents: [124, 235],
price_currency: "USD",
},
{
size_score: 3000,
price_cents: [154, 185],
price_currency: "USD",
},
{
size_score: 10300,
price_cents: [12443], // THIS ONE SHOULD BE CHOSEN
price_currency: "USD",
},
],
})
Expand All @@ -185,14 +188,17 @@ describe("PricingContext type", () => {
{
size_score: 600,
price_cents: [124, 235],
price_currency: "USD",
},
{
size_score: 3000,
price_cents: [154, 18555], // THIS ONE SHOULD BE CHOSEN
price_currency: "USD",
},
{
size_score: 10300,
price_cents: [12443],
price_currency: "USD",
},
],
})
Expand Down
4 changes: 2 additions & 2 deletions src/schema/v2/artwork/__tests__/artwork.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ describe("Artwork type", () => {
maxPrice: {
minor: 42000,
major: 420,
display: null,
display: "US$420",
currencyCode: "USD",
},
},
Expand All @@ -676,7 +676,7 @@ describe("Artwork type", () => {
minPrice: {
minor: 42000,
major: 420,
display: null,
display: "US$420",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

currencyCode: "USD",
},
maxPrice: null,
Expand Down
98 changes: 68 additions & 30 deletions src/schema/v2/artwork/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ import Fair from "schema/v2/fair"
import cached from "schema/v2/fields/cached"
import { listPrice } from "schema/v2/fields/listPrice"
import { markdown } from "schema/v2/fields/markdown"
import { amount, Money, symbolFromCurrencyCode } from "schema/v2/fields/money"
import {
amount,
Money,
resolvePriceAndCurrencyFieldsToMoney,
symbolFromCurrencyCode,
} from "schema/v2/fields/money"
import {
connectionWithCursorInfo,
PageCursorsType,
Expand Down Expand Up @@ -93,7 +98,6 @@ import { CursorPageable, pageable } from "relay-cursor-paging"
import { convertConnectionArgsToGravityArgs } from "lib/helpers"
import { error } from "lib/loggers"
import { PartnerOfferType } from "../partnerOffer"
import currencyCodes from "lib/currency_codes.json"
import { date } from "../fields/date"
import { ArtworkVisibility } from "./artworkVisibility"
import { ArtworkConditionType } from "./artworkCondition"
Expand Down Expand Up @@ -1297,11 +1301,19 @@ export const ArtworkType = new GraphQLObjectType<any, ResolverContext>({
},
priceListed: {
type: Money,
resolve: ({ price_listed: price_listed, price_currency: currency }) => {
const factor =
currencyCodes[currency?.toLowerCase()]?.subunit_to_unit ?? 100
const cents = price_listed * factor
return { cents, currency }
resolve: (
{ price_listed: major, price_currency: currencyCode },
_args,
context,
info
) => {
if (typeof major !== "number" || !currencyCode) return null
return resolvePriceAndCurrencyFieldsToMoney(
{ major, currencyCode },
_args,
context,
info
)
},
},
taxInfo: TaxInfo,
Expand Down Expand Up @@ -1363,25 +1375,42 @@ export const ArtworkType = new GraphQLObjectType<any, ResolverContext>({
domesticShippingFee: {
type: Money,
description: "Domestic shipping fee.",
resolve: ({
domestic_shipping_fee_cents: cents,
price_currency: currency,
}) => {
if (typeof cents !== "number" || !currency) return null
resolve: (
{ domestic_shipping_fee_cents: minor, price_currency: currencyCode },
_args,
ctx,
info
) => {
if (typeof minor !== "number" || !currencyCode) return null

return { cents, currency }
return resolvePriceAndCurrencyFieldsToMoney(
{ minor, currencyCode },
_args,
ctx,
info
)
},
},
internationalShippingFee: {
type: Money,
description: "International shipping fee.",
resolve: ({
international_shipping_fee_cents: cents,
price_currency: currency,
}) => {
if (typeof cents !== "number" || !currency) return null
resolve: (
{
international_shipping_fee_cents: minor,
price_currency: currencyCode,
},
_args,
ctx,
info
) => {
if (typeof minor !== "number" || !currencyCode) return null

return { cents, currency }
return resolvePriceAndCurrencyFieldsToMoney(
{ minor, currencyCode },
_args,
ctx,
info
)
},
},
shippingInfo: {
Expand Down Expand Up @@ -1542,18 +1571,27 @@ export const ArtworkType = new GraphQLObjectType<any, ResolverContext>({
type: Money,
description:
"The price paid for the artwork in a user's 'my collection'",
resolve: (artwork) => {
const { price_paid_cents } = artwork
resolve: async (artwork, args, ctx, info) => {
const { price_paid_cents, price_paid_currency = "USD" } = artwork
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly surprised that we ask collectors to enter price in cents, but I guess it was here already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is probably calculated on the artwork from some input using the money helper gem

if (!price_paid_cents && price_paid_cents !== 0) return null
const price_paid_currency = artwork.price_paid_currency || "USD"
return {
cents: price_paid_cents,
currency: price_paid_currency,
display: amount(() => price_paid_cents).resolve(artwork, {
precision: 0,
symbol: symbolFromCurrencyCode(price_paid_currency),
}),
}
const moneyFields =
(await resolvePriceAndCurrencyFieldsToMoney(
{ minor: price_paid_cents, currencyCode: price_paid_currency },
args,
ctx,
info
)) || {}
return (
moneyFields && {
...moneyFields,
// TODO: Display field legacy implementation maintained until we verify
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create a separate card for this?

// that clients can request the real display field correctly
display: amount(() => price_paid_cents).resolve(artwork, {
precision: 0,
symbol: symbolFromCurrencyCode(price_paid_currency),
}),
}
)
},
},
provenance: markdown(({ provenance }) =>
Expand Down
45 changes: 44 additions & 1 deletion src/schema/v2/fields/__tests__/money.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { amount } from "../money"
import { amount, resolvePriceAndCurrencyFieldsToMoney } from "../money"
import { runQuery } from "schema/v2/test/utils"
import gql from "lib/gql"

Expand Down Expand Up @@ -175,6 +175,7 @@ describe("major(convertTo:)", () => {
})
})

// eslint-disable-next-line jest/no-disabled-tests
it.skip("only supports USD", async () => {
expect.assertions(1)

Expand Down Expand Up @@ -256,3 +257,45 @@ describe("major(convertTo:)", () => {
)
})
})

describe("resolvePriceAndCurrencyFieldsToMoney()", () => {
const args = {}
const ctx = { exchangeRatesLoader: jest.fn(() => Promise.resolve()) }
const info = {}
it("returns money fields for major + currencyCode", async () => {
const result = await resolvePriceAndCurrencyFieldsToMoney(
{
major: 1234,
currencyCode: "USD",
},
args,
ctx,
info
)
expect(result).toEqual({
cents: 123400,
major: 1234,
display: "US$1,234",
currency: "USD",
})
})

it("returns money fields for minor + currencyCode", async () => {
const result = await resolvePriceAndCurrencyFieldsToMoney(
{
minor: 123400,
currencyCode: "USD",
},
args,
ctx,
info
)

expect(result).toEqual({
cents: 123400,
major: 1234,
display: "US$1,234",
currency: "USD",
})
})
})
88 changes: 58 additions & 30 deletions src/schema/v2/fields/listPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
GraphQLUnionType,
GraphQLString,
} from "graphql"
import { Money } from "./money"
import { Money, resolvePriceAndCurrencyFieldsToMoney } from "./money"

const PriceRange = new GraphQLObjectType({
name: "PriceRange",
Expand All @@ -14,22 +14,36 @@ const PriceRange = new GraphQLObjectType({
},
minPrice: {
type: Money,
resolve: ({ minPriceCents, price_currency }) => {
if (!minPriceCents) return null
return {
cents: minPriceCents,
currency: price_currency,
}
resolve: (
{ minPriceCents: minor, price_currency: currencyCode },
args,
ctx,
info
) => {
if (!minor) return null
return resolvePriceAndCurrencyFieldsToMoney(
{ minor, currencyCode },
args,
ctx,
info
)
},
},
maxPrice: {
type: Money,
resolve: ({ maxPriceCents, price_currency }) => {
if (!maxPriceCents) return null
return {
cents: maxPriceCents,
currency: price_currency,
}
resolve: (
{ maxPriceCents: minor, price_currency: currencyCode },
args,
ctx,
info
) => {
if (!minor) return null
return resolvePriceAndCurrencyFieldsToMoney(
{ minor, currencyCode },
args,
ctx,
info
)
},
},
},
Expand All @@ -40,28 +54,42 @@ export const listPrice: GraphQLFieldConfig<any, any> = {
name: "ListPrice",
types: [PriceRange, Money],
}),
resolve: ({ price_cents, price, price_currency }) => {
resolve: async (
{ price_cents, price: displayPrice, price_currency },
args,
ctx,
info
) => {
if (!price_cents || price_cents.length === 0) {
return null
}
const isExactPrice = price_cents.length === 1

return isExactPrice
? {
__typename: Money.name,
cents: price_cents[0],
display: price,
currency: price_currency,
}
: {
// For deprecated types
__typename: PriceRange.name,
minPriceCents: price_cents[0],
maxPriceCents: price_cents[1],
if (isExactPrice) {
const moneyFields = await resolvePriceAndCurrencyFieldsToMoney(
{ minor: price_cents[0], currencyCode: price_currency },
args,
ctx,
info
)
return {
__typename: Money.name,
...moneyFields,
// To support existing usage which assumes Gravity's Artwork#display_price
// TODO: Update clients to use real `display` field with formatting string
// if necessary
display: displayPrice,
}
}
return {
// For deprecated types
__typename: PriceRange.name,
minPriceCents: price_cents[0],
maxPriceCents: price_cents[1],

// For preferred types
price_currency,
display: price,
}
// For preferred types
price_currency,
display: displayPrice,
}
},
}
Loading