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
Binary file modified bun.lockb
Binary file not shown.
34 changes: 33 additions & 1 deletion lib/db/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { createStore, type StoreApi } from "zustand/vanilla"
import { immer } from "zustand/middleware/immer"
import { hoist, type HoistedStoreApi } from "zustand-hoist"

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

export const createDatabase = () => {
Expand All @@ -21,4 +26,31 @@ const initializer = combine(databaseSchema.parse({}), (set) => ({
idCounter: state.idCounter + 1,
}))
},
addPayment: (
payment: Omit<Payment, "payment_id" | "created_at" | "status">,
) => {
set((state) => ({
payments: [
...state.payments,
{
...payment,
payment_id: `pay_${state.idCounter}`,
status: "pending" as const,
created_at: new Date().toISOString(),
},
],
idCounter: state.idCounter + 1,
}))
},
updatePaymentStatus: (
payment_id: string,
status: Payment["status"],
sent_at?: string,
) => {
set((state) => ({
payments: state.payments.map((p) =>
p.payment_id === payment_id ? { ...p, status, sent_at } : p,
),
}))
},
}))
12 changes: 12 additions & 0 deletions lib/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@ export const thingSchema = z.object({
})
export type Thing = z.infer<typeof thingSchema>

export const paymentSchema = z.object({
payment_id: z.string(),
recipient_email: z.string().email(),
amount_usd: z.number().positive(),
note: z.string().optional(),
status: z.enum(["pending", "sent", "failed"]).default("pending"),
created_at: z.string(),
sent_at: z.string().optional(),
})
export type Payment = z.infer<typeof paymentSchema>

export const databaseSchema = z.object({
idCounter: z.number().default(0),
things: z.array(thingSchema).default([]),
payments: z.array(paymentSchema).default([]),
})
export type DatabaseSchema = z.infer<typeof databaseSchema>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@types/bun": "latest",
"@types/react": "18.3.4",
"next": "^14.2.5",
"redaxios": "^0.5.1"
"ky": "^1.8.1"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand Down
43 changes: 43 additions & 0 deletions routes/payments/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

const paymentResponseSchema = z.object({
payment_id: z.string(),
recipient_email: z.string(),
amount_usd: z.number(),
note: z.string().optional(),
status: z.enum(["pending", "sent", "failed"]),
created_at: z.string(),
sent_at: z.string().optional(),
})

export default withRouteSpec({
methods: ["GET"],
queryParams: z.object({
recipient_email: z.string().email().optional(),
status: z.enum(["pending", "sent", "failed"]).optional(),
}),
jsonResponse: z.object({
ok: z.boolean(),
payments: z.array(paymentResponseSchema),
}),
})(async (req, ctx) => {
const url = new URL(req.url)
const recipient_email = url.searchParams.get("recipient_email") ?? undefined
const status = url.searchParams.get("status") as
| "pending"
| "sent"
| "failed"
| undefined

let payments = ctx.db.getState().payments

if (recipient_email) {
payments = payments.filter((p) => p.recipient_email === recipient_email)
}
if (status) {
payments = payments.filter((p) => p.status === status)
}

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

export default withRouteSpec({
methods: ["POST"],
jsonBody: z.object({
recipient_email: z.string().email(),
amount_usd: z.number().positive(),
note: z.string().optional(),
}),
jsonResponse: z.object({
ok: z.boolean(),
payment: z.object({
payment_id: z.string(),
recipient_email: z.string(),
amount_usd: z.number(),
note: z.string().optional(),
status: z.enum(["pending", "sent", "failed"]),
created_at: z.string(),
sent_at: z.string().optional(),
}),
}),
})(async (req, ctx) => {
const { recipient_email, amount_usd, note } = await req.json()

ctx.db.addPayment({ recipient_email, amount_usd, note })

const payments = ctx.db.getState().payments
const payment = payments[payments.length - 1]

// Simulate async send: immediately mark as sent in fake mode
ctx.db.updatePaymentStatus(
payment.payment_id,
"sent",
new Date().toISOString(),
)

const sent = ctx.db
.getState()
.payments.find((p) => p.payment_id === payment.payment_id)!

return ctx.json({ ok: true, payment: sent })
})
12 changes: 5 additions & 7 deletions tests/fixtures/get-test-server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { afterEach } from "bun:test"
import { tmpdir } from "node:os"
import defaultAxios from "redaxios"
import ky from "ky"
import { startServer } from "./start-server"

interface TestFixture {
url: string
server: any
axios: typeof defaultAxios
ky: typeof ky
}

export const getTestServer = async (): Promise<TestFixture> => {
Expand All @@ -20,18 +19,17 @@ export const getTestServer = async (): Promise<TestFixture> => {
})

const url = `http://127.0.0.1:${port}`
const axios = defaultAxios.create({
baseURL: url,
const kyInstance = ky.create({
prefixUrl: url,
})

afterEach(async () => {
await server.stop()
// Here you might want to add logic to drop the test database
})

return {
url,
server,
axios,
ky: kyInstance,
}
}
7 changes: 3 additions & 4 deletions tests/routes/health.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { it, expect } from "bun:test"
import { getTestServer } from "tests/fixtures/get-test-server"

it("GET /health should return ok", async () => {
const { axios } = await getTestServer()
const res = await axios.get("/health")
expect(res.status).toBe(200)
expect(res.data).toEqual({ ok: true })
const { ky } = await getTestServer()
const data = await ky.get("health").json()
expect(data).toEqual({ ok: true })
})
45 changes: 45 additions & 0 deletions tests/routes/payments/send.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { getTestServer } from "tests/fixtures/get-test-server"
import { test, expect } from "bun:test"

test("send a payment and verify it appears in list", async () => {
const { ky } = await getTestServer()

const sendData = await ky
.post("payments/send", {
json: {
recipient_email: "[email protected]",
amount_usd: 25,
note: "Bounty reward",
},
})
.json<any>()

expect(sendData.ok).toBe(true)
expect(sendData.payment.recipient_email).toBe("[email protected]")
expect(sendData.payment.amount_usd).toBe(25)
expect(sendData.payment.status).toBe("sent")
expect(sendData.payment.sent_at).toBeDefined()

const listData = await ky.get("payments/list").json<any>()
expect(listData.payments).toHaveLength(1)
expect(listData.payments[0].payment_id).toBe(sendData.payment.payment_id)
})

test("filter payments by recipient_email", async () => {
const { ky } = await getTestServer()

await ky.post("payments/send", {
json: { recipient_email: "[email protected]", amount_usd: 25 },
})
await ky.post("payments/send", {
json: { recipient_email: "[email protected]", amount_usd: 50 },
})

const data = await ky
.get("payments/list", {
searchParams: { recipient_email: "[email protected]" },
})
.json<any>()
expect(data.payments).toHaveLength(1)
expect(data.payments[0].recipient_email).toBe("[email protected]")
})
9 changes: 4 additions & 5 deletions tests/routes/things/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { getTestServer } from "tests/fixtures/get-test-server"
import { test, expect } from "bun:test"

test("create a thing", async () => {
const { axios } = await getTestServer()
const { ky } = await getTestServer()

axios.post("/things/create", {
name: "Thing1",
description: "Thing1 Description",
await ky.post("things/create", {
json: { name: "Thing1", description: "Thing1 Description" },
})

const { data } = await axios.get("/things/list")
const data = await ky.get("things/list").json<any>()

expect(data.things).toHaveLength(1)
})