@htlkg/data 0.0.15 → 0.0.16

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 });
@@ -656,6 +899,68 @@ async function toggleProductInstanceEnabled(client, id, enabled) {
656
899
  }
657
900
  }
658
901
 
902
+ // src/mutations/systemSettings.ts
903
+ async function updateSystemSettings(client, input) {
904
+ try {
905
+ if (input.softDeleteRetentionDays < 1 || input.softDeleteRetentionDays > 365) {
906
+ console.error(
907
+ "[updateSystemSettings] Invalid retention days. Must be between 1 and 365."
908
+ );
909
+ return null;
910
+ }
911
+ const { data: existing } = await client.models.SystemSettings.get({
912
+ key: SYSTEM_SETTINGS_KEY
913
+ });
914
+ const settingsData = {
915
+ key: SYSTEM_SETTINGS_KEY,
916
+ softDeleteRetentionDays: input.softDeleteRetentionDays,
917
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
918
+ updatedBy: input.updatedBy
919
+ };
920
+ let result;
921
+ if (existing) {
922
+ result = await client.models.SystemSettings.update(settingsData);
923
+ } else {
924
+ result = await client.models.SystemSettings.create(settingsData);
925
+ }
926
+ if (result.errors) {
927
+ console.error("[updateSystemSettings] GraphQL errors:", result.errors);
928
+ return null;
929
+ }
930
+ return result.data;
931
+ } catch (error) {
932
+ console.error("[updateSystemSettings] Error updating system settings:", error);
933
+ throw error;
934
+ }
935
+ }
936
+ async function initializeSystemSettings(client, initializedBy) {
937
+ try {
938
+ const { data: existing } = await client.models.SystemSettings.get({
939
+ key: SYSTEM_SETTINGS_KEY
940
+ });
941
+ if (existing) {
942
+ return existing;
943
+ }
944
+ const { data, errors } = await client.models.SystemSettings.create({
945
+ key: SYSTEM_SETTINGS_KEY,
946
+ softDeleteRetentionDays: DEFAULT_SOFT_DELETE_RETENTION_DAYS,
947
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
948
+ updatedBy: initializedBy
949
+ });
950
+ if (errors) {
951
+ console.error("[initializeSystemSettings] GraphQL errors:", errors);
952
+ return null;
953
+ }
954
+ return data;
955
+ } catch (error) {
956
+ console.error(
957
+ "[initializeSystemSettings] Error initializing system settings:",
958
+ error
959
+ );
960
+ throw error;
961
+ }
962
+ }
963
+
659
964
  // src/hooks/createDataHook.ts
660
965
  import { ref, computed, onMounted } from "vue";
661
966
  function createDataHook(config) {
@@ -679,7 +984,7 @@ function createDataHook(config) {
679
984
  }
680
985
  return baseFilter && Object.keys(baseFilter).length > 0 ? baseFilter : void 0;
681
986
  };
682
- async function fetch() {
987
+ async function fetch2() {
683
988
  loading.value = true;
684
989
  error.value = null;
685
990
  try {
@@ -712,14 +1017,14 @@ function createDataHook(config) {
712
1017
  }
713
1018
  if (autoFetch) {
714
1019
  onMounted(() => {
715
- fetch();
1020
+ fetch2();
716
1021
  });
717
1022
  }
718
1023
  const result = {
719
1024
  data,
720
1025
  loading,
721
1026
  error,
722
- refetch: fetch,
1027
+ refetch: fetch2,
723
1028
  computed: computedRefs
724
1029
  };
725
1030
  if (dataPropertyName !== "data") {
@@ -1001,6 +1306,9 @@ function createSingleStore(shared, name, defaultValue) {
1001
1306
  return shared(name, atom(defaultValue));
1002
1307
  }
1003
1308
  export {
1309
+ DEFAULT_SOFT_DELETE_RETENTION_DAYS,
1310
+ SYSTEM_SETTINGS_KEY,
1311
+ checkRestoreEligibility,
1004
1312
  createAccount,
1005
1313
  createBrand,
1006
1314
  createDataHook,
@@ -1021,12 +1329,17 @@ export {
1021
1329
  getAccountWithBrands,
1022
1330
  getBrand,
1023
1331
  getBrandWithProducts,
1332
+ getErrorMessage,
1024
1333
  getProduct,
1025
1334
  getProductInstance,
1026
1335
  getSharedClient,
1336
+ getSoftDeleteRetentionDays,
1337
+ getSystemSettings,
1027
1338
  getUser,
1028
1339
  getUserByCognitoId,
1029
1340
  getUserByEmail,
1341
+ hasErrors,
1342
+ initializeSystemSettings,
1030
1343
  listAccounts,
1031
1344
  listActiveBrands,
1032
1345
  listActiveProducts,
@@ -1039,12 +1352,21 @@ export {
1039
1352
  listProducts,
1040
1353
  listUsers,
1041
1354
  listUsersByAccount,
1355
+ mutate,
1356
+ query,
1042
1357
  resetSharedClient as resetClientInstance,
1043
1358
  resetSharedClient,
1359
+ restoreAccount,
1360
+ restoreBrand,
1361
+ restoreUser,
1362
+ softDeleteAccount,
1363
+ softDeleteBrand,
1364
+ softDeleteUser,
1044
1365
  toggleProductInstanceEnabled,
1045
1366
  updateAccount,
1046
1367
  updateBrand,
1047
1368
  updateProductInstance,
1369
+ updateSystemSettings,
1048
1370
  updateUser,
1049
1371
  useAccounts,
1050
1372
  useBrands,