@elqnt/admin 2.2.1 → 2.3.1

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.
@@ -20,12 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // hooks/index.ts
21
21
  var hooks_exports = {};
22
22
  __export(hooks_exports, {
23
- useAnalyticsContext: () => useAnalyticsContext,
24
- useInvitesAdmin: () => useInvitesAdmin,
25
23
  useOrgAdmin: () => useOrgAdmin,
26
- useOrgProvisioning: () => useOrgProvisioning,
27
- useOrgSettings: () => useOrgSettings,
28
- useProductAnalytics: () => useProductAnalytics,
29
24
  useUsersAdmin: () => useUsersAdmin
30
25
  });
31
26
  module.exports = __toCommonJS(hooks_exports);
@@ -34,15 +29,15 @@ module.exports = __toCommonJS(hooks_exports);
34
29
  var import_react = require("react");
35
30
 
36
31
  // api/index.ts
37
- var import_browser6 = require("@elqnt/api-client/browser");
32
+ var import_browser4 = require("@elqnt/api-client/browser");
38
33
 
39
34
  // api/orgs.ts
40
35
  var import_browser = require("@elqnt/api-client/browser");
41
36
  async function listOrgsApi(filter, options) {
42
37
  const params = new URLSearchParams();
43
- if (filter?.product) params.set("product", filter.product);
44
38
  if (filter?.status) params.set("status", filter.status);
45
39
  if (filter?.type) params.set("type", filter.type);
40
+ if (filter?.product) params.set("product", filter.product);
46
41
  const queryString = params.toString();
47
42
  const url = queryString ? `/api/v1/admin/orgs?${queryString}` : "/api/v1/admin/orgs";
48
43
  return (0, import_browser.browserApiRequest)(url, {
@@ -82,13 +77,6 @@ async function getOrgInfoApi(orgId, options) {
82
77
  ...options
83
78
  });
84
79
  }
85
- async function createOrgWithSchemasApi(org, schemas, options) {
86
- return (0, import_browser.browserApiRequest)("/api/v1/admin/orgs/with-schemas", {
87
- method: "POST",
88
- body: { org, schemas },
89
- ...options
90
- });
91
- }
92
80
 
93
81
  // api/users.ts
94
82
  var import_browser2 = require("@elqnt/api-client/browser");
@@ -197,212 +185,67 @@ async function acceptInviteApi(inviteId, options) {
197
185
  });
198
186
  }
199
187
 
200
- // api/analytics.ts
201
- var import_browser4 = require("@elqnt/api-client/browser");
202
- function buildDateFilterParams(filter) {
203
- if (!filter) return "";
204
- const params = new URLSearchParams();
205
- if (filter.from) params.set("from", filter.from);
206
- if (filter.to) params.set("to", filter.to);
207
- const queryString = params.toString();
208
- return queryString ? `?${queryString}` : "";
209
- }
210
- async function getAnalyticsSummaryApi(filter, options) {
211
- const queryString = buildDateFilterParams(filter);
212
- return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/summary${queryString}`, {
213
- method: "GET",
214
- ...options
215
- });
216
- }
217
- async function getChatsAnalyticsApi(filter, agentId, options) {
218
- const params = new URLSearchParams();
219
- if (filter?.from) params.set("from", filter.from);
220
- if (filter?.to) params.set("to", filter.to);
221
- if (agentId) params.set("agent_id", agentId);
222
- const queryString = params.toString();
223
- return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/chats${queryString ? `?${queryString}` : ""}`, {
224
- method: "GET",
225
- ...options
226
- });
227
- }
228
- async function getAgentsAnalyticsApi(filter, options) {
229
- const queryString = buildDateFilterParams(filter);
230
- return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/agents${queryString}`, {
231
- method: "GET",
232
- ...options
233
- });
234
- }
235
- async function getUsageAnalyticsApi(filter, options) {
236
- const queryString = buildDateFilterParams(filter);
237
- return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/usage${queryString}`, {
238
- method: "GET",
239
- ...options
240
- });
241
- }
242
- async function getDailyAnalyticsApi(filter, options) {
243
- const queryString = buildDateFilterParams(filter);
244
- return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/daily${queryString}`, {
245
- method: "GET",
246
- ...options
247
- });
248
- }
249
- async function getAnalyticsEventsApi(filter, options) {
250
- const queryString = buildDateFilterParams(filter);
251
- return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/events${queryString}`, {
252
- method: "GET",
253
- ...options
254
- });
255
- }
256
- async function logAnalyticsEventApi(event, options) {
257
- return (0, import_browser4.browserApiRequest)("/api/v1/analytics/events", {
258
- method: "POST",
259
- body: event,
260
- ...options
261
- });
262
- }
263
- async function getGlobalSummaryApi(filter, options) {
264
- const queryString = buildDateFilterParams(filter);
265
- return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/global/summary${queryString}`, {
266
- method: "GET",
267
- ...options
268
- });
269
- }
270
- async function getGlobalOrgsAnalyticsApi(filter, options) {
271
- const queryString = buildDateFilterParams(filter);
272
- return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/global/orgs${queryString}`, {
273
- method: "GET",
274
- ...options
275
- });
276
- }
277
-
278
- // api/provisioning.ts
279
- var import_browser5 = require("@elqnt/api-client/browser");
280
- async function createOrgWithProvisioningApi(request, options) {
281
- return (0, import_browser5.browserApiRequest)("/api/v1/admin/provisioning/orgs", {
282
- method: "POST",
283
- body: request,
284
- ...options
285
- });
286
- }
287
- async function getProvisioningStatusApi(orgId, options) {
288
- return (0, import_browser5.browserApiRequest)(`/api/v1/admin/orgs/${orgId}/provisioning`, {
289
- method: "GET",
290
- ...options
291
- });
292
- }
293
- async function retryProvisioningApi(orgId, request, options) {
294
- return (0, import_browser5.browserApiRequest)(`/api/v1/admin/orgs/${orgId}/provisioning/retry`, {
295
- method: "POST",
296
- body: request || {},
297
- ...options
298
- });
299
- }
300
- async function validateProvisioningApi(orgId, options) {
301
- return (0, import_browser5.browserApiRequest)(
302
- `/api/v1/admin/orgs/${orgId}/provisioning/validate`,
303
- {
304
- method: "POST",
305
- ...options
306
- }
307
- );
308
- }
309
- async function cancelProvisioningApi(orgId, options) {
310
- return (0, import_browser5.browserApiRequest)(
311
- `/api/v1/admin/orgs/${orgId}/provisioning/cancel`,
312
- {
313
- method: "POST",
314
- ...options
315
- }
316
- );
188
+ // api/index.ts
189
+ function orgRowToSettings(orgId, row) {
190
+ return {
191
+ id: row?.id ?? "",
192
+ org_id: orgId,
193
+ title: row?.title ?? "",
194
+ description: row?.description ?? "",
195
+ logo_url: row?.logoUrl ?? "",
196
+ default_lang: row?.defaultLang ?? "en",
197
+ timezone: row?.timezone ?? "UTC",
198
+ additional_settings: row?.settings ?? {},
199
+ created_at: "",
200
+ updated_at: "",
201
+ created_by: "",
202
+ updated_by: ""
203
+ };
317
204
  }
318
- async function cleanupProvisioningApi(orgId, options) {
319
- return (0, import_browser5.browserApiRequest)(`/api/v1/admin/orgs/${orgId}/provisioning`, {
320
- method: "DELETE",
321
- ...options
322
- });
205
+ function settingsToOrgRow(settings) {
206
+ return {
207
+ title: settings.title,
208
+ description: settings.description,
209
+ logoUrl: settings.logo_url,
210
+ defaultLang: settings.default_lang,
211
+ timezone: settings.timezone,
212
+ settings: settings.additional_settings
213
+ };
323
214
  }
324
- function streamProvisioningProgress(orgId, callbacks, options) {
325
- const url = new URL(
326
- `/api/v1/admin/orgs/${orgId}/provisioning/stream`,
327
- options.baseUrl
215
+ async function getOrgSettingsApi(options) {
216
+ const orgId = options.orgId ?? "";
217
+ const response = await (0, import_browser4.browserApiRequest)(
218
+ `/api/v1/admin/orgs/${orgId}`,
219
+ { method: "GET", ...options }
328
220
  );
329
- if (options.token) {
330
- url.searchParams.set("token", options.token);
221
+ if (response.error) {
222
+ return { error: response.error, status: response.status };
331
223
  }
332
- const eventSource = new EventSource(url.toString());
333
- eventSource.addEventListener("connected", (event) => {
334
- try {
335
- const data = JSON.parse(event.data);
336
- callbacks.onConnected?.(data.orgId);
337
- } catch (e) {
338
- console.error("Failed to parse connected event", e);
339
- }
340
- });
341
- eventSource.addEventListener("progress", (event) => {
342
- try {
343
- const progress = JSON.parse(event.data);
344
- callbacks.onProgress?.(progress);
345
- } catch (e) {
346
- console.error("Failed to parse progress event", e);
224
+ return {
225
+ status: response.status,
226
+ data: {
227
+ success: true,
228
+ settings: orgRowToSettings(orgId, response.data?.organization)
347
229
  }
348
- });
349
- eventSource.addEventListener("done", () => {
350
- callbacks.onDone?.();
351
- eventSource.close();
352
- });
353
- eventSource.addEventListener("timeout", () => {
354
- callbacks.onTimeout?.();
355
- eventSource.close();
356
- });
357
- eventSource.onerror = (error) => {
358
- callbacks.onError?.(new Error("SSE connection error"));
359
- eventSource.close();
360
- };
361
- return () => {
362
- eventSource.close();
363
230
  };
364
231
  }
365
- function calculateProgressPercentage(progress) {
366
- if (progress.totalArtifacts === 0) return 0;
367
- return Math.round(
368
- progress.completedArtifacts / progress.totalArtifacts * 100
369
- );
370
- }
371
- function isProvisioningComplete(progress) {
372
- return progress.status === "completed" || progress.status === "failed" || progress.status === "partial";
373
- }
374
- function isProvisioningSuccessful(progress) {
375
- return progress.status === "completed";
376
- }
377
- function getFailedArtifacts(progress) {
378
- return progress.artifacts.filter((a) => a.status === "failed");
379
- }
380
- function hasCriticalFailures(progress) {
381
- return progress.artifacts.some(
382
- (a) => a.status === "failed" && a.critical
383
- );
384
- }
385
-
386
- // api/index.ts
387
- async function getOrgSettingsApi(options) {
388
- return (0, import_browser6.browserApiRequest)("/api/v1/org/settings", {
389
- method: "GET",
390
- ...options
391
- });
392
- }
393
232
  async function createOrgSettingsApi(settings, options) {
394
- return (0, import_browser6.browserApiRequest)("/api/v1/org/settings", {
395
- method: "POST",
396
- body: settings,
397
- ...options
398
- });
233
+ return updateOrgSettingsApi(settings, options);
399
234
  }
400
235
  async function updateOrgSettingsApi(settings, options) {
401
- return (0, import_browser6.browserApiRequest)("/api/v1/org/settings", {
402
- method: "PUT",
403
- body: settings,
404
- ...options
405
- });
236
+ const orgId = options.orgId ?? "";
237
+ const response = await (0, import_browser4.browserApiRequest)(
238
+ `/api/v1/admin/orgs/${orgId}`,
239
+ {
240
+ method: "PUT",
241
+ body: settingsToOrgRow(settings),
242
+ ...options
243
+ }
244
+ );
245
+ if (response.error) {
246
+ return { error: response.error, status: response.status };
247
+ }
248
+ return getOrgSettingsApi(options);
406
249
  }
407
250
 
408
251
  // hooks/use-org-admin.ts
@@ -490,19 +333,19 @@ function useOrgAdmin(options) {
490
333
  },
491
334
  [options]
492
335
  );
493
- const createOrgWithSchemas = (0, import_react.useCallback)(
494
- async (org, schemas) => {
336
+ const updateOrg = (0, import_react.useCallback)(
337
+ async (orgId, updates) => {
495
338
  setLoading(true);
496
339
  setError(null);
497
340
  try {
498
- const response = await createOrgWithSchemasApi(org, schemas, options);
341
+ const response = await updateOrgApi(orgId, updates, options);
499
342
  if (response.error) {
500
343
  setError(response.error);
501
344
  return null;
502
345
  }
503
- return response.data || null;
346
+ return response.data?.org || null;
504
347
  } catch (err) {
505
- const message = err instanceof Error ? err.message : "Failed to create organization with schemas";
348
+ const message = err instanceof Error ? err.message : "Failed to update organization";
506
349
  setError(message);
507
350
  return null;
508
351
  } finally {
@@ -511,19 +354,58 @@ function useOrgAdmin(options) {
511
354
  },
512
355
  [options]
513
356
  );
514
- const updateOrg = (0, import_react.useCallback)(
515
- async (orgId, updates) => {
357
+ const deleteOrg = (0, import_react.useCallback)(
358
+ async (orgId) => {
516
359
  setLoading(true);
517
360
  setError(null);
518
361
  try {
519
- const response = await updateOrgApi(orgId, updates, options);
362
+ const response = await deleteOrgApi(orgId, options);
363
+ if (response.error) {
364
+ setError(response.error);
365
+ return false;
366
+ }
367
+ return response.data?.success || false;
368
+ } catch (err) {
369
+ const message = err instanceof Error ? err.message : "Failed to delete organization";
370
+ setError(message);
371
+ return false;
372
+ } finally {
373
+ setLoading(false);
374
+ }
375
+ },
376
+ [options]
377
+ );
378
+ const getSettings = (0, import_react.useCallback)(async () => {
379
+ setLoading(true);
380
+ setError(null);
381
+ try {
382
+ const response = await getOrgSettingsApi(options);
383
+ if (response.error) {
384
+ setError(response.error);
385
+ return null;
386
+ }
387
+ return response.data?.settings || null;
388
+ } catch (err) {
389
+ const message = err instanceof Error ? err.message : "Failed to get organization settings";
390
+ setError(message);
391
+ return null;
392
+ } finally {
393
+ setLoading(false);
394
+ }
395
+ }, [options]);
396
+ const createSettings = (0, import_react.useCallback)(
397
+ async (settings) => {
398
+ setLoading(true);
399
+ setError(null);
400
+ try {
401
+ const response = await createOrgSettingsApi(settings, options);
520
402
  if (response.error) {
521
403
  setError(response.error);
522
404
  return null;
523
405
  }
524
- return response.data?.org || null;
406
+ return response.data?.settings || null;
525
407
  } catch (err) {
526
- const message = err instanceof Error ? err.message : "Failed to update organization";
408
+ const message = err instanceof Error ? err.message : "Failed to create organization settings";
527
409
  setError(message);
528
410
  return null;
529
411
  } finally {
@@ -532,21 +414,21 @@ function useOrgAdmin(options) {
532
414
  },
533
415
  [options]
534
416
  );
535
- const deleteOrg = (0, import_react.useCallback)(
536
- async (orgId) => {
417
+ const updateSettings = (0, import_react.useCallback)(
418
+ async (settings) => {
537
419
  setLoading(true);
538
420
  setError(null);
539
421
  try {
540
- const response = await deleteOrgApi(orgId, options);
422
+ const response = await updateOrgSettingsApi(settings, options);
541
423
  if (response.error) {
542
424
  setError(response.error);
543
- return false;
425
+ return null;
544
426
  }
545
- return response.data?.success || false;
427
+ return response.data?.settings || null;
546
428
  } catch (err) {
547
- const message = err instanceof Error ? err.message : "Failed to delete organization";
429
+ const message = err instanceof Error ? err.message : "Failed to update organization settings";
548
430
  setError(message);
549
- return false;
431
+ return null;
550
432
  } finally {
551
433
  setLoading(false);
552
434
  }
@@ -560,9 +442,12 @@ function useOrgAdmin(options) {
560
442
  getOrg,
561
443
  getOrgInfo,
562
444
  createOrg,
563
- createOrgWithSchemas,
564
445
  updateOrg,
565
- deleteOrg
446
+ deleteOrg,
447
+ // Org settings
448
+ getSettings,
449
+ createSettings,
450
+ updateSettings
566
451
  };
567
452
  }
568
453
 
@@ -757,27 +642,7 @@ function useUsersAdmin(options) {
757
642
  },
758
643
  [options]
759
644
  );
760
- return {
761
- loading,
762
- error,
763
- listUsers,
764
- getUser,
765
- getUserByEmail,
766
- getUserByPhone,
767
- createUser,
768
- updateUser,
769
- deleteUser,
770
- getUserSettings,
771
- updateUserSettings
772
- };
773
- }
774
-
775
- // hooks/use-invites-admin.ts
776
- var import_react3 = require("react");
777
- function useInvitesAdmin(options) {
778
- const [loading, setLoading] = (0, import_react3.useState)(false);
779
- const [error, setError] = (0, import_react3.useState)(null);
780
- const listInvites = (0, import_react3.useCallback)(async () => {
645
+ const listInvites = (0, import_react2.useCallback)(async () => {
781
646
  setLoading(true);
782
647
  setError(null);
783
648
  try {
@@ -795,7 +660,7 @@ function useInvitesAdmin(options) {
795
660
  setLoading(false);
796
661
  }
797
662
  }, [options]);
798
- const getInvite = (0, import_react3.useCallback)(
663
+ const getInvite = (0, import_react2.useCallback)(
799
664
  async (inviteId) => {
800
665
  setLoading(true);
801
666
  setError(null);
@@ -816,7 +681,7 @@ function useInvitesAdmin(options) {
816
681
  },
817
682
  [options]
818
683
  );
819
- const sendInvite = (0, import_react3.useCallback)(
684
+ const sendInvite = (0, import_react2.useCallback)(
820
685
  async (invite) => {
821
686
  setLoading(true);
822
687
  setError(null);
@@ -837,7 +702,7 @@ function useInvitesAdmin(options) {
837
702
  },
838
703
  [options]
839
704
  );
840
- const sendInvites = (0, import_react3.useCallback)(
705
+ const sendInvites = (0, import_react2.useCallback)(
841
706
  async (invites) => {
842
707
  setLoading(true);
843
708
  setError(null);
@@ -858,7 +723,7 @@ function useInvitesAdmin(options) {
858
723
  },
859
724
  [options]
860
725
  );
861
- const resendInvite = (0, import_react3.useCallback)(
726
+ const resendInvite = (0, import_react2.useCallback)(
862
727
  async (inviteId) => {
863
728
  setLoading(true);
864
729
  setError(null);
@@ -879,7 +744,7 @@ function useInvitesAdmin(options) {
879
744
  },
880
745
  [options]
881
746
  );
882
- const revokeInvite = (0, import_react3.useCallback)(
747
+ const revokeInvite = (0, import_react2.useCallback)(
883
748
  async (inviteId) => {
884
749
  setLoading(true);
885
750
  setError(null);
@@ -900,7 +765,7 @@ function useInvitesAdmin(options) {
900
765
  },
901
766
  [options]
902
767
  );
903
- const acceptInvite = (0, import_react3.useCallback)(
768
+ const acceptInvite = (0, import_react2.useCallback)(
904
769
  async (inviteId) => {
905
770
  setLoading(true);
906
771
  setError(null);
@@ -924,6 +789,16 @@ function useInvitesAdmin(options) {
924
789
  return {
925
790
  loading,
926
791
  error,
792
+ listUsers,
793
+ getUser,
794
+ getUserByEmail,
795
+ getUserByPhone,
796
+ createUser,
797
+ updateUser,
798
+ deleteUser,
799
+ getUserSettings,
800
+ updateUserSettings,
801
+ // Invitations
927
802
  listInvites,
928
803
  getInvite,
929
804
  sendInvite,
@@ -933,594 +808,9 @@ function useInvitesAdmin(options) {
933
808
  acceptInvite
934
809
  };
935
810
  }
936
-
937
- // hooks/use-org-settings.ts
938
- var import_react4 = require("react");
939
- function useOrgSettings(options) {
940
- const [loading, setLoading] = (0, import_react4.useState)(false);
941
- const [error, setError] = (0, import_react4.useState)(null);
942
- const getSettings = (0, import_react4.useCallback)(async () => {
943
- setLoading(true);
944
- setError(null);
945
- try {
946
- const response = await getOrgSettingsApi(options);
947
- if (response.error) {
948
- setError(response.error);
949
- return null;
950
- }
951
- return response.data?.settings || null;
952
- } catch (err) {
953
- const message = err instanceof Error ? err.message : "Failed to get organization settings";
954
- setError(message);
955
- return null;
956
- } finally {
957
- setLoading(false);
958
- }
959
- }, [options]);
960
- const createSettings = (0, import_react4.useCallback)(
961
- async (settings) => {
962
- setLoading(true);
963
- setError(null);
964
- try {
965
- const response = await createOrgSettingsApi(settings, options);
966
- if (response.error) {
967
- setError(response.error);
968
- return null;
969
- }
970
- return response.data?.settings || null;
971
- } catch (err) {
972
- const message = err instanceof Error ? err.message : "Failed to create organization settings";
973
- setError(message);
974
- return null;
975
- } finally {
976
- setLoading(false);
977
- }
978
- },
979
- [options]
980
- );
981
- const updateSettings = (0, import_react4.useCallback)(
982
- async (settings) => {
983
- setLoading(true);
984
- setError(null);
985
- try {
986
- const response = await updateOrgSettingsApi(settings, options);
987
- if (response.error) {
988
- setError(response.error);
989
- return null;
990
- }
991
- return response.data?.settings || null;
992
- } catch (err) {
993
- const message = err instanceof Error ? err.message : "Failed to update organization settings";
994
- setError(message);
995
- return null;
996
- } finally {
997
- setLoading(false);
998
- }
999
- },
1000
- [options]
1001
- );
1002
- return {
1003
- loading,
1004
- error,
1005
- getSettings,
1006
- createSettings,
1007
- updateSettings
1008
- };
1009
- }
1010
-
1011
- // hooks/use-product-analytics.ts
1012
- var import_react5 = require("react");
1013
- function useProductAnalytics(options) {
1014
- const [state, setState] = (0, import_react5.useState)({
1015
- summary: null,
1016
- chats: [],
1017
- agents: [],
1018
- usage: [],
1019
- daily: [],
1020
- events: [],
1021
- loading: false,
1022
- error: null
1023
- });
1024
- const sessionIdRef = (0, import_react5.useRef)("");
1025
- (0, import_react5.useEffect)(() => {
1026
- if (typeof window !== "undefined") {
1027
- let sessionId = sessionStorage.getItem("analytics_session_id");
1028
- if (!sessionId) {
1029
- sessionId = crypto.randomUUID();
1030
- sessionStorage.setItem("analytics_session_id", sessionId);
1031
- }
1032
- sessionIdRef.current = sessionId;
1033
- }
1034
- }, []);
1035
- const getSummary = (0, import_react5.useCallback)(
1036
- async (filter) => {
1037
- setState((prev) => ({ ...prev, loading: true, error: null }));
1038
- try {
1039
- const response = await getAnalyticsSummaryApi(filter, options);
1040
- if (response.error) {
1041
- setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1042
- return null;
1043
- }
1044
- const summary = response.data?.data || null;
1045
- setState((prev) => ({ ...prev, loading: false, summary }));
1046
- return summary;
1047
- } catch (err) {
1048
- const message = err instanceof Error ? err.message : "Failed to get analytics summary";
1049
- setState((prev) => ({ ...prev, loading: false, error: message }));
1050
- return null;
1051
- }
1052
- },
1053
- [options]
1054
- );
1055
- const getChats = (0, import_react5.useCallback)(
1056
- async (filter, agentId) => {
1057
- setState((prev) => ({ ...prev, loading: true, error: null }));
1058
- try {
1059
- const response = await getChatsAnalyticsApi(filter, agentId, options);
1060
- if (response.error) {
1061
- setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1062
- return [];
1063
- }
1064
- const chats = response.data?.data || [];
1065
- setState((prev) => ({ ...prev, loading: false, chats }));
1066
- return chats;
1067
- } catch (err) {
1068
- const message = err instanceof Error ? err.message : "Failed to get chat analytics";
1069
- setState((prev) => ({ ...prev, loading: false, error: message }));
1070
- return [];
1071
- }
1072
- },
1073
- [options]
1074
- );
1075
- const getAgents = (0, import_react5.useCallback)(
1076
- async (filter) => {
1077
- setState((prev) => ({ ...prev, loading: true, error: null }));
1078
- try {
1079
- const response = await getAgentsAnalyticsApi(filter, options);
1080
- if (response.error) {
1081
- setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1082
- return [];
1083
- }
1084
- const agents = response.data?.data || [];
1085
- setState((prev) => ({ ...prev, loading: false, agents }));
1086
- return agents;
1087
- } catch (err) {
1088
- const message = err instanceof Error ? err.message : "Failed to get agent analytics";
1089
- setState((prev) => ({ ...prev, loading: false, error: message }));
1090
- return [];
1091
- }
1092
- },
1093
- [options]
1094
- );
1095
- const getUsage = (0, import_react5.useCallback)(
1096
- async (filter) => {
1097
- setState((prev) => ({ ...prev, loading: true, error: null }));
1098
- try {
1099
- const response = await getUsageAnalyticsApi(filter, options);
1100
- if (response.error) {
1101
- setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1102
- return [];
1103
- }
1104
- const usage = response.data?.data || [];
1105
- setState((prev) => ({ ...prev, loading: false, usage }));
1106
- return usage;
1107
- } catch (err) {
1108
- const message = err instanceof Error ? err.message : "Failed to get usage analytics";
1109
- setState((prev) => ({ ...prev, loading: false, error: message }));
1110
- return [];
1111
- }
1112
- },
1113
- [options]
1114
- );
1115
- const getDaily = (0, import_react5.useCallback)(
1116
- async (filter) => {
1117
- setState((prev) => ({ ...prev, loading: true, error: null }));
1118
- try {
1119
- const response = await getDailyAnalyticsApi(filter, options);
1120
- if (response.error) {
1121
- setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1122
- return [];
1123
- }
1124
- const daily = response.data?.data || [];
1125
- setState((prev) => ({ ...prev, loading: false, daily }));
1126
- return daily;
1127
- } catch (err) {
1128
- const message = err instanceof Error ? err.message : "Failed to get daily analytics";
1129
- setState((prev) => ({ ...prev, loading: false, error: message }));
1130
- return [];
1131
- }
1132
- },
1133
- [options]
1134
- );
1135
- const getEvents = (0, import_react5.useCallback)(
1136
- async (filter) => {
1137
- setState((prev) => ({ ...prev, loading: true, error: null }));
1138
- try {
1139
- const response = await getAnalyticsEventsApi(filter, options);
1140
- if (response.error) {
1141
- setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1142
- return [];
1143
- }
1144
- const events = response.data?.data || [];
1145
- setState((prev) => ({ ...prev, loading: false, events }));
1146
- return events;
1147
- } catch (err) {
1148
- const message = err instanceof Error ? err.message : "Failed to get analytics events";
1149
- setState((prev) => ({ ...prev, loading: false, error: message }));
1150
- return [];
1151
- }
1152
- },
1153
- [options]
1154
- );
1155
- const trackEvent = (0, import_react5.useCallback)(
1156
- async (eventType, eventCategory, properties) => {
1157
- try {
1158
- const event = {
1159
- sessionId: sessionIdRef.current,
1160
- userId: options.userId || "",
1161
- eventType,
1162
- eventCategory,
1163
- pagePath: typeof window !== "undefined" ? window.location.pathname : void 0,
1164
- properties
1165
- };
1166
- await logAnalyticsEventApi(event, options);
1167
- } catch {
1168
- console.debug("Failed to track analytics event:", eventType);
1169
- }
1170
- },
1171
- [options]
1172
- );
1173
- const trackPageView = (0, import_react5.useCallback)(
1174
- async (pageName) => {
1175
- await trackEvent("page_view", "navigation", {
1176
- pageName: pageName || (typeof window !== "undefined" ? document.title : "")
1177
- });
1178
- },
1179
- [trackEvent]
1180
- );
1181
- const getGlobalSummary = (0, import_react5.useCallback)(
1182
- async (filter) => {
1183
- setState((prev) => ({ ...prev, loading: true, error: null }));
1184
- try {
1185
- const response = await getGlobalSummaryApi(filter, options);
1186
- if (response.error) {
1187
- setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1188
- return null;
1189
- }
1190
- setState((prev) => ({ ...prev, loading: false }));
1191
- return response.data?.data || null;
1192
- } catch (err) {
1193
- const message = err instanceof Error ? err.message : "Failed to get global summary";
1194
- setState((prev) => ({ ...prev, loading: false, error: message }));
1195
- return null;
1196
- }
1197
- },
1198
- [options]
1199
- );
1200
- const getGlobalOrgs = (0, import_react5.useCallback)(
1201
- async (filter) => {
1202
- setState((prev) => ({ ...prev, loading: true, error: null }));
1203
- try {
1204
- const response = await getGlobalOrgsAnalyticsApi(filter, options);
1205
- if (response.error) {
1206
- setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1207
- return [];
1208
- }
1209
- setState((prev) => ({ ...prev, loading: false }));
1210
- return response.data?.data || [];
1211
- } catch (err) {
1212
- const message = err instanceof Error ? err.message : "Failed to get org analytics";
1213
- setState((prev) => ({ ...prev, loading: false, error: message }));
1214
- return [];
1215
- }
1216
- },
1217
- [options]
1218
- );
1219
- return {
1220
- // State
1221
- ...state,
1222
- // Query functions
1223
- getSummary,
1224
- getChats,
1225
- getAgents,
1226
- getUsage,
1227
- getDaily,
1228
- getEvents,
1229
- // Event tracking
1230
- trackEvent,
1231
- trackPageView,
1232
- // Global analytics (admin)
1233
- getGlobalSummary,
1234
- getGlobalOrgs
1235
- };
1236
- }
1237
- var useAnalyticsContext = useProductAnalytics;
1238
-
1239
- // hooks/use-org-provisioning.ts
1240
- var import_react6 = require("react");
1241
- function useOrgProvisioning(options) {
1242
- const [state, setState] = (0, import_react6.useState)({
1243
- status: "idle",
1244
- progress: null,
1245
- org: null,
1246
- error: null,
1247
- percentage: 0
1248
- });
1249
- const cleanupRef = (0, import_react6.useRef)(null);
1250
- (0, import_react6.useEffect)(() => {
1251
- return () => {
1252
- if (cleanupRef.current) {
1253
- cleanupRef.current();
1254
- }
1255
- };
1256
- }, []);
1257
- const createOrgWithProvisioning = (0, import_react6.useCallback)(
1258
- async (request) => {
1259
- setState((prev) => ({
1260
- ...prev,
1261
- status: "creating",
1262
- error: null,
1263
- progress: null,
1264
- percentage: 0
1265
- }));
1266
- try {
1267
- const response = await createOrgWithProvisioningApi(request, options);
1268
- if (response.error) {
1269
- setState((prev) => ({
1270
- ...prev,
1271
- status: "failed",
1272
- error: response.error || "Failed to create organization"
1273
- }));
1274
- return null;
1275
- }
1276
- const org = response.data?.organization || response.data?.org;
1277
- if (!org) {
1278
- setState((prev) => ({
1279
- ...prev,
1280
- status: "failed",
1281
- error: "No organization returned"
1282
- }));
1283
- return null;
1284
- }
1285
- setState((prev) => ({
1286
- ...prev,
1287
- status: "provisioning",
1288
- org
1289
- }));
1290
- const cleanup = streamProvisioningProgress(
1291
- org.id,
1292
- {
1293
- onProgress: (progress) => {
1294
- const percentage = calculateProgressPercentage(progress);
1295
- const status = isProvisioningComplete(progress) ? isProvisioningSuccessful(progress) ? "completed" : progress.status === "partial" ? "partial" : "failed" : "provisioning";
1296
- setState((prev) => ({
1297
- ...prev,
1298
- status,
1299
- progress,
1300
- percentage,
1301
- error: progress.error || null
1302
- }));
1303
- },
1304
- onDone: () => {
1305
- cleanupRef.current = null;
1306
- },
1307
- onError: (error) => {
1308
- setState((prev) => ({
1309
- ...prev,
1310
- status: "failed",
1311
- error: error.message
1312
- }));
1313
- cleanupRef.current = null;
1314
- },
1315
- onTimeout: () => {
1316
- setState((prev) => ({
1317
- ...prev,
1318
- error: "Provisioning timed out"
1319
- }));
1320
- cleanupRef.current = null;
1321
- }
1322
- },
1323
- { baseUrl: options.baseUrl }
1324
- );
1325
- cleanupRef.current = cleanup;
1326
- return org;
1327
- } catch (err) {
1328
- const message = err instanceof Error ? err.message : "Failed to create organization";
1329
- setState((prev) => ({
1330
- ...prev,
1331
- status: "failed",
1332
- error: message
1333
- }));
1334
- return null;
1335
- }
1336
- },
1337
- [options]
1338
- );
1339
- const getProvisioningStatus = (0, import_react6.useCallback)(
1340
- async (orgId) => {
1341
- try {
1342
- const response = await getProvisioningStatusApi(orgId, options);
1343
- if (response.error) {
1344
- setState((prev) => ({ ...prev, error: response.error || null }));
1345
- return null;
1346
- }
1347
- const progress = response.data?.progress || null;
1348
- if (progress) {
1349
- setState((prev) => ({
1350
- ...prev,
1351
- progress,
1352
- percentage: calculateProgressPercentage(progress),
1353
- status: isProvisioningComplete(progress) ? isProvisioningSuccessful(progress) ? "completed" : progress.status === "partial" ? "partial" : "failed" : "provisioning"
1354
- }));
1355
- }
1356
- return progress;
1357
- } catch (err) {
1358
- const message = err instanceof Error ? err.message : "Failed to get provisioning status";
1359
- setState((prev) => ({ ...prev, error: message }));
1360
- return null;
1361
- }
1362
- },
1363
- [options]
1364
- );
1365
- const retryProvisioning = (0, import_react6.useCallback)(
1366
- async (orgId, artifacts) => {
1367
- setState((prev) => ({
1368
- ...prev,
1369
- status: "provisioning",
1370
- error: null
1371
- }));
1372
- try {
1373
- const request = artifacts ? { artifacts } : void 0;
1374
- const response = await retryProvisioningApi(orgId, request, options);
1375
- if (response.error) {
1376
- setState((prev) => ({
1377
- ...prev,
1378
- status: "failed",
1379
- error: response.error || "Failed to retry provisioning"
1380
- }));
1381
- return false;
1382
- }
1383
- const cleanup = streamProvisioningProgress(
1384
- orgId,
1385
- {
1386
- onProgress: (progress) => {
1387
- const percentage = calculateProgressPercentage(progress);
1388
- const status = isProvisioningComplete(progress) ? isProvisioningSuccessful(progress) ? "completed" : progress.status === "partial" ? "partial" : "failed" : "provisioning";
1389
- setState((prev) => ({
1390
- ...prev,
1391
- status,
1392
- progress,
1393
- percentage,
1394
- error: progress.error || null
1395
- }));
1396
- },
1397
- onDone: () => {
1398
- cleanupRef.current = null;
1399
- },
1400
- onError: (error) => {
1401
- setState((prev) => ({
1402
- ...prev,
1403
- status: "failed",
1404
- error: error.message
1405
- }));
1406
- cleanupRef.current = null;
1407
- }
1408
- },
1409
- { baseUrl: options.baseUrl }
1410
- );
1411
- cleanupRef.current = cleanup;
1412
- return true;
1413
- } catch (err) {
1414
- const message = err instanceof Error ? err.message : "Failed to retry provisioning";
1415
- setState((prev) => ({
1416
- ...prev,
1417
- status: "failed",
1418
- error: message
1419
- }));
1420
- return false;
1421
- }
1422
- },
1423
- [options]
1424
- );
1425
- const validateProvisioning = (0, import_react6.useCallback)(
1426
- async (orgId) => {
1427
- try {
1428
- const response = await validateProvisioningApi(orgId, options);
1429
- if (response.error) {
1430
- setState((prev) => ({ ...prev, error: response.error || null }));
1431
- return null;
1432
- }
1433
- return response.data || null;
1434
- } catch (err) {
1435
- const message = err instanceof Error ? err.message : "Failed to validate provisioning";
1436
- setState((prev) => ({ ...prev, error: message }));
1437
- return null;
1438
- }
1439
- },
1440
- [options]
1441
- );
1442
- const cancelProvisioning = (0, import_react6.useCallback)(
1443
- async (orgId) => {
1444
- try {
1445
- if (cleanupRef.current) {
1446
- cleanupRef.current();
1447
- cleanupRef.current = null;
1448
- }
1449
- const response = await cancelProvisioningApi(orgId, options);
1450
- if (response.error) {
1451
- setState((prev) => ({ ...prev, error: response.error || null }));
1452
- return false;
1453
- }
1454
- setState((prev) => ({
1455
- ...prev,
1456
- status: "failed",
1457
- error: "Provisioning cancelled"
1458
- }));
1459
- return true;
1460
- } catch (err) {
1461
- const message = err instanceof Error ? err.message : "Failed to cancel provisioning";
1462
- setState((prev) => ({ ...prev, error: message }));
1463
- return false;
1464
- }
1465
- },
1466
- [options]
1467
- );
1468
- const cleanupOrg = (0, import_react6.useCallback)(
1469
- async (orgId) => {
1470
- try {
1471
- const response = await cleanupProvisioningApi(orgId, options);
1472
- if (response.error) {
1473
- setState((prev) => ({ ...prev, error: response.error || null }));
1474
- return false;
1475
- }
1476
- return true;
1477
- } catch (err) {
1478
- const message = err instanceof Error ? err.message : "Failed to cleanup organization";
1479
- setState((prev) => ({ ...prev, error: message }));
1480
- return false;
1481
- }
1482
- },
1483
- [options]
1484
- );
1485
- const reset = (0, import_react6.useCallback)(() => {
1486
- if (cleanupRef.current) {
1487
- cleanupRef.current();
1488
- cleanupRef.current = null;
1489
- }
1490
- setState({
1491
- status: "idle",
1492
- progress: null,
1493
- org: null,
1494
- error: null,
1495
- percentage: 0
1496
- });
1497
- }, []);
1498
- return {
1499
- state,
1500
- createOrgWithProvisioning,
1501
- getProvisioningStatus,
1502
- retryProvisioning,
1503
- validateProvisioning,
1504
- cancelProvisioning,
1505
- cleanupOrg,
1506
- reset,
1507
- // Helper functions
1508
- isComplete: state.status === "completed",
1509
- isFailed: state.status === "failed",
1510
- isPartial: state.status === "partial",
1511
- isProvisioning: state.status === "provisioning" || state.status === "creating",
1512
- failedArtifacts: state.progress ? getFailedArtifacts(state.progress) : [],
1513
- hasCriticalFailures: state.progress ? hasCriticalFailures(state.progress) : false
1514
- };
1515
- }
1516
811
  // Annotate the CommonJS export names for ESM import in node:
1517
812
  0 && (module.exports = {
1518
- useAnalyticsContext,
1519
- useInvitesAdmin,
1520
813
  useOrgAdmin,
1521
- useOrgProvisioning,
1522
- useOrgSettings,
1523
- useProductAnalytics,
1524
814
  useUsersAdmin
1525
815
  });
1526
816
  //# sourceMappingURL=index.cjs.map