@burdenoff/be-sdk 2026.1110.3 → 2026.1110.4
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/dist/activity/index.d.ts +165 -0
- package/dist/activity/index.js +389 -0
- package/dist/activity/index.js.map +1 -0
- package/dist/ai-credits/index.js +1 -1
- package/dist/{ai-credits-Ca-NNcVx.js → ai-credits-xxfwGpHS.js} +2 -2
- package/dist/{ai-credits-Ca-NNcVx.js.map → ai-credits-xxfwGpHS.js.map} +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.js +1 -1
- package/dist/{auth-CEuBEBQN.js → auth-C5ctwSLp.js} +2 -2
- package/dist/{auth-CEuBEBQN.js.map → auth-C5ctwSLp.js.map} +1 -1
- package/dist/bootstrap/index.d.ts +74 -0
- package/dist/bootstrap/index.js +282 -0
- package/dist/bootstrap/index.js.map +1 -0
- package/dist/{client-DcZ3iGES.js → client-sAJTrb4i.js} +2 -2
- package/dist/{client-DcZ3iGES.js.map → client-sAJTrb4i.js.map} +1 -1
- package/dist/context/index.d.ts +35 -0
- package/dist/{context-CZnXf2yb.js → context/index.js} +38 -2
- package/dist/context/index.js.map +1 -0
- package/dist/database/index.js +1 -1
- package/dist/event-queue/index.d.ts +1 -1
- package/dist/explicit-activity-scope-DikJ7NAE.js +57 -0
- package/dist/explicit-activity-scope-DikJ7NAE.js.map +1 -0
- package/dist/export/index.js +1 -1
- package/dist/{export-B6fphx8a.js → export-DJXE3Osa.js} +3 -3
- package/dist/{export-B6fphx8a.js.map → export-DJXE3Osa.js.map} +1 -1
- package/dist/feature-flags/index.js +1 -1
- package/dist/{feature-flags-71oDkrUe.js → feature-flags-CP1KRgwD.js} +2 -2
- package/dist/{feature-flags-71oDkrUe.js.map → feature-flags-CP1KRgwD.js.map} +1 -1
- package/dist/files/index.js +1 -1
- package/dist/{files-DjH0099H.js → files-h5U-D2AF.js} +2 -2
- package/dist/{files-DjH0099H.js.map → files-h5U-D2AF.js.map} +1 -1
- package/dist/gateway/index.js +2 -2
- package/dist/{gateway-DBFmxWks.js → gateway-ByVo-n37.js} +2 -2
- package/dist/{gateway-DBFmxWks.js.map → gateway-ByVo-n37.js.map} +1 -1
- package/dist/graph/index.d.ts +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/{graph-DDYgIryR.js → graph-CR7u4lpl.js} +2 -2
- package/dist/{graph-DDYgIryR.js.map → graph-CR7u4lpl.js.map} +1 -1
- package/dist/{graphql-DNkMkGBZ.d.ts → graphql-CUDyhibV.d.ts} +1 -1
- package/dist/{index-bZKZUvBu.d.ts → index-ADmsX0BI.d.ts} +3 -3
- package/dist/{index-BqYfCQSP.d.ts → index-B2LCaK0R.d.ts} +1 -1
- package/dist/{index-D6UeLAye.d.ts → index-CHwMhK6v.d.ts} +2 -2
- package/dist/{index-BQDsNAMR.d.ts → index-CTTwo9av.d.ts} +22 -1
- package/dist/{index-DZC-2o9R.d.ts → index-CVCyKzhB.d.ts} +2 -2
- package/dist/{index-CkT2aUr3.d.ts → index-C_GXXF32.d.ts} +2 -2
- package/dist/{index-BKwHQtHQ.d.ts → index-Cot73bdh.d.ts} +1 -1
- package/dist/{index-CnzbJZP7.d.ts → index-FRRaa6h2.d.ts} +2 -2
- package/dist/{index-N-huORcj.d.ts → index-U_tu3zUl.d.ts} +2 -2
- package/dist/index-cmR9k9ga.d.ts +26 -0
- package/dist/{index-CZTnQIRU.d.ts → index-zd_NPGJo.d.ts} +6 -141
- package/dist/index.d.ts +19 -479
- package/dist/index.js +22 -1492
- package/dist/index.js.map +1 -1
- package/dist/init/index.js +1 -1
- package/dist/{init-Dy3KhT6Y.js → init-CQPFmOOe.js} +3 -3
- package/dist/{init-Dy3KhT6Y.js.map → init-CQPFmOOe.js.map} +1 -1
- package/dist/instrumentation/index.d.ts +2 -2
- package/dist/logger/index.d.ts +1 -1
- package/dist/notifications/index.d.ts +1 -1
- package/dist/notifications/index.js +1 -1
- package/dist/{notifications-CdETrtFK.js → notifications-DY_i37WN.js} +2 -2
- package/dist/{notifications-CdETrtFK.js.map → notifications-DY_i37WN.js.map} +1 -1
- package/dist/payments/index.d.ts +144 -0
- package/dist/payments/index.js +404 -0
- package/dist/payments/index.js.map +1 -0
- package/dist/rate-limit-CBrK_U5H.js +142 -0
- package/dist/rate-limit-CBrK_U5H.js.map +1 -0
- package/dist/rbac/index.d.ts +1 -1
- package/dist/rbac/index.js +1 -1
- package/dist/{rbac-BB9l9Xph.js → rbac-Dvz0YzEt.js} +2 -2
- package/dist/{rbac-BB9l9Xph.js.map → rbac-Dvz0YzEt.js.map} +1 -1
- package/dist/resource-caps/index.d.ts +1 -1
- package/dist/search/index.d.ts +112 -0
- package/dist/search/index.js +266 -0
- package/dist/search/index.js.map +1 -0
- package/dist/security/index.d.ts +2 -0
- package/dist/security/index.js +4 -0
- package/dist/security/index.js.map +1 -0
- package/dist/security/rate-limit/index.d.ts +2 -0
- package/dist/security/rate-limit/index.js +4 -0
- package/dist/security/rate-limit/index.js.map +1 -0
- package/dist/server/index.js +3 -2
- package/dist/server/index.js.map +1 -1
- package/dist/{stub-BqwKn4eh.js → stub-BUnfMiAS.js} +3 -56
- package/dist/stub-BUnfMiAS.js.map +1 -0
- package/dist/tags/index.js +1 -1
- package/dist/{tags-B5xpbgsk.js → tags-CbijgoqR.js} +2 -2
- package/dist/{tags-B5xpbgsk.js.map → tags-CbijgoqR.js.map} +1 -1
- package/dist/telemetry/index.d.ts +2 -2
- package/dist/tracker/index.d.ts +2 -2
- package/dist/tracker/index.js +5 -3
- package/dist/{tracker-BQVoEFUo.js → tracker-PQztSlhJ.js} +9 -390
- package/dist/tracker-PQztSlhJ.js.map +1 -0
- package/dist/translation/index.d.ts +120 -0
- package/dist/translation/index.js +573 -0
- package/dist/translation/index.js.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/{types-Cj2RWXbz.js → types/index.js} +3 -3
- package/dist/types/index.js.map +1 -0
- package/dist/usage/index.d.ts +1 -1
- package/dist/usage/index.js +1 -1
- package/dist/{usage-BKFZiWQn.js → usage-BRqVP3CJ.js} +4 -4
- package/dist/{usage-BKFZiWQn.js.map → usage-BRqVP3CJ.js.map} +1 -1
- package/package.json +1 -1
- package/dist/context-CZnXf2yb.js.map +0 -1
- package/dist/stub-BqwKn4eh.js.map +0 -1
- package/dist/tracker-BQVoEFUo.js.map +0 -1
- package/dist/types-Cj2RWXbz.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,26 +1,31 @@
|
|
|
1
|
-
import { t as GatewayClient } from "./client-
|
|
1
|
+
import { t as GatewayClient } from "./client-sAJTrb4i.js";
|
|
2
2
|
import { a as DEFAULT_LAYER_STREAMS, i as DEFAULT_LAYER_RESOURCES, n as DEFAULT_GATEWAY_RETRY_CONFIG, r as DEFAULT_GATEWAY_ROUTING, t as DEFAULT_CIRCUIT_BREAKER_CONFIG } from "./layer-Bcix-WCl.js";
|
|
3
3
|
import { n as GatewayError, r as GatewayErrorType } from "./types-BYMYND1L.js";
|
|
4
|
-
import {
|
|
4
|
+
import { APIError, NetworkError, TimeoutError } from "./types/index.js";
|
|
5
5
|
import { a as logger, o as logger_exports } from "./logger-B6rCpb7D.js";
|
|
6
|
-
import {
|
|
7
|
-
import { t as AuthModule } from "./auth-
|
|
8
|
-
import { c as tenant_notifications_exports, r as NotificationsModule, s as TenantNotificationsModule } from "./notifications-
|
|
9
|
-
import { a as createRBACDirectiveForLayer, i as createRBACDirective, o as createRBACDirectiveFromEnv, t as RBACModule } from "./rbac-
|
|
10
|
-
import {
|
|
11
|
-
import { a as UsageModule, c as BillingUsageClient } from "./usage-
|
|
6
|
+
import { ContextManager } from "./context/index.js";
|
|
7
|
+
import { t as AuthModule } from "./auth-C5ctwSLp.js";
|
|
8
|
+
import { c as tenant_notifications_exports, r as NotificationsModule, s as TenantNotificationsModule } from "./notifications-DY_i37WN.js";
|
|
9
|
+
import { a as createRBACDirectiveForLayer, i as createRBACDirective, o as createRBACDirectiveFromEnv, t as RBACModule } from "./rbac-Dvz0YzEt.js";
|
|
10
|
+
import { ActivityModule, ActivityOperationUnsupportedError } from "./activity/index.js";
|
|
11
|
+
import { a as UsageModule, c as BillingUsageClient } from "./usage-BRqVP3CJ.js";
|
|
12
|
+
import { t as TrackerModule } from "./tracker-PQztSlhJ.js";
|
|
12
13
|
import { DatabaseModule } from "./database/index.js";
|
|
13
14
|
import { T as EventsModule, t as event_queue_exports } from "./event-queue-Cw-YJPY_.js";
|
|
14
15
|
import { a as unregisterDisposable, i as registerDisposable, n as disposeAll, r as getRegisteredDisposableCount, t as lifecycle_exports } from "./lifecycle-DeoLSX0U.js";
|
|
15
|
-
import { n as graph_exports, t as GraphModule } from "./graph-
|
|
16
|
+
import { n as graph_exports, t as GraphModule } from "./graph-CR7u4lpl.js";
|
|
16
17
|
import { l as QueueType, t as EventQueueFactory } from "./EventQueueFactory-9NGZV9jl.js";
|
|
17
|
-
import {
|
|
18
|
+
import { SearchModule } from "./search/index.js";
|
|
18
19
|
import { a as resolveEventStreamsUrl } from "./env-DdaoGjIt.js";
|
|
20
|
+
import { TranslationModule } from "./translation/index.js";
|
|
21
|
+
import { PaymentsModule } from "./payments/index.js";
|
|
22
|
+
import { BootstrapModule, createBootstrap } from "./bootstrap/index.js";
|
|
23
|
+
import { createInternalClient, createInternalClientForTenant, createInternalClientForWorkspaces } from "./client/internal-client.js";
|
|
19
24
|
import { RESOURCE_CAP_DEFAULTS, ResourceCapExceededError, ResourceCapsModule, configureResourceCaps, getResourceCaps } from "./resource-caps/index.js";
|
|
20
25
|
import { t as sharing_exports } from "./sharing-CflDsSJ-.js";
|
|
21
|
-
import { t as tags_exports } from "./tags-
|
|
22
|
-
import { t as files_exports } from "./files-
|
|
23
|
-
import { t as ai_credits_exports } from "./ai-credits-
|
|
26
|
+
import { t as tags_exports } from "./tags-CbijgoqR.js";
|
|
27
|
+
import { t as files_exports } from "./files-h5U-D2AF.js";
|
|
28
|
+
import { t as ai_credits_exports } from "./ai-credits-xxfwGpHS.js";
|
|
24
29
|
import { a as TemporalClient, i as TemporalWorkerManager, n as TemporalWorker, o as TemporalClientFactory, r as TemporalWorkerFactory, t as TemporalModule } from "./temporal-B534uYK8.js";
|
|
25
30
|
import { t as cache_exports } from "./cache-CU0uLwcS.js";
|
|
26
31
|
import { t as middleware_exports } from "./middleware-Cq0zFMGp.js";
|
|
@@ -29,14 +34,14 @@ import { t as graphql_exports } from "./graphql-DHAHHcww.js";
|
|
|
29
34
|
import { t as prisma_exports } from "./prisma-BzOOdt79.js";
|
|
30
35
|
import { _ as utils_exports } from "./utils-DL85Tnbn.js";
|
|
31
36
|
import { a as getLanguage, c as t, i as createTranslator, o as getLocale, r as i18n_exports, s as hasI18n } from "./i18n-BBMYFd0P.js";
|
|
32
|
-
import { t as export_exports } from "./export-
|
|
33
|
-
import { n as init_exports } from "./init-
|
|
37
|
+
import { t as export_exports } from "./export-DJXE3Osa.js";
|
|
38
|
+
import { n as init_exports } from "./init-CQPFmOOe.js";
|
|
34
39
|
import { s as dataloader_exports } from "./dataloader-DwpT3YoS.js";
|
|
35
40
|
import { r as instrumentation_exports } from "./instrumentation-xkkmDgJ0.js";
|
|
36
41
|
import { a as layer_exports } from "./layer-CEKy6cm8.js";
|
|
37
42
|
import { t as crypto_exports } from "./crypto-DO9fM0FO.js";
|
|
38
|
-
import { t as gateway_exports } from "./gateway-
|
|
39
|
-
import { t as feature_flags_exports } from "./feature-flags-
|
|
43
|
+
import { t as gateway_exports } from "./gateway-ByVo-n37.js";
|
|
44
|
+
import { t as feature_flags_exports } from "./feature-flags-CP1KRgwD.js";
|
|
40
45
|
import { i as verifyCaptchaIfEnabled, n as isCaptchaEnabled, r as verifyCaptcha, t as captcha_exports } from "./captcha-BT7yh1YS.js";
|
|
41
46
|
import { t as platform_exports } from "./platform-DZwczW7v.js";
|
|
42
47
|
import { C as NAME_MIN_LENGTH, S as NAME_MAX_LENGTH, T as validateName, b as USERNAME_MIN_LENGTH, f as RbacCoverageError, m as assertRbacCoverage, p as analyzeRbacCoverage, t as validation_exports, v as validateUserFields, w as lengthInCodePoints, x as validateUsername, y as USERNAME_MAX_LENGTH } from "./validation-CPUITdMk.js";
|
|
@@ -240,1481 +245,6 @@ var BurdenoffGraphQLClient = class {
|
|
|
240
245
|
}
|
|
241
246
|
};
|
|
242
247
|
|
|
243
|
-
// src/search/index.ts
|
|
244
|
-
/**
|
|
245
|
-
* Search Module
|
|
246
|
-
*/
|
|
247
|
-
var SearchModule = class {
|
|
248
|
-
client;
|
|
249
|
-
context;
|
|
250
|
-
constructor(client, context) {
|
|
251
|
-
this.client = client;
|
|
252
|
-
this.context = context;
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Search across all resources
|
|
256
|
-
*/
|
|
257
|
-
async search(options) {
|
|
258
|
-
if (!options.query || options.query.trim().length === 0) throw new ValidationError("Search query is required");
|
|
259
|
-
logger.debug("[Search] Searching", {
|
|
260
|
-
query: options.query,
|
|
261
|
-
resourceTypes: options.resourceTypes
|
|
262
|
-
});
|
|
263
|
-
const query = `
|
|
264
|
-
query Search($input: SearchInput!) {
|
|
265
|
-
search(input: $input) {
|
|
266
|
-
results {
|
|
267
|
-
id
|
|
268
|
-
type
|
|
269
|
-
title
|
|
270
|
-
description
|
|
271
|
-
data
|
|
272
|
-
workspaceId
|
|
273
|
-
organizationId
|
|
274
|
-
projectId
|
|
275
|
-
score
|
|
276
|
-
highlights
|
|
277
|
-
createdAt
|
|
278
|
-
updatedAt
|
|
279
|
-
}
|
|
280
|
-
total
|
|
281
|
-
took
|
|
282
|
-
aggregations
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
`;
|
|
286
|
-
const variables = { input: {
|
|
287
|
-
query: options.query,
|
|
288
|
-
resourceTypes: options.resourceTypes,
|
|
289
|
-
workspaceId: options.workspaceId || this.context.workspaceId,
|
|
290
|
-
organizationId: options.organizationId,
|
|
291
|
-
projectId: options.projectId || this.context.projectId,
|
|
292
|
-
fields: options.fields,
|
|
293
|
-
fuzziness: options.fuzziness || .8,
|
|
294
|
-
boosts: options.boosts,
|
|
295
|
-
filters: options.filters,
|
|
296
|
-
sort: options.sort,
|
|
297
|
-
limit: options.limit || 20,
|
|
298
|
-
offset: options.offset || 0,
|
|
299
|
-
highlight: options.highlight !== false
|
|
300
|
-
} };
|
|
301
|
-
const response = await this.client.query(query, variables, this.context);
|
|
302
|
-
logger.info("[Search] Search completed", {
|
|
303
|
-
total: response.search.total,
|
|
304
|
-
took: response.search.took
|
|
305
|
-
});
|
|
306
|
-
return response.search;
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Index document for search
|
|
310
|
-
*/
|
|
311
|
-
async indexDocument(options) {
|
|
312
|
-
if (!options.id) throw new ValidationError("Document ID is required");
|
|
313
|
-
if (!options.type) throw new ValidationError("Document type is required");
|
|
314
|
-
if (!options.title) throw new ValidationError("Document title is required");
|
|
315
|
-
logger.debug("[Search] Indexing document", {
|
|
316
|
-
id: options.id,
|
|
317
|
-
type: options.type
|
|
318
|
-
});
|
|
319
|
-
const mutation = `
|
|
320
|
-
mutation IndexDocument($input: IndexDocumentInput!) {
|
|
321
|
-
indexDocument(input: $input) {
|
|
322
|
-
id
|
|
323
|
-
indexed
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
`;
|
|
327
|
-
const variables = { input: {
|
|
328
|
-
id: options.id,
|
|
329
|
-
type: options.type,
|
|
330
|
-
title: options.title,
|
|
331
|
-
description: options.description,
|
|
332
|
-
content: options.content,
|
|
333
|
-
data: options.data,
|
|
334
|
-
workspaceId: options.workspaceId || this.context.workspaceId,
|
|
335
|
-
organizationId: options.organizationId,
|
|
336
|
-
projectId: options.projectId || this.context.projectId,
|
|
337
|
-
tags: options.tags,
|
|
338
|
-
metadata: options.metadata
|
|
339
|
-
} };
|
|
340
|
-
const response = await this.client.query(mutation, variables, this.context);
|
|
341
|
-
logger.info("[Search] Document indexed", { id: response.indexDocument.id });
|
|
342
|
-
return response.indexDocument;
|
|
343
|
-
}
|
|
344
|
-
/**
|
|
345
|
-
* Remove document from search index
|
|
346
|
-
*/
|
|
347
|
-
async removeDocument(id, type) {
|
|
348
|
-
logger.debug("[Search] Removing document", {
|
|
349
|
-
id,
|
|
350
|
-
type
|
|
351
|
-
});
|
|
352
|
-
await this.client.query(`
|
|
353
|
-
mutation RemoveDocument($id: ID!, $type: String!) {
|
|
354
|
-
removeDocument(id: $id, type: $type) {
|
|
355
|
-
success
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
`, {
|
|
359
|
-
id,
|
|
360
|
-
type
|
|
361
|
-
}, this.context);
|
|
362
|
-
logger.info("[Search] Document removed", { id });
|
|
363
|
-
return true;
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Update document in search index
|
|
367
|
-
*/
|
|
368
|
-
async updateDocument(id, type, updates) {
|
|
369
|
-
logger.debug("[Search] Updating document", {
|
|
370
|
-
id,
|
|
371
|
-
type
|
|
372
|
-
});
|
|
373
|
-
const response = await this.client.query(`
|
|
374
|
-
mutation UpdateDocument($id: ID!, $type: String!, $updates: JSON!) {
|
|
375
|
-
updateDocument(id: $id, type: $type, updates: $updates) {
|
|
376
|
-
id
|
|
377
|
-
updated
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
`, {
|
|
381
|
-
id,
|
|
382
|
-
type,
|
|
383
|
-
updates
|
|
384
|
-
}, this.context);
|
|
385
|
-
logger.info("[Search] Document updated", { id });
|
|
386
|
-
return response.updateDocument;
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Bulk index documents
|
|
390
|
-
*/
|
|
391
|
-
async indexDocumentsBulk(documents) {
|
|
392
|
-
logger.debug("[Search] Bulk indexing documents", { count: documents.length });
|
|
393
|
-
const response = await this.client.query(`
|
|
394
|
-
mutation IndexDocumentsBulk($documents: [IndexDocumentInput!]!) {
|
|
395
|
-
indexDocumentsBulk(documents: $documents) {
|
|
396
|
-
indexed
|
|
397
|
-
failed
|
|
398
|
-
errors
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
`, { documents }, this.context);
|
|
402
|
-
logger.info("[Search] Bulk indexing completed", {
|
|
403
|
-
indexed: response.indexDocumentsBulk.indexed,
|
|
404
|
-
failed: response.indexDocumentsBulk.failed
|
|
405
|
-
});
|
|
406
|
-
return response.indexDocumentsBulk;
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Get search suggestions (autocomplete)
|
|
410
|
-
*/
|
|
411
|
-
async suggest(prefix, options) {
|
|
412
|
-
if (!prefix || prefix.trim().length === 0) throw new ValidationError("Prefix is required for suggestions");
|
|
413
|
-
logger.debug("[Search] Getting suggestions", { prefix });
|
|
414
|
-
return (await this.client.query(`
|
|
415
|
-
query GetSuggestions($prefix: String!, $resourceTypes: [String!], $limit: Int) {
|
|
416
|
-
getSuggestions(prefix: $prefix, resourceTypes: $resourceTypes, limit: $limit) {
|
|
417
|
-
text
|
|
418
|
-
score
|
|
419
|
-
type
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
`, {
|
|
423
|
-
prefix,
|
|
424
|
-
resourceTypes: options?.resourceTypes,
|
|
425
|
-
limit: options?.limit || 10
|
|
426
|
-
}, this.context)).getSuggestions;
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* Get related documents
|
|
430
|
-
*/
|
|
431
|
-
async getRelated(documentId, options) {
|
|
432
|
-
logger.debug("[Search] Getting related documents", { documentId });
|
|
433
|
-
return (await this.client.query(`
|
|
434
|
-
query GetRelatedDocuments($documentId: ID!, $limit: Int, $minScore: Float) {
|
|
435
|
-
getRelatedDocuments(documentId: $documentId, limit: $limit, minScore: $minScore) {
|
|
436
|
-
id
|
|
437
|
-
type
|
|
438
|
-
title
|
|
439
|
-
description
|
|
440
|
-
data
|
|
441
|
-
score
|
|
442
|
-
createdAt
|
|
443
|
-
updatedAt
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
`, {
|
|
447
|
-
documentId,
|
|
448
|
-
limit: options?.limit || 10,
|
|
449
|
-
minScore: options?.minScore || .5
|
|
450
|
-
}, this.context)).getRelatedDocuments;
|
|
451
|
-
}
|
|
452
|
-
/**
|
|
453
|
-
* Get search aggregations
|
|
454
|
-
*/
|
|
455
|
-
async aggregate(query, aggregations) {
|
|
456
|
-
logger.debug("[Search] Getting aggregations", { query });
|
|
457
|
-
return (await this.client.query(`
|
|
458
|
-
query GetAggregations($query: String!, $aggregations: [AggregationInput!]!) {
|
|
459
|
-
getAggregations(query: $query, aggregations: $aggregations) {
|
|
460
|
-
aggregations
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
`, {
|
|
464
|
-
query,
|
|
465
|
-
aggregations
|
|
466
|
-
}, this.context)).getAggregations.aggregations;
|
|
467
|
-
}
|
|
468
|
-
/**
|
|
469
|
-
* Clear search index for workspace
|
|
470
|
-
*/
|
|
471
|
-
async clearIndex(workspaceId) {
|
|
472
|
-
logger.debug("[Search] Clearing index", { workspaceId });
|
|
473
|
-
const response = await this.client.query(`
|
|
474
|
-
mutation ClearSearchIndex($workspaceId: ID) {
|
|
475
|
-
clearSearchIndex(workspaceId: $workspaceId) {
|
|
476
|
-
deletedCount
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
`, { workspaceId: workspaceId || this.context.workspaceId }, this.context);
|
|
480
|
-
logger.info("[Search] Index cleared", { deletedCount: response.clearSearchIndex.deletedCount });
|
|
481
|
-
return response.clearSearchIndex;
|
|
482
|
-
}
|
|
483
|
-
/**
|
|
484
|
-
* Reindex all documents
|
|
485
|
-
*/
|
|
486
|
-
async reindex(options) {
|
|
487
|
-
logger.debug("[Search] Reindexing", options);
|
|
488
|
-
const response = await this.client.query(`
|
|
489
|
-
mutation ReindexDocuments($workspaceId: ID, $resourceTypes: [String!]) {
|
|
490
|
-
reindexDocuments(workspaceId: $workspaceId, resourceTypes: $resourceTypes) {
|
|
491
|
-
indexed
|
|
492
|
-
failed
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
`, {
|
|
496
|
-
workspaceId: options?.workspaceId || this.context.workspaceId,
|
|
497
|
-
resourceTypes: options?.resourceTypes
|
|
498
|
-
}, this.context);
|
|
499
|
-
logger.info("[Search] Reindexing completed", response.reindexDocuments);
|
|
500
|
-
return response.reindexDocuments;
|
|
501
|
-
}
|
|
502
|
-
};
|
|
503
|
-
|
|
504
|
-
// src/translation/index.ts
|
|
505
|
-
/**
|
|
506
|
-
* Translation Module
|
|
507
|
-
*/
|
|
508
|
-
var TranslationModule = class {
|
|
509
|
-
client;
|
|
510
|
-
context;
|
|
511
|
-
constructor(client, context) {
|
|
512
|
-
this.client = client;
|
|
513
|
-
this.context = context;
|
|
514
|
-
}
|
|
515
|
-
/**
|
|
516
|
-
* Translate text
|
|
517
|
-
*/
|
|
518
|
-
async translate(options) {
|
|
519
|
-
if (!options.text || options.text.trim().length === 0) throw new ValidationError("Text to translate is required");
|
|
520
|
-
if (!options.targetLanguage) throw new ValidationError("Target language is required");
|
|
521
|
-
logger.debug("[Translation] Translating text", {
|
|
522
|
-
sourceLanguage: options.sourceLanguage || "auto",
|
|
523
|
-
targetLanguage: options.targetLanguage,
|
|
524
|
-
textLength: options.text.length
|
|
525
|
-
});
|
|
526
|
-
const query = `
|
|
527
|
-
query Translate($input: TranslateInput!) {
|
|
528
|
-
translate(input: $input) {
|
|
529
|
-
original
|
|
530
|
-
translated
|
|
531
|
-
sourceLanguage
|
|
532
|
-
targetLanguage
|
|
533
|
-
confidence
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
`;
|
|
537
|
-
const variables = { input: {
|
|
538
|
-
text: options.text,
|
|
539
|
-
sourceLanguage: options.sourceLanguage,
|
|
540
|
-
targetLanguage: options.targetLanguage,
|
|
541
|
-
context: options.context
|
|
542
|
-
} };
|
|
543
|
-
const response = await this.client.query(query, variables, this.context);
|
|
544
|
-
let translated = response.translate.translated;
|
|
545
|
-
if (options.variables) translated = this.interpolate(translated, options.variables);
|
|
546
|
-
logger.info("[Translation] Text translated", {
|
|
547
|
-
sourceLanguage: response.translate.sourceLanguage,
|
|
548
|
-
targetLanguage: response.translate.targetLanguage
|
|
549
|
-
});
|
|
550
|
-
return {
|
|
551
|
-
...response.translate,
|
|
552
|
-
translated
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* Get translations for keys
|
|
557
|
-
*/
|
|
558
|
-
async getTranslations(options) {
|
|
559
|
-
if (!options.keys || options.keys.length === 0) throw new ValidationError("Translation keys are required");
|
|
560
|
-
if (!options.language) throw new ValidationError("Language is required");
|
|
561
|
-
logger.debug("[Translation] Getting translations", {
|
|
562
|
-
language: options.language,
|
|
563
|
-
count: options.keys.length
|
|
564
|
-
});
|
|
565
|
-
return (await this.client.query(`
|
|
566
|
-
query GetTranslations(
|
|
567
|
-
$keys: [String!]!
|
|
568
|
-
$language: String!
|
|
569
|
-
$namespace: String
|
|
570
|
-
$fallbackLanguage: String
|
|
571
|
-
) {
|
|
572
|
-
getTranslations(
|
|
573
|
-
keys: $keys
|
|
574
|
-
language: $language
|
|
575
|
-
namespace: $namespace
|
|
576
|
-
fallbackLanguage: $fallbackLanguage
|
|
577
|
-
) {
|
|
578
|
-
translations
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
`, {
|
|
582
|
-
keys: options.keys,
|
|
583
|
-
language: options.language,
|
|
584
|
-
namespace: options.namespace,
|
|
585
|
-
fallbackLanguage: options.fallbackLanguage || "en"
|
|
586
|
-
}, this.context)).getTranslations.translations;
|
|
587
|
-
}
|
|
588
|
-
/**
|
|
589
|
-
* Get single translation
|
|
590
|
-
*/
|
|
591
|
-
async get(key, language, variables) {
|
|
592
|
-
let translation = (await this.getTranslations({
|
|
593
|
-
keys: [key],
|
|
594
|
-
language
|
|
595
|
-
}))[key] || key;
|
|
596
|
-
if (variables) translation = this.interpolate(translation, variables);
|
|
597
|
-
return translation;
|
|
598
|
-
}
|
|
599
|
-
/**
|
|
600
|
-
* Set translation
|
|
601
|
-
*/
|
|
602
|
-
async setTranslation(options) {
|
|
603
|
-
if (!options.key) throw new ValidationError("Translation key is required");
|
|
604
|
-
if (!options.value) throw new ValidationError("Translation value is required");
|
|
605
|
-
if (!options.language) throw new ValidationError("Language is required");
|
|
606
|
-
logger.debug("[Translation] Setting translation", {
|
|
607
|
-
key: options.key,
|
|
608
|
-
language: options.language
|
|
609
|
-
});
|
|
610
|
-
const mutation = `
|
|
611
|
-
mutation SetTranslation($input: SetTranslationInput!) {
|
|
612
|
-
setTranslation(input: $input) {
|
|
613
|
-
key
|
|
614
|
-
value
|
|
615
|
-
language
|
|
616
|
-
namespace
|
|
617
|
-
description
|
|
618
|
-
variables
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
`;
|
|
622
|
-
const variables = { input: {
|
|
623
|
-
key: options.key,
|
|
624
|
-
value: options.value,
|
|
625
|
-
language: options.language,
|
|
626
|
-
namespace: options.namespace,
|
|
627
|
-
description: options.description,
|
|
628
|
-
variables: options.variables
|
|
629
|
-
} };
|
|
630
|
-
const response = await this.client.query(mutation, variables, this.context);
|
|
631
|
-
logger.info("[Translation] Translation set", {
|
|
632
|
-
key: options.key,
|
|
633
|
-
language: options.language
|
|
634
|
-
});
|
|
635
|
-
return response.setTranslation;
|
|
636
|
-
}
|
|
637
|
-
/**
|
|
638
|
-
* Delete translation
|
|
639
|
-
*/
|
|
640
|
-
async deleteTranslation(key, language, namespace) {
|
|
641
|
-
logger.debug("[Translation] Deleting translation", {
|
|
642
|
-
key,
|
|
643
|
-
language
|
|
644
|
-
});
|
|
645
|
-
await this.client.query(`
|
|
646
|
-
mutation DeleteTranslation($key: String!, $language: String!, $namespace: String) {
|
|
647
|
-
deleteTranslation(key: $key, language: $language, namespace: $namespace) {
|
|
648
|
-
success
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
`, {
|
|
652
|
-
key,
|
|
653
|
-
language,
|
|
654
|
-
namespace
|
|
655
|
-
}, this.context);
|
|
656
|
-
logger.info("[Translation] Translation deleted", {
|
|
657
|
-
key,
|
|
658
|
-
language
|
|
659
|
-
});
|
|
660
|
-
return true;
|
|
661
|
-
}
|
|
662
|
-
/**
|
|
663
|
-
* Batch set translations
|
|
664
|
-
*/
|
|
665
|
-
async setTranslationsBatch(translations) {
|
|
666
|
-
logger.debug("[Translation] Batch setting translations", { count: translations.length });
|
|
667
|
-
const response = await this.client.query(`
|
|
668
|
-
mutation SetTranslationsBatch($translations: [SetTranslationInput!]!) {
|
|
669
|
-
setTranslationsBatch(translations: $translations) {
|
|
670
|
-
key
|
|
671
|
-
value
|
|
672
|
-
language
|
|
673
|
-
namespace
|
|
674
|
-
description
|
|
675
|
-
variables
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
`, { translations }, this.context);
|
|
679
|
-
logger.info("[Translation] Batch translations set", { count: response.setTranslationsBatch.length });
|
|
680
|
-
return response.setTranslationsBatch;
|
|
681
|
-
}
|
|
682
|
-
/**
|
|
683
|
-
* Get all translations for language
|
|
684
|
-
*/
|
|
685
|
-
async getAllTranslations(language, namespace) {
|
|
686
|
-
logger.debug("[Translation] Getting all translations", {
|
|
687
|
-
language,
|
|
688
|
-
namespace
|
|
689
|
-
});
|
|
690
|
-
return (await this.client.query(`
|
|
691
|
-
query GetAllTranslations($language: String!, $namespace: String) {
|
|
692
|
-
getAllTranslations(language: $language, namespace: $namespace) {
|
|
693
|
-
key
|
|
694
|
-
value
|
|
695
|
-
language
|
|
696
|
-
namespace
|
|
697
|
-
description
|
|
698
|
-
variables
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
`, {
|
|
702
|
-
language,
|
|
703
|
-
namespace
|
|
704
|
-
}, this.context)).getAllTranslations;
|
|
705
|
-
}
|
|
706
|
-
/**
|
|
707
|
-
* Detect language
|
|
708
|
-
*/
|
|
709
|
-
async detectLanguage(text) {
|
|
710
|
-
if (!text || text.trim().length === 0) throw new ValidationError("Text is required for language detection");
|
|
711
|
-
logger.debug("[Translation] Detecting language");
|
|
712
|
-
return (await this.client.query(`
|
|
713
|
-
query DetectLanguage($text: String!) {
|
|
714
|
-
detectLanguage(text: $text) {
|
|
715
|
-
language
|
|
716
|
-
confidence
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
`, { text }, this.context)).detectLanguage;
|
|
720
|
-
}
|
|
721
|
-
/**
|
|
722
|
-
* Get supported languages
|
|
723
|
-
*/
|
|
724
|
-
async getSupportedLanguages() {
|
|
725
|
-
logger.debug("[Translation] Getting supported languages");
|
|
726
|
-
return (await this.client.query(`
|
|
727
|
-
query GetSupportedLanguages {
|
|
728
|
-
getSupportedLanguages {
|
|
729
|
-
code
|
|
730
|
-
name
|
|
731
|
-
nativeName
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
`, {}, this.context)).getSupportedLanguages;
|
|
735
|
-
}
|
|
736
|
-
/**
|
|
737
|
-
* Import translations from file
|
|
738
|
-
*/
|
|
739
|
-
async importTranslations(language, translations, namespace) {
|
|
740
|
-
logger.debug("[Translation] Importing translations", {
|
|
741
|
-
language,
|
|
742
|
-
count: Object.keys(translations).length
|
|
743
|
-
});
|
|
744
|
-
const response = await this.client.query(`
|
|
745
|
-
mutation ImportTranslations($language: String!, $translations: JSON!, $namespace: String) {
|
|
746
|
-
importTranslations(language: $language, translations: $translations, namespace: $namespace) {
|
|
747
|
-
imported
|
|
748
|
-
failed
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
`, {
|
|
752
|
-
language,
|
|
753
|
-
translations,
|
|
754
|
-
namespace
|
|
755
|
-
}, this.context);
|
|
756
|
-
logger.info("[Translation] Translations imported", response.importTranslations);
|
|
757
|
-
return response.importTranslations;
|
|
758
|
-
}
|
|
759
|
-
/**
|
|
760
|
-
* Export translations to JSON
|
|
761
|
-
*/
|
|
762
|
-
async exportTranslations(language, namespace) {
|
|
763
|
-
logger.debug("[Translation] Exporting translations", {
|
|
764
|
-
language,
|
|
765
|
-
namespace
|
|
766
|
-
});
|
|
767
|
-
const response = await this.client.query(`
|
|
768
|
-
query ExportTranslations($language: String!, $namespace: String) {
|
|
769
|
-
exportTranslations(language: $language, namespace: $namespace) {
|
|
770
|
-
translations
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
`, {
|
|
774
|
-
language,
|
|
775
|
-
namespace
|
|
776
|
-
}, this.context);
|
|
777
|
-
logger.info("[Translation] Translations exported", {
|
|
778
|
-
language,
|
|
779
|
-
count: Object.keys(response.exportTranslations.translations).length
|
|
780
|
-
});
|
|
781
|
-
return response.exportTranslations.translations;
|
|
782
|
-
}
|
|
783
|
-
/**
|
|
784
|
-
* Export product translations from global i18n service (federated schema).
|
|
785
|
-
*
|
|
786
|
-
* This uses the modern i18n GraphQL contract:
|
|
787
|
-
* - i18nProductByName
|
|
788
|
-
* - exportTranslations(productId, languageCode, format)
|
|
789
|
-
*/
|
|
790
|
-
async exportProductTranslations(options) {
|
|
791
|
-
const productId = await this.resolveProductId(options.productId, options.productName);
|
|
792
|
-
const languageCode = options.languageCode ?? "en";
|
|
793
|
-
const format = options.format ?? "JSON";
|
|
794
|
-
const response = await this.client.query(`
|
|
795
|
-
query ExportProductTranslations(
|
|
796
|
-
$productId: String!
|
|
797
|
-
$languageCode: String!
|
|
798
|
-
$format: ImportJobFormat!
|
|
799
|
-
) {
|
|
800
|
-
exportTranslations(productId: $productId, languageCode: $languageCode, format: $format) {
|
|
801
|
-
format
|
|
802
|
-
url
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
`, {
|
|
806
|
-
productId,
|
|
807
|
-
languageCode,
|
|
808
|
-
format
|
|
809
|
-
}, this.context);
|
|
810
|
-
const result = {
|
|
811
|
-
format: response.exportTranslations.format,
|
|
812
|
-
url: response.exportTranslations.url
|
|
813
|
-
};
|
|
814
|
-
if ((options.decodeJson ?? true) && format === "JSON") result.translations = this.decodeDataUrlJson(response.exportTranslations.url);
|
|
815
|
-
return result;
|
|
816
|
-
}
|
|
817
|
-
/**
|
|
818
|
-
* Upsert product translations in global i18n service.
|
|
819
|
-
*
|
|
820
|
-
* Behavior:
|
|
821
|
-
* - Ensures translation keys exist (create if missing)
|
|
822
|
-
* - Optionally updates translation key defaultValue
|
|
823
|
-
* - Creates/updates language translation entries
|
|
824
|
-
* - Optionally publishes translations
|
|
825
|
-
*/
|
|
826
|
-
async upsertProductTranslations(options) {
|
|
827
|
-
if (!options.languageCode) throw new ValidationError("languageCode is required");
|
|
828
|
-
const flatTranslations = this.flattenTranslationTree(options.translations);
|
|
829
|
-
if (Object.keys(flatTranslations).length === 0) throw new ValidationError("translations cannot be empty");
|
|
830
|
-
const flatDefaults = options.defaultValues ? this.flattenTranslationTree(options.defaultValues) : flatTranslations;
|
|
831
|
-
const productId = await this.resolveProductId(options.productId, options.productName);
|
|
832
|
-
const publish = options.publish ?? true;
|
|
833
|
-
const updateExisting = options.updateExisting ?? true;
|
|
834
|
-
const result = {
|
|
835
|
-
productId,
|
|
836
|
-
languageCode: options.languageCode,
|
|
837
|
-
keysProcessed: 0,
|
|
838
|
-
createdKeys: 0,
|
|
839
|
-
updatedKeyDefaults: 0,
|
|
840
|
-
createdTranslations: 0,
|
|
841
|
-
updatedTranslations: 0
|
|
842
|
-
};
|
|
843
|
-
const entries = Object.entries(flatTranslations);
|
|
844
|
-
const existingKeysMap = /* @__PURE__ */ new Map();
|
|
845
|
-
for (const [key] of entries) {
|
|
846
|
-
const keyRow = await this.findTranslationKeyByKey(productId, key);
|
|
847
|
-
if (keyRow) existingKeysMap.set(key, keyRow);
|
|
848
|
-
}
|
|
849
|
-
for (const [key, value] of entries) {
|
|
850
|
-
const defaultValue = flatDefaults[key] ?? value;
|
|
851
|
-
let keyId = "";
|
|
852
|
-
const keyRow = existingKeysMap.get(key);
|
|
853
|
-
if (!keyRow) {
|
|
854
|
-
keyId = (await this.createTranslationKey(productId, key, defaultValue)).id;
|
|
855
|
-
result.createdKeys += 1;
|
|
856
|
-
} else {
|
|
857
|
-
keyId = keyRow.id;
|
|
858
|
-
if (defaultValue && keyRow.defaultValue !== defaultValue) {
|
|
859
|
-
await this.updateTranslationKeyDefaultValue(keyId, defaultValue);
|
|
860
|
-
result.updatedKeyDefaults += 1;
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
const translationRow = await this.findGlobalTranslation(keyId, options.languageCode);
|
|
864
|
-
if (!translationRow) {
|
|
865
|
-
const createdTranslation = await this.createLanguageTranslation(keyId, options.languageCode, value);
|
|
866
|
-
result.createdTranslations += 1;
|
|
867
|
-
if (publish && createdTranslation.status !== "PUBLISHED") await this.updateLanguageTranslation(createdTranslation.id, value, "PUBLISHED");
|
|
868
|
-
} else {
|
|
869
|
-
const shouldUpdateValue = updateExisting && translationRow.value !== value;
|
|
870
|
-
const shouldPublish = publish && translationRow.status !== "PUBLISHED";
|
|
871
|
-
if (shouldUpdateValue || shouldPublish) {
|
|
872
|
-
await this.updateLanguageTranslation(translationRow.id, shouldUpdateValue ? value : translationRow.value, publish ? "PUBLISHED" : translationRow.status);
|
|
873
|
-
if (shouldUpdateValue) result.updatedTranslations += 1;
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
result.keysProcessed += 1;
|
|
877
|
-
}
|
|
878
|
-
logger.info("[Translation] Product translations upsert completed", {
|
|
879
|
-
productId,
|
|
880
|
-
languageCode: options.languageCode,
|
|
881
|
-
keysProcessed: result.keysProcessed,
|
|
882
|
-
createdKeys: result.createdKeys,
|
|
883
|
-
createdTranslations: result.createdTranslations,
|
|
884
|
-
updatedTranslations: result.updatedTranslations
|
|
885
|
-
});
|
|
886
|
-
return result;
|
|
887
|
-
}
|
|
888
|
-
flattenTranslationTree(input, prefix = "") {
|
|
889
|
-
const out = {};
|
|
890
|
-
for (const [key, rawValue] of Object.entries(input)) {
|
|
891
|
-
const nextKey = prefix ? `${prefix}.${key}` : key;
|
|
892
|
-
if (typeof rawValue === "string") {
|
|
893
|
-
out[nextKey] = rawValue;
|
|
894
|
-
continue;
|
|
895
|
-
}
|
|
896
|
-
if (rawValue && typeof rawValue === "object") Object.assign(out, this.flattenTranslationTree(rawValue, nextKey));
|
|
897
|
-
}
|
|
898
|
-
return out;
|
|
899
|
-
}
|
|
900
|
-
decodeDataUrlJson(url) {
|
|
901
|
-
if (!url.startsWith("data:")) throw new ValidationError("Expected exportTranslations URL to be a data URL");
|
|
902
|
-
const commaIdx = url.indexOf(",");
|
|
903
|
-
if (commaIdx === -1) throw new ValidationError("Invalid data URL: missing comma separator");
|
|
904
|
-
const header = url.slice(0, commaIdx);
|
|
905
|
-
const payload = url.slice(commaIdx + 1);
|
|
906
|
-
if (!payload) throw new ValidationError("Invalid data URL payload");
|
|
907
|
-
try {
|
|
908
|
-
const decoded = header.includes("base64") ? Buffer.from(payload, "base64").toString("utf8") : decodeURIComponent(payload);
|
|
909
|
-
return JSON.parse(decoded);
|
|
910
|
-
} catch (error) {
|
|
911
|
-
logger.error("[Translation] Failed to decode data URL JSON", {
|
|
912
|
-
error,
|
|
913
|
-
isBase64: header.includes("base64")
|
|
914
|
-
});
|
|
915
|
-
throw new ValidationError("Failed to decode JSON translation payload");
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
async resolveProductId(productId, productName) {
|
|
919
|
-
if (productId) return productId;
|
|
920
|
-
if (!productName) throw new ValidationError("Either productId or productName is required");
|
|
921
|
-
const response = await this.client.query(`
|
|
922
|
-
query GetI18nProductByName($name: String!) {
|
|
923
|
-
i18nProductByName(name: $name) {
|
|
924
|
-
id
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
`, { name: productName }, this.context);
|
|
928
|
-
if (!response.i18nProductByName?.id) throw new ValidationError(`i18n product not found: ${productName}`);
|
|
929
|
-
return response.i18nProductByName.id;
|
|
930
|
-
}
|
|
931
|
-
async findTranslationKeyByKey(productId, key) {
|
|
932
|
-
return (await this.client.query(`
|
|
933
|
-
query FindTranslationKey($productId: String!, $key: String!) {
|
|
934
|
-
translationKeyByKey(productId: $productId, key: $key) {
|
|
935
|
-
id
|
|
936
|
-
defaultValue
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
`, {
|
|
940
|
-
productId,
|
|
941
|
-
key
|
|
942
|
-
}, this.context)).translationKeyByKey;
|
|
943
|
-
}
|
|
944
|
-
async createTranslationKey(productId, key, defaultValue) {
|
|
945
|
-
return (await this.client.query(`
|
|
946
|
-
mutation CreateTranslationKey($input: CreateTranslationKeyInput!) {
|
|
947
|
-
createTranslationKey(input: $input) {
|
|
948
|
-
id
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
`, { input: {
|
|
952
|
-
productId,
|
|
953
|
-
key,
|
|
954
|
-
defaultValue
|
|
955
|
-
} }, this.context)).createTranslationKey;
|
|
956
|
-
}
|
|
957
|
-
async updateTranslationKeyDefaultValue(id, defaultValue) {
|
|
958
|
-
await this.client.query(`
|
|
959
|
-
mutation UpdateTranslationKeyDefault($id: ID!, $input: UpdateTranslationKeyInput!) {
|
|
960
|
-
updateTranslationKey(id: $id, input: $input) {
|
|
961
|
-
id
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
`, {
|
|
965
|
-
id,
|
|
966
|
-
input: { defaultValue }
|
|
967
|
-
}, this.context);
|
|
968
|
-
}
|
|
969
|
-
async findGlobalTranslation(keyId, languageCode) {
|
|
970
|
-
const globalTranslation = (await this.client.query(`
|
|
971
|
-
query FindTranslation($filter: TranslationFilterInput) {
|
|
972
|
-
translations(filter: $filter, limit: 50, offset: 0) {
|
|
973
|
-
items {
|
|
974
|
-
id
|
|
975
|
-
value
|
|
976
|
-
status
|
|
977
|
-
tenantId
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
`, { filter: {
|
|
982
|
-
keyId,
|
|
983
|
-
languageCode
|
|
984
|
-
} }, this.context)).translations.items.find((item) => item.tenantId === null);
|
|
985
|
-
if (!globalTranslation) return null;
|
|
986
|
-
return {
|
|
987
|
-
id: globalTranslation.id,
|
|
988
|
-
value: globalTranslation.value,
|
|
989
|
-
status: globalTranslation.status
|
|
990
|
-
};
|
|
991
|
-
}
|
|
992
|
-
async createLanguageTranslation(keyId, languageCode, value) {
|
|
993
|
-
return (await this.client.query(`
|
|
994
|
-
mutation CreateTranslation($input: CreateTranslationInput!) {
|
|
995
|
-
createTranslation(input: $input) {
|
|
996
|
-
id
|
|
997
|
-
status
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
`, { input: {
|
|
1001
|
-
keyId,
|
|
1002
|
-
languageCode,
|
|
1003
|
-
value
|
|
1004
|
-
} }, this.context)).createTranslation;
|
|
1005
|
-
}
|
|
1006
|
-
async updateLanguageTranslation(id, value, status) {
|
|
1007
|
-
await this.client.query(`
|
|
1008
|
-
mutation UpdateTranslation($id: ID!, $input: UpdateTranslationInput!) {
|
|
1009
|
-
updateTranslation(id: $id, input: $input) {
|
|
1010
|
-
id
|
|
1011
|
-
status
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
`, {
|
|
1015
|
-
id,
|
|
1016
|
-
input: {
|
|
1017
|
-
value,
|
|
1018
|
-
status
|
|
1019
|
-
}
|
|
1020
|
-
}, this.context);
|
|
1021
|
-
}
|
|
1022
|
-
/**
|
|
1023
|
-
* Escape regex special characters in a string
|
|
1024
|
-
*/
|
|
1025
|
-
escapeRegex(str) {
|
|
1026
|
-
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1027
|
-
}
|
|
1028
|
-
/**
|
|
1029
|
-
* Interpolate variables in translation string
|
|
1030
|
-
*/
|
|
1031
|
-
interpolate(text, variables) {
|
|
1032
|
-
let result = text;
|
|
1033
|
-
for (const [key, value] of Object.entries(variables)) {
|
|
1034
|
-
const escapedKey = this.escapeRegex(key);
|
|
1035
|
-
const regex = new RegExp(`{{\\s*${escapedKey}\\s*}}`, "g");
|
|
1036
|
-
result = result.replace(regex, String(value));
|
|
1037
|
-
}
|
|
1038
|
-
return result;
|
|
1039
|
-
}
|
|
1040
|
-
/**
|
|
1041
|
-
* Format translation with pluralization
|
|
1042
|
-
*/
|
|
1043
|
-
formatPlural(_language, count, translations) {
|
|
1044
|
-
if (count === 0 && translations.zero) return translations.zero;
|
|
1045
|
-
if (count === 1) return translations.one;
|
|
1046
|
-
return translations.other;
|
|
1047
|
-
}
|
|
1048
|
-
/**
|
|
1049
|
-
* Format date/time for locale
|
|
1050
|
-
*/
|
|
1051
|
-
formatDateTime(date, language, options) {
|
|
1052
|
-
const dateObj = typeof date === "string" ? new Date(date) : date;
|
|
1053
|
-
return new Intl.DateTimeFormat(language, options).format(dateObj);
|
|
1054
|
-
}
|
|
1055
|
-
/**
|
|
1056
|
-
* Format number for locale
|
|
1057
|
-
*/
|
|
1058
|
-
formatNumber(number, language, options) {
|
|
1059
|
-
return new Intl.NumberFormat(language, options).format(number);
|
|
1060
|
-
}
|
|
1061
|
-
/**
|
|
1062
|
-
* Format currency for locale
|
|
1063
|
-
*/
|
|
1064
|
-
formatCurrency(amount, currency, language) {
|
|
1065
|
-
return new Intl.NumberFormat(language, {
|
|
1066
|
-
style: "currency",
|
|
1067
|
-
currency
|
|
1068
|
-
}).format(amount);
|
|
1069
|
-
}
|
|
1070
|
-
};
|
|
1071
|
-
|
|
1072
|
-
// src/payments/index.ts
|
|
1073
|
-
/**
|
|
1074
|
-
* Payments Module
|
|
1075
|
-
*/
|
|
1076
|
-
var PaymentsModule = class {
|
|
1077
|
-
client;
|
|
1078
|
-
context;
|
|
1079
|
-
constructor(client, context) {
|
|
1080
|
-
this.client = client;
|
|
1081
|
-
this.context = context;
|
|
1082
|
-
}
|
|
1083
|
-
/**
|
|
1084
|
-
* Create payment
|
|
1085
|
-
*/
|
|
1086
|
-
async createPayment(options) {
|
|
1087
|
-
if (typeof options.amount !== "number" || options.amount <= 0) throw new ValidationError("Amount must be a positive number");
|
|
1088
|
-
if (!options.currency) throw new ValidationError("Currency is required");
|
|
1089
|
-
if (!options.paymentMethod) throw new ValidationError("Payment method is required");
|
|
1090
|
-
if (!options.customerId) throw new ValidationError("Customer ID is required");
|
|
1091
|
-
logger.debug("[Payments] Creating payment", {
|
|
1092
|
-
amount: options.amount,
|
|
1093
|
-
currency: options.currency,
|
|
1094
|
-
customerId: options.customerId
|
|
1095
|
-
});
|
|
1096
|
-
const mutation = `
|
|
1097
|
-
mutation CreatePayment($input: CreatePaymentInput!) {
|
|
1098
|
-
createPayment(input: $input) {
|
|
1099
|
-
id
|
|
1100
|
-
amount
|
|
1101
|
-
currency
|
|
1102
|
-
status
|
|
1103
|
-
paymentMethod
|
|
1104
|
-
customerId
|
|
1105
|
-
description
|
|
1106
|
-
metadata
|
|
1107
|
-
workspaceId
|
|
1108
|
-
organizationId
|
|
1109
|
-
transactionId
|
|
1110
|
-
failureReason
|
|
1111
|
-
createdAt
|
|
1112
|
-
updatedAt
|
|
1113
|
-
}
|
|
1114
|
-
}
|
|
1115
|
-
`;
|
|
1116
|
-
const variables = { input: {
|
|
1117
|
-
amount: options.amount,
|
|
1118
|
-
currency: options.currency,
|
|
1119
|
-
paymentMethod: options.paymentMethod,
|
|
1120
|
-
customerId: options.customerId,
|
|
1121
|
-
description: options.description,
|
|
1122
|
-
metadata: options.metadata,
|
|
1123
|
-
workspaceId: options.workspaceId || this.context.workspaceId,
|
|
1124
|
-
organizationId: options.organizationId
|
|
1125
|
-
} };
|
|
1126
|
-
const response = await this.client.query(mutation, variables, this.context);
|
|
1127
|
-
logger.info("[Payments] Payment created", { paymentId: response.createPayment.id });
|
|
1128
|
-
return response.createPayment;
|
|
1129
|
-
}
|
|
1130
|
-
/**
|
|
1131
|
-
* Get payment by ID
|
|
1132
|
-
*/
|
|
1133
|
-
async getPayment(paymentId) {
|
|
1134
|
-
logger.debug("[Payments] Getting payment", { paymentId });
|
|
1135
|
-
return (await this.client.query(`
|
|
1136
|
-
query GetPayment($paymentId: ID!) {
|
|
1137
|
-
getPayment(paymentId: $paymentId) {
|
|
1138
|
-
id
|
|
1139
|
-
amount
|
|
1140
|
-
currency
|
|
1141
|
-
status
|
|
1142
|
-
paymentMethod
|
|
1143
|
-
customerId
|
|
1144
|
-
description
|
|
1145
|
-
metadata
|
|
1146
|
-
workspaceId
|
|
1147
|
-
organizationId
|
|
1148
|
-
transactionId
|
|
1149
|
-
failureReason
|
|
1150
|
-
createdAt
|
|
1151
|
-
updatedAt
|
|
1152
|
-
}
|
|
1153
|
-
}
|
|
1154
|
-
`, { paymentId }, this.context)).getPayment;
|
|
1155
|
-
}
|
|
1156
|
-
/**
|
|
1157
|
-
* Get payments with filters
|
|
1158
|
-
*/
|
|
1159
|
-
async getPayments(options) {
|
|
1160
|
-
logger.debug("[Payments] Getting payments", options);
|
|
1161
|
-
const query = `
|
|
1162
|
-
query GetPayments(
|
|
1163
|
-
$customerId: ID
|
|
1164
|
-
$status: PaymentStatus
|
|
1165
|
-
$workspaceId: ID
|
|
1166
|
-
$organizationId: ID
|
|
1167
|
-
$startDate: DateTime
|
|
1168
|
-
$endDate: DateTime
|
|
1169
|
-
$limit: Int
|
|
1170
|
-
$offset: Int
|
|
1171
|
-
) {
|
|
1172
|
-
getPayments(
|
|
1173
|
-
customerId: $customerId
|
|
1174
|
-
status: $status
|
|
1175
|
-
workspaceId: $workspaceId
|
|
1176
|
-
organizationId: $organizationId
|
|
1177
|
-
startDate: $startDate
|
|
1178
|
-
endDate: $endDate
|
|
1179
|
-
limit: $limit
|
|
1180
|
-
offset: $offset
|
|
1181
|
-
) {
|
|
1182
|
-
payments {
|
|
1183
|
-
id
|
|
1184
|
-
amount
|
|
1185
|
-
currency
|
|
1186
|
-
status
|
|
1187
|
-
paymentMethod
|
|
1188
|
-
customerId
|
|
1189
|
-
description
|
|
1190
|
-
transactionId
|
|
1191
|
-
createdAt
|
|
1192
|
-
updatedAt
|
|
1193
|
-
}
|
|
1194
|
-
total
|
|
1195
|
-
}
|
|
1196
|
-
}
|
|
1197
|
-
`;
|
|
1198
|
-
const variables = {
|
|
1199
|
-
customerId: options.customerId,
|
|
1200
|
-
status: options.status,
|
|
1201
|
-
workspaceId: options.workspaceId || this.context.workspaceId,
|
|
1202
|
-
organizationId: options.organizationId,
|
|
1203
|
-
startDate: options.startDate,
|
|
1204
|
-
endDate: options.endDate,
|
|
1205
|
-
limit: options.limit || 50,
|
|
1206
|
-
offset: options.offset || 0
|
|
1207
|
-
};
|
|
1208
|
-
return (await this.client.query(query, variables, this.context)).getPayments;
|
|
1209
|
-
}
|
|
1210
|
-
/**
|
|
1211
|
-
* Refund payment
|
|
1212
|
-
*/
|
|
1213
|
-
async refundPayment(paymentId, amount, reason) {
|
|
1214
|
-
logger.debug("[Payments] Refunding payment", {
|
|
1215
|
-
paymentId,
|
|
1216
|
-
amount
|
|
1217
|
-
});
|
|
1218
|
-
const response = await this.client.query(`
|
|
1219
|
-
mutation RefundPayment($paymentId: ID!, $amount: Int, $reason: String) {
|
|
1220
|
-
refundPayment(paymentId: $paymentId, amount: $amount, reason: $reason) {
|
|
1221
|
-
id
|
|
1222
|
-
amount
|
|
1223
|
-
currency
|
|
1224
|
-
status
|
|
1225
|
-
paymentMethod
|
|
1226
|
-
customerId
|
|
1227
|
-
failureReason
|
|
1228
|
-
updatedAt
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
`, {
|
|
1232
|
-
paymentId,
|
|
1233
|
-
amount,
|
|
1234
|
-
reason
|
|
1235
|
-
}, this.context);
|
|
1236
|
-
logger.info("[Payments] Payment refunded", { paymentId });
|
|
1237
|
-
return response.refundPayment;
|
|
1238
|
-
}
|
|
1239
|
-
/**
|
|
1240
|
-
* Verify payment
|
|
1241
|
-
*/
|
|
1242
|
-
async verifyPayment(paymentId, verificationData) {
|
|
1243
|
-
logger.debug("[Payments] Verifying payment", { paymentId });
|
|
1244
|
-
const response = await this.client.query(`
|
|
1245
|
-
mutation VerifyPayment($paymentId: ID!, $verificationData: JSON!) {
|
|
1246
|
-
verifyPayment(paymentId: $paymentId, verificationData: $verificationData) {
|
|
1247
|
-
verified
|
|
1248
|
-
status
|
|
1249
|
-
}
|
|
1250
|
-
}
|
|
1251
|
-
`, {
|
|
1252
|
-
paymentId,
|
|
1253
|
-
verificationData
|
|
1254
|
-
}, this.context);
|
|
1255
|
-
logger.info("[Payments] Payment verified", {
|
|
1256
|
-
paymentId,
|
|
1257
|
-
verified: response.verifyPayment.verified
|
|
1258
|
-
});
|
|
1259
|
-
return response.verifyPayment;
|
|
1260
|
-
}
|
|
1261
|
-
/**
|
|
1262
|
-
* Create subscription
|
|
1263
|
-
*/
|
|
1264
|
-
async createSubscription(options) {
|
|
1265
|
-
if (!options.customerId) throw new ValidationError("Customer ID is required");
|
|
1266
|
-
if (!options.planId) throw new ValidationError("Plan ID is required");
|
|
1267
|
-
if (!options.paymentMethod) throw new ValidationError("Payment method is required");
|
|
1268
|
-
logger.debug("[Payments] Creating subscription", {
|
|
1269
|
-
customerId: options.customerId,
|
|
1270
|
-
planId: options.planId
|
|
1271
|
-
});
|
|
1272
|
-
const mutation = `
|
|
1273
|
-
mutation CreateSubscription($input: CreateSubscriptionInput!) {
|
|
1274
|
-
createSubscription(input: $input) {
|
|
1275
|
-
id
|
|
1276
|
-
customerId
|
|
1277
|
-
planId
|
|
1278
|
-
status
|
|
1279
|
-
currentPeriodStart
|
|
1280
|
-
currentPeriodEnd
|
|
1281
|
-
trialEnd
|
|
1282
|
-
cancelledAt
|
|
1283
|
-
endedAt
|
|
1284
|
-
metadata
|
|
1285
|
-
workspaceId
|
|
1286
|
-
organizationId
|
|
1287
|
-
createdAt
|
|
1288
|
-
updatedAt
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1291
|
-
`;
|
|
1292
|
-
const variables = { input: {
|
|
1293
|
-
customerId: options.customerId,
|
|
1294
|
-
planId: options.planId,
|
|
1295
|
-
paymentMethod: options.paymentMethod,
|
|
1296
|
-
trialDays: options.trialDays,
|
|
1297
|
-
metadata: options.metadata,
|
|
1298
|
-
workspaceId: options.workspaceId || this.context.workspaceId,
|
|
1299
|
-
organizationId: options.organizationId
|
|
1300
|
-
} };
|
|
1301
|
-
const response = await this.client.query(mutation, variables, this.context);
|
|
1302
|
-
logger.info("[Payments] Subscription created", { subscriptionId: response.createSubscription.id });
|
|
1303
|
-
return response.createSubscription;
|
|
1304
|
-
}
|
|
1305
|
-
/**
|
|
1306
|
-
* Get subscription
|
|
1307
|
-
*/
|
|
1308
|
-
async getSubscription(subscriptionId) {
|
|
1309
|
-
logger.debug("[Payments] Getting subscription", { subscriptionId });
|
|
1310
|
-
return (await this.client.query(`
|
|
1311
|
-
query GetSubscription($subscriptionId: ID!) {
|
|
1312
|
-
getSubscription(subscriptionId: $subscriptionId) {
|
|
1313
|
-
id
|
|
1314
|
-
customerId
|
|
1315
|
-
planId
|
|
1316
|
-
status
|
|
1317
|
-
currentPeriodStart
|
|
1318
|
-
currentPeriodEnd
|
|
1319
|
-
trialEnd
|
|
1320
|
-
cancelledAt
|
|
1321
|
-
endedAt
|
|
1322
|
-
metadata
|
|
1323
|
-
workspaceId
|
|
1324
|
-
organizationId
|
|
1325
|
-
createdAt
|
|
1326
|
-
updatedAt
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
`, { subscriptionId }, this.context)).getSubscription;
|
|
1330
|
-
}
|
|
1331
|
-
/**
|
|
1332
|
-
* Cancel subscription
|
|
1333
|
-
*/
|
|
1334
|
-
async cancelSubscription(subscriptionId, cancelAtPeriodEnd = false) {
|
|
1335
|
-
logger.debug("[Payments] Cancelling subscription", {
|
|
1336
|
-
subscriptionId,
|
|
1337
|
-
cancelAtPeriodEnd
|
|
1338
|
-
});
|
|
1339
|
-
const response = await this.client.query(`
|
|
1340
|
-
mutation CancelSubscription($subscriptionId: ID!, $cancelAtPeriodEnd: Boolean!) {
|
|
1341
|
-
cancelSubscription(subscriptionId: $subscriptionId, cancelAtPeriodEnd: $cancelAtPeriodEnd) {
|
|
1342
|
-
id
|
|
1343
|
-
status
|
|
1344
|
-
cancelledAt
|
|
1345
|
-
endedAt
|
|
1346
|
-
updatedAt
|
|
1347
|
-
}
|
|
1348
|
-
}
|
|
1349
|
-
`, {
|
|
1350
|
-
subscriptionId,
|
|
1351
|
-
cancelAtPeriodEnd
|
|
1352
|
-
}, this.context);
|
|
1353
|
-
logger.info("[Payments] Subscription cancelled", { subscriptionId });
|
|
1354
|
-
return response.cancelSubscription;
|
|
1355
|
-
}
|
|
1356
|
-
/**
|
|
1357
|
-
* Get customer subscriptions
|
|
1358
|
-
*/
|
|
1359
|
-
async getCustomerSubscriptions(customerId) {
|
|
1360
|
-
logger.debug("[Payments] Getting customer subscriptions", { customerId });
|
|
1361
|
-
return (await this.client.query(`
|
|
1362
|
-
query GetCustomerSubscriptions($customerId: ID!) {
|
|
1363
|
-
getCustomerSubscriptions(customerId: $customerId) {
|
|
1364
|
-
id
|
|
1365
|
-
customerId
|
|
1366
|
-
planId
|
|
1367
|
-
status
|
|
1368
|
-
currentPeriodStart
|
|
1369
|
-
currentPeriodEnd
|
|
1370
|
-
trialEnd
|
|
1371
|
-
cancelledAt
|
|
1372
|
-
createdAt
|
|
1373
|
-
}
|
|
1374
|
-
}
|
|
1375
|
-
`, { customerId }, this.context)).getCustomerSubscriptions;
|
|
1376
|
-
}
|
|
1377
|
-
/**
|
|
1378
|
-
* Get plans
|
|
1379
|
-
*/
|
|
1380
|
-
async getPlans(activeOnly = true) {
|
|
1381
|
-
logger.debug("[Payments] Getting plans", { activeOnly });
|
|
1382
|
-
return (await this.client.query(`
|
|
1383
|
-
query GetPlans($activeOnly: Boolean!) {
|
|
1384
|
-
getPlans(activeOnly: $activeOnly) {
|
|
1385
|
-
id
|
|
1386
|
-
name
|
|
1387
|
-
description
|
|
1388
|
-
amount
|
|
1389
|
-
currency
|
|
1390
|
-
interval
|
|
1391
|
-
intervalCount
|
|
1392
|
-
features
|
|
1393
|
-
metadata
|
|
1394
|
-
active
|
|
1395
|
-
createdAt
|
|
1396
|
-
}
|
|
1397
|
-
}
|
|
1398
|
-
`, { activeOnly }, this.context)).getPlans;
|
|
1399
|
-
}
|
|
1400
|
-
/**
|
|
1401
|
-
* Create payment intent
|
|
1402
|
-
*/
|
|
1403
|
-
async createPaymentIntent(options) {
|
|
1404
|
-
logger.debug("[Payments] Creating payment intent", {
|
|
1405
|
-
amount: options.amount,
|
|
1406
|
-
currency: options.currency
|
|
1407
|
-
});
|
|
1408
|
-
const response = await this.client.query(`
|
|
1409
|
-
mutation CreatePaymentIntent($input: CreatePaymentIntentInput!) {
|
|
1410
|
-
createPaymentIntent(input: $input) {
|
|
1411
|
-
clientSecret
|
|
1412
|
-
paymentIntentId
|
|
1413
|
-
}
|
|
1414
|
-
}
|
|
1415
|
-
`, { input: options }, this.context);
|
|
1416
|
-
logger.info("[Payments] Payment intent created", { paymentIntentId: response.createPaymentIntent.paymentIntentId });
|
|
1417
|
-
return response.createPaymentIntent;
|
|
1418
|
-
}
|
|
1419
|
-
/**
|
|
1420
|
-
* Get payment statistics
|
|
1421
|
-
*/
|
|
1422
|
-
async getPaymentStats(options) {
|
|
1423
|
-
logger.debug("[Payments] Getting payment statistics", options);
|
|
1424
|
-
return (await this.client.query(`
|
|
1425
|
-
query GetPaymentStats($startDate: DateTime, $endDate: DateTime, $workspaceId: ID) {
|
|
1426
|
-
getPaymentStats(startDate: $startDate, endDate: $endDate, workspaceId: $workspaceId) {
|
|
1427
|
-
totalRevenue
|
|
1428
|
-
totalPayments
|
|
1429
|
-
successfulPayments
|
|
1430
|
-
failedPayments
|
|
1431
|
-
refundedAmount
|
|
1432
|
-
averagePaymentAmount
|
|
1433
|
-
}
|
|
1434
|
-
}
|
|
1435
|
-
`, {
|
|
1436
|
-
startDate: options?.startDate,
|
|
1437
|
-
endDate: options?.endDate,
|
|
1438
|
-
workspaceId: options?.workspaceId || this.context.workspaceId
|
|
1439
|
-
}, this.context)).getPaymentStats;
|
|
1440
|
-
}
|
|
1441
|
-
};
|
|
1442
|
-
|
|
1443
|
-
// src/bootstrap/index.ts
|
|
1444
|
-
/**
|
|
1445
|
-
* Bootstrap Module
|
|
1446
|
-
* Helpers for bootstrapping server, loading modules, and setting up schemas
|
|
1447
|
-
*/
|
|
1448
|
-
/**
|
|
1449
|
-
* Bootstrap Module
|
|
1450
|
-
*/
|
|
1451
|
-
var BootstrapModule = class {
|
|
1452
|
-
config;
|
|
1453
|
-
startTime;
|
|
1454
|
-
healthCheckInterval;
|
|
1455
|
-
constructor(config) {
|
|
1456
|
-
this.config = config;
|
|
1457
|
-
this.startTime = /* @__PURE__ */ new Date();
|
|
1458
|
-
}
|
|
1459
|
-
/**
|
|
1460
|
-
* Bootstrap the application
|
|
1461
|
-
*/
|
|
1462
|
-
async bootstrap() {
|
|
1463
|
-
logger.info("[Bootstrap] Starting application bootstrap", {
|
|
1464
|
-
serviceName: this.config.serviceName,
|
|
1465
|
-
environment: this.config.environment || "development"
|
|
1466
|
-
});
|
|
1467
|
-
try {
|
|
1468
|
-
await this.validateConfiguration();
|
|
1469
|
-
const databaseConnected = await this.connectDatabase();
|
|
1470
|
-
const loadedModules = await this.loadModules();
|
|
1471
|
-
if (this.config.healthCheck) await this.setupHealthChecks();
|
|
1472
|
-
this.setupSignalHandlers();
|
|
1473
|
-
const result = {
|
|
1474
|
-
success: true,
|
|
1475
|
-
serviceName: this.config.serviceName || "burdenoff-service",
|
|
1476
|
-
serviceVersion: this.config.serviceVersion || "1.0.0",
|
|
1477
|
-
environment: this.config.environment || "development",
|
|
1478
|
-
startedAt: this.startTime.toISOString(),
|
|
1479
|
-
modules: loadedModules,
|
|
1480
|
-
databaseConnected,
|
|
1481
|
-
healthCheckEnabled: this.config.healthCheck || false
|
|
1482
|
-
};
|
|
1483
|
-
logger.info("[Bootstrap] Application bootstrapped successfully", result);
|
|
1484
|
-
return result;
|
|
1485
|
-
} catch (error) {
|
|
1486
|
-
logger.error("[Bootstrap] Failed to bootstrap application", { error });
|
|
1487
|
-
throw error;
|
|
1488
|
-
}
|
|
1489
|
-
}
|
|
1490
|
-
/**
|
|
1491
|
-
* Validate configuration
|
|
1492
|
-
*/
|
|
1493
|
-
async validateConfiguration() {
|
|
1494
|
-
logger.debug("[Bootstrap] Validating configuration");
|
|
1495
|
-
if (!this.config.workspacesApiUrl) throw new Error("Workspaces API URL is required");
|
|
1496
|
-
if (this.config.database?.url && !DatabaseModule.validateDatabaseUrl(this.config.database.url)) throw new Error("Invalid database URL");
|
|
1497
|
-
logger.debug("[Bootstrap] Configuration validated");
|
|
1498
|
-
}
|
|
1499
|
-
/**
|
|
1500
|
-
* Connect to database
|
|
1501
|
-
*/
|
|
1502
|
-
async connectDatabase() {
|
|
1503
|
-
if (!this.config.database) {
|
|
1504
|
-
logger.debug("[Bootstrap] No database configuration provided, skipping");
|
|
1505
|
-
return false;
|
|
1506
|
-
}
|
|
1507
|
-
logger.info("[Bootstrap] Connecting to database");
|
|
1508
|
-
try {
|
|
1509
|
-
const sanitizedUrl = DatabaseModule.sanitizeDatabaseUrl(this.config.database.url);
|
|
1510
|
-
logger.info("[Bootstrap] Database connection configured", { url: sanitizedUrl });
|
|
1511
|
-
return true;
|
|
1512
|
-
} catch (error) {
|
|
1513
|
-
logger.error("[Bootstrap] Failed to connect to database", { error });
|
|
1514
|
-
return false;
|
|
1515
|
-
}
|
|
1516
|
-
}
|
|
1517
|
-
/**
|
|
1518
|
-
* Load modules
|
|
1519
|
-
*/
|
|
1520
|
-
async loadModules() {
|
|
1521
|
-
const modules = this.config.modules || [];
|
|
1522
|
-
if (modules.length === 0) {
|
|
1523
|
-
logger.debug("[Bootstrap] No modules to load");
|
|
1524
|
-
return [];
|
|
1525
|
-
}
|
|
1526
|
-
logger.info("[Bootstrap] Loading modules", { modules });
|
|
1527
|
-
const loadedModules = [];
|
|
1528
|
-
for (const moduleName of modules) try {
|
|
1529
|
-
logger.debug(`[Bootstrap] Loading module: ${moduleName}`);
|
|
1530
|
-
loadedModules.push(moduleName);
|
|
1531
|
-
} catch (error) {
|
|
1532
|
-
logger.error(`[Bootstrap] Failed to load module: ${moduleName}`, { error });
|
|
1533
|
-
}
|
|
1534
|
-
logger.info("[Bootstrap] Modules loaded", { loaded: loadedModules });
|
|
1535
|
-
return loadedModules;
|
|
1536
|
-
}
|
|
1537
|
-
/**
|
|
1538
|
-
* Setup health checks
|
|
1539
|
-
*/
|
|
1540
|
-
async setupHealthChecks() {
|
|
1541
|
-
logger.info("[Bootstrap] Setting up health checks");
|
|
1542
|
-
const interval = this.config.healthCheckInterval || 3e4;
|
|
1543
|
-
this.healthCheckInterval = setInterval(() => {
|
|
1544
|
-
(async () => {
|
|
1545
|
-
const health = await this.performHealthCheck();
|
|
1546
|
-
if (!health.healthy) logger.warn("[Bootstrap] Health check failed", health);
|
|
1547
|
-
else logger.debug("[Bootstrap] Health check passed", health);
|
|
1548
|
-
})();
|
|
1549
|
-
}, interval);
|
|
1550
|
-
logger.info("[Bootstrap] Health checks configured", { interval });
|
|
1551
|
-
}
|
|
1552
|
-
/**
|
|
1553
|
-
* Perform health check
|
|
1554
|
-
*/
|
|
1555
|
-
async performHealthCheck() {
|
|
1556
|
-
const checks = {};
|
|
1557
|
-
const memUsage = process.memoryUsage();
|
|
1558
|
-
checks.memory = {
|
|
1559
|
-
used: memUsage.heapUsed,
|
|
1560
|
-
total: memUsage.heapTotal,
|
|
1561
|
-
percentage: memUsage.heapUsed / memUsage.heapTotal * 100
|
|
1562
|
-
};
|
|
1563
|
-
checks.uptime = Math.floor((Date.now() - this.startTime.getTime()) / 1e3);
|
|
1564
|
-
return {
|
|
1565
|
-
healthy: checks.memory.percentage < 90 && checks.uptime > 0,
|
|
1566
|
-
checks,
|
|
1567
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1568
|
-
};
|
|
1569
|
-
}
|
|
1570
|
-
/**
|
|
1571
|
-
* Setup signal handlers for graceful shutdown
|
|
1572
|
-
*/
|
|
1573
|
-
setupSignalHandlers() {
|
|
1574
|
-
logger.debug("[Bootstrap] Setting up signal handlers");
|
|
1575
|
-
const gracefulShutdown = async (signal) => {
|
|
1576
|
-
logger.info(`[Bootstrap] Received ${signal}, shutting down gracefully`);
|
|
1577
|
-
if (this.healthCheckInterval) clearInterval(this.healthCheckInterval);
|
|
1578
|
-
await this.cleanup();
|
|
1579
|
-
process.exit(0);
|
|
1580
|
-
};
|
|
1581
|
-
process.on("SIGTERM", () => {
|
|
1582
|
-
gracefulShutdown("SIGTERM");
|
|
1583
|
-
});
|
|
1584
|
-
process.on("SIGINT", () => {
|
|
1585
|
-
gracefulShutdown("SIGINT");
|
|
1586
|
-
});
|
|
1587
|
-
process.on("unhandledRejection", (reason, promise) => {
|
|
1588
|
-
logger.error("[Bootstrap] Unhandled rejection", {
|
|
1589
|
-
reason,
|
|
1590
|
-
promise
|
|
1591
|
-
});
|
|
1592
|
-
});
|
|
1593
|
-
process.on("uncaughtException", (error) => {
|
|
1594
|
-
logger.error("[Bootstrap] Uncaught exception", { error });
|
|
1595
|
-
process.exit(1);
|
|
1596
|
-
});
|
|
1597
|
-
}
|
|
1598
|
-
/**
|
|
1599
|
-
* Cleanup on shutdown
|
|
1600
|
-
*/
|
|
1601
|
-
async cleanup() {
|
|
1602
|
-
logger.info("[Bootstrap] Performing cleanup");
|
|
1603
|
-
await disposeAll();
|
|
1604
|
-
ContextManager.clearContext();
|
|
1605
|
-
logger.info("[Bootstrap] Cleanup completed");
|
|
1606
|
-
}
|
|
1607
|
-
/**
|
|
1608
|
-
* Get application info
|
|
1609
|
-
*/
|
|
1610
|
-
getInfo() {
|
|
1611
|
-
return {
|
|
1612
|
-
serviceName: this.config.serviceName || "burdenoff-service",
|
|
1613
|
-
serviceVersion: this.config.serviceVersion || "1.0.0",
|
|
1614
|
-
environment: this.config.environment || "development",
|
|
1615
|
-
uptime: Math.floor((Date.now() - this.startTime.getTime()) / 1e3),
|
|
1616
|
-
startedAt: this.startTime.toISOString()
|
|
1617
|
-
};
|
|
1618
|
-
}
|
|
1619
|
-
/**
|
|
1620
|
-
* Setup default context
|
|
1621
|
-
*/
|
|
1622
|
-
setupDefaultContext(context) {
|
|
1623
|
-
logger.debug("[Bootstrap] Setting up default context", context);
|
|
1624
|
-
ContextManager.setContext(context);
|
|
1625
|
-
}
|
|
1626
|
-
/**
|
|
1627
|
-
* Create context middleware (for Elysia/HTTP frameworks)
|
|
1628
|
-
*/
|
|
1629
|
-
createContextMiddleware() {
|
|
1630
|
-
return async (req, _res, next) => {
|
|
1631
|
-
const context = this.extractContextFromRequest(req);
|
|
1632
|
-
ContextManager.setContext(context);
|
|
1633
|
-
next();
|
|
1634
|
-
};
|
|
1635
|
-
}
|
|
1636
|
-
/**
|
|
1637
|
-
* Extract context from request
|
|
1638
|
-
*/
|
|
1639
|
-
extractContextFromRequest(req) {
|
|
1640
|
-
const context = {};
|
|
1641
|
-
if (req.user) {
|
|
1642
|
-
context.actorId = req.user.user_id;
|
|
1643
|
-
context.workspaceId = req.user.workspace_id;
|
|
1644
|
-
context.tenantId = req.user.tenant_endpoint;
|
|
1645
|
-
}
|
|
1646
|
-
const getHeader = (key) => {
|
|
1647
|
-
const value = req.headers[key.toLowerCase()];
|
|
1648
|
-
return Array.isArray(value) ? value[0] : value;
|
|
1649
|
-
};
|
|
1650
|
-
const authHeader = getHeader("authorization");
|
|
1651
|
-
if (authHeader) context.token = authHeader.replace("Bearer ", "");
|
|
1652
|
-
const workspaceId = getHeader("x-workspace-id");
|
|
1653
|
-
if (workspaceId && !context.workspaceId) context.workspaceId = workspaceId;
|
|
1654
|
-
const productId = getHeader("x-product-id");
|
|
1655
|
-
if (productId) context.productId = productId;
|
|
1656
|
-
const projectId = getHeader("x-project-id");
|
|
1657
|
-
if (projectId) context.projectId = projectId;
|
|
1658
|
-
const appId = getHeader("x-app-id");
|
|
1659
|
-
if (appId) context.appId = appId;
|
|
1660
|
-
const cfAccessClientId = getHeader("cf-access-client-id");
|
|
1661
|
-
if (cfAccessClientId) context.cfAccessClientId = cfAccessClientId;
|
|
1662
|
-
const cfAccessClientSecret = getHeader("cf-access-client-secret");
|
|
1663
|
-
if (cfAccessClientSecret) context.cfAccessClientSecret = cfAccessClientSecret;
|
|
1664
|
-
const cfAuthorization = getHeader("x-cf-authorization");
|
|
1665
|
-
if (cfAuthorization) context.cfAuthorization = cfAuthorization;
|
|
1666
|
-
return context;
|
|
1667
|
-
}
|
|
1668
|
-
/**
|
|
1669
|
-
* Load GraphQL schema
|
|
1670
|
-
*/
|
|
1671
|
-
async loadSchema(schemaPath) {
|
|
1672
|
-
logger.info("[Bootstrap] Loading GraphQL schema", { schemaPath });
|
|
1673
|
-
try {
|
|
1674
|
-
logger.info("[Bootstrap] Schema loaded successfully");
|
|
1675
|
-
return true;
|
|
1676
|
-
} catch (error) {
|
|
1677
|
-
logger.error("[Bootstrap] Failed to load schema", { error });
|
|
1678
|
-
return false;
|
|
1679
|
-
}
|
|
1680
|
-
}
|
|
1681
|
-
/**
|
|
1682
|
-
* Register shutdown hook
|
|
1683
|
-
*/
|
|
1684
|
-
onShutdown(callback) {
|
|
1685
|
-
const originalHandler = process.listeners("SIGTERM")[0];
|
|
1686
|
-
process.removeAllListeners("SIGTERM");
|
|
1687
|
-
process.on("SIGTERM", () => {
|
|
1688
|
-
(async () => {
|
|
1689
|
-
await callback();
|
|
1690
|
-
if (typeof originalHandler === "function") originalHandler();
|
|
1691
|
-
})();
|
|
1692
|
-
});
|
|
1693
|
-
}
|
|
1694
|
-
/**
|
|
1695
|
-
* Check if application is ready
|
|
1696
|
-
*/
|
|
1697
|
-
async isReady() {
|
|
1698
|
-
return (await this.performHealthCheck()).healthy;
|
|
1699
|
-
}
|
|
1700
|
-
/**
|
|
1701
|
-
* Get metrics
|
|
1702
|
-
*/
|
|
1703
|
-
getMetrics() {
|
|
1704
|
-
return {
|
|
1705
|
-
memory: process.memoryUsage(),
|
|
1706
|
-
uptime: Math.floor((Date.now() - this.startTime.getTime()) / 1e3),
|
|
1707
|
-
cpu: process.cpuUsage()
|
|
1708
|
-
};
|
|
1709
|
-
}
|
|
1710
|
-
};
|
|
1711
|
-
/**
|
|
1712
|
-
* Create bootstrap instance
|
|
1713
|
-
*/
|
|
1714
|
-
function createBootstrap(config) {
|
|
1715
|
-
return new BootstrapModule(config);
|
|
1716
|
-
}
|
|
1717
|
-
|
|
1718
248
|
// src/client/tenant-backend.ts
|
|
1719
249
|
/**
|
|
1720
250
|
* Tenant Backend Client
|