Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/graphql/shared/types/scalar/usd-cents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { USDAmount } from "@domain/shared"
import { USDAmount, USDTAmount } from "@domain/shared"
import { GT } from "@graphql/index"

const USDCentsScalar = GT.Scalar({
Expand All @@ -16,6 +16,9 @@ const USDCentsScalar = GT.Scalar({
if (value instanceof USDAmount) {
return Number(value.asCents(2))
}
if (value instanceof USDTAmount) {
return Number(value.asSmallestUnits())
}
else throw new Error(`Failed to serialize USDAmount: ${value}`)
}
})
Expand Down
18 changes: 18 additions & 0 deletions test/flash/unit/graphql/shared/types/scalar/usd-cents.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { USDAmount, USDTAmount } from "@domain/shared"
import USDCentsScalar from "@graphql/shared/types/scalar/usd-cents"

describe("USDCentsScalar", () => {
it("serializes USD amounts as cents", () => {
const amount = USDAmount.cents("123")
if (amount instanceof Error) throw amount

expect(USDCentsScalar.serialize(amount)).toBe(123)
})

it("serializes USDT amounts as micro-USDT units", () => {
const amount = USDTAmount.smallestUnits("1234567")
if (amount instanceof Error) throw amount

expect(USDCentsScalar.serialize(amount)).toBe(1234567)
})
})