@react-pakistan/util-functions 1.25.48 → 1.25.50
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/app-user/index.d.ts +1 -1
- package/api/stellar-solutions/bank/index.d.ts +1 -1
- package/api/stellar-solutions/branch/index.d.ts +1 -1
- package/api/stellar-solutions/company/index.d.ts +1 -1
- package/api/stellar-solutions/company-report/index.d.ts +1 -1
- package/api/stellar-solutions/contact/index.d.ts +1 -1
- package/api/stellar-solutions/currency/index.d.ts +1 -1
- package/api/stellar-solutions/customer/index.d.ts +1 -1
- package/api/stellar-solutions/expense/index.d.ts +1 -1
- package/api/stellar-solutions/expense-category/index.d.ts +1 -1
- package/api/stellar-solutions/lead/index.d.ts +1 -1
- package/api/stellar-solutions/menu-order/index.d.ts +1 -1
- package/api/stellar-solutions/payment/index.d.ts +1 -1
- package/api/stellar-solutions/payment/index.js +2 -2
- package/api/stellar-solutions/payment-mode/index.d.ts +1 -1
- package/api/stellar-solutions/preference/index.d.ts +1 -1
- package/api/stellar-solutions/product/index.d.ts +1 -1
- package/api/stellar-solutions/product-category/index.d.ts +1 -1
- package/api/stellar-solutions/profile/index.d.ts +1 -1
- package/api/stellar-solutions/quote-invoice/index.d.ts +1 -1
- package/api/stellar-solutions/quote-invoice-report/index.d.ts +1 -1
- package/api/stellar-solutions/tax/index.d.ts +1 -1
- package/api/stellar-solutions/types/common.d.ts +24 -0
- package/api/stellar-solutions/types/common.js +22 -0
- package/api/stellar-solutions/{type.d.ts → types/erp.d.ts} +1 -199
- package/api/stellar-solutions/types/erp.js +68 -0
- package/api/stellar-solutions/types/index.d.ts +3 -0
- package/api/stellar-solutions/types/index.js +19 -0
- package/api/stellar-solutions/types/pos.d.ts +176 -0
- package/api/stellar-solutions/types/pos.js +61 -0
- package/general/index.d.ts +0 -1
- package/general/index.js +0 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +1 -1
- package/api/stellar-solutions/bank/cache.d.ts +0 -0
- package/api/stellar-solutions/bank/cache.js +0 -237
- package/api/stellar-solutions/branch/cache.d.ts +0 -0
- package/api/stellar-solutions/branch/cache.js +0 -241
- package/api/stellar-solutions/company/cache.d.ts +0 -0
- package/api/stellar-solutions/company/cache.js +0 -247
- package/api/stellar-solutions/contact/cache.d.ts +0 -0
- package/api/stellar-solutions/contact/cache.js +0 -247
- package/api/stellar-solutions/currency/cache.d.ts +0 -0
- package/api/stellar-solutions/currency/cache.js +0 -243
- package/api/stellar-solutions/customer/cache.d.ts +0 -0
- package/api/stellar-solutions/customer/cache.js +0 -249
- package/api/stellar-solutions/expense/cache.d.ts +0 -0
- package/api/stellar-solutions/expense/cache.js +0 -247
- package/api/stellar-solutions/expense-category/cache.d.ts +0 -0
- package/api/stellar-solutions/expense-category/cache.js +0 -224
- package/api/stellar-solutions/payment-mode/cache.d.ts +0 -0
- package/api/stellar-solutions/payment-mode/cache.js +0 -245
- package/api/stellar-solutions/preference/cache.d.ts +0 -0
- package/api/stellar-solutions/preference/cache.js +0 -218
- package/api/stellar-solutions/product/cache.d.ts +0 -0
- package/api/stellar-solutions/product/cache.js +0 -222
- package/api/stellar-solutions/product-category/cache.d.ts +0 -0
- package/api/stellar-solutions/product-category/cache.js +0 -224
- package/api/stellar-solutions/tax/cache.d.ts +0 -0
- package/api/stellar-solutions/tax/cache.js +0 -235
- package/api/stellar-solutions/type.js +0 -142
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
// import { API_ROUTES } from '../constants';
|
|
2
|
-
// import { CustomerBE, 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 CachedCustomers {
|
|
9
|
-
// customers: CustomerBE[];
|
|
10
|
-
// cachedAt: string;
|
|
11
|
-
// }
|
|
12
|
-
// /**
|
|
13
|
-
// * Synchronous utility function to get customers 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 CustomerBE[] - Array of cached customers or empty array wrapper ({count, items})
|
|
18
|
-
// *
|
|
19
|
-
// * @example
|
|
20
|
-
// * const customers = getCachedCustomersSync();
|
|
21
|
-
// * if (customers.items.length > 0) {
|
|
22
|
-
// * console.log(customers.items[0].firstName);
|
|
23
|
-
// * }
|
|
24
|
-
// */
|
|
25
|
-
// export const getCachedCustomersSync = (): {
|
|
26
|
-
// count: number;
|
|
27
|
-
// items: CustomerBE[];
|
|
28
|
-
// } => {
|
|
29
|
-
// try {
|
|
30
|
-
// const cachedData = getStorageValue(
|
|
31
|
-
// LS_KEYS.CUSTOMERS
|
|
32
|
-
// ) as CachedCustomers | 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.customers.length,
|
|
43
|
-
// items: cachedData.customers,
|
|
44
|
-
// };
|
|
45
|
-
// }
|
|
46
|
-
// return { count: 0, items: [] };
|
|
47
|
-
// } catch (error) {
|
|
48
|
-
// console.error('Error getting cached customers:', error);
|
|
49
|
-
// return { count: 0, items: [] };
|
|
50
|
-
// }
|
|
51
|
-
// };
|
|
52
|
-
// /**
|
|
53
|
-
// * Utility function to get customers from cache or fetch from API
|
|
54
|
-
// *
|
|
55
|
-
// * This function manages a localStorage cache of customers with the following logic:
|
|
56
|
-
// * - If customers exist in cache and are less than 1 day old, return cached version
|
|
57
|
-
// * - If customers exist but are older than 1 day, fetch fresh data and update cache
|
|
58
|
-
// * - If customers don't exist in cache, fetch from API and cache them
|
|
59
|
-
// *
|
|
60
|
-
// * @param searchQuery - Optional search query to filter customers
|
|
61
|
-
// * @param filters - Optional filters object to apply additional filtering (bypasses cache)
|
|
62
|
-
// * @param pageLimit - Number of customers to fetch (default: 100)
|
|
63
|
-
// * @returns Promise<{count:number, items: CustomerBE[]}> - Paged customers
|
|
64
|
-
// *
|
|
65
|
-
// * @example
|
|
66
|
-
// * const customers = await getCachedCustomers();
|
|
67
|
-
// * console.log(customers.items[0].firstName);
|
|
68
|
-
// *
|
|
69
|
-
// * // With search
|
|
70
|
-
// * const filtered = await getCachedCustomers('John');
|
|
71
|
-
// *
|
|
72
|
-
// * // With filters
|
|
73
|
-
// * const filteredByCountry = await getCachedCustomers(undefined, { country: 'USA' });
|
|
74
|
-
// */
|
|
75
|
-
// export const getCachedCustomers = async (
|
|
76
|
-
// searchQuery?: string,
|
|
77
|
-
// filters?: Record<string, unknown>,
|
|
78
|
-
// pageLimit: number = 100
|
|
79
|
-
// ): Promise<{ count: number; items: CustomerBE[] }> => {
|
|
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.CUSTOMERS,
|
|
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.CUSTOMERS
|
|
101
|
-
// ) as CachedCustomers | 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.customers.length,
|
|
111
|
-
// items: cachedData.customers,
|
|
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.CUSTOMERS,
|
|
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: CachedCustomers = {
|
|
127
|
-
// customers: response.data.items,
|
|
128
|
-
// cachedAt: new Date().toISOString(),
|
|
129
|
-
// };
|
|
130
|
-
// setStorageValue(LS_KEYS.CUSTOMERS, updatedCache);
|
|
131
|
-
// return response.data;
|
|
132
|
-
// }
|
|
133
|
-
// return { count: 0, items: [] };
|
|
134
|
-
// } catch (error) {
|
|
135
|
-
// console.error('Error fetching customers:', error);
|
|
136
|
-
// return { count: 0, items: [] };
|
|
137
|
-
// }
|
|
138
|
-
// };
|
|
139
|
-
// /**
|
|
140
|
-
// * Utility function to get a specific customer by ID from cache
|
|
141
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
142
|
-
// *
|
|
143
|
-
// * @param customerId - The ID of the customer to retrieve
|
|
144
|
-
// * @returns CustomerBE | null - The customer or null if not found
|
|
145
|
-
// *
|
|
146
|
-
// * @example
|
|
147
|
-
// * const customer = getCachedCustomerById('customer-123');
|
|
148
|
-
// * if (customer) {
|
|
149
|
-
// * console.log(customer.firstName);
|
|
150
|
-
// * }
|
|
151
|
-
// */
|
|
152
|
-
// export const getCachedCustomerById = (
|
|
153
|
-
// customerId: string
|
|
154
|
-
// ): CustomerBE | null => {
|
|
155
|
-
// if (!customerId) {
|
|
156
|
-
// return null;
|
|
157
|
-
// }
|
|
158
|
-
// try {
|
|
159
|
-
// const customers = getCachedCustomersSync().items;
|
|
160
|
-
// return customers.find((customer) => customer.id === customerId) || null;
|
|
161
|
-
// } catch (error) {
|
|
162
|
-
// console.error('Error getting cached customer by ID:', error);
|
|
163
|
-
// return null;
|
|
164
|
-
// }
|
|
165
|
-
// };
|
|
166
|
-
// /**
|
|
167
|
-
// * Utility function to get a specific customer by phone from cache
|
|
168
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
169
|
-
// *
|
|
170
|
-
// * @param phone - The phone number of the customer to search for
|
|
171
|
-
// * @returns CustomerBE | null - The customer or null if not found
|
|
172
|
-
// *
|
|
173
|
-
// * @example
|
|
174
|
-
// * const customer = getCachedCustomerByPhone('+1234567890');
|
|
175
|
-
// * if (customer) {
|
|
176
|
-
// * console.log(customer.firstName);
|
|
177
|
-
// * }
|
|
178
|
-
// */
|
|
179
|
-
// export const getCachedCustomerByPhone = (phone: string): CustomerBE | null => {
|
|
180
|
-
// if (!phone) {
|
|
181
|
-
// return null;
|
|
182
|
-
// }
|
|
183
|
-
// try {
|
|
184
|
-
// const customers = getCachedCustomersSync().items;
|
|
185
|
-
// return customers.find((customer) => customer.phone === phone) || null;
|
|
186
|
-
// } catch (error) {
|
|
187
|
-
// console.error('Error getting cached customer by phone:', error);
|
|
188
|
-
// return null;
|
|
189
|
-
// }
|
|
190
|
-
// };
|
|
191
|
-
// /**
|
|
192
|
-
// * Utility function to invalidate (remove) customers from cache
|
|
193
|
-
// * Useful when customers have been updated and you want to force a refresh
|
|
194
|
-
// *
|
|
195
|
-
// * @example
|
|
196
|
-
// * invalidateCustomersCache();
|
|
197
|
-
// * const freshCustomers = await getCachedCustomers();
|
|
198
|
-
// */
|
|
199
|
-
// export const invalidateCustomersCache = (): void => {
|
|
200
|
-
// try {
|
|
201
|
-
// localStorage.removeItem(LS_KEYS.CUSTOMERS);
|
|
202
|
-
// } catch (error) {
|
|
203
|
-
// console.error('Error invalidating customers cache:', error);
|
|
204
|
-
// }
|
|
205
|
-
// };
|
|
206
|
-
// /**
|
|
207
|
-
// * Utility function to preload customers into cache
|
|
208
|
-
// * Useful to call on app initialization or login
|
|
209
|
-
// *
|
|
210
|
-
// * @returns Promise<{count:number, items: CustomerBE[]}> - Array of preloaded customers
|
|
211
|
-
// *
|
|
212
|
-
// * @example
|
|
213
|
-
// * // On app initialization
|
|
214
|
-
// * await preloadCustomers();
|
|
215
|
-
// */
|
|
216
|
-
// export const preloadCustomers = async (): Promise<{
|
|
217
|
-
// count: number;
|
|
218
|
-
// items: CustomerBE[];
|
|
219
|
-
// }> => {
|
|
220
|
-
// return getCachedCustomers();
|
|
221
|
-
// };
|
|
222
|
-
// /**
|
|
223
|
-
// * Utility function to check if customers cache is stale
|
|
224
|
-
// * Returns true if cache is older than 1 day or doesn't exist
|
|
225
|
-
// *
|
|
226
|
-
// * @returns boolean - True if cache is stale or doesn't exist
|
|
227
|
-
// *
|
|
228
|
-
// * @example
|
|
229
|
-
// * if (isCustomersCacheStale()) {
|
|
230
|
-
// * await getCachedCustomers(); // This will fetch fresh data
|
|
231
|
-
// * }
|
|
232
|
-
// */
|
|
233
|
-
// export const isCustomersCacheStale = (): boolean => {
|
|
234
|
-
// try {
|
|
235
|
-
// const cachedData = getStorageValue(
|
|
236
|
-
// LS_KEYS.CUSTOMERS
|
|
237
|
-
// ) as CachedCustomers | null;
|
|
238
|
-
// if (!cachedData) {
|
|
239
|
-
// return true;
|
|
240
|
-
// }
|
|
241
|
-
// const currentTime = new Date().getTime();
|
|
242
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
243
|
-
// const ageInMs = currentTime - cachedTime;
|
|
244
|
-
// return ageInMs >= ONE_DAY_IN_MS;
|
|
245
|
-
// } catch (error) {
|
|
246
|
-
// console.error('Error checking customers cache staleness:', error);
|
|
247
|
-
// return true;
|
|
248
|
-
// }
|
|
249
|
-
// };
|
|
File without changes
|
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
// import { API_ROUTES } from '../constants';
|
|
2
|
-
// import { ExpenseBE, 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 CachedExpenses {
|
|
9
|
-
// expenses: ExpenseBE[];
|
|
10
|
-
// cachedAt: string;
|
|
11
|
-
// }
|
|
12
|
-
// /**
|
|
13
|
-
// * Synchronous utility function to get expenses 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 ExpenseBE[] - Array of cached expenses or empty array wrapper ({count, items})
|
|
18
|
-
// *
|
|
19
|
-
// * @example
|
|
20
|
-
// * const expenses = getCachedExpensesSync();
|
|
21
|
-
// * if (expenses.items.length > 0) {
|
|
22
|
-
// * console.log(expenses.items[0].name);
|
|
23
|
-
// * }
|
|
24
|
-
// */
|
|
25
|
-
// export const getCachedExpensesSync = (): {
|
|
26
|
-
// count: number;
|
|
27
|
-
// items: ExpenseBE[];
|
|
28
|
-
// } => {
|
|
29
|
-
// try {
|
|
30
|
-
// const cachedData = getStorageValue(
|
|
31
|
-
// LS_KEYS.EXPENSES
|
|
32
|
-
// ) as CachedExpenses | 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.expenses.length,
|
|
43
|
-
// items: cachedData.expenses,
|
|
44
|
-
// };
|
|
45
|
-
// }
|
|
46
|
-
// return { count: 0, items: [] };
|
|
47
|
-
// } catch (error) {
|
|
48
|
-
// console.error('Error getting cached expenses:', error);
|
|
49
|
-
// return { count: 0, items: [] };
|
|
50
|
-
// }
|
|
51
|
-
// };
|
|
52
|
-
// /**
|
|
53
|
-
// * Utility function to get expenses from cache or fetch from API
|
|
54
|
-
// *
|
|
55
|
-
// * This function manages a localStorage cache of expenses with the following logic:
|
|
56
|
-
// * - If expenses exist in cache and are less than 1 day old, return cached version
|
|
57
|
-
// * - If expenses exist but are older than 1 day, fetch fresh data and update cache
|
|
58
|
-
// * - If expenses don't exist in cache, fetch from API and cache them
|
|
59
|
-
// *
|
|
60
|
-
// * @param searchQuery - Optional search query to filter expenses
|
|
61
|
-
// * @param filters - Optional filters object to apply additional filtering (bypasses cache)
|
|
62
|
-
// * @param pageLimit - Number of expenses to fetch (default: 100)
|
|
63
|
-
// * @returns Promise<{count:number, items: ExpenseBE[]}> - Paged expenses
|
|
64
|
-
// *
|
|
65
|
-
// * @example
|
|
66
|
-
// * const expenses = await getCachedExpenses();
|
|
67
|
-
// * console.log(expenses.items[0].name);
|
|
68
|
-
// *
|
|
69
|
-
// * // With search
|
|
70
|
-
// * const filtered = await getCachedExpenses('Office');
|
|
71
|
-
// *
|
|
72
|
-
// * // With filters
|
|
73
|
-
// * const filteredByCategory = await getCachedExpenses(undefined, { expenseCategoryId: 'cat-123' });
|
|
74
|
-
// */
|
|
75
|
-
// export const getCachedExpenses = async (
|
|
76
|
-
// searchQuery?: string,
|
|
77
|
-
// filters?: Record<string, unknown>,
|
|
78
|
-
// pageLimit: number = 100
|
|
79
|
-
// ): Promise<{ count: number; items: ExpenseBE[] }> => {
|
|
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.EXPENSES,
|
|
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.EXPENSES
|
|
101
|
-
// ) as CachedExpenses | 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.expenses.length,
|
|
111
|
-
// items: cachedData.expenses,
|
|
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.EXPENSES,
|
|
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: CachedExpenses = {
|
|
127
|
-
// expenses: response.data.items,
|
|
128
|
-
// cachedAt: new Date().toISOString(),
|
|
129
|
-
// };
|
|
130
|
-
// setStorageValue(LS_KEYS.EXPENSES, updatedCache);
|
|
131
|
-
// return response.data;
|
|
132
|
-
// }
|
|
133
|
-
// return { count: 0, items: [] };
|
|
134
|
-
// } catch (error) {
|
|
135
|
-
// console.error('Error fetching expenses:', error);
|
|
136
|
-
// return { count: 0, items: [] };
|
|
137
|
-
// }
|
|
138
|
-
// };
|
|
139
|
-
// /**
|
|
140
|
-
// * Utility function to get a specific expense by ID from cache
|
|
141
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
142
|
-
// *
|
|
143
|
-
// * @param expenseId - The ID of the expense to retrieve
|
|
144
|
-
// * @returns ExpenseBE | null - The expense or null if not found
|
|
145
|
-
// *
|
|
146
|
-
// * @example
|
|
147
|
-
// * const expense = getCachedExpenseById('expense-123');
|
|
148
|
-
// * if (expense) {
|
|
149
|
-
// * console.log(expense.name);
|
|
150
|
-
// * }
|
|
151
|
-
// */
|
|
152
|
-
// export const getCachedExpenseById = (expenseId: string): ExpenseBE | null => {
|
|
153
|
-
// if (!expenseId) {
|
|
154
|
-
// return null;
|
|
155
|
-
// }
|
|
156
|
-
// try {
|
|
157
|
-
// const expenses = getCachedExpensesSync().items;
|
|
158
|
-
// return expenses.find((expense) => expense.id === expenseId) || null;
|
|
159
|
-
// } catch (error) {
|
|
160
|
-
// console.error('Error getting cached expense by ID:', error);
|
|
161
|
-
// return null;
|
|
162
|
-
// }
|
|
163
|
-
// };
|
|
164
|
-
// /**
|
|
165
|
-
// * Utility function to get expenses by name from cache
|
|
166
|
-
// * If not found in cache, returns empty array (does not trigger API call)
|
|
167
|
-
// *
|
|
168
|
-
// * @param name - The name of the expense to search for
|
|
169
|
-
// * @returns ExpenseBE[] - Array of matching expenses or empty array
|
|
170
|
-
// *
|
|
171
|
-
// * @example
|
|
172
|
-
// * const expenses = getCachedExpensesByName('Office');
|
|
173
|
-
// * console.log(expenses.length);
|
|
174
|
-
// */
|
|
175
|
-
// export const getCachedExpensesByName = (name: string): ExpenseBE[] => {
|
|
176
|
-
// if (!name) {
|
|
177
|
-
// return [];
|
|
178
|
-
// }
|
|
179
|
-
// try {
|
|
180
|
-
// const expenses = getCachedExpensesSync().items;
|
|
181
|
-
// return expenses.filter((expense) =>
|
|
182
|
-
// expense.name.toLowerCase().includes(name.toLowerCase())
|
|
183
|
-
// );
|
|
184
|
-
// } catch (error) {
|
|
185
|
-
// console.error('Error getting cached expenses by name:', error);
|
|
186
|
-
// return [];
|
|
187
|
-
// }
|
|
188
|
-
// };
|
|
189
|
-
// /**
|
|
190
|
-
// * Utility function to invalidate (remove) expenses from cache
|
|
191
|
-
// * Useful when expenses have been updated and you want to force a refresh
|
|
192
|
-
// *
|
|
193
|
-
// * @example
|
|
194
|
-
// * invalidateExpensesCache();
|
|
195
|
-
// * const freshExpenses = await getCachedExpenses();
|
|
196
|
-
// */
|
|
197
|
-
// export const invalidateExpensesCache = (): void => {
|
|
198
|
-
// try {
|
|
199
|
-
// localStorage.removeItem(LS_KEYS.EXPENSES);
|
|
200
|
-
// } catch (error) {
|
|
201
|
-
// console.error('Error invalidating expenses cache:', error);
|
|
202
|
-
// }
|
|
203
|
-
// };
|
|
204
|
-
// /**
|
|
205
|
-
// * Utility function to preload expenses into cache
|
|
206
|
-
// * Useful to call on app initialization or login
|
|
207
|
-
// *
|
|
208
|
-
// * @returns Promise<{count:number, items: ExpenseBE[]}> - Array of preloaded expenses
|
|
209
|
-
// *
|
|
210
|
-
// * @example
|
|
211
|
-
// * // On app initialization
|
|
212
|
-
// * await preloadExpenses();
|
|
213
|
-
// */
|
|
214
|
-
// export const preloadExpenses = async (): Promise<{
|
|
215
|
-
// count: number;
|
|
216
|
-
// items: ExpenseBE[];
|
|
217
|
-
// }> => {
|
|
218
|
-
// return getCachedExpenses();
|
|
219
|
-
// };
|
|
220
|
-
// /**
|
|
221
|
-
// * Utility function to check if expenses cache is stale
|
|
222
|
-
// * Returns true if cache is older than 1 day or doesn't exist
|
|
223
|
-
// *
|
|
224
|
-
// * @returns boolean - True if cache is stale or doesn't exist
|
|
225
|
-
// *
|
|
226
|
-
// * @example
|
|
227
|
-
// * if (isExpensesCacheStale()) {
|
|
228
|
-
// * await getCachedExpenses(); // This will fetch fresh data
|
|
229
|
-
// * }
|
|
230
|
-
// */
|
|
231
|
-
// export const isExpensesCacheStale = (): boolean => {
|
|
232
|
-
// try {
|
|
233
|
-
// const cachedData = getStorageValue(
|
|
234
|
-
// LS_KEYS.EXPENSES
|
|
235
|
-
// ) as CachedExpenses | null;
|
|
236
|
-
// if (!cachedData) {
|
|
237
|
-
// return true;
|
|
238
|
-
// }
|
|
239
|
-
// const currentTime = new Date().getTime();
|
|
240
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
241
|
-
// const ageInMs = currentTime - cachedTime;
|
|
242
|
-
// return ageInMs >= ONE_DAY_IN_MS;
|
|
243
|
-
// } catch (error) {
|
|
244
|
-
// console.error('Error checking expenses cache staleness:', error);
|
|
245
|
-
// return true;
|
|
246
|
-
// }
|
|
247
|
-
// };
|
|
File without changes
|