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
26 changes: 25 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,23 @@ const initializer = combine(databaseSchema.parse({}), (set) => ({
idCounter: state.idCounter + 1,
}))
},
sendPayment: (payment: Omit<Payment, "payment_id" | "status" | "created_at">) => {
let createdPayment: Payment | undefined

set((state) => {
createdPayment = {
...payment,
payment_id: state.paymentCounter.toString(),
status: "sent",
created_at: new Date().toISOString(),
}

return {
payments: [...state.payments, createdPayment],
paymentCounter: state.paymentCounter + 1,
}
})

return createdPayment!
},
}))
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: z.string(),
amount_usd: z.number(),
memo: z.string().optional(),
status: z.enum(["sent"]),
created_at: z.string(),
})
export type Payment = z.infer<typeof paymentSchema>

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

export default withRouteSpec({
methods: ["GET"],
jsonResponse: z.object({
payments: z.array(paymentSchema),
}),
})((req, ctx) => {
return ctx.json({ payments: ctx.db.payments })
})
21 changes: 21 additions & 0 deletions routes/payments/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { paymentSchema } from "lib/db/schema"
import { z } from "zod"

export default withRouteSpec({
methods: ["POST"],
jsonBody: z.object({
recipient: z.string().min(1),
amount_usd: z.number().positive(),
memo: z.string().optional(),
}),
jsonResponse: z.object({
ok: z.boolean(),
payment: paymentSchema,
}),
})(async (req, ctx) => {
const paymentRequest = await req.json()
const payment = ctx.db.sendPayment(paymentRequest)

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

test("send and list payments", async () => {
const { axios } = await getTestServer()

const sendResponse = await axios.post("/payments/send", {
recipient: "maintainer@example.com",
amount_usd: 10,
memo: "Bounty payout",
})

expect(sendResponse.status).toBe(200)
expect(sendResponse.data.ok).toBe(true)
expect(sendResponse.data.payment).toMatchObject({
payment_id: "0",
recipient: "maintainer@example.com",
amount_usd: 10,
memo: "Bounty payout",
status: "sent",
})
expect(typeof sendResponse.data.payment.created_at).toBe("string")

const listResponse = await axios.get("/payments/list")

expect(listResponse.data.payments).toHaveLength(1)
expect(listResponse.data.payments[0]).toEqual(sendResponse.data.payment)
})