@explorins/pers-sdk 1.0.0-alpha.1 → 1.1.2
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.
- package/config/domains.js +22 -0
- package/explorins-pers-sdk-1.0.0-alpha.1.tgz +0 -0
- package/package.json +116 -23
- package/rollup.config.js +50 -54
- package/scripts/copy-declarations.js +147 -0
- package/src/analytics/api/analytics-api.ts +24 -0
- package/src/analytics/index.ts +52 -0
- package/src/analytics/models/index.ts +74 -0
- package/src/analytics/services/analytics-service.ts +28 -0
- package/src/auth-admin/api/auth-admin-api.ts +42 -0
- package/src/auth-admin/index.ts +47 -0
- package/src/auth-admin/services/auth-admin-service.ts +36 -0
- package/src/business/api/business-api.ts +181 -19
- package/src/business/index.ts +4 -3
- package/src/business/models/index.ts +4 -4
- package/src/business/services/business-service.ts +1 -1
- package/src/campaign/api/campaign-api.ts +376 -0
- package/src/campaign/index.ts +67 -0
- package/src/campaign/services/campaign-service.ts +164 -0
- package/src/core/abstractions/http-client.ts +1 -0
- package/src/core/auth/auth-provider.interface.ts +2 -2
- package/src/core/auth/create-auth-provider.ts +6 -6
- package/src/core/index.ts +33 -0
- package/src/core/pers-api-client.ts +211 -19
- package/src/core/pers-config.ts +34 -7
- package/src/core/utils/jwt.function.ts +24 -0
- package/src/donation/api/donation-api.ts +24 -0
- package/src/donation/index.ts +47 -0
- package/src/donation/models/index.ts +11 -0
- package/src/donation/services/donation-service.ts +25 -0
- package/src/index.ts +40 -1
- package/src/payment/api/payment-api.ts +185 -0
- package/src/payment/index.ts +64 -0
- package/src/payment/models/index.ts +29 -0
- package/src/payment/services/payment-service.ts +70 -0
- package/src/redemption/api/redemption-api.ts +241 -0
- package/src/redemption/index.ts +60 -0
- package/src/redemption/models/index.ts +17 -0
- package/src/redemption/services/redemption-service.ts +103 -0
- package/src/shared/interfaces/pers-shared-lib.interfaces.ts +99 -0
- package/src/tenant/api/tenant-api.ts +92 -0
- package/src/tenant/index.ts +61 -0
- package/src/tenant/models/index.ts +20 -0
- package/src/tenant/services/tenant-service.ts +78 -0
- package/src/token/api/token-api.ts +129 -0
- package/src/token/base/base-token-service.ts +167 -0
- package/src/token/index.ts +38 -0
- package/src/token/models/index.ts +30 -0
- package/src/token/services/token-service.ts +125 -0
- package/src/token/token-sdk.ts +231 -0
- package/src/transaction/api/transaction-api.ts +296 -0
- package/src/transaction/index.ts +65 -0
- package/src/transaction/models/index.ts +60 -0
- package/src/transaction/services/transaction-service.ts +104 -0
- package/src/user/api/user-api.ts +98 -0
- package/src/user/index.ts +62 -0
- package/src/user/models/index.ts +10 -0
- package/src/user/services/user-service.ts +75 -0
- package/src/user-status/api/user-status-api.ts +78 -0
- package/src/user-status/index.ts +55 -0
- package/src/user-status/models/index.ts +11 -0
- package/src/user-status/services/user-status-service.ts +51 -0
- package/src/web3/api/web3-api.ts +68 -0
- package/src/web3/index.ts +38 -0
- package/src/web3/models/index.ts +150 -0
- package/src/web3/services/web3-service.ts +338 -0
- package/src/web3-chain/api/web3-chain-api.ts +42 -0
- package/src/web3-chain/index.ts +27 -0
- package/src/web3-chain/models/index.ts +45 -0
- package/src/web3-chain/services/getWeb3FCD.service.ts +47 -0
- package/src/web3-chain/services/provider.service.ts +123 -0
- package/src/web3-chain/services/public-http-provider.service.ts +26 -0
- package/src/web3-chain/services/web3-chain-service.ts +131 -0
- package/src/business/business/tsconfig.json +0 -18
- package/src/core/abstractions/core-interfaces.ts +0 -56
- package/src/core/core.ts +0 -30
- package/src/core.ts +0 -30
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { PersApiClient } from '../../core/pers-api-client';
|
|
2
|
+
import {
|
|
3
|
+
PurchaseTokenDTO,
|
|
4
|
+
PurchaseDTO,
|
|
5
|
+
PurchaseCurrency,
|
|
6
|
+
PurchaseCreateRequestDTO,
|
|
7
|
+
PaymentIntentDTO,
|
|
8
|
+
PaymentIntentCreateDTO,
|
|
9
|
+
PurchaseCreateResponseDTO,
|
|
10
|
+
DonationTypeDTO
|
|
11
|
+
} from '../models';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Platform-Agnostic Purchase API Client (RESTful Architecture)
|
|
15
|
+
*
|
|
16
|
+
* Handles purchase and payment operations using the PERS backend's new RESTful endpoints.
|
|
17
|
+
* Uses @explorins/pers-shared DTOs for consistency with backend.
|
|
18
|
+
*
|
|
19
|
+
* Migration Status: Updated to match /purchases controller (replaces /purchase endpoints)
|
|
20
|
+
*
|
|
21
|
+
* Available Access Levels:
|
|
22
|
+
* - PUBLIC: Project key authentication for catalog browsing and payment operations
|
|
23
|
+
* - USER: Requires user authentication JWT (purchase creation, history access)
|
|
24
|
+
* - ADMIN: Requires tenant admin privileges (not implemented in this client)
|
|
25
|
+
*
|
|
26
|
+
* Note: This SDK focuses on backend purchase operations only.
|
|
27
|
+
* Payment provider integrations (Stripe, etc.) should remain in infrastructure layer.
|
|
28
|
+
*/
|
|
29
|
+
export class PurchaseApi {
|
|
30
|
+
constructor(private apiClient: PersApiClient) {}
|
|
31
|
+
|
|
32
|
+
private readonly basePath = '/purchases';
|
|
33
|
+
|
|
34
|
+
// ==========================================
|
|
35
|
+
// PUBLIC OPERATIONS (Project Key)
|
|
36
|
+
// ==========================================
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* PUBLIC: Get purchase tokens (Intelligent Access)
|
|
40
|
+
*
|
|
41
|
+
* RESTful endpoint: GET /purchases/tokens
|
|
42
|
+
* Replaces: GET /purchase/token
|
|
43
|
+
*
|
|
44
|
+
* INTELLIGENT ACCESS:
|
|
45
|
+
* - PUBLIC (Project Key): Returns active tokens only (active parameter ignored)
|
|
46
|
+
* - ADMIN (Tenant Admin JWT): Returns filtered results based on active parameter
|
|
47
|
+
*/
|
|
48
|
+
async getPurchaseTokens(active?: boolean): Promise<PurchaseTokenDTO[]> {
|
|
49
|
+
let url = `${this.basePath}/tokens`;
|
|
50
|
+
if (active !== undefined) {
|
|
51
|
+
url += `?active=${active}`;
|
|
52
|
+
}
|
|
53
|
+
return this.apiClient.get<PurchaseTokenDTO[]>(url);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* PUBLIC: Get donation types
|
|
58
|
+
*
|
|
59
|
+
* RESTful endpoint: GET /purchases/donation-types
|
|
60
|
+
* Replaces: GET /purchase/donation/type
|
|
61
|
+
*/
|
|
62
|
+
async getDonationTypes(): Promise<DonationTypeDTO[]> {
|
|
63
|
+
return this.apiClient.get<DonationTypeDTO[]>(`${this.basePath}/donation-types`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ==========================================
|
|
67
|
+
// PAYMENT OPERATIONS (FINANCIAL - CRITICAL)
|
|
68
|
+
// ==========================================
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* PUBLIC: Create payment intent (FINANCIAL OPERATION)
|
|
72
|
+
*
|
|
73
|
+
* RESTful endpoint: POST /purchases/payment-intents
|
|
74
|
+
* Replaces: POST /purchase/payment-intent
|
|
75
|
+
*
|
|
76
|
+
* CRITICAL: Handles real money operations - tenant context required
|
|
77
|
+
*/
|
|
78
|
+
async createPaymentIntent(amount: number, currency: PurchaseCurrency, receiptEmail: string, description: string): Promise<PaymentIntentDTO> {
|
|
79
|
+
const body: PaymentIntentCreateDTO = {
|
|
80
|
+
amount,
|
|
81
|
+
currency,
|
|
82
|
+
receiptEmail,
|
|
83
|
+
description
|
|
84
|
+
};
|
|
85
|
+
return this.apiClient.post<PaymentIntentDTO>(`${this.basePath}/payment-intents`, body);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* PUBLIC: Update payment intent (FINANCIAL OPERATION)
|
|
90
|
+
*
|
|
91
|
+
* RESTful endpoint: PUT /purchases/payment-intents/{paymentIntentId}
|
|
92
|
+
* Replaces: PUT /purchase/payment-intent/{paymentIntentId}
|
|
93
|
+
*
|
|
94
|
+
* CRITICAL: Handles real money operations - tenant context required
|
|
95
|
+
*/
|
|
96
|
+
async updatePaymentIntent(paymentIntentId: string, amount: number, currency: PurchaseCurrency, receiptEmail: string, description: string): Promise<PaymentIntentDTO> {
|
|
97
|
+
const body: PaymentIntentCreateDTO = {
|
|
98
|
+
amount,
|
|
99
|
+
currency,
|
|
100
|
+
receiptEmail,
|
|
101
|
+
description
|
|
102
|
+
};
|
|
103
|
+
return this.apiClient.put<PaymentIntentDTO>(`${this.basePath}/payment-intents/${paymentIntentId}`, body);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* PUBLIC: Cancel payment intent (FINANCIAL OPERATION)
|
|
108
|
+
*
|
|
109
|
+
* RESTful endpoint: DELETE /purchases/payment-intents/{paymentIntentId}
|
|
110
|
+
* Replaces: DELETE /purchase/payment-intent/{paymentIntentId}
|
|
111
|
+
*
|
|
112
|
+
* CRITICAL: Handles real money operations - tenant context required
|
|
113
|
+
*/
|
|
114
|
+
async cancelPaymentIntent(paymentIntentId: string): Promise<PaymentIntentDTO> {
|
|
115
|
+
return this.apiClient.delete<PaymentIntentDTO>(`${this.basePath}/payment-intents/${paymentIntentId}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ==========================================
|
|
119
|
+
// USER OPERATIONS (JWT + Project Key)
|
|
120
|
+
// ==========================================
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* USER: Create purchase (BUSINESS CRITICAL - FINANCIAL TRANSACTION)
|
|
124
|
+
*
|
|
125
|
+
* RESTful endpoint: POST /purchases
|
|
126
|
+
* Replaces: POST /purchase/auth
|
|
127
|
+
*
|
|
128
|
+
* USER-ONLY: Requires user authentication JWT for purchase creation
|
|
129
|
+
* CRITICAL: Real financial transaction with Stripe integration
|
|
130
|
+
*/
|
|
131
|
+
async createUserPurchase(paymentIntentId: string, amount: number, purchaseTokenId?: string, donationTypeId?: number, donationAccountAddress?: string): Promise<PurchaseCreateResponseDTO> {
|
|
132
|
+
const body: PurchaseCreateRequestDTO = {
|
|
133
|
+
quantity: amount,
|
|
134
|
+
purchaseTokenId: purchaseTokenId || '',
|
|
135
|
+
donationTypeId,
|
|
136
|
+
donationAccountAddress,
|
|
137
|
+
paymentIntentId
|
|
138
|
+
};
|
|
139
|
+
return this.apiClient.post<PurchaseCreateResponseDTO>(`${this.basePath}`, body);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* USER: Get user purchase history
|
|
144
|
+
*
|
|
145
|
+
* RESTful endpoint: GET /purchases/me/history
|
|
146
|
+
* Replaces: GET /purchase/auth
|
|
147
|
+
*
|
|
148
|
+
* USER-ONLY: Get authenticated user's purchase history
|
|
149
|
+
* FINANCIAL RECORDS: User attribution critical for compliance
|
|
150
|
+
*/
|
|
151
|
+
async getUserPurchaseHistory(): Promise<PurchaseDTO[]> {
|
|
152
|
+
return this.apiClient.get<PurchaseDTO[]>(`${this.basePath}/me/history`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// ==========================================
|
|
156
|
+
// CONVENIENCE METHODS (Backward Compatibility)
|
|
157
|
+
// ==========================================
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @deprecated Use getPurchaseTokens() instead
|
|
161
|
+
* Backward compatibility alias for getActivePurchaseTokens
|
|
162
|
+
*/
|
|
163
|
+
async getActivePurchaseTokens(active: boolean = true): Promise<PurchaseTokenDTO[]> {
|
|
164
|
+
return this.getPurchaseTokens(active);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @deprecated Use createUserPurchase() instead
|
|
169
|
+
* Backward compatibility alias for createPurchase
|
|
170
|
+
*/
|
|
171
|
+
async createPurchase(paymentIntentId: string, amount: number, purchaseTokenId?: string, donationTypeId?: number, donationAccountAddress?: string): Promise<PurchaseCreateResponseDTO> {
|
|
172
|
+
return this.createUserPurchase(paymentIntentId, amount, purchaseTokenId, donationTypeId, donationAccountAddress);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @deprecated Use getUserPurchaseHistory() instead
|
|
177
|
+
* Backward compatibility alias for getAllUserPurchases
|
|
178
|
+
*/
|
|
179
|
+
async getAllUserPurchases(): Promise<PurchaseDTO[]> {
|
|
180
|
+
return this.getUserPurchaseHistory();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Backward compatibility - export as PaymentApi as well
|
|
185
|
+
export { PurchaseApi as PaymentApi };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @explorins/pers-sdk-payment
|
|
3
|
+
*
|
|
4
|
+
* Platform-agnostic Payment Domain SDK for PERS ecosystem
|
|
5
|
+
* Handles payment intents, purchases, and purchase tokens
|
|
6
|
+
*
|
|
7
|
+
* Note: Payment provider integrations (Stripe, etc.) are kept separate
|
|
8
|
+
* in the infrastructure layer to maintain platform-agnostic principles.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// API Layer
|
|
12
|
+
export { PaymentApi } from './api/payment-api';
|
|
13
|
+
|
|
14
|
+
// Service Layer
|
|
15
|
+
export { PaymentService } from './services/payment-service';
|
|
16
|
+
|
|
17
|
+
// Models & Types
|
|
18
|
+
export * from './models';
|
|
19
|
+
export * from '../shared/interfaces/pers-shared-lib.interfaces';
|
|
20
|
+
|
|
21
|
+
// Models & Types
|
|
22
|
+
export * from './models';
|
|
23
|
+
|
|
24
|
+
// Factory function for creating payment SDK instance
|
|
25
|
+
import { PersApiClient } from '../core/pers-api-client';
|
|
26
|
+
import { PaymentApi } from './api/payment-api';
|
|
27
|
+
import { PurchaseCurrency } from './models';
|
|
28
|
+
import { PaymentService } from './services/payment-service';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Create a complete Payment SDK instance
|
|
32
|
+
*
|
|
33
|
+
* @param apiClient - Configured PERS API client
|
|
34
|
+
* @returns Payment SDK with flattened structure for better DX
|
|
35
|
+
*/
|
|
36
|
+
export function createPaymentSDK(apiClient: PersApiClient) {
|
|
37
|
+
const paymentApi = new PaymentApi(apiClient);
|
|
38
|
+
const paymentService = new PaymentService(paymentApi);
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
// Direct access to service methods (primary interface)
|
|
42
|
+
|
|
43
|
+
// Public methods
|
|
44
|
+
getActivePurchaseTokens: (active?: boolean) => paymentService.getActivePurchaseTokens(active),
|
|
45
|
+
// ✅ FIXED: Proper type instead of any
|
|
46
|
+
createPaymentIntent: (amount: number, currency: PurchaseCurrency, receiptEmail: string, description: string) =>
|
|
47
|
+
paymentService.createPaymentIntent(amount, currency, receiptEmail, description),
|
|
48
|
+
// ✅ FIXED: Proper type instead of any
|
|
49
|
+
updatePaymentIntent: (paymentIntentId: string, amount: number, currency: PurchaseCurrency, receiptEmail: string, description: string) =>
|
|
50
|
+
paymentService.updatePaymentIntent(paymentIntentId, amount, currency, receiptEmail, description),
|
|
51
|
+
cancelPaymentIntent: (paymentIntentId: string) => paymentService.cancelPaymentIntent(paymentIntentId),
|
|
52
|
+
|
|
53
|
+
// Auth methods
|
|
54
|
+
createPurchase: (paymentIntentId: string, amount: number, purchaseTokenId?: string, donationTypeId?: number, donationAccountAddress?: string) =>
|
|
55
|
+
paymentService.createPurchase(paymentIntentId, amount, purchaseTokenId, donationTypeId, donationAccountAddress),
|
|
56
|
+
getAllUserPurchases: () => paymentService.getAllUserPurchases(),
|
|
57
|
+
|
|
58
|
+
// Advanced access for edge cases
|
|
59
|
+
api: paymentApi,
|
|
60
|
+
service: paymentService
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type PaymentSDK = ReturnType<typeof createPaymentSDK>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment Domain Models
|
|
3
|
+
*
|
|
4
|
+
* Re-exports from centralized pers-shared interfaces for consistency with backend
|
|
5
|
+
* and to provide a single import source for payment-related types.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Core payment entities from centralized pers-shared interfaces
|
|
9
|
+
export type {
|
|
10
|
+
UserDTO,
|
|
11
|
+
PurchaseDTO,
|
|
12
|
+
PurchaseTokenDTO,
|
|
13
|
+
PurchaseTypeDTO,
|
|
14
|
+
PaymentIntentDTO,
|
|
15
|
+
PurchaseCreateRequestDTO,
|
|
16
|
+
PurchaseCreateResponseDTO,
|
|
17
|
+
PaymentIntentCreateDTO,
|
|
18
|
+
PurchaseCurrency,
|
|
19
|
+
DonationTypeDTO
|
|
20
|
+
} from '../../shared/interfaces/pers-shared-lib.interfaces';
|
|
21
|
+
|
|
22
|
+
// Import for local interface usage
|
|
23
|
+
import { PaymentIntentCreateDTO } from '../../shared/interfaces/pers-shared-lib.interfaces';
|
|
24
|
+
|
|
25
|
+
// Local payment interfaces
|
|
26
|
+
export interface PaymentIntentUpdateDTO extends PaymentIntentCreateDTO {
|
|
27
|
+
paymentIntentId: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { PaymentApi } from '../api/payment-api';
|
|
2
|
+
import {
|
|
3
|
+
PurchaseTokenDTO,
|
|
4
|
+
PurchaseDTO,
|
|
5
|
+
PurchaseCurrency,
|
|
6
|
+
PaymentIntentDTO,
|
|
7
|
+
} from '../models';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Platform-Agnostic Payment Service
|
|
11
|
+
*
|
|
12
|
+
* Contains payment business logic and operations that work across platforms.
|
|
13
|
+
* No framework dependencies - pure TypeScript business logic.
|
|
14
|
+
*
|
|
15
|
+
* Focuses only on actual backend capabilities.
|
|
16
|
+
* Payment provider logic (Stripe, etc.) should remain in infrastructure layer.
|
|
17
|
+
*/
|
|
18
|
+
export class PaymentService {
|
|
19
|
+
constructor(private paymentApi: PaymentApi) {}
|
|
20
|
+
|
|
21
|
+
// ==========================================
|
|
22
|
+
// PUBLIC OPERATIONS
|
|
23
|
+
// ==========================================
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* PUBLIC: Get active purchase tokens
|
|
27
|
+
*/
|
|
28
|
+
async getActivePurchaseTokens(active: boolean = true): Promise<PurchaseTokenDTO[]> {
|
|
29
|
+
return this.paymentApi.getActivePurchaseTokens(active);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* PUBLIC: Create payment intent
|
|
34
|
+
*/
|
|
35
|
+
async createPaymentIntent(amount: number, currency: PurchaseCurrency, receiptEmail: string, description: string): Promise<PaymentIntentDTO> {
|
|
36
|
+
return this.paymentApi.createPaymentIntent(amount, currency, receiptEmail, description);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* PUBLIC: Update payment intent
|
|
41
|
+
*/
|
|
42
|
+
async updatePaymentIntent(paymentIntentId: string, amount: number, currency: PurchaseCurrency, receiptEmail: string, description: string): Promise<PaymentIntentDTO> {
|
|
43
|
+
return this.paymentApi.updatePaymentIntent(paymentIntentId, amount, currency, receiptEmail, description);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* PUBLIC: Cancel payment intent
|
|
48
|
+
*/
|
|
49
|
+
async cancelPaymentIntent(paymentIntentId: string): Promise<PaymentIntentDTO> {
|
|
50
|
+
return this.paymentApi.cancelPaymentIntent(paymentIntentId);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ==========================================
|
|
54
|
+
// AUTHENTICATED OPERATIONS
|
|
55
|
+
// ==========================================
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* AUTH: Create purchase
|
|
59
|
+
*/
|
|
60
|
+
async createPurchase(paymentIntentId: string, amount: number, purchaseTokenId?: string, donationTypeId?: number, donationAccountAddress?: string): Promise<PurchaseDTO> {
|
|
61
|
+
return this.paymentApi.createPurchase(paymentIntentId, amount, purchaseTokenId, donationTypeId, donationAccountAddress);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* AUTH: Get all user purchases
|
|
66
|
+
*/
|
|
67
|
+
async getAllUserPurchases(): Promise<PurchaseDTO[]> {
|
|
68
|
+
return this.paymentApi.getAllUserPurchases();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { TokenUnitCreateRequestDTO, RedemptionTypeCreateRequestDTO } from '../../shared/interfaces/pers-shared-lib.interfaces';
|
|
2
|
+
import { PersApiClient } from '../../core/pers-api-client';
|
|
3
|
+
import {
|
|
4
|
+
RedemptionDTO,
|
|
5
|
+
RedemptionCreateRequestDTO,
|
|
6
|
+
RedemptionRedeemRequestDTO,
|
|
7
|
+
RedemptionTypeDTO,
|
|
8
|
+
RedemptionUserRedeemDTO,
|
|
9
|
+
RedemptionUserRedeemDetailedDTO
|
|
10
|
+
} from '../../shared/interfaces/pers-shared-lib.interfaces';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Platform-Agnostic Redemption API Client (UPDATED - RESTful Design)
|
|
14
|
+
*
|
|
15
|
+
* Updated to work with the new RESTful /redemptions endpoints.
|
|
16
|
+
* Handles redemption operations using the PERS backend with intelligent access detection.
|
|
17
|
+
* Uses @explorins/pers-shared DTOs for consistency with backend.
|
|
18
|
+
*
|
|
19
|
+
* Migration Update: Updated all endpoints from /redemption to /redemptions
|
|
20
|
+
* - Removed role revelation from URLs (no more /admin, /auth paths)
|
|
21
|
+
* - Added intelligent access detection for unified endpoints
|
|
22
|
+
* - Updated toggle endpoint to follow /status pattern
|
|
23
|
+
* - Enhanced redemption process with path-based IDs
|
|
24
|
+
*/
|
|
25
|
+
export class RedemptionApi {
|
|
26
|
+
constructor(private apiClient: PersApiClient) {}
|
|
27
|
+
|
|
28
|
+
private readonly basePath = '/redemptions';
|
|
29
|
+
|
|
30
|
+
// ==========================================
|
|
31
|
+
// PUBLIC OPERATIONS (Project Key)
|
|
32
|
+
// ==========================================
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* PUBLIC: Get redemptions (intelligent access)
|
|
36
|
+
*
|
|
37
|
+
* NEW: Intelligent endpoint that adapts based on authentication
|
|
38
|
+
* - Public users: Get active redemptions only
|
|
39
|
+
* - Admin users: Get all redemptions with optional filtering
|
|
40
|
+
*
|
|
41
|
+
* Replaces: getActiveRedemptions() + getRedemptionsAsAdmin()
|
|
42
|
+
*/
|
|
43
|
+
async getRedemptions(active?: boolean): Promise<RedemptionDTO[]> {
|
|
44
|
+
let url = `${this.basePath}`;
|
|
45
|
+
if (active !== undefined) {
|
|
46
|
+
url += `?active=${active}`;
|
|
47
|
+
}
|
|
48
|
+
return this.apiClient.get<RedemptionDTO[]>(url);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* PUBLIC: Get active redemptions
|
|
53
|
+
*
|
|
54
|
+
* Updated: Now uses unified endpoint (backward compatibility)
|
|
55
|
+
*/
|
|
56
|
+
async getActiveRedemptions(): Promise<RedemptionDTO[]> {
|
|
57
|
+
return this.getRedemptions(); // Will return active only for public access
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* PUBLIC: Get redemption types
|
|
62
|
+
*
|
|
63
|
+
* Updated: /redemption/type → /redemptions/types
|
|
64
|
+
*/
|
|
65
|
+
async getRedemptionTypes(): Promise<RedemptionTypeDTO[]> {
|
|
66
|
+
return this.apiClient.get<RedemptionTypeDTO[]>(`${this.basePath}/types`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* PUBLIC: Get redemption by ID
|
|
71
|
+
*
|
|
72
|
+
* Updated: /redemption/:id → /redemptions/:id
|
|
73
|
+
*/
|
|
74
|
+
async getRedemptionById(id: string): Promise<RedemptionDTO> {
|
|
75
|
+
return this.apiClient.get<RedemptionDTO>(`${this.basePath}/${id}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* PUBLIC: Get available supply for redemption
|
|
80
|
+
*
|
|
81
|
+
* Updated: /redemption/:id/available-supply → /redemptions/:id/available-supply
|
|
82
|
+
*/
|
|
83
|
+
async getRedemptionAvailableSupply(id: string): Promise<number> {
|
|
84
|
+
return this.apiClient.get<number>(`${this.basePath}/${id}/available-supply`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ==========================================
|
|
88
|
+
// USER OPERATIONS (JWT + Project Key)
|
|
89
|
+
// ==========================================
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* USER: Redeem a redemption
|
|
93
|
+
*
|
|
94
|
+
* Updated: /redemption/auth/redeem → /redemptions/:id/redeem
|
|
95
|
+
* Enhanced: Path-based redemption ID for better RESTful design
|
|
96
|
+
*/
|
|
97
|
+
async redeemRedemption(redemptionId: string): Promise<RedemptionUserRedeemDetailedDTO> {
|
|
98
|
+
const body: RedemptionRedeemRequestDTO = {
|
|
99
|
+
redemptionId: redemptionId,
|
|
100
|
+
};
|
|
101
|
+
return this.apiClient.post<RedemptionUserRedeemDetailedDTO>(`${this.basePath}/${redemptionId}/redeem`, body);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* USER: Get user redemption history
|
|
106
|
+
*
|
|
107
|
+
* Updated: /redemption/auth/redeem → /redemptions/me/history
|
|
108
|
+
*/
|
|
109
|
+
async getUserRedemptionHistory(): Promise<RedemptionUserRedeemDTO[]> {
|
|
110
|
+
return this.apiClient.get<RedemptionUserRedeemDTO[]>(`${this.basePath}/me/history`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* USER: Get user redemptions (backward compatibility)
|
|
115
|
+
*
|
|
116
|
+
* Deprecated: Use getUserRedemptionHistory() instead
|
|
117
|
+
*/
|
|
118
|
+
async getUserRedeems(): Promise<RedemptionUserRedeemDTO[]> {
|
|
119
|
+
return this.getUserRedemptionHistory();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ==========================================
|
|
123
|
+
// ADMIN OPERATIONS (Tenant Admin JWT)
|
|
124
|
+
// ==========================================
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* ADMIN: Get redemptions with filtering (using intelligent endpoint)
|
|
128
|
+
*
|
|
129
|
+
* Updated: /redemption/admin → /redemptions (intelligent access detection)
|
|
130
|
+
* The unified endpoint will detect admin privileges and allow filtering
|
|
131
|
+
*/
|
|
132
|
+
async getRedemptionsAsAdmin(active?: boolean): Promise<RedemptionDTO[]> {
|
|
133
|
+
return this.getRedemptions(active); // Uses intelligent endpoint
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* ADMIN: Create redemption
|
|
138
|
+
*
|
|
139
|
+
* Updated: /redemption/admin → /redemptions
|
|
140
|
+
*/
|
|
141
|
+
async createRedemption(redemption: RedemptionCreateRequestDTO): Promise<RedemptionDTO> {
|
|
142
|
+
return this.apiClient.post<RedemptionDTO>(`${this.basePath}`, redemption);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* ADMIN: Update redemption
|
|
147
|
+
*
|
|
148
|
+
* Updated: /redemption/admin/:id → /redemptions/:id
|
|
149
|
+
*/
|
|
150
|
+
async updateRedemption(id: string, redemptionCreateRequest: RedemptionCreateRequestDTO): Promise<RedemptionDTO> {
|
|
151
|
+
return this.apiClient.put<RedemptionDTO>(`${this.basePath}/${id}`, redemptionCreateRequest);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* ADMIN: Toggle redemption status
|
|
156
|
+
*
|
|
157
|
+
* Updated: /redemption/admin/:id/toggle-active → /redemptions/:id/status
|
|
158
|
+
* Following standard /status pattern used across domains
|
|
159
|
+
*/
|
|
160
|
+
async toggleRedemptionStatus(redemptionId: string): Promise<RedemptionDTO> {
|
|
161
|
+
return this.apiClient.put<RedemptionDTO>(`${this.basePath}/${redemptionId}/status`, {});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* ADMIN: Toggle redemption active (backward compatibility)
|
|
166
|
+
*
|
|
167
|
+
* Deprecated: Use toggleRedemptionStatus() instead
|
|
168
|
+
*/
|
|
169
|
+
async toggleRedemptionActive(redemptionId: string): Promise<RedemptionDTO> {
|
|
170
|
+
return this.toggleRedemptionStatus(redemptionId);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* ADMIN: Delete redemption
|
|
175
|
+
*
|
|
176
|
+
* Updated: /redemption/admin/:id → /redemptions/:id
|
|
177
|
+
*/
|
|
178
|
+
async deleteRedemption(id: string): Promise<boolean> {
|
|
179
|
+
return this.apiClient.delete<boolean>(`${this.basePath}/${id}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* ADMIN: Create redemption type
|
|
184
|
+
*
|
|
185
|
+
* Updated: /redemption/admin/type → /redemptions/types
|
|
186
|
+
*/
|
|
187
|
+
async createRedemptionType(redemptionType: RedemptionTypeCreateRequestDTO): Promise<RedemptionTypeDTO> {
|
|
188
|
+
return this.apiClient.post<RedemptionTypeDTO>(`${this.basePath}/types`, redemptionType);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ==========================================
|
|
192
|
+
// TOKEN UNIT MANAGEMENT (Admin)
|
|
193
|
+
// ==========================================
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* ADMIN: Create redemption token unit
|
|
197
|
+
*
|
|
198
|
+
* Updated: /redemption/admin/:id/token-units → /redemptions/:id/token-units
|
|
199
|
+
*/
|
|
200
|
+
async createRedemptionTokenUnit(redemptionId: string, redemptionTokenUnit: TokenUnitCreateRequestDTO): Promise<RedemptionDTO> {
|
|
201
|
+
return this.apiClient.post<RedemptionDTO>(`${this.basePath}/${redemptionId}/token-units`, redemptionTokenUnit);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* ADMIN: Update redemption token unit
|
|
206
|
+
*
|
|
207
|
+
* Updated: /redemption/admin/:id/token-units/:tokenUnitId → /redemptions/:id/token-units/:tokenUnitId
|
|
208
|
+
*/
|
|
209
|
+
async updateRedemptionTokenUnit(redemptionId: string, tokenUnitId: string, redemptionTokenUnit: TokenUnitCreateRequestDTO): Promise<RedemptionDTO> {
|
|
210
|
+
return this.apiClient.put<RedemptionDTO>(`${this.basePath}/${redemptionId}/token-units/${tokenUnitId}`, redemptionTokenUnit);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* ADMIN: Delete redemption token unit
|
|
215
|
+
*
|
|
216
|
+
* Updated: /redemption/admin/:id/token-units/:tokenUnitId → /redemptions/:id/token-units/:tokenUnitId
|
|
217
|
+
*/
|
|
218
|
+
async deleteRedemptionTokenUnit(redemptionId: string, redemptionTokenUnitId: string): Promise<RedemptionDTO> {
|
|
219
|
+
return this.apiClient.delete<RedemptionDTO>(`${this.basePath}/${redemptionId}/token-units/${redemptionTokenUnitId}`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ==========================================
|
|
223
|
+
// BACKWARD COMPATIBILITY METHODS
|
|
224
|
+
// ==========================================
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @deprecated Use getRedemptions() instead
|
|
228
|
+
* Backward compatibility for old admin endpoint
|
|
229
|
+
*/
|
|
230
|
+
async getRedemptionsAdmin(active?: boolean): Promise<RedemptionDTO[]> {
|
|
231
|
+
return this.getRedemptionsAsAdmin(active);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* @deprecated Use redeemRedemption() instead
|
|
236
|
+
* Backward compatibility for old redeem method
|
|
237
|
+
*/
|
|
238
|
+
async redeem(redemptionId: string): Promise<RedemptionUserRedeemDetailedDTO> {
|
|
239
|
+
return this.redeemRedemption(redemptionId);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @explorins/pers-sdk-redemption
|
|
3
|
+
*
|
|
4
|
+
* Platform-agnostic Redemption Domain SDK for PERS ecosystem
|
|
5
|
+
* Handles redemption operations across different authorization levels
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// API Layer
|
|
9
|
+
export { RedemptionApi } from './api/redemption-api';
|
|
10
|
+
|
|
11
|
+
// Service Layer
|
|
12
|
+
export { RedemptionService } from './services/redemption-service';
|
|
13
|
+
|
|
14
|
+
// Models & Types - re-export all centralized interfaces
|
|
15
|
+
export * from '../shared/interfaces/pers-shared-lib.interfaces';
|
|
16
|
+
|
|
17
|
+
// Models & Types - imported from centralized interfaces
|
|
18
|
+
|
|
19
|
+
import { RedemptionCreateRequestDTO, TokenUnitCreateRequestDTO } from '../shared/interfaces/pers-shared-lib.interfaces';
|
|
20
|
+
// Factory function for creating redemption SDK instance
|
|
21
|
+
import { PersApiClient } from '../core/pers-api-client';
|
|
22
|
+
import { RedemptionApi } from './api/redemption-api';
|
|
23
|
+
import { RedemptionService } from './services/redemption-service';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Create a complete Redemption SDK instance
|
|
27
|
+
*
|
|
28
|
+
* @param apiClient - Configured PERS API client
|
|
29
|
+
* @returns Redemption SDK with flattened structure for better DX
|
|
30
|
+
*/
|
|
31
|
+
export function createRedemptionSDK(apiClient: PersApiClient) {
|
|
32
|
+
const redemptionApi = new RedemptionApi(apiClient);
|
|
33
|
+
const redemptionService = new RedemptionService(redemptionApi);
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
// Direct access to service methods (primary interface)
|
|
37
|
+
|
|
38
|
+
// Public methods
|
|
39
|
+
getActiveRedemptions: () => redemptionService.getActiveRedemptions(),
|
|
40
|
+
getRedemptionTypes: () => redemptionService.getRedemptionTypes(),
|
|
41
|
+
|
|
42
|
+
// Auth methods
|
|
43
|
+
redeemRedemption: (redemptionId: string) => redemptionService.redeemRedemption(redemptionId),
|
|
44
|
+
getUserRedeems: () => redemptionService.getUserRedeems(),
|
|
45
|
+
|
|
46
|
+
// Admin methods
|
|
47
|
+
getRedemptionsAsAdmin: (active?: boolean) => redemptionService.getRedemptionsAsAdmin(active),
|
|
48
|
+
createRedemption: (redemption: RedemptionCreateRequestDTO) => redemptionService.createRedemption(redemption),
|
|
49
|
+
updateRedemption: (id: string, redemptionCreateRequest: RedemptionCreateRequestDTO) => redemptionService.updateRedemption(id, redemptionCreateRequest),
|
|
50
|
+
toggleRedemptionActive: (redemptionId: string) => redemptionService.toggleRedemptionActive(redemptionId),
|
|
51
|
+
createRedemptionTokenUnit: (redemptionId: string, redemptionTokenUnit: TokenUnitCreateRequestDTO) => redemptionService.createRedemptionTokenUnit(redemptionId, redemptionTokenUnit),
|
|
52
|
+
deleteRedemptionTokenUnit: (redemptionId: string, redemptionTokenUnitId: string) => redemptionService.deleteRedemptionTokenUnit(redemptionId, redemptionTokenUnitId),
|
|
53
|
+
|
|
54
|
+
// Advanced access for edge cases
|
|
55
|
+
api: redemptionApi,
|
|
56
|
+
service: redemptionService
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type RedemptionSDK = ReturnType<typeof createRedemptionSDK>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redemption Domain Models
|
|
3
|
+
*
|
|
4
|
+
* Re-exports from centralized pers-shared interfaces for consistency with backend
|
|
5
|
+
* and to provide a single import source for redemption-related types.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Core redemption entities from centralized pers-shared interfaces
|
|
9
|
+
export type {
|
|
10
|
+
RedemptionDTO,
|
|
11
|
+
RedemptionCreateRequestDTO,
|
|
12
|
+
RedemptionRedeemRequestDTO,
|
|
13
|
+
RedemptionTypeDTO,
|
|
14
|
+
RedemptionTypeCreateRequestDTO,
|
|
15
|
+
RedemptionUserRedeemDTO,
|
|
16
|
+
RedemptionUserRedeemDetailedDTO
|
|
17
|
+
} from '../../shared/interfaces/pers-shared-lib.interfaces';
|