MCPcopy
hub / github.com/loggerhead/json4u / getActiveOrder

Function getActiveOrder

src/stores/userStore.ts:117–150  ·  view source on GitHub ↗
(email: string | undefined)

Source from the content-addressed store, hash-verified

115
116// TODO: Think about how to keep multiple subscriptions mutually exclusive
117async function getActiveOrder(email: string | undefined): Promise<{
118 order: Order | null;
119 error: string | null;
120}> {
121 if (!email) {
122 return { order: null, error: null };
123 }
124
125 // API: https://supabase.com/docs/reference/javascript/select
126 const { data: items, error } = await supabase
127 .from("orders")
128 .select()
129 .eq("email", email)
130 .neq("plan", "free")
131 .order("created_at", { ascending: false });
132 if (error) {
133 return { order: null, error: error.toString() };
134 }
135
136 try {
137 const orders = items.map((item) => OrderSchema.parse(item));
138
139 for (const order of orders) {
140 if (order.status === "active") {
141 return { order, error: null };
142 }
143 }
144
145 const order = last(sortBy(orders, "ends_at")) ?? null;
146 return { order, error: null };
147 } catch (error) {
148 return { order: null, error: (error as Error).toString() };
149 }
150}

Callers 1

updateActiveOrderFunction · 0.85

Calls 2

eqMethod · 0.80
toStringMethod · 0.80

Tested by

no test coverage detected