@htlkg/data 0.0.14 → 0.0.15
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/hooks/index.d.ts +75 -2
- package/dist/hooks/index.js +220 -3
- package/dist/hooks/index.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +224 -3
- package/dist/index.js.map +1 -1
- package/dist/mutations/index.d.ts +1 -0
- package/dist/mutations/index.js +152 -0
- package/dist/mutations/index.js.map +1 -1
- package/dist/productInstances-CzT3NZKU.d.ts +98 -0
- package/package.json +2 -2
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useProductInstances.ts +174 -0
- package/src/mutations/index.ts +10 -0
- package/src/mutations/productInstances/index.ts +14 -0
- package/src/mutations/productInstances/productInstances.integration.test.ts +621 -0
- package/src/mutations/productInstances/productInstances.test.ts +680 -0
- package/src/mutations/productInstances/productInstances.ts +280 -0
package/dist/index.js
CHANGED
|
@@ -508,6 +508,154 @@ async function deleteUser(client, id) {
|
|
|
508
508
|
}
|
|
509
509
|
}
|
|
510
510
|
|
|
511
|
+
// src/mutations/productInstances/productInstances.ts
|
|
512
|
+
import { getClientUser } from "@htlkg/core/auth";
|
|
513
|
+
import { getCurrentTimestamp } from "@htlkg/core/utils";
|
|
514
|
+
import { AppError } from "@htlkg/core/errors";
|
|
515
|
+
async function getUserIdentifier(fallback = "system") {
|
|
516
|
+
try {
|
|
517
|
+
const user = await getClientUser();
|
|
518
|
+
if (user) {
|
|
519
|
+
return user.email || user.username || fallback;
|
|
520
|
+
}
|
|
521
|
+
return fallback;
|
|
522
|
+
} catch {
|
|
523
|
+
return fallback;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
async function createProductInstance(client, input) {
|
|
527
|
+
try {
|
|
528
|
+
const createInput = {
|
|
529
|
+
productId: input.productId,
|
|
530
|
+
productName: input.productName,
|
|
531
|
+
brandId: input.brandId,
|
|
532
|
+
accountId: input.accountId,
|
|
533
|
+
enabled: input.enabled,
|
|
534
|
+
version: input.version,
|
|
535
|
+
lastUpdated: input.lastUpdated || getCurrentTimestamp(),
|
|
536
|
+
updatedBy: input.updatedBy || await getUserIdentifier()
|
|
537
|
+
};
|
|
538
|
+
if (input.config) {
|
|
539
|
+
createInput.config = JSON.stringify(JSON.parse(JSON.stringify(input.config)));
|
|
540
|
+
}
|
|
541
|
+
console.log("[createProductInstance] Config as string:", createInput.config);
|
|
542
|
+
console.log("[createProductInstance] Config type:", typeof createInput.config);
|
|
543
|
+
const { data, errors } = await client.models.ProductInstance.create(createInput);
|
|
544
|
+
if (errors) {
|
|
545
|
+
console.error("[createProductInstance] GraphQL errors:", errors);
|
|
546
|
+
throw new AppError(
|
|
547
|
+
"Failed to create product instance",
|
|
548
|
+
"PRODUCT_INSTANCE_CREATE_ERROR",
|
|
549
|
+
500,
|
|
550
|
+
{ errors }
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
return data;
|
|
554
|
+
} catch (error) {
|
|
555
|
+
console.error("[createProductInstance] Error creating product instance:", error);
|
|
556
|
+
if (error instanceof AppError) {
|
|
557
|
+
throw error;
|
|
558
|
+
}
|
|
559
|
+
throw new AppError(
|
|
560
|
+
"Failed to create product instance",
|
|
561
|
+
"PRODUCT_INSTANCE_CREATE_ERROR",
|
|
562
|
+
500,
|
|
563
|
+
{ originalError: error }
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
async function updateProductInstance(client, input) {
|
|
568
|
+
try {
|
|
569
|
+
const updateInput = {
|
|
570
|
+
...input,
|
|
571
|
+
lastUpdated: input.lastUpdated || getCurrentTimestamp(),
|
|
572
|
+
updatedBy: input.updatedBy || await getUserIdentifier()
|
|
573
|
+
};
|
|
574
|
+
if (input.config) {
|
|
575
|
+
updateInput.config = JSON.stringify(JSON.parse(JSON.stringify(input.config)));
|
|
576
|
+
}
|
|
577
|
+
const { data, errors } = await client.models.ProductInstance.update(updateInput);
|
|
578
|
+
if (errors) {
|
|
579
|
+
console.error("[updateProductInstance] GraphQL errors:", errors);
|
|
580
|
+
throw new AppError(
|
|
581
|
+
"Failed to update product instance",
|
|
582
|
+
"PRODUCT_INSTANCE_UPDATE_ERROR",
|
|
583
|
+
500,
|
|
584
|
+
{ errors }
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
return data;
|
|
588
|
+
} catch (error) {
|
|
589
|
+
console.error("[updateProductInstance] Error updating product instance:", error);
|
|
590
|
+
if (error instanceof AppError) {
|
|
591
|
+
throw error;
|
|
592
|
+
}
|
|
593
|
+
throw new AppError(
|
|
594
|
+
"Failed to update product instance",
|
|
595
|
+
"PRODUCT_INSTANCE_UPDATE_ERROR",
|
|
596
|
+
500,
|
|
597
|
+
{ originalError: error }
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
async function deleteProductInstance(client, id) {
|
|
602
|
+
try {
|
|
603
|
+
const { errors } = await client.models.ProductInstance.delete({ id });
|
|
604
|
+
if (errors) {
|
|
605
|
+
console.error("[deleteProductInstance] GraphQL errors:", errors);
|
|
606
|
+
throw new AppError(
|
|
607
|
+
"Failed to delete product instance",
|
|
608
|
+
"PRODUCT_INSTANCE_DELETE_ERROR",
|
|
609
|
+
500,
|
|
610
|
+
{ errors }
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
return true;
|
|
614
|
+
} catch (error) {
|
|
615
|
+
console.error("[deleteProductInstance] Error deleting product instance:", error);
|
|
616
|
+
if (error instanceof AppError) {
|
|
617
|
+
throw error;
|
|
618
|
+
}
|
|
619
|
+
throw new AppError(
|
|
620
|
+
"Failed to delete product instance",
|
|
621
|
+
"PRODUCT_INSTANCE_DELETE_ERROR",
|
|
622
|
+
500,
|
|
623
|
+
{ originalError: error }
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
async function toggleProductInstanceEnabled(client, id, enabled) {
|
|
628
|
+
try {
|
|
629
|
+
const { data, errors } = await client.models.ProductInstance.update({
|
|
630
|
+
id,
|
|
631
|
+
enabled,
|
|
632
|
+
lastUpdated: getCurrentTimestamp(),
|
|
633
|
+
updatedBy: await getUserIdentifier()
|
|
634
|
+
});
|
|
635
|
+
if (errors) {
|
|
636
|
+
console.error("[toggleProductInstanceEnabled] GraphQL errors:", errors);
|
|
637
|
+
throw new AppError(
|
|
638
|
+
"Failed to toggle product instance",
|
|
639
|
+
"PRODUCT_INSTANCE_TOGGLE_ERROR",
|
|
640
|
+
500,
|
|
641
|
+
{ errors }
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
return data;
|
|
645
|
+
} catch (error) {
|
|
646
|
+
console.error("[toggleProductInstanceEnabled] Error toggling product instance:", error);
|
|
647
|
+
if (error instanceof AppError) {
|
|
648
|
+
throw error;
|
|
649
|
+
}
|
|
650
|
+
throw new AppError(
|
|
651
|
+
"Failed to toggle product instance",
|
|
652
|
+
"PRODUCT_INSTANCE_TOGGLE_ERROR",
|
|
653
|
+
500,
|
|
654
|
+
{ originalError: error }
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
511
659
|
// src/hooks/createDataHook.ts
|
|
512
660
|
import { ref, computed, onMounted } from "vue";
|
|
513
661
|
function createDataHook(config) {
|
|
@@ -516,7 +664,7 @@ function createDataHook(config) {
|
|
|
516
664
|
defaultLimit,
|
|
517
665
|
selectionSet,
|
|
518
666
|
transform,
|
|
519
|
-
buildFilter:
|
|
667
|
+
buildFilter: buildFilter5,
|
|
520
668
|
computedProperties,
|
|
521
669
|
dataPropertyName = "data"
|
|
522
670
|
} = config;
|
|
@@ -526,8 +674,8 @@ function createDataHook(config) {
|
|
|
526
674
|
const loading = ref(false);
|
|
527
675
|
const error = ref(null);
|
|
528
676
|
const getFilter = () => {
|
|
529
|
-
if (
|
|
530
|
-
return
|
|
677
|
+
if (buildFilter5) {
|
|
678
|
+
return buildFilter5(options);
|
|
531
679
|
}
|
|
532
680
|
return baseFilter && Object.keys(baseFilter).length > 0 ? baseFilter : void 0;
|
|
533
681
|
};
|
|
@@ -682,6 +830,74 @@ function useProducts(options = {}) {
|
|
|
682
830
|
};
|
|
683
831
|
}
|
|
684
832
|
|
|
833
|
+
// src/hooks/useProductInstances.ts
|
|
834
|
+
function buildFilter4(options) {
|
|
835
|
+
let filter = options.filter || {};
|
|
836
|
+
if (options.brandId) {
|
|
837
|
+
filter = { ...filter, brandId: { eq: options.brandId } };
|
|
838
|
+
}
|
|
839
|
+
if (options.accountId) {
|
|
840
|
+
filter = { ...filter, accountId: { eq: options.accountId } };
|
|
841
|
+
}
|
|
842
|
+
if (options.productId) {
|
|
843
|
+
filter = { ...filter, productId: { eq: options.productId } };
|
|
844
|
+
}
|
|
845
|
+
if (options.enabledOnly) {
|
|
846
|
+
filter = { ...filter, enabled: { eq: true } };
|
|
847
|
+
}
|
|
848
|
+
return Object.keys(filter).length > 0 ? filter : void 0;
|
|
849
|
+
}
|
|
850
|
+
var useProductInstancesInternal = createDataHook({
|
|
851
|
+
model: "ProductInstance",
|
|
852
|
+
dataPropertyName: "instances",
|
|
853
|
+
buildFilter: buildFilter4,
|
|
854
|
+
computedProperties: {
|
|
855
|
+
enabledInstances: (instances) => instances.filter((i) => i.enabled)
|
|
856
|
+
}
|
|
857
|
+
});
|
|
858
|
+
function useProductInstances(options = {}) {
|
|
859
|
+
const result = useProductInstancesInternal(options);
|
|
860
|
+
async function createInstance(input) {
|
|
861
|
+
const client = getSharedClient();
|
|
862
|
+
const instance = await createProductInstance(client, input);
|
|
863
|
+
if (!instance) {
|
|
864
|
+
throw new Error("Failed to create product instance");
|
|
865
|
+
}
|
|
866
|
+
return instance;
|
|
867
|
+
}
|
|
868
|
+
async function updateInstance(input) {
|
|
869
|
+
const client = getSharedClient();
|
|
870
|
+
const instance = await updateProductInstance(client, input);
|
|
871
|
+
if (!instance) {
|
|
872
|
+
throw new Error("Failed to update product instance");
|
|
873
|
+
}
|
|
874
|
+
return instance;
|
|
875
|
+
}
|
|
876
|
+
async function deleteInstance(id) {
|
|
877
|
+
const client = getSharedClient();
|
|
878
|
+
return deleteProductInstance(client, id);
|
|
879
|
+
}
|
|
880
|
+
async function toggleEnabled(id, enabled) {
|
|
881
|
+
const client = getSharedClient();
|
|
882
|
+
const instance = await toggleProductInstanceEnabled(client, id, enabled);
|
|
883
|
+
if (!instance) {
|
|
884
|
+
throw new Error("Failed to toggle product instance");
|
|
885
|
+
}
|
|
886
|
+
return instance;
|
|
887
|
+
}
|
|
888
|
+
return {
|
|
889
|
+
instances: result.instances,
|
|
890
|
+
enabledInstances: result.enabledInstances,
|
|
891
|
+
loading: result.loading,
|
|
892
|
+
error: result.error,
|
|
893
|
+
refetch: result.refetch,
|
|
894
|
+
createInstance,
|
|
895
|
+
updateInstance,
|
|
896
|
+
deleteInstance,
|
|
897
|
+
toggleEnabled
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
|
|
685
901
|
// ../../node_modules/.pnpm/nanostores@1.1.0/node_modules/nanostores/clean-stores/index.js
|
|
686
902
|
var clean = /* @__PURE__ */ Symbol("clean");
|
|
687
903
|
|
|
@@ -788,12 +1004,14 @@ export {
|
|
|
788
1004
|
createAccount,
|
|
789
1005
|
createBrand,
|
|
790
1006
|
createDataHook,
|
|
1007
|
+
createProductInstance,
|
|
791
1008
|
createResourceStores,
|
|
792
1009
|
createSingleStore,
|
|
793
1010
|
createStore,
|
|
794
1011
|
createUser,
|
|
795
1012
|
deleteAccount,
|
|
796
1013
|
deleteBrand,
|
|
1014
|
+
deleteProductInstance,
|
|
797
1015
|
deleteUser,
|
|
798
1016
|
executePublicQuery,
|
|
799
1017
|
executeServerQuery,
|
|
@@ -823,11 +1041,14 @@ export {
|
|
|
823
1041
|
listUsersByAccount,
|
|
824
1042
|
resetSharedClient as resetClientInstance,
|
|
825
1043
|
resetSharedClient,
|
|
1044
|
+
toggleProductInstanceEnabled,
|
|
826
1045
|
updateAccount,
|
|
827
1046
|
updateBrand,
|
|
1047
|
+
updateProductInstance,
|
|
828
1048
|
updateUser,
|
|
829
1049
|
useAccounts,
|
|
830
1050
|
useBrands,
|
|
1051
|
+
useProductInstances,
|
|
831
1052
|
useProducts,
|
|
832
1053
|
useUsers
|
|
833
1054
|
};
|