@zodic/shared 0.0.392 → 0.0.394

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.
@@ -0,0 +1,116 @@
1
+ import { eq } from 'drizzle-orm';
2
+ import { injectable } from 'inversify';
3
+ import { AppContext } from '../base';
4
+ import { payments, userProducts } from '../../db/schema';
5
+
6
+ @injectable()
7
+ export class PaymentService {
8
+ constructor(private context: AppContext) {}
9
+
10
+ async handleCheckoutEvent(payload: any): Promise<void> {
11
+ const { event, checkout } = payload;
12
+ const checkoutId = checkout.id;
13
+
14
+ switch (event) {
15
+ case 'CHECKOUT_CREATED':
16
+ await this.handleCheckoutCreated(checkout);
17
+ break;
18
+ case 'CHECKOUT_PAID':
19
+ await this.handleCheckoutPaid(checkout);
20
+ break;
21
+ case 'CHECKOUT_CANCELED':
22
+ await this.handleCheckoutCanceled(checkout);
23
+ break;
24
+ case 'CHECKOUT_EXPIRED':
25
+ await this.handleCheckoutExpired(checkout);
26
+ break;
27
+ default:
28
+ console.log(`⚠️ Unsupported event: ${event}`);
29
+ return;
30
+ }
31
+ }
32
+
33
+ private async handleCheckoutCreated(checkout: any): Promise<void> {
34
+ const checkoutId = checkout.id;
35
+ const amount = checkout.items.reduce(
36
+ (sum: number, item: any) => sum + item.value * item.quantity,
37
+ 0
38
+ );
39
+ const db = this.context.drizzle();
40
+ // Create a payment record with 'pending' status
41
+ await db
42
+ .insert(payments)
43
+ .values({
44
+ id: `pay_${checkoutId}`,
45
+ userId: checkout.customer, // Assuming customer ID is the userId
46
+ checkoutId,
47
+ amount,
48
+ status: 'pending',
49
+ createdAt: new Date(),
50
+ updatedAt: new Date(),
51
+ })
52
+ .onConflictDoNothing(); // Avoid duplicate if already created
53
+
54
+ console.log(`✅ Created payment record for checkoutId: ${checkoutId}`);
55
+ }
56
+
57
+ private async handleCheckoutPaid(checkout: any): Promise<void> {
58
+ const checkoutId = checkout.id;
59
+ const db = this.context.drizzle();
60
+ // Update userProducts to 'unlocked'
61
+ await db
62
+ .update(userProducts)
63
+ .set({ status: 'unlocked', updatedAt: new Date() })
64
+ .where(eq(userProducts.checkoutId, checkoutId));
65
+
66
+ // Update payment to 'completed'
67
+ await db
68
+ .update(payments)
69
+ .set({ status: 'completed', updatedAt: new Date() })
70
+ .where(eq(payments.checkoutId, checkoutId));
71
+
72
+ console.log(
73
+ `✅ Updated userProducts to unlocked and payment to completed for checkoutId: ${checkoutId}`
74
+ );
75
+ }
76
+
77
+ private async handleCheckoutCanceled(checkout: any): Promise<void> {
78
+ const checkoutId = checkout.id;
79
+ const db = this.context.drizzle();
80
+ // Update userProducts to 'locked'
81
+ await db
82
+ .update(userProducts)
83
+ .set({ status: 'locked', updatedAt: new Date() })
84
+ .where(eq(userProducts.checkoutId, checkoutId));
85
+
86
+ // Update payment to 'canceled'
87
+ await db
88
+ .update(payments)
89
+ .set({ status: 'canceled', updatedAt: new Date() })
90
+ .where(eq(payments.checkoutId, checkoutId));
91
+
92
+ console.log(
93
+ `✅ Updated userProducts to locked and payment to canceled for checkoutId: ${checkoutId}`
94
+ );
95
+ }
96
+
97
+ private async handleCheckoutExpired(checkout: any): Promise<void> {
98
+ const checkoutId = checkout.id;
99
+ const db = this.context.drizzle();
100
+ // Update userProducts to 'locked'
101
+ await db
102
+ .update(userProducts)
103
+ .set({ status: 'locked', updatedAt: new Date() })
104
+ .where(eq(userProducts.checkoutId, checkoutId));
105
+
106
+ // Update payment to 'expired'
107
+ await db
108
+ .update(payments)
109
+ .set({ status: 'expired', updatedAt: new Date() })
110
+ .where(eq(payments.checkoutId, checkoutId));
111
+
112
+ console.log(
113
+ `✅ Updated userProducts to locked and payment to expired for checkoutId: ${checkoutId}`
114
+ );
115
+ }
116
+ }
@@ -3,3 +3,4 @@ export * from './ArtifactService';
3
3
  export * from './ConceptService';
4
4
  export * from './ImageDescriberService';
5
5
  export * from './LeonardoService';
6
+ export * from './PaymentService';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zodic/shared",
3
- "version": "0.0.392",
3
+ "version": "0.0.394",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -663,44 +663,3 @@ export interface ProductLabels {
663
663
  };
664
664
  }
665
665
 
666
- export const productLabels: ProductLabels = {
667
- 'premium-astrology-reports': {
668
- 'pt-br': {
669
- name: 'Leitura Astrológica Premium',
670
- description: 'Desbloqueie relatórios de posições avançadas, casas e aspectos.',
671
- },
672
- 'en-us': {
673
- name: 'Premium Astrology Reports',
674
- description: 'Unlock advanced reports, houses, and aspects.',
675
- },
676
- },
677
- concepts: {
678
- 'pt-br': {
679
- name: 'Conceitos',
680
- description:
681
- 'Desbloqueie todos os Conceitos.',
682
- },
683
- 'en-us': {
684
- name: 'Concepts',
685
- description: 'Unlock all Concepts.',
686
- },
687
- },
688
- 'cosmic-mirror': {
689
- 'pt-br': {
690
- name: 'Espelho Cósmico',
691
- description: 'Contemple-se com os olhos do Universo.',
692
- },
693
- 'en-us': {
694
- name: 'Cosmic Mirror',
695
- description: 'See yourself as the Universe sees you.',
696
- },
697
- },
698
- };
699
-
700
- export const getProductLabel = (slug: string, lang: Languages) => {
701
- const labels = productLabels[slug] || {
702
- 'pt-br': { name: slug, description: '' },
703
- 'en-us': { name: slug, description: '' },
704
- };
705
- return labels[lang];
706
- };
@@ -0,0 +1,46 @@
1
+ import { Languages, ProductLabels } from "../types";
2
+
3
+ export const productLabels: ProductLabels = {
4
+ 'premium-astrology-reports': {
5
+ 'pt-br': {
6
+ name: 'Leitura Astrológica Premium',
7
+ description: 'Desbloqueie relatórios de posições avançadas, casas e aspectos.',
8
+ },
9
+ 'en-us': {
10
+ name: 'Premium Astrology Reports',
11
+ description: 'Unlock advanced reports, houses, and aspects.',
12
+ },
13
+ },
14
+ concepts: {
15
+ 'pt-br': {
16
+ name: 'Conceitos',
17
+ description:
18
+ 'Desbloqueie todos os Conceitos.',
19
+ },
20
+ 'en-us': {
21
+ name: 'Concepts',
22
+ description: 'Unlock all Concepts.',
23
+ },
24
+ },
25
+ 'cosmic-mirror': {
26
+ 'pt-br': {
27
+ name: 'Espelho Cósmico',
28
+ description: 'Contemple-se com os olhos do Universo.',
29
+ },
30
+ 'en-us': {
31
+ name: 'Cosmic Mirror',
32
+ description: 'See yourself as the Universe sees you.',
33
+ },
34
+ },
35
+ };
36
+
37
+ export const getProductLabel = (slug: string, lang: Languages) => {
38
+ console.log('getProductLabel -> Slug: ', slug)
39
+ console.log('getProductLabel -> Lang: ', lang)
40
+ const labels = productLabels[slug] || {
41
+ 'pt-br': { name: slug, description: '' },
42
+ 'en-us': { name: slug, description: '' },
43
+ };
44
+ console.log('getProductLabel -> Labels: ', labels)
45
+ return labels[lang];
46
+ };