@zodic/shared 0.0.420 → 0.0.422

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,30 +11,25 @@ export class PaymentService {
11
11
  private conceptService: ConceptService
12
12
  ) {}
13
13
 
14
- async handleCheckoutEvent(payload: any): Promise<void> {
14
+ async handleCheckoutEvent(payload: any): Promise<{ userId?: string; productSlug?: string }> {
15
15
  const { event, checkout } = payload;
16
- const checkoutId = checkout.id;
17
16
 
18
17
  switch (event) {
19
18
  case 'CHECKOUT_CREATED':
20
- await this.handleCheckoutCreated(checkout);
21
- break;
19
+ return this.handleCheckoutCreated(checkout);
22
20
  case 'CHECKOUT_PAID':
23
- await this.handleCheckoutPaid(checkout);
24
- break;
21
+ return this.handleCheckoutPaid(checkout);
25
22
  case 'CHECKOUT_CANCELED':
26
- await this.handleCheckoutCanceled(checkout);
27
- break;
23
+ return this.handleCheckoutCanceled(checkout);
28
24
  case 'CHECKOUT_EXPIRED':
29
- await this.handleCheckoutExpired(checkout);
30
- break;
25
+ return this.handleCheckoutExpired(checkout);
31
26
  default:
32
27
  console.log(`⚠️ Unsupported event: ${event}`);
33
- return;
28
+ return {};
34
29
  }
35
30
  }
36
31
 
37
- private async handleCheckoutCreated(checkout: any): Promise<void> {
32
+ private async handleCheckoutCreated(checkout: any): Promise<{ userId?: string; productSlug?: string }> {
38
33
  const checkoutId = checkout.id;
39
34
  const amount = checkout.items.reduce(
40
35
  (sum: number, item: any) => sum + item.value * item.quantity,
@@ -56,9 +51,10 @@ export class PaymentService {
56
51
  .onConflictDoNothing(); // Avoid duplicate if already created
57
52
 
58
53
  console.log(`✅ Created payment record for checkoutId: ${checkoutId}`);
54
+ return {};
59
55
  }
60
56
 
61
- private async handleCheckoutPaid(checkout: any): Promise<void> {
57
+ private async handleCheckoutPaid(checkout: any): Promise<{ userId?: string; productSlug?: string }> {
62
58
  const checkoutId = checkout.id;
63
59
  const db = this.context.drizzle();
64
60
 
@@ -77,7 +73,7 @@ export class PaymentService {
77
73
  console.log(
78
74
  `[${new Date().toISOString()}] ⚠️ No user product found for checkoutId: ${checkoutId}`
79
75
  );
80
- return;
76
+ return {};
81
77
  }
82
78
 
83
79
  // Fetch the product slug
@@ -94,7 +90,7 @@ export class PaymentService {
94
90
  userProduct.productId
95
91
  }`
96
92
  );
97
- return;
93
+ return {};
98
94
  }
99
95
 
100
96
  // Queue concept generations if the product slug is "concepts"
@@ -130,13 +126,21 @@ export class PaymentService {
130
126
  console.log(
131
127
  `✅ Updated userProducts to unlocked and payment to completed for checkoutId: ${checkoutId}`
132
128
  );
129
+ return { userId: userProduct.userId, productSlug: product.slug };
133
130
  }
134
131
 
135
- private async handleCheckoutCanceled(checkout: any): Promise<void> {
132
+ private async handleCheckoutCanceled(checkout: any): Promise<{ userId?: string; productSlug?: string }> {
136
133
  const checkoutId = checkout.id;
137
134
  const db = this.context.drizzle();
138
- // Update userProducts to 'locked'
139
135
 
136
+ const userProduct = await db
137
+ .select({ userId: userProducts.userId, productId: userProducts.productId })
138
+ .from(userProducts)
139
+ .where(eq(userProducts.checkoutId, checkoutId))
140
+ .limit(1)
141
+ .get();
142
+
143
+ // Update userProducts to 'locked'
140
144
  await db
141
145
  .update(userProducts)
142
146
  .set({ status: 'locked', updatedAt: new Date() })
@@ -151,11 +155,20 @@ export class PaymentService {
151
155
  console.log(
152
156
  `✅ Updated userProducts to locked and payment to canceled for checkoutId: ${checkoutId}`
153
157
  );
158
+ return { userId: userProduct?.userId };
154
159
  }
155
160
 
156
- private async handleCheckoutExpired(checkout: any): Promise<void> {
161
+ private async handleCheckoutExpired(checkout: any): Promise<{ userId?: string; productSlug?: string }> {
157
162
  const checkoutId = checkout.id;
158
163
  const db = this.context.drizzle();
164
+
165
+ const userProduct = await db
166
+ .select({ userId: userProducts.userId })
167
+ .from(userProducts)
168
+ .where(eq(userProducts.checkoutId, checkoutId))
169
+ .limit(1)
170
+ .get();
171
+
159
172
  // Update userProducts to 'locked'
160
173
  await db
161
174
  .update(userProducts)
@@ -171,5 +184,6 @@ export class PaymentService {
171
184
  console.log(
172
185
  `✅ Updated userProducts to locked and payment to expired for checkoutId: ${checkoutId}`
173
186
  );
187
+ return { userId: userProduct?.userId };
174
188
  }
175
189
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zodic/shared",
3
- "version": "0.0.420",
3
+ "version": "0.0.422",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/release.ts CHANGED
@@ -99,8 +99,8 @@ if (!found) {
99
99
  }
100
100
 
101
101
  // Give bun's registry a moment to catch up with npm
102
- console.log("\n⏳ Waiting 5s for bun registry propagation...");
103
- await new Promise((resolve) => setTimeout(resolve, 5000));
102
+ console.log("\n⏳ Waiting 12s for bun registry propagation...");
103
+ await new Promise((resolve) => setTimeout(resolve, 12000));
104
104
 
105
105
  // ─── Step 6: Run `bun upshare` in all zodic- workers ─────────────────────────
106
106
 
package/types/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './backend';
2
2
  export * from './frontend';
3
+ export * from './sse';
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  Ai,
3
3
  D1Database,
4
+ DurableObjectNamespace,
4
5
  Fetcher,
5
6
  KVNamespace,
6
7
  Queue,
@@ -64,6 +65,8 @@ export type CentralBindings = {
64
65
  ASTRO_API: Fetcher;
65
66
  ASTRO_ENGINE: Service;
66
67
  IMAGE_HANDLER: Service;
68
+ REALTIME_NOTIFIER: DurableObjectNamespace;
69
+ INTERNAL_SECRET: string;
67
70
  };
68
71
 
69
72
  export type Variables = {
@@ -113,6 +116,8 @@ export type BackendBindings = Env &
113
116
  | 'ASTRO_API'
114
117
  | 'ASTRO_ENGINE'
115
118
  | 'IMAGE_HANDLER'
119
+ | 'REALTIME_NOTIFIER'
120
+ | 'INTERNAL_SECRET'
116
121
  >;
117
122
 
118
123
  export type BackendCtx = Context<
package/types/sse.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { Fetcher } from '@cloudflare/workers-types';
2
+
3
+ export type SSEEventType =
4
+ | 'payment:completed'
5
+ | 'payment:failed'
6
+ | 'concept:completed'
7
+ | 'concept:failed'
8
+ | 'artifact:completed'
9
+ | 'artifact:failed';
10
+
11
+ export type SSEEvent = {
12
+ userId: string;
13
+ event: SSEEventType;
14
+ data: Record<string, unknown>;
15
+ };
16
+
17
+ export async function broadcastSSE(
18
+ backend: Fetcher,
19
+ internalSecret: string,
20
+ event: SSEEvent
21
+ ): Promise<void> {
22
+ await backend.fetch('https://internal/internal/sse/broadcast', {
23
+ method: 'POST',
24
+ headers: {
25
+ 'Content-Type': 'application/json',
26
+ 'X-Internal-Secret': internalSecret,
27
+ },
28
+ body: JSON.stringify(event),
29
+ });
30
+ }
package/wrangler.toml CHANGED
@@ -5,7 +5,7 @@ compatibility_date = "2024-12-10"
5
5
  [[d1_databases]]
6
6
  binding = "DB"
7
7
  database_name = "zodic-db-dev"
8
- database_id = "23e0ee5c-6a17-40ee-b731-4f52913501db"
8
+ database_id = "f9b24c4f-d2d1-4c0a-8eca-2df43910b497"
9
9
 
10
10
  # Production environment
11
11
  [env.production]
@@ -19,11 +19,11 @@ database_id = "b4b0a981-f921-4258-ad7a-b2d7b50eb381"
19
19
  [[env.staging.d1_databases]]
20
20
  binding = "DB"
21
21
  database_name = "zodic-db-stg"
22
- database_id = "3ef831f8-c65e-4e6b-b8b8-934628da14cf"
22
+ database_id = "2e722bf2-b597-4be1-9e78-74fbe31792fd"
23
23
 
24
24
  # Development environment
25
25
  [env.development]
26
26
  [[env.development.d1_databases]]
27
27
  binding = "DB"
28
28
  database_name = "zodic-db-dev"
29
- database_id = "23e0ee5c-6a17-40ee-b731-4f52913501db"
29
+ database_id = "f9b24c4f-d2d1-4c0a-8eca-2df43910b497"