ingeniuscliq-core 0.5.21 → 0.5.24

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.
Files changed (28) hide show
  1. package/dist/classes/CoreBaseBuilder.d.ts +67 -0
  2. package/dist/classes/CoreBaseBuilder.d.ts.map +1 -0
  3. package/dist/classes/CoreBaseBuilder.js +65 -0
  4. package/dist/classes/index.d.ts +1 -0
  5. package/dist/classes/index.d.ts.map +1 -1
  6. package/dist/index.js +1 -0
  7. package/dist/modules/CoreAuth/classes/CoreAuthBuilder.d.ts +10 -9
  8. package/dist/modules/CoreAuth/classes/CoreAuthBuilder.d.ts.map +1 -1
  9. package/dist/modules/CoreAuth/classes/CoreAuthBuilder.js +153 -147
  10. package/dist/modules/CoreCustomization/classes/CoreCustomizationBuilder.d.ts +10 -9
  11. package/dist/modules/CoreCustomization/classes/CoreCustomizationBuilder.d.ts.map +1 -1
  12. package/dist/modules/CoreCustomization/classes/CoreCustomizationBuilder.js +66 -60
  13. package/dist/modules/CoreOrder/classes/CoreOrderBuilder.d.ts +10 -9
  14. package/dist/modules/CoreOrder/classes/CoreOrderBuilder.d.ts.map +1 -1
  15. package/dist/modules/CoreOrder/classes/CoreOrderBuilder.js +95 -89
  16. package/dist/modules/CorePayForm/classes/CorePayFormBuilder.d.ts +10 -9
  17. package/dist/modules/CorePayForm/classes/CorePayFormBuilder.d.ts.map +1 -1
  18. package/dist/modules/CorePayForm/classes/CorePayFormBuilder.js +50 -44
  19. package/dist/modules/CoreProduct/classes/CoreProductBuilder.d.ts +10 -9
  20. package/dist/modules/CoreProduct/classes/CoreProductBuilder.d.ts.map +1 -1
  21. package/dist/modules/CoreProduct/classes/CoreProductBuilder.js +119 -113
  22. package/dist/modules/CoreShipment/classes/CoreShipmentBuilder.d.ts +10 -9
  23. package/dist/modules/CoreShipment/classes/CoreShipmentBuilder.d.ts.map +1 -1
  24. package/dist/modules/CoreShipment/classes/CoreShipmentBuilder.js +174 -165
  25. package/dist/modules/CoreShopCart/classes/CoreShopCartBuilder.d.ts +10 -19
  26. package/dist/modules/CoreShopCart/classes/CoreShopCartBuilder.d.ts.map +1 -1
  27. package/dist/modules/CoreShopCart/classes/CoreShopCartBuilder.js +159 -150
  28. package/package.json +1 -1
@@ -1,8 +1,9 @@
1
1
  import { create } from 'zustand';
2
2
  import { persist } from '../../../node_modules/zustand/esm/middleware.js';
3
3
  import { CoreProductBaseService } from '../services/base.js';
4
+ import { CoreBaseBuilder } from '../../../classes/CoreBaseBuilder.js';
4
5
 
5
- class CoreProductBuilder {
6
+ class CoreProductBuilder extends CoreBaseBuilder {
6
7
  productService;
7
8
  initialState = {
8
9
  productDetails: null,
@@ -13,132 +14,137 @@ class CoreProductBuilder {
13
14
  error: null
14
15
  };
15
16
  constructor(service = new CoreProductBaseService(), initialState) {
17
+ super();
16
18
  this.productService = service;
17
19
  if (initialState) {
18
20
  this.initialState = { ...this.initialState, ...initialState };
19
21
  }
20
22
  }
23
+ getBaseState(set, get) {
24
+ return {
25
+ ...this.initialState,
26
+ // Setters
27
+ setProductDetails: (productDetails) => set({ productDetails }),
28
+ setCategories: (categories) => set({ categories }),
29
+ setProducts: (products) => set({ products }),
30
+ setLoading: (loading) => set({ loading }),
31
+ setError: (error) => set({ error }),
32
+ // Getters
33
+ getProducts: () => get().products,
34
+ getProductDetails: () => get().productDetails,
35
+ getCategories: () => get().categories,
36
+ // Actions
37
+ fetchProducts: async (params, actions) => {
38
+ const { onSuccess = () => {
39
+ }, onError = () => {
40
+ }, onFinish = () => {
41
+ } } = actions || {};
42
+ const { onlyFetch, ...queryParams } = params || {};
43
+ try {
44
+ if (!onlyFetch) {
45
+ set({ loading: true, error: null });
46
+ }
47
+ const response = await this.productService.getAllProducts(queryParams);
48
+ if (!onlyFetch) {
49
+ set({ products: response.data.data, loading: false });
50
+ }
51
+ onSuccess(response);
52
+ return response.data;
53
+ } catch (error) {
54
+ if (!onlyFetch) {
55
+ set((state) => ({ ...state, error, loading: false }));
56
+ }
57
+ onError(error);
58
+ throw error;
59
+ } finally {
60
+ onFinish();
61
+ }
62
+ },
63
+ fetchProductDetails: async (id) => {
64
+ const currentProduct = get().productDetails;
65
+ if (currentProduct && String(currentProduct.id) === String(id)) {
66
+ return {
67
+ data: currentProduct,
68
+ message: ""
69
+ };
70
+ }
71
+ set({ productDetails: null, loading: true, error: null });
72
+ try {
73
+ const response = await this.productService.getProductById(id);
74
+ set({ productDetails: response.data.data, loading: false });
75
+ return response.data;
76
+ } catch (error) {
77
+ set((state) => ({ ...state, error, loading: false }));
78
+ throw error;
79
+ }
80
+ },
81
+ fetchCategories: async (params, actions) => {
82
+ const { onSuccess = () => {
83
+ }, onError = () => {
84
+ }, onFinish = () => {
85
+ } } = actions || {};
86
+ const { onlyFetch, ...queryParams } = params || {};
87
+ try {
88
+ if (!onlyFetch) {
89
+ set({ loading: true, error: null });
90
+ }
91
+ const response = await this.productService.getAllCategories(queryParams);
92
+ if (!onlyFetch) {
93
+ set({ categories: response.data.data, loading: false });
94
+ }
95
+ onSuccess(response);
96
+ return response.data;
97
+ } catch (error) {
98
+ if (!onlyFetch) {
99
+ set((state) => ({ ...state, error, loading: false }));
100
+ }
101
+ onError(error);
102
+ throw error;
103
+ } finally {
104
+ onFinish();
105
+ }
106
+ },
107
+ fetchBestSellers: async (params, actions) => {
108
+ const { onSuccess = () => {
109
+ }, onError = () => {
110
+ }, onFinish = () => {
111
+ } } = actions || {};
112
+ const { onlyFetch, ...queryParams } = params || {};
113
+ try {
114
+ if (!onlyFetch) {
115
+ set({ loading: true, error: null });
116
+ }
117
+ const response = await this.productService.fetchBestSellers(queryParams);
118
+ if (!onlyFetch) {
119
+ set({ loading: false, bestSellers: response.data.data });
120
+ }
121
+ onSuccess(response);
122
+ return response.data;
123
+ } catch (error) {
124
+ if (!onlyFetch) {
125
+ set((state) => ({ ...state, error, loading: false, bestSellers: null }));
126
+ }
127
+ onError(error);
128
+ throw error;
129
+ } finally {
130
+ onFinish();
131
+ }
132
+ },
133
+ reset: () => set({ ...this.initialState, ...this.extendInitialState() })
134
+ };
135
+ }
21
136
  build() {
22
137
  return create()(
23
138
  persist(
24
- (set, get) => ({
25
- ...this.initialState,
26
- // Setters
27
- setProductDetails: (productDetails) => set({ productDetails }),
28
- setCategories: (categories) => set({ categories }),
29
- setProducts: (products) => set({ products }),
30
- setLoading: (loading) => set({ loading }),
31
- setError: (error) => set({ error }),
32
- // Getters
33
- getProducts: () => get().products,
34
- getProductDetails: () => get().productDetails,
35
- getCategories: () => get().categories,
36
- // Actions
37
- fetchProducts: async (params, actions) => {
38
- const { onSuccess = () => {
39
- }, onError = () => {
40
- }, onFinish = () => {
41
- } } = actions || {};
42
- const { onlyFetch, ...queryParams } = params || {};
43
- try {
44
- if (!onlyFetch) {
45
- set({ loading: true, error: null });
46
- }
47
- const response = await this.productService.getAllProducts(queryParams);
48
- if (!onlyFetch) {
49
- set({ products: response.data.data, loading: false });
50
- }
51
- onSuccess(response);
52
- return response.data;
53
- } catch (error) {
54
- if (!onlyFetch) {
55
- set((state) => ({ ...state, error, loading: false }));
56
- }
57
- onError(error);
58
- throw error;
59
- } finally {
60
- onFinish();
61
- }
62
- },
63
- fetchProductDetails: async (id) => {
64
- const currentProduct = get().productDetails;
65
- if (currentProduct && String(currentProduct.id) === String(id)) {
66
- return {
67
- data: currentProduct,
68
- message: ""
69
- };
70
- }
71
- set({ productDetails: null, loading: true, error: null });
72
- try {
73
- const response = await this.productService.getProductById(id);
74
- set({ productDetails: response.data.data, loading: false });
75
- return response.data;
76
- } catch (error) {
77
- set((state) => ({ ...state, error, loading: false }));
78
- throw error;
79
- }
80
- },
81
- fetchCategories: async (params, actions) => {
82
- const { onSuccess = () => {
83
- }, onError = () => {
84
- }, onFinish = () => {
85
- } } = actions || {};
86
- const { onlyFetch, ...queryParams } = params || {};
87
- try {
88
- if (!onlyFetch) {
89
- set({ loading: true, error: null });
90
- }
91
- const response = await this.productService.getAllCategories(queryParams);
92
- if (!onlyFetch) {
93
- set({ categories: response.data.data, loading: false });
94
- }
95
- onSuccess(response);
96
- return response.data;
97
- } catch (error) {
98
- if (!onlyFetch) {
99
- set((state) => ({ ...state, error, loading: false }));
100
- }
101
- onError(error);
102
- throw error;
103
- } finally {
104
- onFinish();
105
- }
106
- },
107
- fetchBestSellers: async (params, actions) => {
108
- const { onSuccess = () => {
109
- }, onError = () => {
110
- }, onFinish = () => {
111
- } } = actions || {};
112
- const { onlyFetch, ...queryParams } = params || {};
113
- try {
114
- if (!onlyFetch) {
115
- set({ loading: true, error: null });
116
- }
117
- const response = await this.productService.fetchBestSellers(queryParams);
118
- if (!onlyFetch) {
119
- set({ loading: false, bestSellers: response.data.data });
120
- }
121
- onSuccess(response);
122
- return response.data;
123
- } catch (error) {
124
- if (!onlyFetch) {
125
- set((state) => ({ ...state, error, loading: false, bestSellers: null }));
126
- }
127
- onError(error);
128
- throw error;
129
- } finally {
130
- onFinish();
131
- }
132
- },
133
- reset: () => set(this.initialState)
134
- }),
139
+ (set, get) => this.buildStoreState(set, get),
135
140
  {
136
141
  name: "product-storage",
137
142
  partialize: (state) => ({
138
143
  productDetails: state.productDetails,
139
144
  products: state.products,
140
145
  categories: state.categories,
141
- bestSellers: state.bestSellers
146
+ bestSellers: state.bestSellers,
147
+ ...this.extendPartialize(state)
142
148
  })
143
149
  }
144
150
  )
@@ -1,13 +1,14 @@
1
- import { CoreBuilder } from '../../../classes/CoreBuilder';
1
+ import { CoreBaseBuilder } from '../../../classes/CoreBaseBuilder';
2
2
  import { CoreShipmentBaseService } from '../services/base';
3
3
  import { CoreShippingAddress, CoreShipmentStore, CoreShippingMethod, CoreShippingMethodTypes, CoreShipmentBeneficiary } from '../types/CoreShipment';
4
- export declare class CoreShipmentBuilder<T extends CoreShippingMethod<Z>, K extends CoreShippingAddress, Z extends CoreShippingMethodTypes, Y extends CoreShipmentBeneficiary> implements CoreBuilder {
4
+ export declare class CoreShipmentBuilder<T extends CoreShippingMethod<Z>, K extends CoreShippingAddress, Z extends CoreShippingMethodTypes, Y extends CoreShipmentBeneficiary, TStore extends CoreShipmentStore<T, K, Z, Y> = CoreShipmentStore<T, K, Z, Y>> extends CoreBaseBuilder<TStore> {
5
5
  protected shipmentService: CoreShipmentBaseService<T, K, Z, Y>;
6
- protected initialState: Pick<CoreShipmentStore<T, K, Z, Y>, 'shippingMethods' | 'shippingAddresses' | 'beneficiaries' | 'selectedAddress' | 'loading' | 'error'>;
7
- constructor(service?: CoreShipmentBaseService<T, K, Z, Y>, initialState?: any);
8
- build(): import('zustand').UseBoundStore<Omit<import('zustand').StoreApi<CoreShipmentStore<T, K, Z, Y>>, "persist"> & {
6
+ protected initialState: Pick<TStore, 'shippingMethods' | 'shippingAddresses' | 'beneficiaries' | 'selectedAddress' | 'loading' | 'error'>;
7
+ constructor(service?: CoreShipmentBaseService<T, K, Z, Y>, initialState?: Partial<TStore>);
8
+ protected getBaseState(set: any, get: any): TStore;
9
+ build(): import('zustand').UseBoundStore<Omit<import('zustand').StoreApi<TStore>, "persist"> & {
9
10
  persist: {
10
- setOptions: (options: Partial<import('zustand/middleware').PersistOptions<CoreShipmentStore<T, K, Z, Y>, {
11
+ setOptions: (options: Partial<import('zustand/middleware').PersistOptions<TStore, {
11
12
  shippingMethods: T[];
12
13
  shippingAddresses: import('../../..').BasePagination<K>;
13
14
  beneficiaries: import('../../..').BasePagination<Y>;
@@ -15,9 +16,9 @@ export declare class CoreShipmentBuilder<T extends CoreShippingMethod<Z>, K exte
15
16
  clearStorage: () => void;
16
17
  rehydrate: () => Promise<void> | void;
17
18
  hasHydrated: () => boolean;
18
- onHydrate: (fn: (state: CoreShipmentStore<T, K, Z, Y>) => void) => () => void;
19
- onFinishHydration: (fn: (state: CoreShipmentStore<T, K, Z, Y>) => void) => () => void;
20
- getOptions: () => Partial<import('zustand/middleware').PersistOptions<CoreShipmentStore<T, K, Z, Y>, {
19
+ onHydrate: (fn: (state: TStore) => void) => () => void;
20
+ onFinishHydration: (fn: (state: TStore) => void) => () => void;
21
+ getOptions: () => Partial<import('zustand/middleware').PersistOptions<TStore, {
21
22
  shippingMethods: T[];
22
23
  shippingAddresses: import('../../..').BasePagination<K>;
23
24
  beneficiaries: import('../../..').BasePagination<Y>;
@@ -1 +1 @@
1
- {"version":3,"file":"CoreShipmentBuilder.d.ts","sourceRoot":"","sources":["../../../../src/modules/CoreShipment/classes/CoreShipmentBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,uBAAuB,EACvB,uBAAuB,EAGxB,MAAM,uBAAuB,CAAC;AAG/B,qBAAa,mBAAmB,CAC9B,CAAC,SAAS,kBAAkB,CAAC,CAAC,CAAC,EAC/B,CAAC,SAAS,mBAAmB,EAC7B,CAAC,SAAS,uBAAuB,EACjC,CAAC,SAAS,uBAAuB,CACjC,YAAW,WAAW;IACtB,SAAS,CAAC,eAAe,EAAE,uBAAuB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/D,SAAS,CAAC,YAAY,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,iBAAiB,GAAG,mBAAmB,GAAG,eAAe,GAAG,iBAAiB,GAAG,SAAS,GAAG,OAAO,CAAC,CAAC;gBAG/J,OAAO,CAAC,EAAE,uBAAuB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAC7C,YAAY,CAAC,EAAE,GAAG;IAab,KAAK;;;;;;;;;;;;;;;;;;;CA2Nb"}
1
+ {"version":3,"file":"CoreShipmentBuilder.d.ts","sourceRoot":"","sources":["../../../../src/modules/CoreShipment/classes/CoreShipmentBuilder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,uBAAuB,EACvB,uBAAuB,EAGxB,MAAM,uBAAuB,CAAC;AAG/B,qBAAa,mBAAmB,CAC9B,CAAC,SAAS,kBAAkB,CAAC,CAAC,CAAC,EAC/B,CAAC,SAAS,mBAAmB,EAC7B,CAAC,SAAS,uBAAuB,EACjC,CAAC,SAAS,uBAAuB,EACjC,MAAM,SAAS,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAC5E,SAAQ,eAAe,CAAC,MAAM,CAAC;IAC/B,SAAS,CAAC,eAAe,EAAE,uBAAuB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/D,SAAS,CAAC,YAAY,EAAE,IAAI,CAAC,MAAM,EAAE,iBAAiB,GAAG,mBAAmB,GAAG,eAAe,GAAG,iBAAiB,GAAG,SAAS,GAAG,OAAO,CAAC,CAAC;gBAGxI,OAAO,CAAC,EAAE,uBAAuB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAC7C,YAAY,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC;IAiBhC,SAAS,CAAC,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,MAAM;IAgN3C,KAAK;;;;;;;;;;;;;;;;;;;CAgBb"}
@@ -1,13 +1,15 @@
1
1
  import { create } from 'zustand';
2
2
  import { persist } from '../../../node_modules/zustand/esm/middleware.js';
3
+ import { CoreBaseBuilder } from '../../../classes/CoreBaseBuilder.js';
3
4
  import { CoreShipmentBaseService } from '../services/base.js';
4
5
 
5
- class CoreShipmentBuilder {
6
+ class CoreShipmentBuilder extends CoreBaseBuilder {
6
7
  shipmentService;
7
8
  initialState;
8
9
  constructor(service, initialState) {
10
+ super();
9
11
  this.shipmentService = service || new CoreShipmentBaseService();
10
- this.initialState = initialState || {
12
+ this.initialState = {
11
13
  shippingMethods: null,
12
14
  shippingAddresses: null,
13
15
  beneficiaries: null,
@@ -15,178 +17,185 @@ class CoreShipmentBuilder {
15
17
  loading: false,
16
18
  error: null
17
19
  };
20
+ if (initialState) {
21
+ this.initialState = { ...this.initialState, ...initialState };
22
+ }
18
23
  }
19
- build() {
20
- return create()(
21
- persist(
22
- (set, get) => ({
23
- ...this.initialState,
24
- // Shipping Methods
25
- setShippingMethods: (shippingMethods) => set({ shippingMethods }),
26
- getShippingMethods: () => get().shippingMethods,
27
- fetchShippingMethods: async (params) => {
28
- const { onlyFetch } = params || {};
29
- try {
30
- if (!onlyFetch) {
31
- set({ loading: true, error: null });
32
- }
33
- const response = await this.shipmentService.getShippingMethods();
34
- if (!onlyFetch) {
35
- set({ shippingMethods: response.data.data, loading: false });
36
- }
37
- return response.data;
38
- } catch (error) {
39
- if (!onlyFetch) {
40
- set({ error: error || "Failed to fetch shipping methods", loading: false });
41
- }
42
- throw error;
43
- }
44
- },
45
- // Shipping Addresses
46
- setShippingAddresses: (shippingAddresses) => set({ shippingAddresses }),
47
- getShippingAddresses: () => get().shippingAddresses,
48
- // Selected Address
49
- setSelectedAddress: (address) => set({ selectedAddress: address }),
50
- getSelectedAddress: () => get().selectedAddress,
51
- fetchShippingAddresses: async (params, actions) => {
52
- const { onlyFetch, ...queryParams } = params || {};
53
- try {
54
- if (!onlyFetch) {
55
- set({ loading: true, error: null });
56
- }
57
- const response = await this.shipmentService.getShippingAddresses(queryParams);
58
- if (!onlyFetch) {
59
- set({ shippingAddresses: response.data.data, loading: false });
60
- }
61
- actions?.onSuccess?.(response.data);
62
- return response.data;
63
- } catch (error) {
64
- if (!onlyFetch) {
65
- set({ error: error || "Failed to fetch shipping addresses", loading: false });
66
- }
67
- actions?.onError?.(error);
68
- throw error;
69
- } finally {
70
- actions?.onFinish?.();
71
- }
72
- },
73
- addShippingAddress: async (newAddress, params, actions) => {
74
- set({ loading: true, error: null });
75
- try {
76
- const response = await this.shipmentService.addShippingAddress(newAddress);
77
- await get().fetchShippingAddresses(params);
78
- actions?.onSuccess?.(response);
79
- } catch (error) {
80
- set({ error: error.message || "Failed to add shipping address", loading: false });
81
- actions?.onError?.(error);
82
- throw error;
83
- } finally {
84
- actions?.onFinish?.();
85
- }
86
- },
87
- updateShippingAddress: async (addressId, updateAddress, params, actions) => {
88
- set({ loading: true, error: null });
89
- try {
90
- const response = await this.shipmentService.updateShippingAddress(addressId, updateAddress);
91
- await get().fetchShippingAddresses(params);
92
- actions?.onSuccess?.(response);
93
- } catch (error) {
94
- set({ error: error || "Failed to update shipping address", loading: false });
95
- actions?.onError?.(error);
96
- throw error;
97
- } finally {
98
- actions?.onFinish?.();
99
- }
100
- },
101
- deleteShippingAddress: async (addressId, params, actions) => {
24
+ getBaseState(set, get) {
25
+ return {
26
+ ...this.initialState,
27
+ // Shipping Methods
28
+ setShippingMethods: (shippingMethods) => set({ shippingMethods }),
29
+ getShippingMethods: () => get().shippingMethods,
30
+ fetchShippingMethods: async (params) => {
31
+ const { onlyFetch } = params || {};
32
+ try {
33
+ if (!onlyFetch) {
102
34
  set({ loading: true, error: null });
103
- try {
104
- const response = await this.shipmentService.deleteShippingAddress(addressId);
105
- await get().fetchShippingAddresses(params);
106
- actions?.onSuccess?.(response);
107
- } catch (error) {
108
- set({ error: error || "Failed to delete shipping address", loading: false });
109
- actions?.onError?.(error);
110
- throw error;
111
- } finally {
112
- actions?.onFinish?.();
113
- }
114
- },
115
- // Beneficiaries
116
- fetchBeneficiaries: async (params, actions) => {
117
- const { onlyFetch, ...queryParams } = params || {};
118
- try {
119
- if (!onlyFetch) {
120
- set({ loading: true, error: null });
121
- }
122
- const response = await this.shipmentService.getBeneficiaries(queryParams);
123
- if (!onlyFetch) {
124
- set({ beneficiaries: response.data.data, loading: false });
125
- }
126
- actions?.onSuccess?.(response.data);
127
- return response.data;
128
- } catch (error) {
129
- if (!onlyFetch) {
130
- set({ error: error || "Failed to fetch beneficiaries", loading: false });
131
- }
132
- actions?.onError?.(error);
133
- throw error;
134
- } finally {
135
- actions?.onFinish?.();
136
- }
137
- },
138
- addBeneficiary: async (newBeneficiary, params, actions) => {
139
- set({ loading: true, error: null });
140
- try {
141
- const response = await this.shipmentService.addBeneficiary(newBeneficiary);
142
- await get().fetchBeneficiaries(params);
143
- actions?.onSuccess?.(response);
144
- } catch (error) {
145
- set({ error: error || "Failed to add beneficiary", loading: false });
146
- actions?.onError?.(error);
147
- throw error;
148
- } finally {
149
- actions?.onFinish?.();
150
- }
151
- },
152
- updateBeneficiary: async (beneficiaryId, updateBeneficiary, params, actions) => {
35
+ }
36
+ const response = await this.shipmentService.getShippingMethods();
37
+ if (!onlyFetch) {
38
+ set({ shippingMethods: response.data.data, loading: false });
39
+ }
40
+ return response.data;
41
+ } catch (error) {
42
+ if (!onlyFetch) {
43
+ set({ error: error || "Failed to fetch shipping methods", loading: false });
44
+ }
45
+ throw error;
46
+ }
47
+ },
48
+ // Shipping Addresses
49
+ setShippingAddresses: (shippingAddresses) => set({ shippingAddresses }),
50
+ getShippingAddresses: () => get().shippingAddresses,
51
+ // Selected Address
52
+ setSelectedAddress: (address) => set({ selectedAddress: address }),
53
+ getSelectedAddress: () => get().selectedAddress,
54
+ fetchShippingAddresses: async (params, actions) => {
55
+ const { onlyFetch, ...queryParams } = params || {};
56
+ try {
57
+ if (!onlyFetch) {
153
58
  set({ loading: true, error: null });
154
- try {
155
- const response = await this.shipmentService.updateBeneficiary(beneficiaryId, updateBeneficiary);
156
- await get().fetchBeneficiaries(params);
157
- actions?.onSuccess?.(response);
158
- } catch (error) {
159
- set({ error: error || "Failed to update beneficiary", loading: false });
160
- actions?.onError?.(error);
161
- throw error;
162
- } finally {
163
- actions?.onFinish?.();
164
- }
165
- },
166
- deleteBeneficiary: async (beneficiaryId, params, actions) => {
59
+ }
60
+ const response = await this.shipmentService.getShippingAddresses(queryParams);
61
+ if (!onlyFetch) {
62
+ set({ shippingAddresses: response.data.data, loading: false });
63
+ }
64
+ actions?.onSuccess?.(response.data);
65
+ return response.data;
66
+ } catch (error) {
67
+ if (!onlyFetch) {
68
+ set({ error: error || "Failed to fetch shipping addresses", loading: false });
69
+ }
70
+ actions?.onError?.(error);
71
+ throw error;
72
+ } finally {
73
+ actions?.onFinish?.();
74
+ }
75
+ },
76
+ addShippingAddress: async (newAddress, params, actions) => {
77
+ set({ loading: true, error: null });
78
+ try {
79
+ const response = await this.shipmentService.addShippingAddress(newAddress);
80
+ await get().fetchShippingAddresses(params);
81
+ actions?.onSuccess?.(response);
82
+ } catch (error) {
83
+ set({ error: error.message || "Failed to add shipping address", loading: false });
84
+ actions?.onError?.(error);
85
+ throw error;
86
+ } finally {
87
+ actions?.onFinish?.();
88
+ }
89
+ },
90
+ updateShippingAddress: async (addressId, updateAddress, params, actions) => {
91
+ set({ loading: true, error: null });
92
+ try {
93
+ const response = await this.shipmentService.updateShippingAddress(addressId, updateAddress);
94
+ await get().fetchShippingAddresses(params);
95
+ actions?.onSuccess?.(response);
96
+ } catch (error) {
97
+ set({ error: error || "Failed to update shipping address", loading: false });
98
+ actions?.onError?.(error);
99
+ throw error;
100
+ } finally {
101
+ actions?.onFinish?.();
102
+ }
103
+ },
104
+ deleteShippingAddress: async (addressId, params, actions) => {
105
+ set({ loading: true, error: null });
106
+ try {
107
+ const response = await this.shipmentService.deleteShippingAddress(addressId);
108
+ await get().fetchShippingAddresses(params);
109
+ actions?.onSuccess?.(response);
110
+ } catch (error) {
111
+ set({ error: error || "Failed to delete shipping address", loading: false });
112
+ actions?.onError?.(error);
113
+ throw error;
114
+ } finally {
115
+ actions?.onFinish?.();
116
+ }
117
+ },
118
+ // Beneficiaries
119
+ fetchBeneficiaries: async (params, actions) => {
120
+ const { onlyFetch, ...queryParams } = params || {};
121
+ try {
122
+ if (!onlyFetch) {
167
123
  set({ loading: true, error: null });
168
- try {
169
- const response = await this.shipmentService.deleteBeneficiary(beneficiaryId);
170
- await get().fetchBeneficiaries(params);
171
- actions?.onSuccess?.(response);
172
- } catch (error) {
173
- set({ error: error || "Failed to delete beneficiary", loading: false });
174
- actions?.onError?.(error);
175
- throw error;
176
- } finally {
177
- actions?.onFinish?.();
178
- }
179
- },
180
- setLoading: (loading) => set({ loading }),
181
- setError: (error) => set({ error }),
182
- reset: () => set(this.initialState)
183
- }),
124
+ }
125
+ const response = await this.shipmentService.getBeneficiaries(queryParams);
126
+ if (!onlyFetch) {
127
+ set({ beneficiaries: response.data.data, loading: false });
128
+ }
129
+ actions?.onSuccess?.(response.data);
130
+ return response.data;
131
+ } catch (error) {
132
+ if (!onlyFetch) {
133
+ set({ error: error || "Failed to fetch beneficiaries", loading: false });
134
+ }
135
+ actions?.onError?.(error);
136
+ throw error;
137
+ } finally {
138
+ actions?.onFinish?.();
139
+ }
140
+ },
141
+ addBeneficiary: async (newBeneficiary, params, actions) => {
142
+ set({ loading: true, error: null });
143
+ try {
144
+ const response = await this.shipmentService.addBeneficiary(newBeneficiary);
145
+ await get().fetchBeneficiaries(params);
146
+ actions?.onSuccess?.(response);
147
+ } catch (error) {
148
+ set({ error: error || "Failed to add beneficiary", loading: false });
149
+ actions?.onError?.(error);
150
+ throw error;
151
+ } finally {
152
+ actions?.onFinish?.();
153
+ }
154
+ },
155
+ updateBeneficiary: async (beneficiaryId, updateBeneficiary, params, actions) => {
156
+ set({ loading: true, error: null });
157
+ try {
158
+ const response = await this.shipmentService.updateBeneficiary(beneficiaryId, updateBeneficiary);
159
+ await get().fetchBeneficiaries(params);
160
+ actions?.onSuccess?.(response);
161
+ } catch (error) {
162
+ set({ error: error || "Failed to update beneficiary", loading: false });
163
+ actions?.onError?.(error);
164
+ throw error;
165
+ } finally {
166
+ actions?.onFinish?.();
167
+ }
168
+ },
169
+ deleteBeneficiary: async (beneficiaryId, params, actions) => {
170
+ set({ loading: true, error: null });
171
+ try {
172
+ const response = await this.shipmentService.deleteBeneficiary(beneficiaryId);
173
+ await get().fetchBeneficiaries(params);
174
+ actions?.onSuccess?.(response);
175
+ } catch (error) {
176
+ set({ error: error || "Failed to delete beneficiary", loading: false });
177
+ actions?.onError?.(error);
178
+ throw error;
179
+ } finally {
180
+ actions?.onFinish?.();
181
+ }
182
+ },
183
+ setLoading: (loading) => set({ loading }),
184
+ setError: (error) => set({ error }),
185
+ reset: () => set({ ...this.initialState, ...this.extendInitialState() })
186
+ };
187
+ }
188
+ build() {
189
+ return create()(
190
+ persist(
191
+ (set, get) => this.buildStoreState(set, get),
184
192
  {
185
193
  name: "shipment-storage",
186
194
  partialize: (state) => ({
187
195
  shippingMethods: state.shippingMethods,
188
196
  shippingAddresses: state.shippingAddresses,
189
- beneficiaries: state.beneficiaries
197
+ beneficiaries: state.beneficiaries,
198
+ ...this.extendPartialize(state)
190
199
  })
191
200
  }
192
201
  )