Skip to content
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ This is a template project with best-practice modules:
- Winterspec for defining the API
- bun testing
- Zustand store with zod definition for database state

## Fake payments

- `POST /payments/send` creates a pending fake payment and replays the same record when `idempotency_key` is reused.
- `GET /payments/list` lists payments, with optional `recipient`, `status`, and `bounty_issue` filters.
- `GET /payments/get?payment_id=...` returns one payment.
- `POST /payments/update-status` changes a payment to `pending`, `completed`, `canceled`, or `failed`.
72 changes: 69 additions & 3 deletions lib/db/db-client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { createStore, type StoreApi } from "zustand/vanilla"
import { type HoistedStoreApi, hoist } from "zustand-hoist"
import { immer } from "zustand/middleware/immer"
import { hoist, type HoistedStoreApi } from "zustand-hoist"
import { type StoreApi, createStore } from "zustand/vanilla"

import { databaseSchema, type DatabaseSchema, type Thing } from "./schema.ts"
import { combine } from "zustand/middleware"
import {
type DatabaseSchema,
type Payment,
type PaymentStatus,
type Thing,
databaseSchema,
} from "./schema.ts"

export const createDatabase = () => {
return hoist(createStore(initializer))
Expand All @@ -21,4 +27,64 @@ const initializer = combine(databaseSchema.parse({}), (set) => ({
idCounter: state.idCounter + 1,
}))
},
sendPayment: (
payment: Omit<
Payment,
"payment_id" | "status" | "created_at" | "updated_at"
>,
) => {
let nextPayment: Payment | undefined
set((state) => {
if (payment.idempotency_key) {
const existingPayment = state.payments.find(
(existing) => existing.idempotency_key === payment.idempotency_key,
)
if (existingPayment) {
nextPayment = existingPayment
return state
}
}

const now = new Date().toISOString()
nextPayment = {
...payment,
payment_id: state.paymentIdCounter.toString(),
status: "pending",
created_at: now,
updated_at: now,
}

return {
payments: [...state.payments, nextPayment],
paymentIdCounter: state.paymentIdCounter + 1,
}
})

return nextPayment!
},
updatePaymentStatus: ({
payment_id,
status,
}: {
payment_id: string
status: PaymentStatus
}) => {
let updatedPayment: Payment | undefined
set((state) => ({
payments: state.payments.map((payment) => {
if (payment.payment_id !== payment_id) {
return payment
}

updatedPayment = {
...payment,
status,
updated_at: new Date().toISOString(),
}
return updatedPayment
}),
}))

return updatedPayment ?? null
},
}))
23 changes: 23 additions & 0 deletions lib/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,31 @@ export const thingSchema = z.object({
})
export type Thing = z.infer<typeof thingSchema>

export const paymentStatusSchema = z.enum([
"pending",
"completed",
"canceled",
"failed",
])
export type PaymentStatus = z.infer<typeof paymentStatusSchema>

export const paymentSchema = z.object({
payment_id: z.string(),
recipient: z.string(),
amount: z.number().positive(),
currency: z.string(),
bounty_issue: z.string().optional(),
idempotency_key: z.string().optional(),
status: paymentStatusSchema.default("pending"),
created_at: z.string(),
updated_at: z.string(),
})
export type Payment = z.infer<typeof paymentSchema>

export const databaseSchema = z.object({
idCounter: z.number().default(0),
paymentIdCounter: z.number().default(0),
things: z.array(thingSchema).default([]),
payments: z.array(paymentSchema).default([]),
})
export type DatabaseSchema = z.infer<typeof databaseSchema>
28 changes: 28 additions & 0 deletions routes/payments/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { paymentSchema } from "lib/db/schema"
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["GET"],
queryParams: z.object({
payment_id: z.string().min(1),
}),
jsonResponse: z.union([
z.object({ payment: paymentSchema }),
z.object({ error: z.string() }),
]),
})((req, ctx) => {
const paymentId = new URL(req.url).searchParams.get("payment_id")
const payment = ctx.db.payments.find(
(existing) => existing.payment_id === paymentId,
)

if (!payment) {
return new Response(JSON.stringify({ error: "Payment not found" }), {
status: 404,
headers: { "content-type": "application/json" },
})
}

return ctx.json({ payment })
})
29 changes: 29 additions & 0 deletions routes/payments/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { paymentSchema, paymentStatusSchema } from "lib/db/schema"
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["GET"],
queryParams: z.object({
recipient: z.string().optional(),
status: paymentStatusSchema.optional(),
bounty_issue: z.string().optional(),
}),
jsonResponse: z.object({
payments: z.array(paymentSchema),
}),
})((req, ctx) => {
const url = new URL(req.url)
const recipient = url.searchParams.get("recipient")
const status = url.searchParams.get("status")
const bountyIssue = url.searchParams.get("bounty_issue")

const payments = ctx.db.payments.filter((payment) => {
if (recipient && payment.recipient !== recipient) return false
if (status && payment.status !== status) return false
if (bountyIssue && payment.bounty_issue !== bountyIssue) return false
return true
})

return ctx.json({ payments })
})
22 changes: 22 additions & 0 deletions routes/payments/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { paymentSchema } from "lib/db/schema"
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["POST"],
jsonBody: z.object({
recipient: z.string().min(1),
amount: z.number().positive(),
currency: z.string().min(1).default("USD"),
bounty_issue: z.string().min(1).optional(),
idempotency_key: z.string().min(1).optional(),
}),
jsonResponse: z.object({
payment: paymentSchema,
}),
})(async (req, ctx) => {
const body = await req.json()
const payment = ctx.db.sendPayment(body)

return ctx.json({ payment })
})
27 changes: 27 additions & 0 deletions routes/payments/update-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { paymentSchema, paymentStatusSchema } from "lib/db/schema"
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["POST"],
jsonBody: z.object({
payment_id: z.string().min(1),
status: paymentStatusSchema,
}),
jsonResponse: z.union([
z.object({ payment: paymentSchema }),
z.object({ error: z.string() }),
]),
})(async (req, ctx) => {
const body = await req.json()
const payment = ctx.db.updatePaymentStatus(body)

if (!payment) {
return new Response(JSON.stringify({ error: "Payment not found" }), {
status: 404,
headers: { "content-type": "application/json" },
})
}

return ctx.json({ payment })
})
62 changes: 62 additions & 0 deletions tests/routes/payments/payment-flow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect, test } from "bun:test"
import { getTestServer } from "tests/fixtures/get-test-server"

test("send, read, list, and update a fake payment", async () => {
const { axios } = await getTestServer()

const sendResponse = await axios.post("/payments/send", {
recipient: "octocat",
amount: 10,
currency: "USD",
bounty_issue: "tscircuit/fake-algora#1",
idempotency_key: "claim-1-pr-1",
})

expect(sendResponse.status).toBe(200)
expect(sendResponse.data.payment).toMatchObject({
payment_id: "0",
recipient: "octocat",
amount: 10,
currency: "USD",
bounty_issue: "tscircuit/fake-algora#1",
status: "pending",
idempotency_key: "claim-1-pr-1",
})

const replayResponse = await axios.post("/payments/send", {
recipient: "octocat",
amount: 10,
currency: "USD",
bounty_issue: "tscircuit/fake-algora#1",
idempotency_key: "claim-1-pr-1",
})

expect(replayResponse.data.payment.payment_id).toBe("0")

const getResponse = await axios.get("/payments/get?payment_id=0")
expect(getResponse.data.payment.status).toBe("pending")

const completeResponse = await axios.post("/payments/update-status", {
payment_id: "0",
status: "completed",
})
expect(completeResponse.data.payment).toMatchObject({
payment_id: "0",
status: "completed",
})

const listResponse = await axios.get("/payments/list?status=completed")
expect(listResponse.data.payments).toHaveLength(1)
expect(listResponse.data.payments[0].payment_id).toBe("0")
})

test("missing fake payments return a 404", async () => {
const { axios } = await getTestServer()

const response = await axios.get("/payments/get?payment_id=missing", {
validateStatus: () => true,
})

expect(response.status).toBe(404)
expect(response.data).toEqual({ error: "Payment not found" })
})
4 changes: 4 additions & 0 deletions types/dist-bundle.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "dist/bundle" {
const bundle: unknown
export default bundle
}