@symbo.ls/sdk 3.2.3 → 3.2.7
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/README.md +141 -0
- package/dist/cjs/config/environment.js +94 -10
- package/dist/cjs/index.js +152 -12
- package/dist/cjs/services/AdminService.js +351 -0
- package/dist/cjs/services/AuthService.js +738 -305
- package/dist/cjs/services/BaseService.js +158 -6
- package/dist/cjs/services/BranchService.js +484 -0
- package/dist/cjs/services/CollabService.js +439 -116
- package/dist/cjs/services/DnsService.js +340 -0
- package/dist/cjs/services/FeatureFlagService.js +175 -0
- package/dist/cjs/services/FileService.js +201 -0
- package/dist/cjs/services/IntegrationService.js +538 -0
- package/dist/cjs/services/MetricsService.js +62 -0
- package/dist/cjs/services/PaymentService.js +271 -0
- package/dist/cjs/services/PlanService.js +426 -0
- package/dist/cjs/services/ProjectService.js +1207 -0
- package/dist/cjs/services/PullRequestService.js +503 -0
- package/dist/cjs/services/ScreenshotService.js +304 -0
- package/dist/cjs/services/SubscriptionService.js +396 -0
- package/dist/cjs/services/TrackingService.js +661 -0
- package/dist/cjs/services/WaitlistService.js +148 -0
- package/dist/cjs/services/index.js +60 -4
- package/dist/cjs/state/RootStateManager.js +2 -23
- package/dist/cjs/state/rootEventBus.js +9 -0
- package/dist/cjs/utils/CollabClient.js +78 -12
- package/dist/cjs/utils/TokenManager.js +16 -3
- package/dist/cjs/utils/changePreprocessor.js +199 -0
- package/dist/cjs/utils/jsonDiff.js +46 -4
- package/dist/cjs/utils/ordering.js +309 -0
- package/dist/cjs/utils/services.js +285 -128
- package/dist/cjs/utils/validation.js +0 -3
- package/dist/esm/config/environment.js +94 -10
- package/dist/esm/index.js +47862 -18248
- package/dist/esm/services/AdminService.js +1132 -0
- package/dist/esm/services/AuthService.js +1493 -386
- package/dist/esm/services/BaseService.js +757 -6
- package/dist/esm/services/BranchService.js +1265 -0
- package/dist/esm/services/CollabService.js +24956 -16089
- package/dist/esm/services/DnsService.js +1121 -0
- package/dist/esm/services/FeatureFlagService.js +956 -0
- package/dist/esm/services/FileService.js +982 -0
- package/dist/esm/services/IntegrationService.js +1319 -0
- package/dist/esm/services/MetricsService.js +843 -0
- package/dist/esm/services/PaymentService.js +1052 -0
- package/dist/esm/services/PlanService.js +1207 -0
- package/dist/esm/services/ProjectService.js +2526 -0
- package/dist/esm/services/PullRequestService.js +1284 -0
- package/dist/esm/services/ScreenshotService.js +1085 -0
- package/dist/esm/services/SubscriptionService.js +1177 -0
- package/dist/esm/services/TrackingService.js +18454 -0
- package/dist/esm/services/WaitlistService.js +929 -0
- package/dist/esm/services/index.js +47373 -18027
- package/dist/esm/state/RootStateManager.js +11 -23
- package/dist/esm/state/rootEventBus.js +9 -0
- package/dist/esm/utils/CollabClient.js +17526 -16120
- package/dist/esm/utils/TokenManager.js +16 -3
- package/dist/esm/utils/changePreprocessor.js +542 -0
- package/dist/esm/utils/jsonDiff.js +958 -43
- package/dist/esm/utils/ordering.js +291 -0
- package/dist/esm/utils/services.js +285 -128
- package/dist/esm/utils/validation.js +116 -50
- package/dist/node/config/environment.js +94 -10
- package/dist/node/index.js +183 -16
- package/dist/node/services/AdminService.js +332 -0
- package/dist/node/services/AuthService.js +742 -310
- package/dist/node/services/BaseService.js +148 -6
- package/dist/node/services/BranchService.js +465 -0
- package/dist/node/services/CollabService.js +439 -116
- package/dist/node/services/DnsService.js +321 -0
- package/dist/node/services/FeatureFlagService.js +156 -0
- package/dist/node/services/FileService.js +182 -0
- package/dist/node/services/IntegrationService.js +519 -0
- package/dist/node/services/MetricsService.js +43 -0
- package/dist/node/services/PaymentService.js +252 -0
- package/dist/node/services/PlanService.js +407 -0
- package/dist/node/services/ProjectService.js +1188 -0
- package/dist/node/services/PullRequestService.js +484 -0
- package/dist/node/services/ScreenshotService.js +285 -0
- package/dist/node/services/SubscriptionService.js +377 -0
- package/dist/node/services/TrackingService.js +632 -0
- package/dist/node/services/WaitlistService.js +129 -0
- package/dist/node/services/index.js +60 -4
- package/dist/node/state/RootStateManager.js +2 -23
- package/dist/node/state/rootEventBus.js +9 -0
- package/dist/node/utils/CollabClient.js +77 -11
- package/dist/node/utils/TokenManager.js +16 -3
- package/dist/node/utils/changePreprocessor.js +180 -0
- package/dist/node/utils/jsonDiff.js +46 -4
- package/dist/node/utils/ordering.js +290 -0
- package/dist/node/utils/services.js +285 -128
- package/dist/node/utils/validation.js +0 -3
- package/package.json +30 -18
- package/src/config/environment.js +95 -10
- package/src/index.js +190 -23
- package/src/services/AdminService.js +374 -0
- package/src/services/AuthService.js +874 -328
- package/src/services/BaseService.js +166 -6
- package/src/services/BranchService.js +536 -0
- package/src/services/CollabService.js +557 -148
- package/src/services/DnsService.js +366 -0
- package/src/services/FeatureFlagService.js +174 -0
- package/src/services/FileService.js +213 -0
- package/src/services/IntegrationService.js +548 -0
- package/src/services/MetricsService.js +40 -0
- package/src/services/PaymentService.js +287 -0
- package/src/services/PlanService.js +468 -0
- package/src/services/ProjectService.js +1366 -0
- package/src/services/PullRequestService.js +537 -0
- package/src/services/ScreenshotService.js +258 -0
- package/src/services/SubscriptionService.js +425 -0
- package/src/services/TrackingService.js +853 -0
- package/src/services/WaitlistService.js +130 -0
- package/src/services/index.js +79 -5
- package/src/services/tests/BranchService/createBranch.test.js +153 -0
- package/src/services/tests/BranchService/deleteBranch.test.js +173 -0
- package/src/services/tests/BranchService/getBranchChanges.test.js +146 -0
- package/src/services/tests/BranchService/listBranches.test.js +87 -0
- package/src/services/tests/BranchService/mergeBranch.test.js +210 -0
- package/src/services/tests/BranchService/publishVersion.test.js +183 -0
- package/src/services/tests/BranchService/renameBranch.test.js +240 -0
- package/src/services/tests/BranchService/resetBranch.test.js +152 -0
- package/src/services/tests/FeatureFlagService/adminFeatureFlags.test.js +67 -0
- package/src/services/tests/FeatureFlagService/getFeatureFlags.test.js +75 -0
- package/src/services/tests/FileService/createFileFormData.test.js +74 -0
- package/src/services/tests/FileService/getFileUrl.test.js +69 -0
- package/src/services/tests/FileService/updateProjectIcon.test.js +109 -0
- package/src/services/tests/FileService/uploadDocument.test.js +36 -0
- package/src/services/tests/FileService/uploadFile.test.js +78 -0
- package/src/services/tests/FileService/uploadFileWithValidation.test.js +114 -0
- package/src/services/tests/FileService/uploadImage.test.js +36 -0
- package/src/services/tests/FileService/uploadMultipleFiles.test.js +111 -0
- package/src/services/tests/FileService/validateFile.test.js +63 -0
- package/src/services/tests/PlanService/createPlan.test.js +104 -0
- package/src/services/tests/PlanService/createPlanWithValidation.test.js +523 -0
- package/src/services/tests/PlanService/deletePlan.test.js +92 -0
- package/src/services/tests/PlanService/getActivePlans.test.js +123 -0
- package/src/services/tests/PlanService/getAdminPlans.test.js +84 -0
- package/src/services/tests/PlanService/getPlan.test.js +50 -0
- package/src/services/tests/PlanService/getPlanByKey.test.js +109 -0
- package/src/services/tests/PlanService/getPlanWithValidation.test.js +85 -0
- package/src/services/tests/PlanService/getPlans.test.js +53 -0
- package/src/services/tests/PlanService/getPlansByPriceRange.test.js +109 -0
- package/src/services/tests/PlanService/getPlansWithValidation.test.js +48 -0
- package/src/services/tests/PlanService/initializePlans.test.js +75 -0
- package/src/services/tests/PlanService/updatePlan.test.js +111 -0
- package/src/services/tests/PlanService/updatePlanWithValidation.test.js +556 -0
- package/src/state/RootStateManager.js +37 -32
- package/src/state/rootEventBus.js +19 -0
- package/src/utils/CollabClient.js +99 -12
- package/src/utils/TokenManager.js +20 -3
- package/src/utils/changePreprocessor.js +239 -0
- package/src/utils/jsonDiff.js +40 -5
- package/src/utils/ordering.js +271 -0
- package/src/utils/services.js +306 -139
- package/src/utils/validation.js +0 -3
- package/dist/cjs/services/AIService.js +0 -155
- package/dist/cjs/services/BasedService.js +0 -1185
- package/dist/cjs/services/CoreService.js +0 -2295
- package/dist/cjs/services/SocketService.js +0 -309
- package/dist/cjs/services/SymstoryService.js +0 -571
- package/dist/cjs/utils/basedQuerys.js +0 -181
- package/dist/cjs/utils/symstoryClient.js +0 -259
- package/dist/esm/services/AIService.js +0 -185
- package/dist/esm/services/BasedService.js +0 -5262
- package/dist/esm/services/CoreService.js +0 -2827
- package/dist/esm/services/SocketService.js +0 -456
- package/dist/esm/services/SymstoryService.js +0 -7025
- package/dist/esm/utils/basedQuerys.js +0 -163
- package/dist/esm/utils/symstoryClient.js +0 -354
- package/dist/node/services/AIService.js +0 -136
- package/dist/node/services/BasedService.js +0 -1156
- package/dist/node/services/CoreService.js +0 -2266
- package/dist/node/services/SocketService.js +0 -280
- package/dist/node/services/SymstoryService.js +0 -542
- package/dist/node/utils/basedQuerys.js +0 -162
- package/dist/node/utils/symstoryClient.js +0 -230
- package/src/services/AIService.js +0 -150
- package/src/services/BasedService.js +0 -1302
- package/src/services/CoreService.js +0 -2548
- package/src/services/SocketService.js +0 -336
- package/src/services/SymstoryService.js +0 -649
- package/src/utils/basedQuerys.js +0 -164
- package/src/utils/symstoryClient.js +0 -252
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import { BaseService } from './BaseService.js'
|
|
2
|
+
|
|
3
|
+
export class PlanService extends BaseService {
|
|
4
|
+
// ==================== PLAN METHODS ====================
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get list of public plans (no authentication required)
|
|
8
|
+
*/
|
|
9
|
+
async getPlans () {
|
|
10
|
+
try {
|
|
11
|
+
const response = await this._request('/plans', {
|
|
12
|
+
method: 'GET',
|
|
13
|
+
methodName: 'getPlans'
|
|
14
|
+
})
|
|
15
|
+
if (response.success) {
|
|
16
|
+
return response.data
|
|
17
|
+
}
|
|
18
|
+
throw new Error(response.message)
|
|
19
|
+
} catch (error) {
|
|
20
|
+
throw new Error(`Failed to get plans: ${error.message}`, { cause: error })
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get list of public plans with enhanced pricing information (no authentication required)
|
|
26
|
+
*/
|
|
27
|
+
async getPlansWithPricing () {
|
|
28
|
+
try {
|
|
29
|
+
const response = await this._request('/plans/pricing', {
|
|
30
|
+
method: 'GET',
|
|
31
|
+
methodName: 'getPlansWithPricing'
|
|
32
|
+
})
|
|
33
|
+
if (response.success) {
|
|
34
|
+
return response.data
|
|
35
|
+
}
|
|
36
|
+
throw new Error(response.message)
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new Error(`Failed to get plans with pricing: ${error.message}`, {
|
|
39
|
+
cause: error
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get a specific plan by ID (no authentication required)
|
|
46
|
+
*/
|
|
47
|
+
async getPlan (planId) {
|
|
48
|
+
if (!planId) {
|
|
49
|
+
throw new Error('Plan ID is required')
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const response = await this._request(`/plans/${planId}`, {
|
|
53
|
+
method: 'GET',
|
|
54
|
+
methodName: 'getPlan'
|
|
55
|
+
})
|
|
56
|
+
if (response.success) {
|
|
57
|
+
return response.data
|
|
58
|
+
}
|
|
59
|
+
throw new Error(response.message)
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw new Error(`Failed to get plan: ${error.message}`, { cause: error })
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ==================== ADMIN PLAN METHODS ====================
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get all plans including inactive ones (admin only)
|
|
69
|
+
*/
|
|
70
|
+
async getAdminPlans () {
|
|
71
|
+
this._requireReady('getAdminPlans')
|
|
72
|
+
try {
|
|
73
|
+
const response = await this._request('/admin/plans', {
|
|
74
|
+
method: 'GET',
|
|
75
|
+
methodName: 'getAdminPlans'
|
|
76
|
+
})
|
|
77
|
+
if (response.success) {
|
|
78
|
+
return response.data
|
|
79
|
+
}
|
|
80
|
+
throw new Error(response.message)
|
|
81
|
+
} catch (error) {
|
|
82
|
+
throw new Error(`Failed to get admin plans: ${error.message}`, {
|
|
83
|
+
cause: error
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Create a new plan (admin only)
|
|
90
|
+
*/
|
|
91
|
+
async createPlan (planData) {
|
|
92
|
+
this._requireReady('createPlan')
|
|
93
|
+
if (!planData || typeof planData !== 'object') {
|
|
94
|
+
throw new Error('Plan data is required')
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const response = await this._request('/admin/plans', {
|
|
98
|
+
method: 'POST',
|
|
99
|
+
body: JSON.stringify(planData),
|
|
100
|
+
methodName: 'createPlan'
|
|
101
|
+
})
|
|
102
|
+
if (response.success) {
|
|
103
|
+
return response.data
|
|
104
|
+
}
|
|
105
|
+
throw new Error(response.message)
|
|
106
|
+
} catch (error) {
|
|
107
|
+
throw new Error(`Failed to create plan: ${error.message}`, {
|
|
108
|
+
cause: error
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Update an existing plan (admin only)
|
|
115
|
+
*/
|
|
116
|
+
async updatePlan (planId, planData) {
|
|
117
|
+
this._requireReady('updatePlan')
|
|
118
|
+
if (!planId) {
|
|
119
|
+
throw new Error('Plan ID is required')
|
|
120
|
+
}
|
|
121
|
+
if (!planData || typeof planData !== 'object') {
|
|
122
|
+
throw new Error('Plan data is required')
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
const response = await this._request(`/admin/plans/${planId}`, {
|
|
126
|
+
method: 'PATCH',
|
|
127
|
+
body: JSON.stringify(planData),
|
|
128
|
+
methodName: 'updatePlan'
|
|
129
|
+
})
|
|
130
|
+
if (response.success) {
|
|
131
|
+
return response.data
|
|
132
|
+
}
|
|
133
|
+
throw new Error(response.message)
|
|
134
|
+
} catch (error) {
|
|
135
|
+
throw new Error(`Failed to update plan: ${error.message}`, {
|
|
136
|
+
cause: error
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Delete a plan (soft delete + archive Stripe product) (admin only)
|
|
143
|
+
*/
|
|
144
|
+
async deletePlan (planId) {
|
|
145
|
+
this._requireReady('deletePlan')
|
|
146
|
+
if (!planId) {
|
|
147
|
+
throw new Error('Plan ID is required')
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const response = await this._request(`/admin/plans/${planId}`, {
|
|
151
|
+
method: 'DELETE',
|
|
152
|
+
methodName: 'deletePlan'
|
|
153
|
+
})
|
|
154
|
+
if (response.success) {
|
|
155
|
+
return response.data
|
|
156
|
+
}
|
|
157
|
+
throw new Error(response.message)
|
|
158
|
+
} catch (error) {
|
|
159
|
+
throw new Error(`Failed to delete plan: ${error.message}`, {
|
|
160
|
+
cause: error
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Initialize default plans (admin only)
|
|
167
|
+
*/
|
|
168
|
+
async initializePlans () {
|
|
169
|
+
this._requireReady('initializePlans')
|
|
170
|
+
try {
|
|
171
|
+
const response = await this._request('/admin/plans/initialize', {
|
|
172
|
+
method: 'POST',
|
|
173
|
+
methodName: 'initializePlans'
|
|
174
|
+
})
|
|
175
|
+
if (response.success) {
|
|
176
|
+
return response
|
|
177
|
+
}
|
|
178
|
+
throw new Error(response.message)
|
|
179
|
+
} catch (error) {
|
|
180
|
+
throw new Error(`Failed to initialize plans: ${error.message}`, {
|
|
181
|
+
cause: error
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ==================== PLAN HELPER METHODS ====================
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Helper method to get plans with validation
|
|
190
|
+
*/
|
|
191
|
+
async getPlansWithValidation () {
|
|
192
|
+
try {
|
|
193
|
+
const plans = await this.getPlans()
|
|
194
|
+
if (!Array.isArray(plans)) {
|
|
195
|
+
throw new Error('Invalid response format: plans should be an array')
|
|
196
|
+
}
|
|
197
|
+
return plans
|
|
198
|
+
} catch (error) {
|
|
199
|
+
throw new Error(`Failed to get plans with validation: ${error.message}`, {
|
|
200
|
+
cause: error
|
|
201
|
+
})
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Helper method to get a plan by ID with validation
|
|
207
|
+
*/
|
|
208
|
+
async getPlanWithValidation (planId) {
|
|
209
|
+
if (!planId || typeof planId !== 'string') {
|
|
210
|
+
throw new Error('Plan ID must be a valid string')
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
const plan = await this.getPlan(planId)
|
|
215
|
+
if (!plan || typeof plan !== 'object') {
|
|
216
|
+
throw new Error('Invalid plan data received')
|
|
217
|
+
}
|
|
218
|
+
return plan
|
|
219
|
+
} catch (error) {
|
|
220
|
+
throw new Error(`Failed to get plan with validation: ${error.message}`, {
|
|
221
|
+
cause: error
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Helper method to create a plan with validation (admin only)
|
|
228
|
+
*/
|
|
229
|
+
async createPlanWithValidation (planData) {
|
|
230
|
+
if (!planData || typeof planData !== 'object') {
|
|
231
|
+
throw new Error('Plan data must be a valid object')
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Basic validation for required fields to match server model
|
|
235
|
+
const requiredFields = ['name', 'description']
|
|
236
|
+
for (const field of requiredFields) {
|
|
237
|
+
if (!planData[field]) {
|
|
238
|
+
throw new Error(`Required field '${field}' is missing`)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Legacy field guard: bare "price" is no longer supported on the server
|
|
243
|
+
if (Object.hasOwn(planData, 'price')) {
|
|
244
|
+
throw new Error(
|
|
245
|
+
'Field "price" is no longer supported. Use unified "pricingOptions" with "amount" instead.'
|
|
246
|
+
)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Validate unified pricingOptions structure if provided
|
|
250
|
+
if (planData.pricingOptions != null) {
|
|
251
|
+
if (
|
|
252
|
+
!Array.isArray(planData.pricingOptions) ||
|
|
253
|
+
planData.pricingOptions.length === 0
|
|
254
|
+
) {
|
|
255
|
+
throw new Error(
|
|
256
|
+
'pricingOptions must be a non-empty array when provided'
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const allowedIntervals = new Set(['month', 'year', 'week', 'day', null])
|
|
261
|
+
planData.pricingOptions.forEach((option, index) => {
|
|
262
|
+
if (!option || typeof option !== 'object') {
|
|
263
|
+
throw new Error(`Pricing option at index ${index} must be an object`)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const { key, displayName, amount, interval, lookupKey } = option
|
|
267
|
+
|
|
268
|
+
if (!key || typeof key !== 'string') {
|
|
269
|
+
throw new Error(
|
|
270
|
+
`Pricing option at index ${index} is missing required field 'key'`
|
|
271
|
+
)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Validate key format (alphanumeric and hyphens only)
|
|
275
|
+
if (!/^[a-z0-9-]+$/u.test(key)) {
|
|
276
|
+
throw new Error(
|
|
277
|
+
`Pricing option key '${key}' must contain only lowercase letters, numbers, and hyphens`
|
|
278
|
+
)
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (!displayName || typeof displayName !== 'string') {
|
|
282
|
+
throw new Error(
|
|
283
|
+
`Pricing option '${key}' is missing required field 'displayName'`
|
|
284
|
+
)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (typeof amount !== 'number' || amount < 0) {
|
|
288
|
+
throw new Error(
|
|
289
|
+
`Pricing option '${key}' must have a non-negative numeric 'amount'`
|
|
290
|
+
)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (interval !== null && !allowedIntervals.has(interval)) {
|
|
294
|
+
throw new Error(
|
|
295
|
+
`Pricing option '${key}' has invalid interval '${interval}'. Allowed: month, year, week, day or null`
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (!lookupKey || typeof lookupKey !== 'string') {
|
|
300
|
+
throw new Error(
|
|
301
|
+
`Pricing option '${key}' is missing required field 'lookupKey'`
|
|
302
|
+
)
|
|
303
|
+
}
|
|
304
|
+
})
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Optional: validate top-level key if provided (legacy support)
|
|
308
|
+
if (Object.hasOwn(planData, 'key') && planData.key == null) {
|
|
309
|
+
throw new Error('Plan key must be a valid string')
|
|
310
|
+
}
|
|
311
|
+
if (planData.key && !/^[a-z0-9-]+$/u.test(planData.key)) {
|
|
312
|
+
throw new Error(
|
|
313
|
+
'Plan key must contain only lowercase letters, numbers, and hyphens'
|
|
314
|
+
)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return await this.createPlan(planData)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Helper method to update a plan with validation (admin only)
|
|
322
|
+
*/
|
|
323
|
+
async updatePlanWithValidation (planId, planData) {
|
|
324
|
+
if (!planId || typeof planId !== 'string') {
|
|
325
|
+
throw new Error('Plan ID must be a valid string')
|
|
326
|
+
}
|
|
327
|
+
if (!planData || typeof planData !== 'object') {
|
|
328
|
+
throw new Error('Plan data must be a valid object')
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Legacy field guard: bare "price" is no longer supported on the server
|
|
332
|
+
if (Object.hasOwn(planData, 'price')) {
|
|
333
|
+
throw new Error(
|
|
334
|
+
'Field "price" is no longer supported. Use unified "pricingOptions" with "amount" instead.'
|
|
335
|
+
)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Validate unified pricingOptions structure if provided
|
|
339
|
+
if (planData.pricingOptions != null) {
|
|
340
|
+
if (
|
|
341
|
+
!Array.isArray(planData.pricingOptions) ||
|
|
342
|
+
planData.pricingOptions.length === 0
|
|
343
|
+
) {
|
|
344
|
+
throw new Error(
|
|
345
|
+
'pricingOptions must be a non-empty array when provided'
|
|
346
|
+
)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const allowedIntervals = new Set(['month', 'year', 'week', 'day', null])
|
|
350
|
+
planData.pricingOptions.forEach((option, index) => {
|
|
351
|
+
if (!option || typeof option !== 'object') {
|
|
352
|
+
throw new Error(`Pricing option at index ${index} must be an object`)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const { key, displayName, amount, interval, lookupKey } = option
|
|
356
|
+
|
|
357
|
+
if (!key || typeof key !== 'string') {
|
|
358
|
+
throw new Error(
|
|
359
|
+
`Pricing option at index ${index} is missing required field 'key'`
|
|
360
|
+
)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Validate key format (alphanumeric and hyphens only)
|
|
364
|
+
if (!/^[a-z0-9-]+$/u.test(key)) {
|
|
365
|
+
throw new Error(
|
|
366
|
+
`Pricing option key '${key}' must contain only lowercase letters, numbers, and hyphens`
|
|
367
|
+
)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (!displayName || typeof displayName !== 'string') {
|
|
371
|
+
throw new Error(
|
|
372
|
+
`Pricing option '${key}' is missing required field 'displayName'`
|
|
373
|
+
)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (typeof amount !== 'number' || amount < 0) {
|
|
377
|
+
throw new Error(
|
|
378
|
+
`Pricing option '${key}' must have a non-negative numeric 'amount'`
|
|
379
|
+
)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (interval !== null && !allowedIntervals.has(interval)) {
|
|
383
|
+
throw new Error(
|
|
384
|
+
`Pricing option '${key}' has invalid interval '${interval}'. Allowed: month, year, week, day or null`
|
|
385
|
+
)
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (!lookupKey || typeof lookupKey !== 'string') {
|
|
389
|
+
throw new Error(
|
|
390
|
+
`Pricing option '${key}' is missing required field 'lookupKey'`
|
|
391
|
+
)
|
|
392
|
+
}
|
|
393
|
+
})
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Validate key format if provided
|
|
397
|
+
if (Object.hasOwn(planData, 'key') && planData.key == null) {
|
|
398
|
+
throw new Error('Plan key must be a valid string')
|
|
399
|
+
}
|
|
400
|
+
if (planData.key && !/^[a-z0-9-]+$/u.test(planData.key)) {
|
|
401
|
+
throw new Error(
|
|
402
|
+
'Plan key must contain only lowercase letters, numbers, and hyphens'
|
|
403
|
+
)
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return await this.updatePlan(planId, planData)
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Helper method to get active plans only
|
|
411
|
+
*/
|
|
412
|
+
async getActivePlans () {
|
|
413
|
+
try {
|
|
414
|
+
const plans = await this.getPlans()
|
|
415
|
+
// Server already returns only active & visible plans for /plans,
|
|
416
|
+
// but we keep this helper aligned with the model fields.
|
|
417
|
+
return plans.filter(
|
|
418
|
+
plan => plan.status === 'active' && plan.isVisible !== false
|
|
419
|
+
)
|
|
420
|
+
} catch (error) {
|
|
421
|
+
throw new Error(`Failed to get active plans: ${error.message}`, {
|
|
422
|
+
cause: error
|
|
423
|
+
})
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Helper method to get plans by price range
|
|
429
|
+
*/
|
|
430
|
+
async getPlansByPriceRange (minPrice = 0, maxPrice = Infinity) {
|
|
431
|
+
try {
|
|
432
|
+
// Use enhanced pricing information from /plans/pricing
|
|
433
|
+
const plans = await this.getPlansWithPricing()
|
|
434
|
+
return plans.filter(plan => {
|
|
435
|
+
const price = plan?.pricing?.bestPrice?.amount ?? 0
|
|
436
|
+
return price >= minPrice && price <= maxPrice
|
|
437
|
+
})
|
|
438
|
+
} catch (error) {
|
|
439
|
+
throw new Error(`Failed to get plans by price range: ${error.message}`, {
|
|
440
|
+
cause: error
|
|
441
|
+
})
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Helper method to find plan by key
|
|
447
|
+
*/
|
|
448
|
+
async getPlanByKey (key) {
|
|
449
|
+
if (!key) {
|
|
450
|
+
throw new Error('Plan key is required')
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
try {
|
|
454
|
+
const plans = await this.getPlans()
|
|
455
|
+
const plan = plans.find(p => p.key === key)
|
|
456
|
+
|
|
457
|
+
if (!plan) {
|
|
458
|
+
throw new Error(`Plan with key '${key}' not found`)
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return plan
|
|
462
|
+
} catch (error) {
|
|
463
|
+
throw new Error(`Failed to get plan by key: ${error.message}`, {
|
|
464
|
+
cause: error
|
|
465
|
+
})
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|