@smess/networking 1.0.22 → 1.0.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 (84) hide show
  1. package/dist/scripts/dev-test.d.ts +1 -0
  2. package/dist/scripts/dev-test.js +803 -0
  3. package/dist/scripts/real-test.d.ts +1 -0
  4. package/dist/scripts/real-test.js +341 -0
  5. package/dist/services/customer.service.js +7 -0
  6. package/dist/src/core/axios.d.ts +51 -0
  7. package/dist/src/core/axios.js +65 -0
  8. package/dist/src/core/axios.observable.d.ts +11 -0
  9. package/dist/src/core/axios.observable.js +58 -0
  10. package/dist/src/core/base.provider.advanced.test.d.ts +1 -0
  11. package/dist/src/core/base.provider.advanced.test.js +29 -0
  12. package/dist/src/core/base.provider.d.ts +15 -0
  13. package/dist/src/core/base.provider.js +13 -0
  14. package/dist/src/core/base.provider.test.d.ts +1 -0
  15. package/dist/src/core/base.provider.test.js +36 -0
  16. package/dist/src/core/base.service.wrapper.advanced.test.d.ts +1 -0
  17. package/dist/src/core/base.service.wrapper.advanced.test.js +39 -0
  18. package/dist/src/core/base.service.wrapper.d.ts +7 -0
  19. package/dist/src/core/base.service.wrapper.js +11 -0
  20. package/dist/src/core/base.service.wrapper.test.d.ts +1 -0
  21. package/dist/src/core/base.service.wrapper.test.js +25 -0
  22. package/dist/src/core/rx.http.service.d.ts +51 -0
  23. package/dist/src/core/rx.http.service.js +65 -0
  24. package/dist/src/index.d.ts +17 -0
  25. package/dist/src/index.js +21 -0
  26. package/dist/src/services/auth.service.d.ts +65 -0
  27. package/dist/src/services/auth.service.js +197 -0
  28. package/dist/src/services/auth.service.test.d.ts +1 -0
  29. package/dist/src/services/auth.service.test.js +334 -0
  30. package/dist/src/services/cache.service.d.ts +31 -0
  31. package/dist/src/services/cache.service.js +77 -0
  32. package/dist/src/services/cache.service.test.d.ts +1 -0
  33. package/dist/src/services/cache.service.test.js +88 -0
  34. package/dist/src/services/customer.service.d.ts +71 -0
  35. package/dist/src/services/customer.service.js +114 -0
  36. package/dist/src/services/customer.service.test.d.ts +1 -0
  37. package/dist/src/services/customer.service.test.js +274 -0
  38. package/dist/src/services/employee.service.d.ts +73 -0
  39. package/dist/src/services/employee.service.js +73 -0
  40. package/dist/src/services/employee.service.test.d.ts +1 -0
  41. package/dist/src/services/employee.service.test.js +120 -0
  42. package/dist/src/services/logger.service.d.ts +31 -0
  43. package/dist/src/services/logger.service.js +93 -0
  44. package/dist/src/services/logger.service.test.d.ts +1 -0
  45. package/dist/src/services/logger.service.test.js +31 -0
  46. package/dist/src/services/order.service.d.ts +73 -0
  47. package/dist/src/services/order.service.js +97 -0
  48. package/dist/src/services/order.service.test.d.ts +1 -0
  49. package/dist/src/services/order.service.test.js +163 -0
  50. package/dist/src/services/product.service.d.ts +21 -0
  51. package/dist/src/services/product.service.js +42 -0
  52. package/dist/src/services/product.service.test.d.ts +1 -0
  53. package/dist/src/services/product.service.test.js +61 -0
  54. package/dist/src/services/smess.service.d.ts +36 -0
  55. package/dist/src/services/smess.service.js +128 -0
  56. package/dist/src/services/smess.service.test.d.ts +1 -0
  57. package/dist/src/services/smess.service.test.js +103 -0
  58. package/dist/src/services/store.cart.service.d.ts +158 -0
  59. package/dist/src/services/store.cart.service.js +177 -0
  60. package/dist/src/services/store.cart.service.test.d.ts +1 -0
  61. package/dist/src/services/store.cart.service.test.js +394 -0
  62. package/dist/src/services/store.product.service.d.ts +123 -0
  63. package/dist/src/services/store.product.service.js +102 -0
  64. package/dist/src/services/store.product.service.test.d.ts +1 -0
  65. package/dist/src/services/store.product.service.test.js +319 -0
  66. package/dist/src/services/store.service.d.ts +79 -0
  67. package/dist/src/services/store.service.js +235 -0
  68. package/dist/src/services/store.service.test.d.ts +1 -0
  69. package/dist/src/services/store.service.test.js +274 -0
  70. package/dist/src/services/user.service.d.ts +16 -0
  71. package/dist/src/services/user.service.js +37 -0
  72. package/dist/src/services/user.service.test.d.ts +1 -0
  73. package/dist/src/services/user.service.test.js +52 -0
  74. package/dist/src/types/types.d.ts +93 -0
  75. package/dist/src/types/types.js +82 -0
  76. package/dist/src/types/types.test.d.ts +1 -0
  77. package/dist/src/types/types.test.js +25 -0
  78. package/dist/src/utils/cache-import.d.ts +3 -0
  79. package/dist/src/utils/cache-import.js +2 -0
  80. package/dist/src/utils/networking-import.d.ts +4 -0
  81. package/dist/src/utils/networking-import.js +3 -0
  82. package/dist/src/utils/rxjs-import.d.ts +5 -0
  83. package/dist/src/utils/rxjs-import.js +5 -0
  84. package/package.json +1 -1
@@ -0,0 +1,319 @@
1
+ import { StoreProductService, } from "./store.product.service";
2
+ import { NetworkingUIEventWrapped, AlertModel, } from "../types/types";
3
+ import { of } from "rxjs";
4
+ describe("StoreProductService", () => {
5
+ const config = {
6
+ baseUrl: "https://api.example.com",
7
+ isProd: false,
8
+ clientId: "test-client-id",
9
+ apiKey: "test-api-key",
10
+ };
11
+ let service;
12
+ beforeEach(() => {
13
+ service = new StoreProductService(config);
14
+ });
15
+ describe("getFeaturedProductList", () => {
16
+ it("should get featured product list successfully", async () => {
17
+ const response = {
18
+ data: [
19
+ {
20
+ id: "1",
21
+ name: "Product 1",
22
+ barcode: "123",
23
+ category: "CONGELE",
24
+ vatInPercent: 5.5,
25
+ unit: "piece",
26
+ quantity: 1,
27
+ picture: "image.png",
28
+ slug: "product-1",
29
+ internalCode: "001",
30
+ inventory: true,
31
+ quantityInStock: 10,
32
+ minimumStockQuantity: 5,
33
+ pricePurchase: 10,
34
+ priceToSell: 15,
35
+ discountInPercent: 0,
36
+ priceAfterDiscount: 0,
37
+ expiryDate: null,
38
+ subCategory: null,
39
+ volume: null,
40
+ unitWeight: null,
41
+ articleWeight: null,
42
+ tareWeight: null,
43
+ supplier: null,
44
+ supplierCode: null,
45
+ supplierItemName: null,
46
+ htmlDescription: null,
47
+ },
48
+ ],
49
+ };
50
+ jest.spyOn(service, "provider").mockReturnValue({
51
+ request: () => of(NetworkingUIEventWrapped.succeeded(response)),
52
+ });
53
+ const result = await service.getFeaturedProductList().toPromise();
54
+ expect(result?.hasSuccess).toBe(true);
55
+ expect(result?.response).toEqual(response);
56
+ });
57
+ it("should handle error", async () => {
58
+ jest.spyOn(service, "provider").mockReturnValue({
59
+ request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
60
+ });
61
+ const result = await service.getFeaturedProductList().toPromise();
62
+ expect(result?.hasFailed).toBe(true);
63
+ });
64
+ });
65
+ describe("getFeaturedProductListByStore", () => {
66
+ it("should get featured product list by store successfully", async () => {
67
+ const storeId = "store-123";
68
+ const response = {
69
+ data: [
70
+ {
71
+ id: "1",
72
+ name: "Product 1",
73
+ barcode: "123",
74
+ category: "CONGELE",
75
+ vatInPercent: 5.5,
76
+ unit: "piece",
77
+ quantity: 1,
78
+ picture: "image.png",
79
+ slug: "product-1",
80
+ internalCode: "001",
81
+ inventory: true,
82
+ quantityInStock: 10,
83
+ minimumStockQuantity: 5,
84
+ pricePurchase: 10,
85
+ priceToSell: 15,
86
+ discountInPercent: 0,
87
+ priceAfterDiscount: 0,
88
+ expiryDate: null,
89
+ subCategory: null,
90
+ volume: null,
91
+ unitWeight: null,
92
+ articleWeight: null,
93
+ tareWeight: null,
94
+ supplier: null,
95
+ supplierCode: null,
96
+ supplierItemName: null,
97
+ htmlDescription: null,
98
+ },
99
+ ],
100
+ };
101
+ jest.spyOn(service, "provider").mockReturnValue({
102
+ request: () => of(NetworkingUIEventWrapped.succeeded(response)),
103
+ });
104
+ const result = await service.getFeaturedProductListByStore(storeId).toPromise();
105
+ expect(result?.hasSuccess).toBe(true);
106
+ expect(result?.response).toEqual(response);
107
+ });
108
+ });
109
+ describe("getProductCategoryList", () => {
110
+ it("should get product category list successfully", async () => {
111
+ const response = {
112
+ data: [
113
+ {
114
+ name: "BOISSON",
115
+ numberOfProducts: 443,
116
+ image: null,
117
+ },
118
+ {
119
+ name: "CONGELE",
120
+ numberOfProducts: 324,
121
+ image: null,
122
+ },
123
+ ],
124
+ };
125
+ jest.spyOn(service, "provider").mockReturnValue({
126
+ request: () => of(NetworkingUIEventWrapped.succeeded(response)),
127
+ });
128
+ const result = await service.getProductCategoryList().toPromise();
129
+ expect(result?.hasSuccess).toBe(true);
130
+ expect(result?.response).toEqual(response);
131
+ });
132
+ });
133
+ describe("getProductById", () => {
134
+ it("should get product by id successfully", async () => {
135
+ const productId = "product-123";
136
+ const response = {
137
+ data: {
138
+ id: productId,
139
+ name: "Product 1",
140
+ barcode: "123",
141
+ category: "CONGELE",
142
+ vatInPercent: 5.5,
143
+ unit: "piece",
144
+ quantity: 1,
145
+ picture: "image.png",
146
+ slug: "product-1",
147
+ internalCode: "001",
148
+ inventory: true,
149
+ quantityInStock: 10,
150
+ minimumStockQuantity: 5,
151
+ pricePurchase: 10,
152
+ priceToSell: 15,
153
+ discountInPercent: 0,
154
+ priceAfterDiscount: 0,
155
+ expiryDate: null,
156
+ subCategory: null,
157
+ volume: null,
158
+ unitWeight: null,
159
+ articleWeight: null,
160
+ tareWeight: null,
161
+ supplier: null,
162
+ supplierCode: null,
163
+ supplierItemName: null,
164
+ htmlDescription: null,
165
+ },
166
+ };
167
+ jest.spyOn(service, "provider").mockReturnValue({
168
+ request: () => of(NetworkingUIEventWrapped.succeeded(response)),
169
+ });
170
+ const result = await service.getProductById(productId).toPromise();
171
+ expect(result?.hasSuccess).toBe(true);
172
+ expect(result?.response).toEqual(response);
173
+ });
174
+ });
175
+ describe("getProductCollection", () => {
176
+ it("should get product collection successfully", async () => {
177
+ const payload = {
178
+ page: 1,
179
+ per_page: 10,
180
+ search: "test",
181
+ };
182
+ const response = {
183
+ data: {
184
+ links: {
185
+ first: "/api/v1/store/product-collection?per_page=10&page=1",
186
+ last: "/api/v1/store/product-collection?per_page=10&page=5",
187
+ next: "/api/v1/store/product-collection?per_page=10&page=2",
188
+ },
189
+ items: [
190
+ {
191
+ id: "1",
192
+ name: "Product 1",
193
+ barcode: "123",
194
+ category: "CONGELE",
195
+ vatInPercent: 5.5,
196
+ unit: "piece",
197
+ quantity: 1,
198
+ picture: "image.png",
199
+ slug: "product-1",
200
+ internalCode: "001",
201
+ inventory: true,
202
+ quantityInStock: 10,
203
+ minimumStockQuantity: 5,
204
+ pricePurchase: 10,
205
+ priceToSell: 15,
206
+ discountInPercent: 0,
207
+ priceAfterDiscount: 0,
208
+ expiryDate: null,
209
+ subCategory: null,
210
+ volume: null,
211
+ unitWeight: null,
212
+ articleWeight: null,
213
+ tareWeight: null,
214
+ supplier: null,
215
+ supplierCode: null,
216
+ supplierItemName: null,
217
+ htmlDescription: null,
218
+ },
219
+ ],
220
+ count: 10,
221
+ total: 50,
222
+ page: 1,
223
+ totalPage: 5,
224
+ perPage: 10,
225
+ metadata: [],
226
+ },
227
+ };
228
+ jest.spyOn(service, "provider").mockReturnValue({
229
+ request: () => of(NetworkingUIEventWrapped.succeeded(response)),
230
+ });
231
+ const result = await service.getProductCollection(payload).toPromise();
232
+ expect(result?.hasSuccess).toBe(true);
233
+ expect(result?.response).toEqual(response);
234
+ });
235
+ it("should get product collection without payload", async () => {
236
+ const response = {
237
+ data: {
238
+ links: {
239
+ first: "/api/v1/store/product-collection?per_page=24&page=1",
240
+ last: "/api/v1/store/product-collection?per_page=24&page=1",
241
+ },
242
+ items: [],
243
+ count: 0,
244
+ total: 0,
245
+ page: 1,
246
+ totalPage: 1,
247
+ perPage: 24,
248
+ metadata: [],
249
+ },
250
+ };
251
+ jest.spyOn(service, "provider").mockReturnValue({
252
+ request: () => of(NetworkingUIEventWrapped.succeeded(response)),
253
+ });
254
+ const result = await service.getProductCollection().toPromise();
255
+ expect(result?.hasSuccess).toBe(true);
256
+ expect(result?.response).toEqual(response);
257
+ });
258
+ });
259
+ describe("getProductCollectionByStore", () => {
260
+ it("should get product collection by store successfully", async () => {
261
+ const storeId = "store-123";
262
+ const payload = {
263
+ page: 1,
264
+ per_page: 10,
265
+ };
266
+ const response = {
267
+ data: {
268
+ links: {
269
+ first: "/api/v1/store/product-collection?per_page=10&page=1",
270
+ last: "/api/v1/store/product-collection?per_page=10&page=1",
271
+ },
272
+ items: [
273
+ {
274
+ id: "1",
275
+ name: "Product 1",
276
+ barcode: "123",
277
+ category: "CONGELE",
278
+ vatInPercent: 5.5,
279
+ unit: "piece",
280
+ quantity: 1,
281
+ picture: "image.png",
282
+ slug: "product-1",
283
+ internalCode: "001",
284
+ inventory: true,
285
+ quantityInStock: 10,
286
+ minimumStockQuantity: 5,
287
+ pricePurchase: 10,
288
+ priceToSell: 15,
289
+ discountInPercent: 0,
290
+ priceAfterDiscount: 0,
291
+ expiryDate: null,
292
+ subCategory: null,
293
+ volume: null,
294
+ unitWeight: null,
295
+ articleWeight: null,
296
+ tareWeight: null,
297
+ supplier: null,
298
+ supplierCode: null,
299
+ supplierItemName: null,
300
+ htmlDescription: null,
301
+ },
302
+ ],
303
+ count: 1,
304
+ total: 1,
305
+ page: 1,
306
+ totalPage: 1,
307
+ perPage: 10,
308
+ metadata: [],
309
+ },
310
+ };
311
+ jest.spyOn(service, "provider").mockReturnValue({
312
+ request: () => of(NetworkingUIEventWrapped.succeeded(response)),
313
+ });
314
+ const result = await service.getProductCollectionByStore(storeId, payload).toPromise();
315
+ expect(result?.hasSuccess).toBe(true);
316
+ expect(result?.response).toEqual(response);
317
+ });
318
+ });
319
+ });
@@ -0,0 +1,79 @@
1
+ import { ProviderType, NetworkingConfig, NetworkingUIEventWrapped, HttpResponse } from "../types/types";
2
+ import { SmessService } from "./smess.service";
3
+ import { BehaviorSubject, Observable } from "rxjs";
4
+ export type StoreTarget = "store-get" | "store-list" | "store-product-category-list" | "store-product-collection" | "store-featured-product-list";
5
+ export interface ProductCollectionRequest {
6
+ page?: number;
7
+ per_page?: number;
8
+ search?: string;
9
+ sort_by?: string;
10
+ category?: string;
11
+ min_price?: number;
12
+ max_price?: number;
13
+ only_discounted?: boolean;
14
+ unit?: string;
15
+ supplier?: string;
16
+ has_offer?: boolean;
17
+ }
18
+ export declare class StoreProvider implements ProviderType {
19
+ private readonly config;
20
+ private readonly target;
21
+ constructor(config: NetworkingConfig, target: StoreTarget);
22
+ get baseURL(): string;
23
+ get path(): string;
24
+ get method(): string;
25
+ get headers(): Record<string, string>;
26
+ getPath(...queryParams: unknown[]): string;
27
+ }
28
+ export interface StoreData {
29
+ id: string;
30
+ data: HttpResponse;
31
+ timestamp: number;
32
+ }
33
+ export interface StoreListData {
34
+ data: HttpResponse;
35
+ timestamp: number;
36
+ }
37
+ export interface ProductCollectionData {
38
+ storeId: string;
39
+ request: ProductCollectionRequest;
40
+ data: HttpResponse;
41
+ timestamp: number;
42
+ }
43
+ export interface ProductCategoryData {
44
+ data: HttpResponse;
45
+ timestamp: number;
46
+ }
47
+ export interface FeaturedProductData {
48
+ storeId: string;
49
+ data: HttpResponse;
50
+ timestamp: number;
51
+ }
52
+ export declare class StoreService {
53
+ private readonly config;
54
+ readonly $selectedStoreId: BehaviorSubject<string | null>;
55
+ private readonly cacheService;
56
+ private readonly storeListCache;
57
+ private readonly productCollectionCache;
58
+ private readonly productCategoryCache;
59
+ private readonly featuredProductCache;
60
+ constructor(config: NetworkingConfig);
61
+ provider(target: StoreTarget): SmessService<StoreProvider>;
62
+ /**
63
+ * Set the selected store ID
64
+ */
65
+ setSelectedStoreId(storeId: string | null): void;
66
+ /**
67
+ * Get the current selected store ID
68
+ */
69
+ get currentSelectedStoreId(): string | null;
70
+ get(id: string): Observable<NetworkingUIEventWrapped<HttpResponse>>;
71
+ list(): Observable<NetworkingUIEventWrapped<HttpResponse>>;
72
+ productCategoryList(): Observable<NetworkingUIEventWrapped<HttpResponse>>;
73
+ productCollection(id: string, payload: ProductCollectionRequest): Observable<NetworkingUIEventWrapped<HttpResponse>>;
74
+ featuredProductList(id: string): Observable<NetworkingUIEventWrapped<HttpResponse>>;
75
+ clearCache(): Promise<void>;
76
+ clearStoreCache(id: string): Promise<void>;
77
+ clearProductCollectionCache(id: string): Promise<void>;
78
+ private isCacheValid;
79
+ }
@@ -0,0 +1,235 @@
1
+ import { NetworkingUIEventWrapped, } from "../types/types";
2
+ import { Accept, SMESSHeader, SmessService } from "./smess.service";
3
+ import { BehaviorSubject, from, of } from "rxjs";
4
+ import { map, switchMap, catchError } from "rxjs/operators";
5
+ import { GenericIndexedDBService } from "./cache.service";
6
+ export class StoreProvider {
7
+ constructor(config, target) {
8
+ this.config = config;
9
+ this.target = target;
10
+ }
11
+ get baseURL() {
12
+ return this.config.version
13
+ ? `${this.config.baseUrl}/${this.config.version}`
14
+ : this.config.baseUrl;
15
+ }
16
+ get path() {
17
+ switch (this.target) {
18
+ case "store-get":
19
+ return "/store/${id}/information";
20
+ case "store-list":
21
+ return "/store/list";
22
+ case "store-product-category-list":
23
+ return "/store/product-category-list";
24
+ case "store-product-collection":
25
+ return "/store/${id}/product-collection";
26
+ case "store-featured-product-list":
27
+ return "/store/${id}/featured-product-list";
28
+ default:
29
+ return "/store/${id}";
30
+ }
31
+ }
32
+ get method() {
33
+ return "GET";
34
+ }
35
+ get headers() {
36
+ let headers = SmessService.getHeaders(Accept.V1, null);
37
+ if (this.config.clientId) {
38
+ headers[SMESSHeader.ClientId] = this.config.clientId;
39
+ }
40
+ if (this.config.apiKey) {
41
+ headers[SMESSHeader.ApiKey] = this.config.apiKey;
42
+ }
43
+ return SmessService.filterHeaders(headers);
44
+ }
45
+ getPath(...queryParams) {
46
+ let definedPath = this.path;
47
+ const params = definedPath.match(/\$\{(.+?)\}/g);
48
+ params?.forEach((value, index) => {
49
+ definedPath = definedPath.replace(value, String(queryParams[index]));
50
+ });
51
+ return definedPath;
52
+ }
53
+ }
54
+ export class StoreService {
55
+ constructor(config) {
56
+ this.config = config;
57
+ this.$selectedStoreId = new BehaviorSubject(null);
58
+ // Initialize cache services for different data types
59
+ this.cacheService = new GenericIndexedDBService("StoreCache", "id, timestamp", (data) => data, (data) => data);
60
+ this.storeListCache = new GenericIndexedDBService("StoreListCache", "timestamp", (data) => data, (data) => data);
61
+ this.productCollectionCache = new GenericIndexedDBService("ProductCollectionCache", "storeId, timestamp", (data) => data, (data) => data);
62
+ this.productCategoryCache = new GenericIndexedDBService("ProductCategoryCache", "timestamp", (data) => data, (data) => data);
63
+ this.featuredProductCache = new GenericIndexedDBService("FeaturedProductCache", "storeId, timestamp", (data) => data, (data) => data);
64
+ }
65
+ provider(target) {
66
+ const targetWrapper = new StoreProvider(this.config, target);
67
+ return new SmessService(targetWrapper);
68
+ }
69
+ /**
70
+ * Set the selected store ID
71
+ */
72
+ setSelectedStoreId(storeId) {
73
+ this.$selectedStoreId.next(storeId);
74
+ }
75
+ /**
76
+ * Get the current selected store ID
77
+ */
78
+ get currentSelectedStoreId() {
79
+ return this.$selectedStoreId.value;
80
+ }
81
+ get(id) {
82
+ return from(this.cacheService.getOne(id)).pipe(switchMap((cachedData) => {
83
+ if (cachedData && this.isCacheValid(cachedData.timestamp)) {
84
+ // Return cached data if valid
85
+ return of(NetworkingUIEventWrapped.succeeded(cachedData.data));
86
+ }
87
+ // Fetch from network if no cache or expired
88
+ return this.provider("store-get")
89
+ .request(null, undefined, id)
90
+ .pipe(map((response) => {
91
+ // Cache the successful response
92
+ if (response.response) {
93
+ this.cacheService.saveOne({
94
+ id,
95
+ data: response.response,
96
+ timestamp: Date.now(),
97
+ });
98
+ }
99
+ return response;
100
+ }), catchError((error) => {
101
+ // If network fails and we have stale cache, return it
102
+ if (cachedData) {
103
+ return of(NetworkingUIEventWrapped.succeeded(cachedData.data));
104
+ }
105
+ throw error;
106
+ }));
107
+ }));
108
+ }
109
+ list() {
110
+ return from(this.storeListCache.getAll("store-list")).pipe(switchMap((cachedData) => {
111
+ if (cachedData &&
112
+ cachedData.length > 0 &&
113
+ this.isCacheValid(cachedData[0].timestamp)) {
114
+ return of(NetworkingUIEventWrapped.succeeded(cachedData[0].data));
115
+ }
116
+ return this.provider("store-list")
117
+ .request()
118
+ .pipe(map((response) => {
119
+ if (response.response) {
120
+ this.storeListCache.saveAll([
121
+ {
122
+ data: response.response,
123
+ timestamp: Date.now(),
124
+ },
125
+ ], "store-list");
126
+ }
127
+ return response;
128
+ }), catchError((error) => {
129
+ if (cachedData && cachedData.length > 0) {
130
+ return of(NetworkingUIEventWrapped.succeeded(cachedData[0].data));
131
+ }
132
+ throw error;
133
+ }));
134
+ }));
135
+ }
136
+ productCategoryList() {
137
+ return from(this.productCategoryCache.getAll("product-category-list")).pipe(switchMap((cachedData) => {
138
+ if (cachedData &&
139
+ cachedData.length > 0 &&
140
+ this.isCacheValid(cachedData[0].timestamp)) {
141
+ return of(NetworkingUIEventWrapped.succeeded(cachedData[0].data));
142
+ }
143
+ return this.provider("store-product-category-list")
144
+ .request()
145
+ .pipe(map((response) => {
146
+ if (response.response) {
147
+ this.productCategoryCache.saveAll([
148
+ {
149
+ data: response.response,
150
+ timestamp: Date.now(),
151
+ },
152
+ ], "product-category-list");
153
+ }
154
+ return response;
155
+ }), catchError((error) => {
156
+ if (cachedData && cachedData.length > 0) {
157
+ return of(NetworkingUIEventWrapped.succeeded(cachedData[0].data));
158
+ }
159
+ throw error;
160
+ }));
161
+ }));
162
+ }
163
+ productCollection(id, payload) {
164
+ const cacheKey = `${id}-${JSON.stringify(payload)}`;
165
+ return from(this.productCollectionCache.getOne(cacheKey)).pipe(switchMap((cachedData) => {
166
+ if (cachedData && this.isCacheValid(cachedData.timestamp)) {
167
+ return of(NetworkingUIEventWrapped.succeeded(cachedData.data));
168
+ }
169
+ return this.provider("store-product-collection")
170
+ .request(payload, undefined, id)
171
+ .pipe(map((response) => {
172
+ if (response.response) {
173
+ this.productCollectionCache.saveOne({
174
+ storeId: cacheKey,
175
+ request: payload,
176
+ data: response.response,
177
+ timestamp: Date.now(),
178
+ });
179
+ }
180
+ return response;
181
+ }), catchError((error) => {
182
+ if (cachedData) {
183
+ return of(NetworkingUIEventWrapped.succeeded(cachedData.data));
184
+ }
185
+ throw error;
186
+ }));
187
+ }));
188
+ }
189
+ featuredProductList(id) {
190
+ return from(this.featuredProductCache.getOne(id)).pipe(switchMap((cachedData) => {
191
+ if (cachedData && this.isCacheValid(cachedData.timestamp)) {
192
+ return of(NetworkingUIEventWrapped.succeeded(cachedData.data));
193
+ }
194
+ return this.provider("store-featured-product-list")
195
+ .request(null, undefined, id)
196
+ .pipe(map((response) => {
197
+ if (response.response) {
198
+ this.featuredProductCache.saveOne({
199
+ storeId: id,
200
+ data: response.response,
201
+ timestamp: Date.now(),
202
+ });
203
+ }
204
+ return response;
205
+ }), catchError((error) => {
206
+ if (cachedData) {
207
+ return of(NetworkingUIEventWrapped.succeeded(cachedData.data));
208
+ }
209
+ throw error;
210
+ }));
211
+ }));
212
+ }
213
+ // Cache management methods
214
+ clearCache() {
215
+ return Promise.all([
216
+ this.cacheService.clearAll(),
217
+ this.storeListCache.clearAll(),
218
+ this.productCollectionCache.clearAll(),
219
+ this.productCategoryCache.clearAll(),
220
+ this.featuredProductCache.clearAll(),
221
+ ]).then(() => { });
222
+ }
223
+ clearStoreCache(id) {
224
+ return this.cacheService.delete(id);
225
+ }
226
+ clearProductCollectionCache(id) {
227
+ // This would need to be implemented with a more sophisticated approach
228
+ // to clear all product collection cache entries for a specific store
229
+ return Promise.resolve();
230
+ }
231
+ isCacheValid(timestamp) {
232
+ const CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours for testing
233
+ return Date.now() - timestamp < CACHE_DURATION;
234
+ }
235
+ }
@@ -0,0 +1 @@
1
+ import "fake-indexeddb/auto";