@react-pakistan/util-functions 1.25.9 → 1.25.10

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 (53) hide show
  1. package/api/stellar-solutions/bank/cache.d.ts +0 -104
  2. package/api/stellar-solutions/bank/cache.js +237 -296
  3. package/api/stellar-solutions/bank/index.d.ts +1 -1
  4. package/api/stellar-solutions/bank/index.js +0 -15
  5. package/api/stellar-solutions/branch/cache.d.ts +0 -102
  6. package/api/stellar-solutions/branch/cache.js +241 -296
  7. package/api/stellar-solutions/branch/index.d.ts +1 -1
  8. package/api/stellar-solutions/branch/index.js +0 -15
  9. package/api/stellar-solutions/company/cache.d.ts +0 -106
  10. package/api/stellar-solutions/company/cache.js +247 -308
  11. package/api/stellar-solutions/company/index.d.ts +1 -1
  12. package/api/stellar-solutions/company/index.js +0 -15
  13. package/api/stellar-solutions/contact/cache.d.ts +0 -108
  14. package/api/stellar-solutions/contact/cache.js +247 -308
  15. package/api/stellar-solutions/contact/index.d.ts +1 -1
  16. package/api/stellar-solutions/contact/index.js +0 -15
  17. package/api/stellar-solutions/currency/cache.d.ts +0 -104
  18. package/api/stellar-solutions/currency/cache.js +243 -296
  19. package/api/stellar-solutions/currency/index.d.ts +1 -1
  20. package/api/stellar-solutions/currency/index.js +0 -15
  21. package/api/stellar-solutions/customer/cache.d.ts +0 -108
  22. package/api/stellar-solutions/customer/cache.js +249 -308
  23. package/api/stellar-solutions/customer/index.d.ts +1 -1
  24. package/api/stellar-solutions/customer/index.js +0 -15
  25. package/api/stellar-solutions/expense/cache.d.ts +0 -106
  26. package/api/stellar-solutions/expense/cache.js +247 -308
  27. package/api/stellar-solutions/expense/index.d.ts +1 -1
  28. package/api/stellar-solutions/expense/index.js +0 -15
  29. package/api/stellar-solutions/expense-category/cache.d.ts +0 -94
  30. package/api/stellar-solutions/expense-category/cache.js +224 -281
  31. package/api/stellar-solutions/expense-category/index.d.ts +1 -1
  32. package/api/stellar-solutions/expense-category/index.js +0 -15
  33. package/api/stellar-solutions/payment-mode/cache.d.ts +0 -102
  34. package/api/stellar-solutions/payment-mode/cache.js +245 -296
  35. package/api/stellar-solutions/payment-mode/index.d.ts +1 -1
  36. package/api/stellar-solutions/payment-mode/index.js +0 -15
  37. package/api/stellar-solutions/preference/cache.d.ts +0 -90
  38. package/api/stellar-solutions/preference/cache.js +218 -269
  39. package/api/stellar-solutions/preference/index.d.ts +1 -1
  40. package/api/stellar-solutions/preference/index.js +0 -15
  41. package/api/stellar-solutions/product/cache.d.ts +0 -94
  42. package/api/stellar-solutions/product/cache.js +222 -281
  43. package/api/stellar-solutions/product/index.d.ts +1 -1
  44. package/api/stellar-solutions/product/index.js +0 -15
  45. package/api/stellar-solutions/product-category/cache.d.ts +0 -94
  46. package/api/stellar-solutions/product-category/cache.js +224 -281
  47. package/api/stellar-solutions/product-category/index.d.ts +1 -1
  48. package/api/stellar-solutions/product-category/index.js +0 -15
  49. package/api/stellar-solutions/tax/cache.d.ts +0 -102
  50. package/api/stellar-solutions/tax/cache.js +235 -296
  51. package/api/stellar-solutions/tax/index.d.ts +1 -1
  52. package/api/stellar-solutions/tax/index.js +0 -15
  53. package/package.json +1 -1
@@ -1,102 +0,0 @@
1
- import { TaxBE } from '../type';
2
- /**
3
- * Synchronous utility function to get taxes from cache only
4
- * Returns cached data immediately if available and fresh, otherwise returns empty array
5
- * Does not trigger any API calls
6
- *
7
- * @returns TaxBE[] - Array of cached taxes or empty array wrapper ({count, items})
8
- *
9
- * @example
10
- * const taxes = getCachedTaxesSync();
11
- * if (taxes.items.length > 0) {
12
- * console.log(taxes.items[0].taxName);
13
- * }
14
- */
15
- export declare const getCachedTaxesSync: () => {
16
- count: number;
17
- items: TaxBE[];
18
- };
19
- /**
20
- * Utility function to get taxes from cache or fetch from API
21
- *
22
- * This function manages a localStorage cache of taxes with the following logic:
23
- * - If taxes exist in cache and are less than 1 week old, return cached version
24
- * - If taxes exist but are older than 1 week, fetch fresh data and update cache
25
- * - If taxes don't exist in cache, fetch from API and cache them
26
- *
27
- * @param searchQuery - Optional search query to filter taxes
28
- * @param pageLimit - Number of taxes to fetch (default: 100)
29
- * @returns Promise<{count:number, items: TaxBE[]}> - Paged taxes
30
- *
31
- * @example
32
- * const taxes = await getCachedTaxes();
33
- * console.log(taxes.items[0].taxName);
34
- *
35
- * // With search
36
- * const filtered = await getCachedTaxes('VAT');
37
- */
38
- export declare const getCachedTaxes: (searchQuery?: string, pageLimit?: number) => Promise<{
39
- count: number;
40
- items: TaxBE[];
41
- }>;
42
- /**
43
- * Utility function to get a specific tax by ID from cache
44
- * If not found in cache, returns null (does not trigger API call)
45
- *
46
- * @param taxId - The ID of the tax to retrieve
47
- * @returns TaxBE | null - The tax or null if not found
48
- *
49
- * @example
50
- * const tax = getCachedTaxById('tax-123');
51
- * if (tax) {
52
- * console.log(tax.taxName);
53
- * }
54
- */
55
- export declare const getCachedTaxById: (taxId: string) => TaxBE | null;
56
- /**
57
- * Utility function to get taxes by name from cache
58
- * If not found in cache, returns empty array (does not trigger API call)
59
- *
60
- * @param taxName - The name of the tax to search for
61
- * @returns TaxBE[] - Array of matching taxes or empty array
62
- *
63
- * @example
64
- * const taxes = getCachedTaxesByName('VAT');
65
- * console.log(taxes.length);
66
- */
67
- export declare const getCachedTaxesByName: (taxName: string) => TaxBE[];
68
- /**
69
- * Utility function to invalidate (remove) taxes from cache
70
- * Useful when taxes have been updated and you want to force a refresh
71
- *
72
- * @example
73
- * invalidateTaxesCache();
74
- * const freshTaxes = await getCachedTaxes();
75
- */
76
- export declare const invalidateTaxesCache: () => void;
77
- /**
78
- * Utility function to preload taxes into cache
79
- * Useful to call on app initialization or login
80
- *
81
- * @returns Promise<{count:number, items: TaxBE[]}> - Array of preloaded taxes
82
- *
83
- * @example
84
- * // On app initialization
85
- * await preloadTaxes();
86
- */
87
- export declare const preloadTaxes: () => Promise<{
88
- count: number;
89
- items: TaxBE[];
90
- }>;
91
- /**
92
- * Utility function to check if taxes cache is stale
93
- * Returns true if cache is older than 1 week or doesn't exist
94
- *
95
- * @returns boolean - True if cache is stale or doesn't exist
96
- *
97
- * @example
98
- * if (isTaxesCacheStale()) {
99
- * await getCachedTaxes(); // This will fetch fresh data
100
- * }
101
- */
102
- export declare const isTaxesCacheStale: () => boolean;
@@ -1,296 +1,235 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
- if (ar || !(i in from)) {
41
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
- ar[i] = from[i];
43
- }
44
- }
45
- return to.concat(ar || Array.prototype.slice.call(from));
46
- };
47
- Object.defineProperty(exports, "__esModule", { value: true });
48
- exports.isTaxesCacheStale = exports.preloadTaxes = exports.invalidateTaxesCache = exports.getCachedTaxesByName = exports.getCachedTaxById = exports.getCachedTaxes = exports.getCachedTaxesSync = void 0;
49
- var constants_1 = require("../constants");
50
- var type_1 = require("../type");
51
- var api_methods_1 = require("../../../constants/api-methods");
52
- var fetch_data_1 = require("../../../general/fetch-data");
53
- var get_storage_value_1 = require("../../../local-storage/get-storage-value");
54
- var set_storage_value_1 = require("../../../local-storage/set-storage-value");
55
- var constants_2 = require("../../../constants");
56
- /**
57
- * Synchronous utility function to get taxes from cache only
58
- * Returns cached data immediately if available and fresh, otherwise returns empty array
59
- * Does not trigger any API calls
60
- *
61
- * @returns TaxBE[] - Array of cached taxes or empty array wrapper ({count, items})
62
- *
63
- * @example
64
- * const taxes = getCachedTaxesSync();
65
- * if (taxes.items.length > 0) {
66
- * console.log(taxes.items[0].taxName);
67
- * }
68
- */
69
- var getCachedTaxesSync = function () {
70
- try {
71
- var cachedData = (0, get_storage_value_1.getStorageValue)(type_1.LS_KEYS.TAXES);
72
- if (!cachedData) {
73
- return { count: 0, items: [] };
74
- }
75
- var currentTime = new Date().getTime();
76
- var cachedTime = new Date(cachedData.cachedAt).getTime();
77
- var ageInMs = currentTime - cachedTime;
78
- // If cached data is less than 1 week old, return it
79
- if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
80
- return {
81
- count: cachedData.taxes.length,
82
- items: cachedData.taxes,
83
- };
84
- }
85
- return { count: 0, items: [] };
86
- }
87
- catch (error) {
88
- console.error('Error getting cached taxes:', error);
89
- return { count: 0, items: [] };
90
- }
91
- };
92
- exports.getCachedTaxesSync = getCachedTaxesSync;
93
- /**
94
- * Utility function to get taxes from cache or fetch from API
95
- *
96
- * This function manages a localStorage cache of taxes with the following logic:
97
- * - If taxes exist in cache and are less than 1 week old, return cached version
98
- * - If taxes exist but are older than 1 week, fetch fresh data and update cache
99
- * - If taxes don't exist in cache, fetch from API and cache them
100
- *
101
- * @param searchQuery - Optional search query to filter taxes
102
- * @param pageLimit - Number of taxes to fetch (default: 100)
103
- * @returns Promise<{count:number, items: TaxBE[]}> - Paged taxes
104
- *
105
- * @example
106
- * const taxes = await getCachedTaxes();
107
- * console.log(taxes.items[0].taxName);
108
- *
109
- * // With search
110
- * const filtered = await getCachedTaxes('VAT');
111
- */
112
- var getCachedTaxes = function (searchQuery_1) {
113
- var args_1 = [];
114
- for (var _i = 1; _i < arguments.length; _i++) {
115
- args_1[_i - 1] = arguments[_i];
116
- }
117
- return __awaiter(void 0, __spreadArray([searchQuery_1], args_1, true), void 0, function (searchQuery, pageLimit) {
118
- var response_1, cachedData, currentTime, cachedTime, ageInMs, response, updatedCache, error_1;
119
- var _a;
120
- if (pageLimit === void 0) { pageLimit = 100; }
121
- return __generator(this, function (_b) {
122
- switch (_b.label) {
123
- case 0:
124
- _b.trys.push([0, 4, , 5]);
125
- if (!(searchQuery && searchQuery.trim())) return [3 /*break*/, 2];
126
- return [4 /*yield*/, (0, fetch_data_1.fetchData)({
127
- url: constants_1.API_ROUTES.TAXES,
128
- body: JSON.stringify({
129
- searchQuery: searchQuery,
130
- pageLimit: pageLimit,
131
- currentPage: 1,
132
- }),
133
- method: api_methods_1.API_METHODS.POST,
134
- })];
135
- case 1:
136
- response_1 = _b.sent();
137
- return [2 /*return*/, (response_1 === null || response_1 === void 0 ? void 0 : response_1.data) || { count: 0, items: [] }];
138
- case 2:
139
- cachedData = (0, get_storage_value_1.getStorageValue)(type_1.LS_KEYS.TAXES);
140
- currentTime = new Date().getTime();
141
- // Check if cached data exists and is still fresh
142
- if (cachedData) {
143
- cachedTime = new Date(cachedData.cachedAt).getTime();
144
- ageInMs = currentTime - cachedTime;
145
- // If cached data is less than 1 week old, return it
146
- if (ageInMs < constants_2.ONE_WEEK_IN_MS) {
147
- return [2 /*return*/, {
148
- count: cachedData.taxes.length,
149
- items: cachedData.taxes,
150
- }];
151
- }
152
- }
153
- return [4 /*yield*/, (0, fetch_data_1.fetchData)({
154
- url: constants_1.API_ROUTES.TAXES,
155
- body: JSON.stringify({
156
- pageLimit: pageLimit,
157
- currentPage: 1,
158
- }),
159
- method: api_methods_1.API_METHODS.POST,
160
- })];
161
- case 3:
162
- response = _b.sent();
163
- if ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) {
164
- updatedCache = {
165
- taxes: response.data.items,
166
- cachedAt: new Date().toISOString(),
167
- };
168
- (0, set_storage_value_1.setStorageValue)(type_1.LS_KEYS.TAXES, updatedCache);
169
- return [2 /*return*/, response.data];
170
- }
171
- return [2 /*return*/, { count: 0, items: [] }];
172
- case 4:
173
- error_1 = _b.sent();
174
- console.error('Error fetching taxes:', error_1);
175
- return [2 /*return*/, { count: 0, items: [] }];
176
- case 5: return [2 /*return*/];
177
- }
178
- });
179
- });
180
- };
181
- exports.getCachedTaxes = getCachedTaxes;
182
- /**
183
- * Utility function to get a specific tax by ID from cache
184
- * If not found in cache, returns null (does not trigger API call)
185
- *
186
- * @param taxId - The ID of the tax to retrieve
187
- * @returns TaxBE | null - The tax or null if not found
188
- *
189
- * @example
190
- * const tax = getCachedTaxById('tax-123');
191
- * if (tax) {
192
- * console.log(tax.taxName);
193
- * }
194
- */
195
- var getCachedTaxById = function (taxId) {
196
- if (!taxId) {
197
- return null;
198
- }
199
- try {
200
- var taxes = (0, exports.getCachedTaxesSync)().items;
201
- return taxes.find(function (tax) { return tax.id === taxId; }) || null;
202
- }
203
- catch (error) {
204
- console.error('Error getting cached tax by ID:', error);
205
- return null;
206
- }
207
- };
208
- exports.getCachedTaxById = getCachedTaxById;
209
- /**
210
- * Utility function to get taxes by name from cache
211
- * If not found in cache, returns empty array (does not trigger API call)
212
- *
213
- * @param taxName - The name of the tax to search for
214
- * @returns TaxBE[] - Array of matching taxes or empty array
215
- *
216
- * @example
217
- * const taxes = getCachedTaxesByName('VAT');
218
- * console.log(taxes.length);
219
- */
220
- var getCachedTaxesByName = function (taxName) {
221
- if (!taxName) {
222
- return [];
223
- }
224
- try {
225
- var taxes = (0, exports.getCachedTaxesSync)().items;
226
- return taxes.filter(function (tax) {
227
- return tax.taxName.toLowerCase().includes(taxName.toLowerCase());
228
- });
229
- }
230
- catch (error) {
231
- console.error('Error getting cached taxes by name:', error);
232
- return [];
233
- }
234
- };
235
- exports.getCachedTaxesByName = getCachedTaxesByName;
236
- /**
237
- * Utility function to invalidate (remove) taxes from cache
238
- * Useful when taxes have been updated and you want to force a refresh
239
- *
240
- * @example
241
- * invalidateTaxesCache();
242
- * const freshTaxes = await getCachedTaxes();
243
- */
244
- var invalidateTaxesCache = function () {
245
- try {
246
- localStorage.removeItem(type_1.LS_KEYS.TAXES);
247
- }
248
- catch (error) {
249
- console.error('Error invalidating taxes cache:', error);
250
- }
251
- };
252
- exports.invalidateTaxesCache = invalidateTaxesCache;
253
- /**
254
- * Utility function to preload taxes into cache
255
- * Useful to call on app initialization or login
256
- *
257
- * @returns Promise<{count:number, items: TaxBE[]}> - Array of preloaded taxes
258
- *
259
- * @example
260
- * // On app initialization
261
- * await preloadTaxes();
262
- */
263
- var preloadTaxes = function () { return __awaiter(void 0, void 0, void 0, function () {
264
- return __generator(this, function (_a) {
265
- return [2 /*return*/, (0, exports.getCachedTaxes)()];
266
- });
267
- }); };
268
- exports.preloadTaxes = preloadTaxes;
269
- /**
270
- * Utility function to check if taxes cache is stale
271
- * Returns true if cache is older than 1 week or doesn't exist
272
- *
273
- * @returns boolean - True if cache is stale or doesn't exist
274
- *
275
- * @example
276
- * if (isTaxesCacheStale()) {
277
- * await getCachedTaxes(); // This will fetch fresh data
278
- * }
279
- */
280
- var isTaxesCacheStale = function () {
281
- try {
282
- var cachedData = (0, get_storage_value_1.getStorageValue)(type_1.LS_KEYS.TAXES);
283
- if (!cachedData) {
284
- return true;
285
- }
286
- var currentTime = new Date().getTime();
287
- var cachedTime = new Date(cachedData.cachedAt).getTime();
288
- var ageInMs = currentTime - cachedTime;
289
- return ageInMs >= constants_2.ONE_WEEK_IN_MS;
290
- }
291
- catch (error) {
292
- console.error('Error checking taxes cache staleness:', error);
293
- return true;
294
- }
295
- };
296
- exports.isTaxesCacheStale = isTaxesCacheStale;
1
+ // import { API_ROUTES } from '../constants';
2
+ // import { TaxBE, LS_KEYS } from '../type';
3
+ // import { API_METHODS } from '../../../constants/api-methods';
4
+ // import { fetchData } from '../../../general/fetch-data';
5
+ // import { getStorageValue } from '../../../local-storage/get-storage-value';
6
+ // import { setStorageValue } from '../../../local-storage/set-storage-value';
7
+ // import { ONE_WEEK_IN_MS } from '../../../constants';
8
+ // interface CachedTaxes {
9
+ // taxes: TaxBE[];
10
+ // cachedAt: string;
11
+ // }
12
+ // /**
13
+ // * Synchronous utility function to get taxes from cache only
14
+ // * Returns cached data immediately if available and fresh, otherwise returns empty array
15
+ // * Does not trigger any API calls
16
+ // *
17
+ // * @returns TaxBE[] - Array of cached taxes or empty array wrapper ({count, items})
18
+ // *
19
+ // * @example
20
+ // * const taxes = getCachedTaxesSync();
21
+ // * if (taxes.items.length > 0) {
22
+ // * console.log(taxes.items[0].taxName);
23
+ // * }
24
+ // */
25
+ // export const getCachedTaxesSync = (): {
26
+ // count: number;
27
+ // items: TaxBE[];
28
+ // } => {
29
+ // try {
30
+ // const cachedData = getStorageValue(LS_KEYS.TAXES) as CachedTaxes | null;
31
+ // if (!cachedData) {
32
+ // return { count: 0, items: [] };
33
+ // }
34
+ // const currentTime = new Date().getTime();
35
+ // const cachedTime = new Date(cachedData.cachedAt).getTime();
36
+ // const ageInMs = currentTime - cachedTime;
37
+ // // If cached data is less than 1 week old, return it
38
+ // if (ageInMs < ONE_WEEK_IN_MS) {
39
+ // return {
40
+ // count: cachedData.taxes.length,
41
+ // items: cachedData.taxes,
42
+ // };
43
+ // }
44
+ // return { count: 0, items: [] };
45
+ // } catch (error) {
46
+ // console.error('Error getting cached taxes:', error);
47
+ // return { count: 0, items: [] };
48
+ // }
49
+ // };
50
+ // /**
51
+ // * Utility function to get taxes from cache or fetch from API
52
+ // *
53
+ // * This function manages a localStorage cache of taxes with the following logic:
54
+ // * - If taxes exist in cache and are less than 1 week old, return cached version
55
+ // * - If taxes exist but are older than 1 week, fetch fresh data and update cache
56
+ // * - If taxes don't exist in cache, fetch from API and cache them
57
+ // *
58
+ // * @param searchQuery - Optional search query to filter taxes
59
+ // * @param pageLimit - Number of taxes to fetch (default: 100)
60
+ // * @returns Promise<{count:number, items: TaxBE[]}> - Paged taxes
61
+ // *
62
+ // * @example
63
+ // * const taxes = await getCachedTaxes();
64
+ // * console.log(taxes.items[0].taxName);
65
+ // *
66
+ // * // With search
67
+ // * const filtered = await getCachedTaxes('VAT');
68
+ // */
69
+ // export const getCachedTaxes = async (
70
+ // searchQuery?: string,
71
+ // pageLimit: number = 100
72
+ // ): Promise<{
73
+ // count: number;
74
+ // items: TaxBE[];
75
+ // }> => {
76
+ // try {
77
+ // // If there's a search query, always fetch fresh data (don't use cache)
78
+ // if (searchQuery && searchQuery.trim()) {
79
+ // const response = await fetchData({
80
+ // url: API_ROUTES.TAXES,
81
+ // body: JSON.stringify({
82
+ // searchQuery,
83
+ // pageLimit,
84
+ // currentPage: 1,
85
+ // }),
86
+ // method: API_METHODS.POST,
87
+ // });
88
+ // return response?.data || { count: 0, items: [] };
89
+ // }
90
+ // // Get the cached data from localStorage
91
+ // const cachedData = getStorageValue(LS_KEYS.TAXES) as CachedTaxes | null;
92
+ // const currentTime = new Date().getTime();
93
+ // // Check if cached data exists and is still fresh
94
+ // if (cachedData) {
95
+ // const cachedTime = new Date(cachedData.cachedAt).getTime();
96
+ // const ageInMs = currentTime - cachedTime;
97
+ // // If cached data is less than 1 week old, return it
98
+ // if (ageInMs < ONE_WEEK_IN_MS) {
99
+ // return {
100
+ // count: cachedData.taxes.length,
101
+ // items: cachedData.taxes,
102
+ // };
103
+ // }
104
+ // }
105
+ // // If no cached data or data is older than 1 week, fetch fresh data
106
+ // const response = await fetchData({
107
+ // url: API_ROUTES.TAXES,
108
+ // body: JSON.stringify({
109
+ // pageLimit,
110
+ // currentPage: 1,
111
+ // }),
112
+ // method: API_METHODS.POST,
113
+ // });
114
+ // if (response?.data?.items) {
115
+ // // Update the cache with fresh data
116
+ // const updatedCache: CachedTaxes = {
117
+ // taxes: response.data.items,
118
+ // cachedAt: new Date().toISOString(),
119
+ // };
120
+ // setStorageValue(LS_KEYS.TAXES, updatedCache);
121
+ // return response.data;
122
+ // }
123
+ // return { count: 0, items: [] };
124
+ // } catch (error) {
125
+ // console.error('Error fetching taxes:', error);
126
+ // return { count: 0, items: [] };
127
+ // }
128
+ // };
129
+ // /**
130
+ // * Utility function to get a specific tax by ID from cache
131
+ // * If not found in cache, returns null (does not trigger API call)
132
+ // *
133
+ // * @param taxId - The ID of the tax to retrieve
134
+ // * @returns TaxBE | null - The tax or null if not found
135
+ // *
136
+ // * @example
137
+ // * const tax = getCachedTaxById('tax-123');
138
+ // * if (tax) {
139
+ // * console.log(tax.taxName);
140
+ // * }
141
+ // */
142
+ // export const getCachedTaxById = (taxId: string): TaxBE | null => {
143
+ // if (!taxId) {
144
+ // return null;
145
+ // }
146
+ // try {
147
+ // const taxes = getCachedTaxesSync().items;
148
+ // return taxes.find((tax) => tax.id === taxId) || null;
149
+ // } catch (error) {
150
+ // console.error('Error getting cached tax by ID:', error);
151
+ // return null;
152
+ // }
153
+ // };
154
+ // /**
155
+ // * Utility function to get taxes by name from cache
156
+ // * If not found in cache, returns empty array (does not trigger API call)
157
+ // *
158
+ // * @param taxName - The name of the tax to search for
159
+ // * @returns TaxBE[] - Array of matching taxes or empty array
160
+ // *
161
+ // * @example
162
+ // * const taxes = getCachedTaxesByName('VAT');
163
+ // * console.log(taxes.length);
164
+ // */
165
+ // export const getCachedTaxesByName = (taxName: string): TaxBE[] => {
166
+ // if (!taxName) {
167
+ // return [];
168
+ // }
169
+ // try {
170
+ // const taxes = getCachedTaxesSync().items;
171
+ // return taxes.filter((tax) =>
172
+ // tax.taxName.toLowerCase().includes(taxName.toLowerCase())
173
+ // );
174
+ // } catch (error) {
175
+ // console.error('Error getting cached taxes by name:', error);
176
+ // return [];
177
+ // }
178
+ // };
179
+ // /**
180
+ // * Utility function to invalidate (remove) taxes from cache
181
+ // * Useful when taxes have been updated and you want to force a refresh
182
+ // *
183
+ // * @example
184
+ // * invalidateTaxesCache();
185
+ // * const freshTaxes = await getCachedTaxes();
186
+ // */
187
+ // export const invalidateTaxesCache = (): void => {
188
+ // try {
189
+ // localStorage.removeItem(LS_KEYS.TAXES);
190
+ // } catch (error) {
191
+ // console.error('Error invalidating taxes cache:', error);
192
+ // }
193
+ // };
194
+ // /**
195
+ // * Utility function to preload taxes into cache
196
+ // * Useful to call on app initialization or login
197
+ // *
198
+ // * @returns Promise<{count:number, items: TaxBE[]}> - Array of preloaded taxes
199
+ // *
200
+ // * @example
201
+ // * // On app initialization
202
+ // * await preloadTaxes();
203
+ // */
204
+ // export const preloadTaxes = async (): Promise<{
205
+ // count: number;
206
+ // items: TaxBE[];
207
+ // }> => {
208
+ // return getCachedTaxes();
209
+ // };
210
+ // /**
211
+ // * Utility function to check if taxes cache is stale
212
+ // * Returns true if cache is older than 1 week or doesn't exist
213
+ // *
214
+ // * @returns boolean - True if cache is stale or doesn't exist
215
+ // *
216
+ // * @example
217
+ // * if (isTaxesCacheStale()) {
218
+ // * await getCachedTaxes(); // This will fetch fresh data
219
+ // * }
220
+ // */
221
+ // export const isTaxesCacheStale = (): boolean => {
222
+ // try {
223
+ // const cachedData = getStorageValue(LS_KEYS.TAXES) as CachedTaxes | null;
224
+ // if (!cachedData) {
225
+ // return true;
226
+ // }
227
+ // const currentTime = new Date().getTime();
228
+ // const cachedTime = new Date(cachedData.cachedAt).getTime();
229
+ // const ageInMs = currentTime - cachedTime;
230
+ // return ageInMs >= ONE_WEEK_IN_MS;
231
+ // } catch (error) {
232
+ // console.error('Error checking taxes cache staleness:', error);
233
+ // return true;
234
+ // }
235
+ // };