@htlkg/data 0.0.15 → 0.0.17

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/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/client/index.ts
2
2
  import { generateClient as generateDataClient } from "aws-amplify/data";
3
+ import { Amplify } from "aws-amplify";
3
4
 
4
5
  // src/client/server.ts
5
6
  import {
@@ -9,6 +10,54 @@ import { getAmplifyServerContext } from "aws-amplify/adapter-core/internals";
9
10
  import { createRunWithAmplifyServerContext, createLogger } from "@htlkg/core/amplify-astro-adapter";
10
11
  var log = createLogger("server-client");
11
12
 
13
+ // src/client/proxy.ts
14
+ var DEFAULT_ENDPOINT = "/api/graphql";
15
+ async function mutate(model, operation, data, options) {
16
+ return proxyRequest(model, operation, data, options);
17
+ }
18
+ async function query(model, operation, data, options) {
19
+ return proxyRequest(model, operation, data, options);
20
+ }
21
+ async function proxyRequest(model, operation, data, options) {
22
+ const endpoint = options?.endpoint ?? DEFAULT_ENDPOINT;
23
+ try {
24
+ const response = await fetch(endpoint, {
25
+ method: "POST",
26
+ headers: {
27
+ "Content-Type": "application/json"
28
+ },
29
+ credentials: "include",
30
+ // Important: include cookies
31
+ ...options?.fetchOptions,
32
+ body: JSON.stringify({ model, operation, data })
33
+ });
34
+ const result = await response.json();
35
+ if (response.status === 401) {
36
+ console.error("[GraphQL Proxy] Unauthorized - session may have expired");
37
+ }
38
+ return result;
39
+ } catch (error) {
40
+ console.error("[GraphQL Proxy] Request failed:", error);
41
+ return {
42
+ data: null,
43
+ errors: [
44
+ {
45
+ message: error instanceof Error ? error.message : "Network error"
46
+ }
47
+ ]
48
+ };
49
+ }
50
+ }
51
+ function hasErrors(response) {
52
+ return !!response.errors && response.errors.length > 0;
53
+ }
54
+ function getErrorMessage(response) {
55
+ if (!response.errors || response.errors.length === 0) {
56
+ return null;
57
+ }
58
+ return response.errors[0].message;
59
+ }
60
+
12
61
  // src/client/index.ts
13
62
  var sharedClientInstance = null;
14
63
  function getSharedClient() {
@@ -23,7 +72,7 @@ function resetSharedClient() {
23
72
  function generateClient() {
24
73
  return generateDataClient();
25
74
  }
26
- function generateServerClient(options) {
75
+ function generateServerClient(_options) {
27
76
  const client = generateDataClient();
28
77
  return client;
29
78
  }
@@ -385,6 +434,59 @@ async function executePublicQuery(astro, runWithAmplifyServerContext, operation)
385
434
  return result;
386
435
  }
387
436
 
437
+ // src/queries/systemSettings.ts
438
+ var DEFAULT_SOFT_DELETE_RETENTION_DAYS = 30;
439
+ var SYSTEM_SETTINGS_KEY = "GLOBAL";
440
+ async function getSystemSettings(client) {
441
+ try {
442
+ const { data, errors } = await client.models.SystemSettings.get({
443
+ key: SYSTEM_SETTINGS_KEY
444
+ });
445
+ if (errors) {
446
+ console.error("[getSystemSettings] GraphQL errors:", errors);
447
+ return null;
448
+ }
449
+ return data;
450
+ } catch (error) {
451
+ console.error("[getSystemSettings] Error fetching system settings:", error);
452
+ throw error;
453
+ }
454
+ }
455
+ async function getSoftDeleteRetentionDays(client) {
456
+ const settings = await getSystemSettings(client);
457
+ return settings?.softDeleteRetentionDays ?? DEFAULT_SOFT_DELETE_RETENTION_DAYS;
458
+ }
459
+ function checkRestoreEligibility(deletedAt, retentionDays) {
460
+ if (!deletedAt) {
461
+ return {
462
+ canRestore: false,
463
+ daysRemaining: 0,
464
+ daysExpired: 0,
465
+ expiresAt: null
466
+ };
467
+ }
468
+ const deletedDate = new Date(deletedAt);
469
+ const expiresAt = new Date(deletedDate);
470
+ expiresAt.setDate(expiresAt.getDate() + retentionDays);
471
+ const now = /* @__PURE__ */ new Date();
472
+ const msRemaining = expiresAt.getTime() - now.getTime();
473
+ const daysRemaining = Math.ceil(msRemaining / (1e3 * 60 * 60 * 24));
474
+ if (daysRemaining <= 0) {
475
+ return {
476
+ canRestore: false,
477
+ daysRemaining: 0,
478
+ daysExpired: Math.abs(daysRemaining),
479
+ expiresAt
480
+ };
481
+ }
482
+ return {
483
+ canRestore: true,
484
+ daysRemaining,
485
+ daysExpired: 0,
486
+ expiresAt
487
+ };
488
+ }
489
+
388
490
  // src/mutations/brands.ts
389
491
  async function createBrand(client, input) {
390
492
  try {
@@ -412,6 +514,53 @@ async function updateBrand(client, input) {
412
514
  throw error;
413
515
  }
414
516
  }
517
+ async function softDeleteBrand(client, id, deletedBy) {
518
+ try {
519
+ const { errors } = await client.models.Brand.update({
520
+ id,
521
+ status: "deleted",
522
+ deletedAt: (/* @__PURE__ */ new Date()).toISOString(),
523
+ deletedBy
524
+ });
525
+ if (errors) {
526
+ console.error("[softDeleteBrand] GraphQL errors:", errors);
527
+ return false;
528
+ }
529
+ return true;
530
+ } catch (error) {
531
+ console.error("[softDeleteBrand] Error soft-deleting brand:", error);
532
+ throw error;
533
+ }
534
+ }
535
+ async function restoreBrand(client, id, retentionDays = DEFAULT_SOFT_DELETE_RETENTION_DAYS) {
536
+ try {
537
+ const { data: brand, errors: getErrors } = await client.models.Brand.get({ id });
538
+ if (getErrors || !brand) {
539
+ console.error("[restoreBrand] Error fetching brand:", getErrors);
540
+ return { success: false, error: "Brand not found" };
541
+ }
542
+ const eligibility = checkRestoreEligibility(brand.deletedAt, retentionDays);
543
+ if (!eligibility.canRestore) {
544
+ const errorMsg = `Cannot restore brand. Retention period of ${retentionDays} days has expired. Item was deleted ${eligibility.daysExpired} days ago.`;
545
+ console.error("[restoreBrand]", errorMsg);
546
+ return { success: false, error: errorMsg };
547
+ }
548
+ const { errors } = await client.models.Brand.update({
549
+ id,
550
+ status: "active",
551
+ deletedAt: null,
552
+ deletedBy: null
553
+ });
554
+ if (errors) {
555
+ console.error("[restoreBrand] GraphQL errors:", errors);
556
+ return { success: false, error: "Failed to restore brand" };
557
+ }
558
+ return { success: true };
559
+ } catch (error) {
560
+ console.error("[restoreBrand] Error restoring brand:", error);
561
+ throw error;
562
+ }
563
+ }
415
564
  async function deleteBrand(client, id) {
416
565
  try {
417
566
  const { errors } = await client.models.Brand.delete({ id });
@@ -453,6 +602,53 @@ async function updateAccount(client, input) {
453
602
  throw error;
454
603
  }
455
604
  }
605
+ async function softDeleteAccount(client, id, deletedBy) {
606
+ try {
607
+ const { errors } = await client.models.Account.update({
608
+ id,
609
+ status: "deleted",
610
+ deletedAt: (/* @__PURE__ */ new Date()).toISOString(),
611
+ deletedBy
612
+ });
613
+ if (errors) {
614
+ console.error("[softDeleteAccount] GraphQL errors:", errors);
615
+ return false;
616
+ }
617
+ return true;
618
+ } catch (error) {
619
+ console.error("[softDeleteAccount] Error soft-deleting account:", error);
620
+ throw error;
621
+ }
622
+ }
623
+ async function restoreAccount(client, id, retentionDays = DEFAULT_SOFT_DELETE_RETENTION_DAYS) {
624
+ try {
625
+ const { data: account, errors: getErrors } = await client.models.Account.get({ id });
626
+ if (getErrors || !account) {
627
+ console.error("[restoreAccount] Error fetching account:", getErrors);
628
+ return { success: false, error: "Account not found" };
629
+ }
630
+ const eligibility = checkRestoreEligibility(account.deletedAt, retentionDays);
631
+ if (!eligibility.canRestore) {
632
+ const errorMsg = `Cannot restore account. Retention period of ${retentionDays} days has expired. Item was deleted ${eligibility.daysExpired} days ago.`;
633
+ console.error("[restoreAccount]", errorMsg);
634
+ return { success: false, error: errorMsg };
635
+ }
636
+ const { errors } = await client.models.Account.update({
637
+ id,
638
+ status: "active",
639
+ deletedAt: null,
640
+ deletedBy: null
641
+ });
642
+ if (errors) {
643
+ console.error("[restoreAccount] GraphQL errors:", errors);
644
+ return { success: false, error: "Failed to restore account" };
645
+ }
646
+ return { success: true };
647
+ } catch (error) {
648
+ console.error("[restoreAccount] Error restoring account:", error);
649
+ throw error;
650
+ }
651
+ }
456
652
  async function deleteAccount(client, id) {
457
653
  try {
458
654
  const { errors } = await client.models.Account.delete({ id });
@@ -494,6 +690,53 @@ async function updateUser(client, input) {
494
690
  throw error;
495
691
  }
496
692
  }
693
+ async function softDeleteUser(client, id, deletedBy) {
694
+ try {
695
+ const { errors } = await client.models.User.update({
696
+ id,
697
+ status: "deleted",
698
+ deletedAt: (/* @__PURE__ */ new Date()).toISOString(),
699
+ deletedBy
700
+ });
701
+ if (errors) {
702
+ console.error("[softDeleteUser] GraphQL errors:", errors);
703
+ return false;
704
+ }
705
+ return true;
706
+ } catch (error) {
707
+ console.error("[softDeleteUser] Error soft-deleting user:", error);
708
+ throw error;
709
+ }
710
+ }
711
+ async function restoreUser(client, id, retentionDays = DEFAULT_SOFT_DELETE_RETENTION_DAYS) {
712
+ try {
713
+ const { data: user, errors: getErrors } = await client.models.User.get({ id });
714
+ if (getErrors || !user) {
715
+ console.error("[restoreUser] Error fetching user:", getErrors);
716
+ return { success: false, error: "User not found" };
717
+ }
718
+ const eligibility = checkRestoreEligibility(user.deletedAt, retentionDays);
719
+ if (!eligibility.canRestore) {
720
+ const errorMsg = `Cannot restore user. Retention period of ${retentionDays} days has expired. Item was deleted ${eligibility.daysExpired} days ago.`;
721
+ console.error("[restoreUser]", errorMsg);
722
+ return { success: false, error: errorMsg };
723
+ }
724
+ const { errors } = await client.models.User.update({
725
+ id,
726
+ status: "active",
727
+ deletedAt: null,
728
+ deletedBy: null
729
+ });
730
+ if (errors) {
731
+ console.error("[restoreUser] GraphQL errors:", errors);
732
+ return { success: false, error: "Failed to restore user" };
733
+ }
734
+ return { success: true };
735
+ } catch (error) {
736
+ console.error("[restoreUser] Error restoring user:", error);
737
+ throw error;
738
+ }
739
+ }
497
740
  async function deleteUser(client, id) {
498
741
  try {
499
742
  const { errors } = await client.models.User.delete({ id });
@@ -525,6 +768,7 @@ async function getUserIdentifier(fallback = "system") {
525
768
  }
526
769
  async function createProductInstance(client, input) {
527
770
  try {
771
+ const userIdentifier = input.createdBy || input.updatedBy || await getUserIdentifier();
528
772
  const createInput = {
529
773
  productId: input.productId,
530
774
  productName: input.productName,
@@ -532,8 +776,9 @@ async function createProductInstance(client, input) {
532
776
  accountId: input.accountId,
533
777
  enabled: input.enabled,
534
778
  version: input.version,
779
+ createdBy: userIdentifier,
535
780
  lastUpdated: input.lastUpdated || getCurrentTimestamp(),
536
- updatedBy: input.updatedBy || await getUserIdentifier()
781
+ updatedBy: userIdentifier
537
782
  };
538
783
  if (input.config) {
539
784
  createInput.config = JSON.stringify(JSON.parse(JSON.stringify(input.config)));
@@ -656,6 +901,68 @@ async function toggleProductInstanceEnabled(client, id, enabled) {
656
901
  }
657
902
  }
658
903
 
904
+ // src/mutations/systemSettings.ts
905
+ async function updateSystemSettings(client, input) {
906
+ try {
907
+ if (input.softDeleteRetentionDays < 1 || input.softDeleteRetentionDays > 365) {
908
+ console.error(
909
+ "[updateSystemSettings] Invalid retention days. Must be between 1 and 365."
910
+ );
911
+ return null;
912
+ }
913
+ const { data: existing } = await client.models.SystemSettings.get({
914
+ key: SYSTEM_SETTINGS_KEY
915
+ });
916
+ const settingsData = {
917
+ key: SYSTEM_SETTINGS_KEY,
918
+ softDeleteRetentionDays: input.softDeleteRetentionDays,
919
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
920
+ updatedBy: input.updatedBy
921
+ };
922
+ let result;
923
+ if (existing) {
924
+ result = await client.models.SystemSettings.update(settingsData);
925
+ } else {
926
+ result = await client.models.SystemSettings.create(settingsData);
927
+ }
928
+ if (result.errors) {
929
+ console.error("[updateSystemSettings] GraphQL errors:", result.errors);
930
+ return null;
931
+ }
932
+ return result.data;
933
+ } catch (error) {
934
+ console.error("[updateSystemSettings] Error updating system settings:", error);
935
+ throw error;
936
+ }
937
+ }
938
+ async function initializeSystemSettings(client, initializedBy) {
939
+ try {
940
+ const { data: existing } = await client.models.SystemSettings.get({
941
+ key: SYSTEM_SETTINGS_KEY
942
+ });
943
+ if (existing) {
944
+ return existing;
945
+ }
946
+ const { data, errors } = await client.models.SystemSettings.create({
947
+ key: SYSTEM_SETTINGS_KEY,
948
+ softDeleteRetentionDays: DEFAULT_SOFT_DELETE_RETENTION_DAYS,
949
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
950
+ updatedBy: initializedBy
951
+ });
952
+ if (errors) {
953
+ console.error("[initializeSystemSettings] GraphQL errors:", errors);
954
+ return null;
955
+ }
956
+ return data;
957
+ } catch (error) {
958
+ console.error(
959
+ "[initializeSystemSettings] Error initializing system settings:",
960
+ error
961
+ );
962
+ throw error;
963
+ }
964
+ }
965
+
659
966
  // src/hooks/createDataHook.ts
660
967
  import { ref, computed, onMounted } from "vue";
661
968
  function createDataHook(config) {
@@ -679,7 +986,7 @@ function createDataHook(config) {
679
986
  }
680
987
  return baseFilter && Object.keys(baseFilter).length > 0 ? baseFilter : void 0;
681
988
  };
682
- async function fetch() {
989
+ async function fetch2() {
683
990
  loading.value = true;
684
991
  error.value = null;
685
992
  try {
@@ -712,14 +1019,14 @@ function createDataHook(config) {
712
1019
  }
713
1020
  if (autoFetch) {
714
1021
  onMounted(() => {
715
- fetch();
1022
+ fetch2();
716
1023
  });
717
1024
  }
718
1025
  const result = {
719
1026
  data,
720
1027
  loading,
721
1028
  error,
722
- refetch: fetch,
1029
+ refetch: fetch2,
723
1030
  computed: computedRefs
724
1031
  };
725
1032
  if (dataPropertyName !== "data") {
@@ -1001,6 +1308,9 @@ function createSingleStore(shared, name, defaultValue) {
1001
1308
  return shared(name, atom(defaultValue));
1002
1309
  }
1003
1310
  export {
1311
+ DEFAULT_SOFT_DELETE_RETENTION_DAYS,
1312
+ SYSTEM_SETTINGS_KEY,
1313
+ checkRestoreEligibility,
1004
1314
  createAccount,
1005
1315
  createBrand,
1006
1316
  createDataHook,
@@ -1021,12 +1331,17 @@ export {
1021
1331
  getAccountWithBrands,
1022
1332
  getBrand,
1023
1333
  getBrandWithProducts,
1334
+ getErrorMessage,
1024
1335
  getProduct,
1025
1336
  getProductInstance,
1026
1337
  getSharedClient,
1338
+ getSoftDeleteRetentionDays,
1339
+ getSystemSettings,
1027
1340
  getUser,
1028
1341
  getUserByCognitoId,
1029
1342
  getUserByEmail,
1343
+ hasErrors,
1344
+ initializeSystemSettings,
1030
1345
  listAccounts,
1031
1346
  listActiveBrands,
1032
1347
  listActiveProducts,
@@ -1039,12 +1354,21 @@ export {
1039
1354
  listProducts,
1040
1355
  listUsers,
1041
1356
  listUsersByAccount,
1357
+ mutate,
1358
+ query,
1042
1359
  resetSharedClient as resetClientInstance,
1043
1360
  resetSharedClient,
1361
+ restoreAccount,
1362
+ restoreBrand,
1363
+ restoreUser,
1364
+ softDeleteAccount,
1365
+ softDeleteBrand,
1366
+ softDeleteUser,
1044
1367
  toggleProductInstanceEnabled,
1045
1368
  updateAccount,
1046
1369
  updateBrand,
1047
1370
  updateProductInstance,
1371
+ updateSystemSettings,
1048
1372
  updateUser,
1049
1373
  useAccounts,
1050
1374
  useBrands,