@react-pakistan/util-functions 1.25.8 → 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.
- package/api/stellar-solutions/bank/cache.d.ts +0 -104
- package/api/stellar-solutions/bank/cache.js +237 -295
- package/api/stellar-solutions/bank/index.d.ts +1 -1
- package/api/stellar-solutions/bank/index.js +0 -15
- package/api/stellar-solutions/branch/cache.d.ts +0 -102
- package/api/stellar-solutions/branch/cache.js +241 -295
- package/api/stellar-solutions/branch/index.d.ts +1 -1
- package/api/stellar-solutions/branch/index.js +0 -15
- package/api/stellar-solutions/company/cache.d.ts +0 -106
- package/api/stellar-solutions/company/cache.js +247 -307
- package/api/stellar-solutions/company/index.d.ts +1 -1
- package/api/stellar-solutions/company/index.js +0 -15
- package/api/stellar-solutions/constants.d.ts +0 -2
- package/api/stellar-solutions/constants.js +1 -3
- package/api/stellar-solutions/contact/cache.d.ts +0 -108
- package/api/stellar-solutions/contact/cache.js +247 -307
- package/api/stellar-solutions/contact/index.d.ts +1 -1
- package/api/stellar-solutions/contact/index.js +0 -15
- package/api/stellar-solutions/currency/cache.d.ts +0 -104
- package/api/stellar-solutions/currency/cache.js +243 -295
- package/api/stellar-solutions/currency/index.d.ts +1 -1
- package/api/stellar-solutions/currency/index.js +0 -15
- package/api/stellar-solutions/customer/cache.d.ts +0 -108
- package/api/stellar-solutions/customer/cache.js +249 -307
- package/api/stellar-solutions/customer/index.d.ts +1 -1
- package/api/stellar-solutions/customer/index.js +0 -15
- package/api/stellar-solutions/expense/cache.d.ts +0 -106
- package/api/stellar-solutions/expense/cache.js +247 -307
- package/api/stellar-solutions/expense/index.d.ts +1 -1
- package/api/stellar-solutions/expense/index.js +0 -15
- package/api/stellar-solutions/expense-category/cache.d.ts +0 -94
- package/api/stellar-solutions/expense-category/cache.js +224 -280
- package/api/stellar-solutions/expense-category/index.d.ts +1 -1
- package/api/stellar-solutions/expense-category/index.js +0 -15
- package/api/stellar-solutions/payment-mode/cache.d.ts +0 -102
- package/api/stellar-solutions/payment-mode/cache.js +245 -295
- package/api/stellar-solutions/payment-mode/index.d.ts +1 -1
- package/api/stellar-solutions/payment-mode/index.js +0 -15
- package/api/stellar-solutions/preference/cache.d.ts +0 -90
- package/api/stellar-solutions/preference/cache.js +218 -268
- package/api/stellar-solutions/preference/index.d.ts +1 -1
- package/api/stellar-solutions/preference/index.js +0 -15
- package/api/stellar-solutions/product/cache.d.ts +0 -94
- package/api/stellar-solutions/product/cache.js +222 -280
- package/api/stellar-solutions/product/index.d.ts +1 -1
- package/api/stellar-solutions/product/index.js +0 -15
- package/api/stellar-solutions/product-category/cache.d.ts +0 -94
- package/api/stellar-solutions/product-category/cache.js +224 -280
- package/api/stellar-solutions/product-category/index.d.ts +1 -1
- package/api/stellar-solutions/product-category/index.js +0 -15
- package/api/stellar-solutions/tax/cache.d.ts +0 -102
- package/api/stellar-solutions/tax/cache.js +235 -295
- package/api/stellar-solutions/tax/index.d.ts +1 -1
- package/api/stellar-solutions/tax/index.js +0 -15
- package/constants/cache-time.d.ts +3 -0
- package/constants/cache-time.js +6 -0
- package/constants/index.d.ts +2 -1
- package/constants/index.js +2 -1
- package/general/generic-cache.d.ts +143 -0
- package/general/generic-cache.js +411 -0
- package/general/index.d.ts +1 -0
- package/general/index.js +1 -0
- package/package.json +1 -1
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { ExpenseCategoryBE } from '../type';
|
|
2
|
-
/**
|
|
3
|
-
* Synchronous utility function to get expense categories 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 ExpenseCategoryBE[] - Array of cached expense categories or empty array wrapper ({count, items})
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const expenseCategories = getCachedExpenseCategoriesSync();
|
|
11
|
-
* if (expenseCategories.items.length > 0) {
|
|
12
|
-
* console.log(expenseCategories.items[0].name);
|
|
13
|
-
* }
|
|
14
|
-
*/
|
|
15
|
-
export declare const getCachedExpenseCategoriesSync: () => {
|
|
16
|
-
count: number;
|
|
17
|
-
items: ExpenseCategoryBE[];
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Utility function to get expense categories from cache or fetch from API
|
|
21
|
-
*
|
|
22
|
-
* This function manages a localStorage cache of expense categories with the following logic:
|
|
23
|
-
* - If expense categories exist in cache and are less than 1 day old, return cached version
|
|
24
|
-
* - If expense categories exist but are older than 1 day, fetch fresh data and update cache
|
|
25
|
-
* - If expense categories don't exist in cache, fetch from API and cache them
|
|
26
|
-
*
|
|
27
|
-
* @param searchQuery - Optional search query to filter expense categories
|
|
28
|
-
* @param filters - Optional filters object to apply additional filtering (bypasses cache)
|
|
29
|
-
* @param pageLimit - Number of expense categories to fetch (default: 100)
|
|
30
|
-
* @returns Promise<{count:number, items: ExpenseCategoryBE[]}> - Paged expense categories
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* const expenseCategories = await getCachedExpenseCategories();
|
|
34
|
-
* console.log(expenseCategories.items[0].name);
|
|
35
|
-
*
|
|
36
|
-
* // With search
|
|
37
|
-
* const filtered = await getCachedExpenseCategories('Travel');
|
|
38
|
-
*
|
|
39
|
-
* // With filters
|
|
40
|
-
* const filteredByColor = await getCachedExpenseCategories(undefined, { color: '#FF0000' });
|
|
41
|
-
*/
|
|
42
|
-
export declare const getCachedExpenseCategories: (searchQuery?: string, filters?: Record<string, unknown>, pageLimit?: number) => Promise<{
|
|
43
|
-
count: number;
|
|
44
|
-
items: ExpenseCategoryBE[];
|
|
45
|
-
}>;
|
|
46
|
-
/**
|
|
47
|
-
* Utility function to get a specific expense category by ID from cache
|
|
48
|
-
* If not found in cache, returns null (does not trigger API call)
|
|
49
|
-
*
|
|
50
|
-
* @param categoryId - The ID of the expense category to retrieve
|
|
51
|
-
* @returns ExpenseCategoryBE | null - The expense category or null if not found
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* const category = getCachedExpenseCategoryById('cat-123');
|
|
55
|
-
* if (category) {
|
|
56
|
-
* console.log(category.name);
|
|
57
|
-
* }
|
|
58
|
-
*/
|
|
59
|
-
export declare const getCachedExpenseCategoryById: (categoryId: string) => ExpenseCategoryBE | null;
|
|
60
|
-
/**
|
|
61
|
-
* Utility function to invalidate (remove) expense categories from cache
|
|
62
|
-
* Useful when expense categories have been updated and you want to force a refresh
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* invalidateExpenseCategoriesCache();
|
|
66
|
-
* const freshCategories = await getCachedExpenseCategories();
|
|
67
|
-
*/
|
|
68
|
-
export declare const invalidateExpenseCategoriesCache: () => void;
|
|
69
|
-
/**
|
|
70
|
-
* Utility function to preload expense categories into cache
|
|
71
|
-
* Useful to call on app initialization or login
|
|
72
|
-
*
|
|
73
|
-
* @returns Promise<{count:number, items: ExpenseCategoryBE[]}> - Array of preloaded expense categories
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* // On app initialization
|
|
77
|
-
* await preloadExpenseCategories();
|
|
78
|
-
*/
|
|
79
|
-
export declare const preloadExpenseCategories: () => Promise<{
|
|
80
|
-
count: number;
|
|
81
|
-
items: ExpenseCategoryBE[];
|
|
82
|
-
}>;
|
|
83
|
-
/**
|
|
84
|
-
* Utility function to check if expense categories cache is stale
|
|
85
|
-
* Returns true if cache is older than 1 day or doesn't exist
|
|
86
|
-
*
|
|
87
|
-
* @returns boolean - True if cache is stale or doesn't exist
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* if (isExpenseCategoriesCacheStale()) {
|
|
91
|
-
* await getCachedExpenseCategories(); // This will fetch fresh data
|
|
92
|
-
* }
|
|
93
|
-
*/
|
|
94
|
-
export declare const isExpenseCategoriesCacheStale: () => boolean;
|
|
@@ -1,280 +1,224 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
* invalidateExpenseCategoriesCache();
|
|
226
|
-
* const freshCategories = await getCachedExpenseCategories();
|
|
227
|
-
*/
|
|
228
|
-
var invalidateExpenseCategoriesCache = function () {
|
|
229
|
-
try {
|
|
230
|
-
localStorage.removeItem(type_1.LS_KEYS.EXPENSE_CATEGORIES);
|
|
231
|
-
}
|
|
232
|
-
catch (error) {
|
|
233
|
-
console.error('Error invalidating expense categories cache:', error);
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
exports.invalidateExpenseCategoriesCache = invalidateExpenseCategoriesCache;
|
|
237
|
-
/**
|
|
238
|
-
* Utility function to preload expense categories into cache
|
|
239
|
-
* Useful to call on app initialization or login
|
|
240
|
-
*
|
|
241
|
-
* @returns Promise<{count:number, items: ExpenseCategoryBE[]}> - Array of preloaded expense categories
|
|
242
|
-
*
|
|
243
|
-
* @example
|
|
244
|
-
* // On app initialization
|
|
245
|
-
* await preloadExpenseCategories();
|
|
246
|
-
*/
|
|
247
|
-
var preloadExpenseCategories = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
248
|
-
return __generator(this, function (_a) {
|
|
249
|
-
return [2 /*return*/, (0, exports.getCachedExpenseCategories)()];
|
|
250
|
-
});
|
|
251
|
-
}); };
|
|
252
|
-
exports.preloadExpenseCategories = preloadExpenseCategories;
|
|
253
|
-
/**
|
|
254
|
-
* Utility function to check if expense categories cache is stale
|
|
255
|
-
* Returns true if cache is older than 1 day or doesn't exist
|
|
256
|
-
*
|
|
257
|
-
* @returns boolean - True if cache is stale or doesn't exist
|
|
258
|
-
*
|
|
259
|
-
* @example
|
|
260
|
-
* if (isExpenseCategoriesCacheStale()) {
|
|
261
|
-
* await getCachedExpenseCategories(); // This will fetch fresh data
|
|
262
|
-
* }
|
|
263
|
-
*/
|
|
264
|
-
var isExpenseCategoriesCacheStale = function () {
|
|
265
|
-
try {
|
|
266
|
-
var cachedData = (0, get_storage_value_1.getStorageValue)(type_1.LS_KEYS.EXPENSE_CATEGORIES);
|
|
267
|
-
if (!cachedData) {
|
|
268
|
-
return true;
|
|
269
|
-
}
|
|
270
|
-
var currentTime = new Date().getTime();
|
|
271
|
-
var cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
272
|
-
var ageInMs = currentTime - cachedTime;
|
|
273
|
-
return ageInMs >= constants_1.ONE_DAY_IN_MS;
|
|
274
|
-
}
|
|
275
|
-
catch (error) {
|
|
276
|
-
console.error('Error checking expense categories cache staleness:', error);
|
|
277
|
-
return true;
|
|
278
|
-
}
|
|
279
|
-
};
|
|
280
|
-
exports.isExpenseCategoriesCacheStale = isExpenseCategoriesCacheStale;
|
|
1
|
+
// import { API_ROUTES } from '../constants';
|
|
2
|
+
// import { ExpenseCategoryBE, 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_DAY_IN_MS } from '../../../constants';
|
|
8
|
+
// interface CachedExpenseCategories {
|
|
9
|
+
// expenseCategories: ExpenseCategoryBE[];
|
|
10
|
+
// cachedAt: string;
|
|
11
|
+
// }
|
|
12
|
+
// /**
|
|
13
|
+
// * Synchronous utility function to get expense categories 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 ExpenseCategoryBE[] - Array of cached expense categories or empty array wrapper ({count, items})
|
|
18
|
+
// *
|
|
19
|
+
// * @example
|
|
20
|
+
// * const expenseCategories = getCachedExpenseCategoriesSync();
|
|
21
|
+
// * if (expenseCategories.items.length > 0) {
|
|
22
|
+
// * console.log(expenseCategories.items[0].name);
|
|
23
|
+
// * }
|
|
24
|
+
// */
|
|
25
|
+
// export const getCachedExpenseCategoriesSync = (): {
|
|
26
|
+
// count: number;
|
|
27
|
+
// items: ExpenseCategoryBE[];
|
|
28
|
+
// } => {
|
|
29
|
+
// try {
|
|
30
|
+
// const cachedData = getStorageValue(
|
|
31
|
+
// LS_KEYS.EXPENSE_CATEGORIES
|
|
32
|
+
// ) as CachedExpenseCategories | null;
|
|
33
|
+
// if (!cachedData) {
|
|
34
|
+
// return { count: 0, items: [] };
|
|
35
|
+
// }
|
|
36
|
+
// const currentTime = new Date().getTime();
|
|
37
|
+
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
38
|
+
// const ageInMs = currentTime - cachedTime;
|
|
39
|
+
// // If cached data is less than 1 day old, return it
|
|
40
|
+
// if (ageInMs < ONE_DAY_IN_MS) {
|
|
41
|
+
// return {
|
|
42
|
+
// count: cachedData.expenseCategories.length,
|
|
43
|
+
// items: cachedData.expenseCategories,
|
|
44
|
+
// };
|
|
45
|
+
// }
|
|
46
|
+
// return { count: 0, items: [] };
|
|
47
|
+
// } catch (error) {
|
|
48
|
+
// console.error('Error getting cached expense categories:', error);
|
|
49
|
+
// return { count: 0, items: [] };
|
|
50
|
+
// }
|
|
51
|
+
// };
|
|
52
|
+
// /**
|
|
53
|
+
// * Utility function to get expense categories from cache or fetch from API
|
|
54
|
+
// *
|
|
55
|
+
// * This function manages a localStorage cache of expense categories with the following logic:
|
|
56
|
+
// * - If expense categories exist in cache and are less than 1 day old, return cached version
|
|
57
|
+
// * - If expense categories exist but are older than 1 day, fetch fresh data and update cache
|
|
58
|
+
// * - If expense categories don't exist in cache, fetch from API and cache them
|
|
59
|
+
// *
|
|
60
|
+
// * @param searchQuery - Optional search query to filter expense categories
|
|
61
|
+
// * @param filters - Optional filters object to apply additional filtering (bypasses cache)
|
|
62
|
+
// * @param pageLimit - Number of expense categories to fetch (default: 100)
|
|
63
|
+
// * @returns Promise<{count:number, items: ExpenseCategoryBE[]}> - Paged expense categories
|
|
64
|
+
// *
|
|
65
|
+
// * @example
|
|
66
|
+
// * const expenseCategories = await getCachedExpenseCategories();
|
|
67
|
+
// * console.log(expenseCategories.items[0].name);
|
|
68
|
+
// *
|
|
69
|
+
// * // With search
|
|
70
|
+
// * const filtered = await getCachedExpenseCategories('Travel');
|
|
71
|
+
// *
|
|
72
|
+
// * // With filters
|
|
73
|
+
// * const filteredByColor = await getCachedExpenseCategories(undefined, { color: '#FF0000' });
|
|
74
|
+
// */
|
|
75
|
+
// export const getCachedExpenseCategories = async (
|
|
76
|
+
// searchQuery?: string,
|
|
77
|
+
// filters?: Record<string, unknown>,
|
|
78
|
+
// pageLimit: number = 100
|
|
79
|
+
// ): Promise<{ count: number; items: ExpenseCategoryBE[] }> => {
|
|
80
|
+
// try {
|
|
81
|
+
// // If there's a search query or filters, always fetch fresh data (don't use cache)
|
|
82
|
+
// if (
|
|
83
|
+
// (searchQuery && searchQuery.trim()) ||
|
|
84
|
+
// (filters && Object.keys(filters).length > 0)
|
|
85
|
+
// ) {
|
|
86
|
+
// const response = await fetchData({
|
|
87
|
+
// url: API_ROUTES.EXPENSE_CATEGORIES,
|
|
88
|
+
// body: JSON.stringify({
|
|
89
|
+
// searchQuery,
|
|
90
|
+
// ...filters,
|
|
91
|
+
// pageLimit,
|
|
92
|
+
// currentPage: 1,
|
|
93
|
+
// }),
|
|
94
|
+
// method: API_METHODS.POST,
|
|
95
|
+
// });
|
|
96
|
+
// return response?.data || { count: 0, items: [] };
|
|
97
|
+
// }
|
|
98
|
+
// // Get the cached data from localStorage
|
|
99
|
+
// const cachedData = getStorageValue(
|
|
100
|
+
// LS_KEYS.EXPENSE_CATEGORIES
|
|
101
|
+
// ) as CachedExpenseCategories | null;
|
|
102
|
+
// const currentTime = new Date().getTime();
|
|
103
|
+
// // Check if cached data exists and is still fresh
|
|
104
|
+
// if (cachedData) {
|
|
105
|
+
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
106
|
+
// const ageInMs = currentTime - cachedTime;
|
|
107
|
+
// // If cached data is less than 1 day old, return it
|
|
108
|
+
// if (ageInMs < ONE_DAY_IN_MS) {
|
|
109
|
+
// return {
|
|
110
|
+
// count: cachedData.expenseCategories.length,
|
|
111
|
+
// items: cachedData.expenseCategories,
|
|
112
|
+
// };
|
|
113
|
+
// }
|
|
114
|
+
// }
|
|
115
|
+
// // If no cached data or data is older than 1 day, fetch fresh data
|
|
116
|
+
// const response = await fetchData({
|
|
117
|
+
// url: API_ROUTES.EXPENSE_CATEGORIES,
|
|
118
|
+
// body: JSON.stringify({
|
|
119
|
+
// pageLimit,
|
|
120
|
+
// currentPage: 1,
|
|
121
|
+
// }),
|
|
122
|
+
// method: API_METHODS.POST,
|
|
123
|
+
// });
|
|
124
|
+
// if (response?.data?.items) {
|
|
125
|
+
// // Update the cache with fresh data
|
|
126
|
+
// const updatedCache: CachedExpenseCategories = {
|
|
127
|
+
// expenseCategories: response.data.items,
|
|
128
|
+
// cachedAt: new Date().toISOString(),
|
|
129
|
+
// };
|
|
130
|
+
// setStorageValue(LS_KEYS.EXPENSE_CATEGORIES, updatedCache);
|
|
131
|
+
// return response.data;
|
|
132
|
+
// }
|
|
133
|
+
// return { count: 0, items: [] };
|
|
134
|
+
// } catch (error) {
|
|
135
|
+
// console.error('Error fetching expense categories:', error);
|
|
136
|
+
// return { count: 0, items: [] };
|
|
137
|
+
// }
|
|
138
|
+
// };
|
|
139
|
+
// /**
|
|
140
|
+
// * Utility function to get a specific expense category by ID from cache
|
|
141
|
+
// * If not found in cache, returns null (does not trigger API call)
|
|
142
|
+
// *
|
|
143
|
+
// * @param categoryId - The ID of the expense category to retrieve
|
|
144
|
+
// * @returns ExpenseCategoryBE | null - The expense category or null if not found
|
|
145
|
+
// *
|
|
146
|
+
// * @example
|
|
147
|
+
// * const category = getCachedExpenseCategoryById('cat-123');
|
|
148
|
+
// * if (category) {
|
|
149
|
+
// * console.log(category.name);
|
|
150
|
+
// * }
|
|
151
|
+
// */
|
|
152
|
+
// export const getCachedExpenseCategoryById = (
|
|
153
|
+
// categoryId: string
|
|
154
|
+
// ): ExpenseCategoryBE | null => {
|
|
155
|
+
// if (!categoryId) {
|
|
156
|
+
// return null;
|
|
157
|
+
// }
|
|
158
|
+
// try {
|
|
159
|
+
// const categories = getCachedExpenseCategoriesSync().items;
|
|
160
|
+
// return categories.find((cat) => cat.id === categoryId) || null;
|
|
161
|
+
// } catch (error) {
|
|
162
|
+
// console.error('Error getting cached expense category by ID:', error);
|
|
163
|
+
// return null;
|
|
164
|
+
// }
|
|
165
|
+
// };
|
|
166
|
+
// /**
|
|
167
|
+
// * Utility function to invalidate (remove) expense categories from cache
|
|
168
|
+
// * Useful when expense categories have been updated and you want to force a refresh
|
|
169
|
+
// *
|
|
170
|
+
// * @example
|
|
171
|
+
// * invalidateExpenseCategoriesCache();
|
|
172
|
+
// * const freshCategories = await getCachedExpenseCategories();
|
|
173
|
+
// */
|
|
174
|
+
// export const invalidateExpenseCategoriesCache = (): void => {
|
|
175
|
+
// try {
|
|
176
|
+
// localStorage.removeItem(LS_KEYS.EXPENSE_CATEGORIES);
|
|
177
|
+
// } catch (error) {
|
|
178
|
+
// console.error('Error invalidating expense categories cache:', error);
|
|
179
|
+
// }
|
|
180
|
+
// };
|
|
181
|
+
// /**
|
|
182
|
+
// * Utility function to preload expense categories into cache
|
|
183
|
+
// * Useful to call on app initialization or login
|
|
184
|
+
// *
|
|
185
|
+
// * @returns Promise<{count:number, items: ExpenseCategoryBE[]}> - Array of preloaded expense categories
|
|
186
|
+
// *
|
|
187
|
+
// * @example
|
|
188
|
+
// * // On app initialization
|
|
189
|
+
// * await preloadExpenseCategories();
|
|
190
|
+
// */
|
|
191
|
+
// export const preloadExpenseCategories = async (): Promise<{
|
|
192
|
+
// count: number;
|
|
193
|
+
// items: ExpenseCategoryBE[];
|
|
194
|
+
// }> => {
|
|
195
|
+
// return getCachedExpenseCategories();
|
|
196
|
+
// };
|
|
197
|
+
// /**
|
|
198
|
+
// * Utility function to check if expense categories cache is stale
|
|
199
|
+
// * Returns true if cache is older than 1 day or doesn't exist
|
|
200
|
+
// *
|
|
201
|
+
// * @returns boolean - True if cache is stale or doesn't exist
|
|
202
|
+
// *
|
|
203
|
+
// * @example
|
|
204
|
+
// * if (isExpenseCategoriesCacheStale()) {
|
|
205
|
+
// * await getCachedExpenseCategories(); // This will fetch fresh data
|
|
206
|
+
// * }
|
|
207
|
+
// */
|
|
208
|
+
// export const isExpenseCategoriesCacheStale = (): boolean => {
|
|
209
|
+
// try {
|
|
210
|
+
// const cachedData = getStorageValue(
|
|
211
|
+
// LS_KEYS.EXPENSE_CATEGORIES
|
|
212
|
+
// ) as CachedExpenseCategories | null;
|
|
213
|
+
// if (!cachedData) {
|
|
214
|
+
// return true;
|
|
215
|
+
// }
|
|
216
|
+
// const currentTime = new Date().getTime();
|
|
217
|
+
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
218
|
+
// const ageInMs = currentTime - cachedTime;
|
|
219
|
+
// return ageInMs >= ONE_DAY_IN_MS;
|
|
220
|
+
// } catch (error) {
|
|
221
|
+
// console.error('Error checking expense categories cache staleness:', error);
|
|
222
|
+
// return true;
|
|
223
|
+
// }
|
|
224
|
+
// };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export * from './cache';
|
|
2
1
|
import { ExpenseCategoryBE } from '../type';
|
|
3
2
|
type PrismaClient = any;
|
|
4
3
|
export interface ListExpenseCategoryArgs {
|
|
@@ -51,3 +50,4 @@ export declare const updateExpenseCategory: ({ color, description, id, name, pri
|
|
|
51
50
|
* @throws {Error} If expense category ID is not provided or expense category not found
|
|
52
51
|
*/
|
|
53
52
|
export declare const deleteExpenseCategory: ({ id, prisma, }: DeleteExpenseCategoryArgs) => Promise<ExpenseCategoryBE>;
|
|
53
|
+
export {};
|