1- import { createStore , type StoreApi } from "zustand/vanilla"
2- import { immer } from "zustand/middleware/immer"
3- import { hoist , type HoistedStoreApi } from "zustand-hoist"
1+ import { createStore } from "zustand/vanilla"
2+ import { hoist } from "zustand-hoist"
43
5- import { databaseSchema , type DatabaseSchema , type Thing } from "./schema.ts"
4+ import {
5+ databaseSchema ,
6+ type Payment ,
7+ type PaymentStatus ,
8+ type Thing ,
9+ } from "./schema.ts"
610import { combine } from "zustand/middleware"
711
812export const createDatabase = ( ) => {
@@ -11,7 +15,15 @@ export const createDatabase = () => {
1115
1216export type DbClient = ReturnType < typeof createDatabase >
1317
14- const initializer = combine ( databaseSchema . parse ( { } ) , ( set ) => ( {
18+ export interface CreatePaymentInput {
19+ amount : number
20+ currency : string
21+ recipient : string
22+ memo ?: string
23+ idempotency_key ?: string
24+ }
25+
26+ const initializer = combine ( databaseSchema . parse ( { } ) , ( set , get ) => ( {
1527 addThing : ( thing : Omit < Thing , "thing_id" > ) => {
1628 set ( ( state ) => ( {
1729 things : [
@@ -21,4 +33,61 @@ const initializer = combine(databaseSchema.parse({}), (set) => ({
2133 idCounter : state . idCounter + 1 ,
2234 } ) )
2335 } ,
36+
37+ createPayment : ( payment : CreatePaymentInput ) : Payment => {
38+ const now = new Date ( ) . toISOString ( )
39+ const existingPayment = payment . idempotency_key
40+ ? get ( ) . payments . find (
41+ ( existing ) => existing . idempotency_key === payment . idempotency_key ,
42+ )
43+ : undefined
44+
45+ if ( existingPayment ) return existingPayment
46+
47+ const newPayment : Payment = {
48+ payment_id : get ( ) . paymentIdCounter . toString ( ) ,
49+ amount : payment . amount ,
50+ currency : payment . currency . toUpperCase ( ) ,
51+ recipient : payment . recipient ,
52+ memo : payment . memo ,
53+ idempotency_key : payment . idempotency_key ,
54+ status : "pending" ,
55+ created_at : now ,
56+ updated_at : now ,
57+ }
58+
59+ set ( ( state ) => ( {
60+ payments : [ ...state . payments , newPayment ] ,
61+ paymentIdCounter : state . paymentIdCounter + 1 ,
62+ } ) )
63+
64+ return newPayment
65+ } ,
66+
67+ getPayment : ( paymentId : string ) : Payment | undefined => {
68+ return get ( ) . payments . find ( ( payment ) => payment . payment_id === paymentId )
69+ } ,
70+
71+ listPayments : ( status ?: PaymentStatus ) : Payment [ ] => {
72+ const payments = get ( ) . payments
73+ return status ? payments . filter ( ( payment ) => payment . status === status ) : payments
74+ } ,
75+
76+ updatePaymentStatus : (
77+ paymentId : string ,
78+ status : PaymentStatus ,
79+ ) : Payment | undefined => {
80+ let updatedPayment : Payment | undefined
81+ const now = new Date ( ) . toISOString ( )
82+
83+ set ( ( state ) => ( {
84+ payments : state . payments . map ( ( payment ) => {
85+ if ( payment . payment_id !== paymentId ) return payment
86+ updatedPayment = { ...payment , status, updated_at : now }
87+ return updatedPayment
88+ } ) ,
89+ } ) )
90+
91+ return updatedPayment
92+ } ,
2493} ) )
0 commit comments