diff --git a/schema/schema.graphql b/schema/schema.graphql index d118aa5..fd8118f 100644 --- a/schema/schema.graphql +++ b/schema/schema.graphql @@ -7,6 +7,7 @@ type Query { product(id: ID!): Product cart(id: ID!): Cart order(id: ID!): Order + stores: [Store!]! "Orders, newest first. Optionally filtered to a single customer." orders(customerId: ID, first: Int = 20): [Order!]! } @@ -14,8 +15,8 @@ type Query { type Mutation { addToCart(cartId: ID!, productId: ID!, quantity: Int = 1): Cart! removeFromCart(cartId: ID!, productId: ID!): Cart! - "Converts a cart into an order and captures payment." - checkout(input: CheckoutInput!): CheckoutResult! + "Converts a cart into an order and captures payment. Orders are fulfilled from the given store." + checkout(storeId: ID!, input: CheckoutInput!): CheckoutResult! "Refunds a paid or fulfilled order. Omit amount to refund the full total." refundOrder(orderId: ID!, amount: Float): Order! } @@ -68,6 +69,14 @@ type Order { currency: String! payment: Payment! shippingAddress: Address + "Store the order is fulfilled from." + store: Store! +} + +type Store { + id: ID! + name: String! + region: String! } type OrderItem { diff --git a/src/data/store.ts b/src/data/store.ts index 7dd725a..4a7544c 100644 --- a/src/data/store.ts +++ b/src/data/store.ts @@ -10,6 +10,12 @@ export interface ProductRecord { inventory: number; } +export interface StoreRecord { + id: string; + name: string; + region: string; +} + export interface CustomerRecord { id: string; name: string; @@ -41,6 +47,7 @@ export interface OrderRecord { items: LineItemRecord[]; shippingAddress?: AddressRecord; paymentId: string; + storeId: string; } export interface PaymentRecord { @@ -69,6 +76,7 @@ export interface CartRecord { * instance so tests never share state. */ export class Store { + stores = new Map(); products = new Map(); customers = new Map(); orders = new Map(); @@ -102,6 +110,12 @@ const seedAddress: AddressRecord = { export function createStore(): Store { const store = new Store(); + const stores: StoreRecord[] = [ + { id: 'store_pdx', name: 'Thicket Portland', region: 'OR' }, + { id: 'store_sea', name: 'Thicket Seattle', region: 'WA' }, + ]; + stores.forEach((s) => store.stores.set(s.id, s)); + const products: ProductRecord[] = [ { id: 'prod_fern', sku: 'PLN-001', name: 'Fern Planter', description: 'Glazed ceramic planter, 8 in.', price: 24.0, inventory: 40 }, { id: 'prod_terrarium', sku: 'KIT-014', name: 'Moss Terrarium Kit', description: 'Glass jar, moss, and stones.', price: 38.5, inventory: 12 }, @@ -124,26 +138,26 @@ export function createStore(): Store { { id: 'li_1', productId: 'prod_fern', quantity: 1, unitPrice: 24.0 }, { id: 'li_2', productId: 'prod_shears', quantity: 1, unitPrice: 19.99 }, ], - shippingAddress: seedAddress, paymentId: 'pay_1001', + shippingAddress: seedAddress, paymentId: 'pay_1001', storeId: 'store_pdx', payment: { id: 'pay_1001', status: 'CAPTURED', method: 'card', last4: '4242', amount: 47.62, capturedAt: '2026-09-02T15:04:05.000Z' }, }, { id: 'ord_1002', number: 'TH-1002', customerId: 'cus_avery', status: 'FULFILLED', createdAt: '2026-09-14T18:30:00.000Z', items: [{ id: 'li_3', productId: 'prod_terrarium', quantity: 1, unitPrice: 38.5 }], - shippingAddress: seedAddress, paymentId: 'pay_1002', + shippingAddress: seedAddress, paymentId: 'pay_1002', storeId: 'store_pdx', payment: { id: 'pay_1002', status: 'CAPTURED', method: 'card', last4: '1881', amount: 41.68, capturedAt: '2026-09-14T18:30:04.000Z' }, }, { id: 'ord_1003', number: 'TH-1003', customerId: 'cus_jordan', status: 'PENDING', createdAt: '2026-10-20T09:12:00.000Z', items: [{ id: 'li_4', productId: 'prod_tray', quantity: 2, unitPrice: 12.75 }], shippingAddress: { ...seedAddress, line1: '18 Birch Court', city: 'Seattle', region: 'WA', postalCode: '98101' }, - paymentId: 'pay_1003', + paymentId: 'pay_1003', storeId: 'store_sea', payment: { id: 'pay_1003', status: 'AUTHORIZED', method: 'card', last4: '0005', amount: 27.6 }, }, { id: 'ord_1004', number: 'TH-1004', customerId: 'cus_jordan', status: 'REFUNDED', createdAt: '2026-08-28T12:00:00.000Z', items: [{ id: 'li_5', productId: 'prod_can', quantity: 1, unitPrice: 29.0 }], - paymentId: 'pay_1004', + paymentId: 'pay_1004', storeId: 'store_sea', payment: { id: 'pay_1004', status: 'REFUNDED', method: 'paypal', amount: 31.39, capturedAt: '2026-08-28T12:00:03.000Z' }, }, ]; diff --git a/src/domain/checkout.ts b/src/domain/checkout.ts index f573686..4fbe879 100644 --- a/src/domain/checkout.ts +++ b/src/domain/checkout.ts @@ -25,7 +25,8 @@ export function last4FromToken(token: string): string { * Converts a cart into a paid order. Prices are snapshotted onto the order's * line items so later catalog changes do not alter historical orders. */ -export function checkout(store: Store, input: CheckoutInput, now = new Date()): CheckoutResult { +export function checkout(store: Store, storeId: string, input: CheckoutInput, now = new Date()): CheckoutResult { + if (!store.stores.has(storeId)) throw notFound('Store', storeId); const cart = store.carts.get(input.cartId); if (!cart) throw notFound('Cart', input.cartId); if (cart.items.length === 0) throw badInput('Cart is empty'); @@ -52,6 +53,7 @@ export function checkout(store: Store, input: CheckoutInput, now = new Date()): items, shippingAddress: input.shippingAddress, paymentId, + storeId, }; const payment: PaymentRecord = { id: paymentId, diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index 75861b1..64ab36d 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -25,6 +25,7 @@ export const resolvers = { product: (_: unknown, { id }: { id: string }, { store }: Context) => store.products.get(id) ?? null, cart: (_: unknown, { id }: { id: string }, { store }: Context) => store.carts.get(id) ?? null, order: (_: unknown, { id }: { id: string }, { store }: Context) => store.orders.get(id) ?? null, + stores: (_: unknown, __: unknown, { store }: Context) => [...store.stores.values()], orders: (_: unknown, args: { customerId?: string | null; first: number }, { store }: Context) => [...store.orders.values()] .filter((o) => !args.customerId || o.customerId === args.customerId) @@ -53,7 +54,8 @@ export const resolvers = { cart.items = cart.items.filter((i) => i.productId !== productId); return cart; }, - checkout: (_: unknown, { input }: { input: CheckoutInput }, { store }: Context) => checkout(store, input), + checkout: (_: unknown, { storeId, input }: { storeId: string; input: CheckoutInput }, { store }: Context) => + checkout(store, storeId, input), refundOrder: (_: unknown, { orderId, amount }: { orderId: string; amount?: number | null }, { store }: Context) => refundOrder(store, orderId, amount), }, @@ -84,6 +86,11 @@ export const resolvers = { return payment; }, shippingAddress: (order: OrderRecord) => order.shippingAddress ?? null, + store: (order: OrderRecord, _: unknown, { store }: Context) => { + const record = store.stores.get(order.storeId); + if (!record) throw notFound('Store', order.storeId); + return record; + }, }, OrderItem: { diff --git a/test/checkout.test.ts b/test/checkout.test.ts index bf01a92..6913d9a 100644 --- a/test/checkout.test.ts +++ b/test/checkout.test.ts @@ -2,9 +2,9 @@ import { describe, expect, it } from 'vitest'; import { setup } from './helpers'; const CHECKOUT = /* GraphQL */ ` - mutation Checkout($input: CheckoutInput!) { - checkout(input: $input) { - order { number status total items { quantity product { id } } } + mutation Checkout($storeId: ID!, $input: CheckoutInput!) { + checkout(storeId: $storeId, input: $input) { + order { number status total store { id } items { quantity product { id } } } payment { status last4 amount } } } @@ -16,10 +16,11 @@ describe('checkout', () => { it('creates a paid order and captures the total', async () => { const { execute, store } = setup(); const { data, errors } = await execute(CHECKOUT, { + storeId: 'store_pdx', input: { cartId: 'cart_avery', email: 'avery@example.com', shippingAddress: address, cardToken: 'tok_visa_4242' }, }); expect(errors).toBeUndefined(); - expect(data.checkout.order).toMatchObject({ number: 'TH-1005', status: 'PAID', total: 73.07 }); + expect(data.checkout.order).toMatchObject({ number: 'TH-1005', status: 'PAID', total: 73.07, store: { id: 'store_pdx' } }); expect(data.checkout.payment).toEqual({ status: 'CAPTURED', last4: '4242', amount: 73.07 }); expect(store.carts.get('cart_avery')!.items).toHaveLength(0); }); @@ -28,6 +29,7 @@ describe('checkout', () => { const { execute, store } = setup(); const before = store.products.get('prod_terrarium')!.inventory; await execute(CHECKOUT, { + storeId: 'store_pdx', input: { cartId: 'cart_avery', email: 'avery@example.com', shippingAddress: address, cardToken: 'tok_visa_4242' }, }); expect(store.products.get('prod_terrarium')!.inventory).toBe(before - 1); @@ -36,6 +38,7 @@ describe('checkout', () => { it('rejects an empty cart', async () => { const { execute } = setup(); const { errors } = await execute(CHECKOUT, { + storeId: 'store_pdx', input: { cartId: 'cart_jordan', email: 'jordan@example.com', shippingAddress: address, cardToken: 'tok_visa_4242' }, }); expect(errors?.[0]).toMatchObject({ message: 'Cart is empty', extensions: { code: 'BAD_USER_INPUT' } }); @@ -44,6 +47,7 @@ describe('checkout', () => { it('rejects an unknown cart', async () => { const { execute } = setup(); const { errors } = await execute(CHECKOUT, { + storeId: 'store_pdx', input: { cartId: 'cart_nope', email: 'x@example.com', shippingAddress: address, cardToken: 'tok_visa_4242' }, }); expect(errors?.[0].extensions?.code).toBe('NOT_FOUND'); @@ -53,8 +57,18 @@ describe('checkout', () => { const { execute } = setup(); await execute(`mutation { addToCart(cartId: "cart_jordan", productId: "prod_tray") { id } }`); const { errors } = await execute(CHECKOUT, { + storeId: 'store_pdx', input: { cartId: 'cart_jordan', email: 'jordan@example.com', shippingAddress: address, cardToken: 'tok_visa_4242' }, }); expect(errors?.[0].message).toBe('Seed Starter Tray is out of stock'); }); + + it('rejects an unknown store', async () => { + const { execute } = setup(); + const { errors } = await execute(CHECKOUT, { + storeId: 'store_nope', + input: { cartId: 'cart_avery', email: 'avery@example.com', shippingAddress: address, cardToken: 'tok_visa_4242' }, + }); + expect(errors?.[0]).toMatchObject({ message: 'Store store_nope not found', extensions: { code: 'NOT_FOUND' } }); + }); });