@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,247 +0,0 @@
|
|
|
1
|
-
// import { API_ROUTES } from '../constants';
|
|
2
|
-
// import { ContactBE, 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 CachedContacts {
|
|
9
|
-
// contacts: ContactBE[];
|
|
10
|
-
// cachedAt: string;
|
|
11
|
-
// }
|
|
12
|
-
// /**
|
|
13
|
-
// * Synchronous utility function to get contacts 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 ContactBE[] - Array of cached contacts or empty array wrapper ({count, items})
|
|
18
|
-
// *
|
|
19
|
-
// * @example
|
|
20
|
-
// * const contacts = getCachedContactsSync();
|
|
21
|
-
// * if (contacts.items.length > 0) {
|
|
22
|
-
// * console.log(contacts.items[0].firstName);
|
|
23
|
-
// * }
|
|
24
|
-
// */
|
|
25
|
-
// export const getCachedContactsSync = (): {
|
|
26
|
-
// count: number;
|
|
27
|
-
// items: ContactBE[];
|
|
28
|
-
// } => {
|
|
29
|
-
// try {
|
|
30
|
-
// const cachedData = getStorageValue(
|
|
31
|
-
// LS_KEYS.CONTACTS
|
|
32
|
-
// ) as CachedContacts | 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.contacts.length,
|
|
43
|
-
// items: cachedData.contacts,
|
|
44
|
-
// };
|
|
45
|
-
// }
|
|
46
|
-
// return { count: 0, items: [] };
|
|
47
|
-
// } catch (error) {
|
|
48
|
-
// console.error('Error getting cached contacts:', error);
|
|
49
|
-
// return { count: 0, items: [] };
|
|
50
|
-
// }
|
|
51
|
-
// };
|
|
52
|
-
// /**
|
|
53
|
-
// * Utility function to get contacts from cache or fetch from API
|
|
54
|
-
// *
|
|
55
|
-
// * This function manages a localStorage cache of contacts with the following logic:
|
|
56
|
-
// * - If contacts exist in cache and are less than 1 day old, return cached version
|
|
57
|
-
// * - If contacts exist but are older than 1 day, fetch fresh data and update cache
|
|
58
|
-
// * - If contacts don't exist in cache, fetch from API and cache them
|
|
59
|
-
// *
|
|
60
|
-
// * @param searchQuery - Optional search query to filter contacts
|
|
61
|
-
// * @param filters - Optional filters object to apply additional filtering (bypasses cache)
|
|
62
|
-
// * @param pageLimit - Number of contacts to fetch (default: 100)
|
|
63
|
-
// * @returns Promise<{count:number, items: ContactBE[]}> - Paged contacts
|
|
64
|
-
// *
|
|
65
|
-
// * @example
|
|
66
|
-
// * const contacts = await getCachedContacts();
|
|
67
|
-
// * console.log(contacts.items[0].firstName);
|
|
68
|
-
// *
|
|
69
|
-
// * // With search
|
|
70
|
-
// * const filtered = await getCachedContacts('John');
|
|
71
|
-
// *
|
|
72
|
-
// * // With filters
|
|
73
|
-
// * const filteredByCompany = await getCachedContacts(undefined, { companyId: 'company-123' });
|
|
74
|
-
// */
|
|
75
|
-
// export const getCachedContacts = async (
|
|
76
|
-
// searchQuery?: string,
|
|
77
|
-
// filters?: Record<string, unknown>,
|
|
78
|
-
// pageLimit: number = 100
|
|
79
|
-
// ): Promise<{ count: number; items: ContactBE[] }> => {
|
|
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.CONTACTS,
|
|
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.CONTACTS
|
|
101
|
-
// ) as CachedContacts | 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.contacts.length,
|
|
111
|
-
// items: cachedData.contacts,
|
|
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.CONTACTS,
|
|
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: CachedContacts = {
|
|
127
|
-
// contacts: response.data.items,
|
|
128
|
-
// cachedAt: new Date().toISOString(),
|
|
129
|
-
// };
|
|
130
|
-
// setStorageValue(LS_KEYS.CONTACTS, updatedCache);
|
|
131
|
-
// return response.data;
|
|
132
|
-
// }
|
|
133
|
-
// return { count: 0, items: [] };
|
|
134
|
-
// } catch (error) {
|
|
135
|
-
// console.error('Error fetching contacts:', error);
|
|
136
|
-
// return { count: 0, items: [] };
|
|
137
|
-
// }
|
|
138
|
-
// };
|
|
139
|
-
// /**
|
|
140
|
-
// * Utility function to get a specific contact by ID from cache
|
|
141
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
142
|
-
// *
|
|
143
|
-
// * @param contactId - The ID of the contact to retrieve
|
|
144
|
-
// * @returns ContactBE | null - The contact or null if not found
|
|
145
|
-
// *
|
|
146
|
-
// * @example
|
|
147
|
-
// * const contact = getCachedContactById('contact-123');
|
|
148
|
-
// * if (contact) {
|
|
149
|
-
// * console.log(contact.firstName);
|
|
150
|
-
// * }
|
|
151
|
-
// */
|
|
152
|
-
// export const getCachedContactById = (contactId: string): ContactBE | null => {
|
|
153
|
-
// if (!contactId) {
|
|
154
|
-
// return null;
|
|
155
|
-
// }
|
|
156
|
-
// try {
|
|
157
|
-
// const contacts = getCachedContactsSync().items;
|
|
158
|
-
// return contacts.find((contact) => contact.id === contactId) || null;
|
|
159
|
-
// } catch (error) {
|
|
160
|
-
// console.error('Error getting cached contact by ID:', error);
|
|
161
|
-
// return null;
|
|
162
|
-
// }
|
|
163
|
-
// };
|
|
164
|
-
// /**
|
|
165
|
-
// * Utility function to get contacts by email from cache
|
|
166
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
167
|
-
// *
|
|
168
|
-
// * @param email - The email of the contact to search for
|
|
169
|
-
// * @returns ContactBE | null - The contact or null if not found
|
|
170
|
-
// *
|
|
171
|
-
// * @example
|
|
172
|
-
// * const contact = getCachedContactByEmail('john@example.com');
|
|
173
|
-
// * if (contact) {
|
|
174
|
-
// * console.log(contact.firstName);
|
|
175
|
-
// * }
|
|
176
|
-
// */
|
|
177
|
-
// export const getCachedContactByEmail = (email: string): ContactBE | null => {
|
|
178
|
-
// if (!email) {
|
|
179
|
-
// return null;
|
|
180
|
-
// }
|
|
181
|
-
// try {
|
|
182
|
-
// const contacts = getCachedContactsSync().items;
|
|
183
|
-
// return contacts.find((contact) => contact.email === email) || null;
|
|
184
|
-
// } catch (error) {
|
|
185
|
-
// console.error('Error getting cached contact by email:', error);
|
|
186
|
-
// return null;
|
|
187
|
-
// }
|
|
188
|
-
// };
|
|
189
|
-
// /**
|
|
190
|
-
// * Utility function to invalidate (remove) contacts from cache
|
|
191
|
-
// * Useful when contacts have been updated and you want to force a refresh
|
|
192
|
-
// *
|
|
193
|
-
// * @example
|
|
194
|
-
// * invalidateContactsCache();
|
|
195
|
-
// * const freshContacts = await getCachedContacts();
|
|
196
|
-
// */
|
|
197
|
-
// export const invalidateContactsCache = (): void => {
|
|
198
|
-
// try {
|
|
199
|
-
// localStorage.removeItem(LS_KEYS.CONTACTS);
|
|
200
|
-
// } catch (error) {
|
|
201
|
-
// console.error('Error invalidating contacts cache:', error);
|
|
202
|
-
// }
|
|
203
|
-
// };
|
|
204
|
-
// /**
|
|
205
|
-
// * Utility function to preload contacts into cache
|
|
206
|
-
// * Useful to call on app initialization or login
|
|
207
|
-
// *
|
|
208
|
-
// * @returns Promise<{count:number, items: ContactBE[]}> - Array of preloaded contacts
|
|
209
|
-
// *
|
|
210
|
-
// * @example
|
|
211
|
-
// * // On app initialization
|
|
212
|
-
// * await preloadContacts();
|
|
213
|
-
// */
|
|
214
|
-
// export const preloadContacts = async (): Promise<{
|
|
215
|
-
// count: number;
|
|
216
|
-
// items: ContactBE[];
|
|
217
|
-
// }> => {
|
|
218
|
-
// return getCachedContacts();
|
|
219
|
-
// };
|
|
220
|
-
// /**
|
|
221
|
-
// * Utility function to check if contacts 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 (isContactsCacheStale()) {
|
|
228
|
-
// * await getCachedContacts(); // This will fetch fresh data
|
|
229
|
-
// * }
|
|
230
|
-
// */
|
|
231
|
-
// export const isContactsCacheStale = (): boolean => {
|
|
232
|
-
// try {
|
|
233
|
-
// const cachedData = getStorageValue(
|
|
234
|
-
// LS_KEYS.CONTACTS
|
|
235
|
-
// ) as CachedContacts | 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 contacts cache staleness:', error);
|
|
245
|
-
// return true;
|
|
246
|
-
// }
|
|
247
|
-
// };
|
|
File without changes
|
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
// import { API_ROUTES } from '../constants';
|
|
2
|
-
// import { CurrencyBE, 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 CachedCurrencies {
|
|
9
|
-
// currencies: CurrencyBE[];
|
|
10
|
-
// cachedAt: string;
|
|
11
|
-
// }
|
|
12
|
-
// /**
|
|
13
|
-
// * Synchronous utility function to get currencies 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 CurrencyBE[] - Array of cached currencies or empty array wrapper ({count, items})
|
|
18
|
-
// *
|
|
19
|
-
// * @example
|
|
20
|
-
// * const currencies = getCachedCurrenciesSync();
|
|
21
|
-
// * if (currencies.items.length > 0) {
|
|
22
|
-
// * console.log(currencies.items[0].label);
|
|
23
|
-
// * }
|
|
24
|
-
// */
|
|
25
|
-
// export const getCachedCurrenciesSync = (): {
|
|
26
|
-
// count: number;
|
|
27
|
-
// items: CurrencyBE[];
|
|
28
|
-
// } => {
|
|
29
|
-
// try {
|
|
30
|
-
// const cachedData = getStorageValue(
|
|
31
|
-
// LS_KEYS.CURRENCIES
|
|
32
|
-
// ) as CachedCurrencies | 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 week old, return it
|
|
40
|
-
// if (ageInMs < ONE_WEEK_IN_MS) {
|
|
41
|
-
// return {
|
|
42
|
-
// count: cachedData.currencies.length,
|
|
43
|
-
// items: cachedData.currencies,
|
|
44
|
-
// };
|
|
45
|
-
// }
|
|
46
|
-
// return { count: 0, items: [] };
|
|
47
|
-
// } catch (error) {
|
|
48
|
-
// console.error('Error getting cached currencies:', error);
|
|
49
|
-
// return { count: 0, items: [] };
|
|
50
|
-
// }
|
|
51
|
-
// };
|
|
52
|
-
// /**
|
|
53
|
-
// * Utility function to get currencies from cache or fetch from API
|
|
54
|
-
// *
|
|
55
|
-
// * This function manages a localStorage cache of currencies with the following logic:
|
|
56
|
-
// * - If currencies exist in cache and are less than 1 week old, return cached version
|
|
57
|
-
// * - If currencies exist but are older than 1 week, fetch fresh data and update cache
|
|
58
|
-
// * - If currencies don't exist in cache, fetch from API and cache them
|
|
59
|
-
// *
|
|
60
|
-
// * @param searchQuery - Optional search query to filter currencies
|
|
61
|
-
// * @param pageLimit - Number of currencies to fetch (default: 100)
|
|
62
|
-
// * @returns Promise<{count:number, items: CurrencyBE[]}> - Paged currencies
|
|
63
|
-
// *
|
|
64
|
-
// * @example
|
|
65
|
-
// * const currencies = await getCachedCurrencies();
|
|
66
|
-
// * console.log(currencies.items[0].label);
|
|
67
|
-
// *
|
|
68
|
-
// * // With search
|
|
69
|
-
// * const filtered = await getCachedCurrencies('USD');
|
|
70
|
-
// */
|
|
71
|
-
// export const getCachedCurrencies = async (
|
|
72
|
-
// searchQuery?: string,
|
|
73
|
-
// pageLimit: number = 100
|
|
74
|
-
// ): Promise<{
|
|
75
|
-
// count: number;
|
|
76
|
-
// items: CurrencyBE[];
|
|
77
|
-
// }> => {
|
|
78
|
-
// try {
|
|
79
|
-
// // If there's a search query, always fetch fresh data (don't use cache)
|
|
80
|
-
// if (searchQuery && searchQuery.trim()) {
|
|
81
|
-
// const response = await fetchData({
|
|
82
|
-
// url: API_ROUTES.CURRENCIES,
|
|
83
|
-
// body: JSON.stringify({
|
|
84
|
-
// searchQuery,
|
|
85
|
-
// pageLimit,
|
|
86
|
-
// currentPage: 1,
|
|
87
|
-
// }),
|
|
88
|
-
// method: API_METHODS.POST,
|
|
89
|
-
// });
|
|
90
|
-
// return response?.data || { count: 0, items: [] };
|
|
91
|
-
// }
|
|
92
|
-
// // Get the cached data from localStorage
|
|
93
|
-
// const cachedData = getStorageValue(
|
|
94
|
-
// LS_KEYS.CURRENCIES
|
|
95
|
-
// ) as CachedCurrencies | null;
|
|
96
|
-
// const currentTime = new Date().getTime();
|
|
97
|
-
// // Check if cached data exists and is still fresh
|
|
98
|
-
// if (cachedData) {
|
|
99
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
100
|
-
// const ageInMs = currentTime - cachedTime;
|
|
101
|
-
// // If cached data is less than 1 week old, return it
|
|
102
|
-
// if (ageInMs < ONE_WEEK_IN_MS) {
|
|
103
|
-
// return {
|
|
104
|
-
// count: cachedData.currencies.length,
|
|
105
|
-
// items: cachedData.currencies,
|
|
106
|
-
// };
|
|
107
|
-
// }
|
|
108
|
-
// }
|
|
109
|
-
// // If no cached data or data is older than 1 week, fetch fresh data
|
|
110
|
-
// const response = await fetchData({
|
|
111
|
-
// url: API_ROUTES.CURRENCIES,
|
|
112
|
-
// body: JSON.stringify({
|
|
113
|
-
// pageLimit,
|
|
114
|
-
// currentPage: 1,
|
|
115
|
-
// }),
|
|
116
|
-
// method: API_METHODS.POST,
|
|
117
|
-
// });
|
|
118
|
-
// if (response?.data?.items) {
|
|
119
|
-
// // Update the cache with fresh data
|
|
120
|
-
// const updatedCache: CachedCurrencies = {
|
|
121
|
-
// currencies: response.data.items,
|
|
122
|
-
// cachedAt: new Date().toISOString(),
|
|
123
|
-
// };
|
|
124
|
-
// setStorageValue(LS_KEYS.CURRENCIES, updatedCache);
|
|
125
|
-
// return response.data;
|
|
126
|
-
// }
|
|
127
|
-
// return { count: 0, items: [] };
|
|
128
|
-
// } catch (error) {
|
|
129
|
-
// console.error('Error fetching currencies:', error);
|
|
130
|
-
// return { count: 0, items: [] };
|
|
131
|
-
// }
|
|
132
|
-
// };
|
|
133
|
-
// /**
|
|
134
|
-
// * Utility function to get a specific currency by ID from cache
|
|
135
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
136
|
-
// *
|
|
137
|
-
// * @param currencyId - The ID of the currency to retrieve
|
|
138
|
-
// * @returns CurrencyBE | null - The currency or null if not found
|
|
139
|
-
// *
|
|
140
|
-
// * @example
|
|
141
|
-
// * const currency = getCachedCurrencyById('curr-123');
|
|
142
|
-
// * if (currency) {
|
|
143
|
-
// * console.log(currency.label);
|
|
144
|
-
// * }
|
|
145
|
-
// */
|
|
146
|
-
// export const getCachedCurrencyById = (
|
|
147
|
-
// currencyId: string
|
|
148
|
-
// ): CurrencyBE | null => {
|
|
149
|
-
// if (!currencyId) {
|
|
150
|
-
// return null;
|
|
151
|
-
// }
|
|
152
|
-
// try {
|
|
153
|
-
// const currencies = getCachedCurrenciesSync().items;
|
|
154
|
-
// return currencies.find((curr) => curr.id === currencyId) || null;
|
|
155
|
-
// } catch (error) {
|
|
156
|
-
// console.error('Error getting cached currency by ID:', error);
|
|
157
|
-
// return null;
|
|
158
|
-
// }
|
|
159
|
-
// };
|
|
160
|
-
// /**
|
|
161
|
-
// * Utility function to get a specific currency by code from cache
|
|
162
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
163
|
-
// *
|
|
164
|
-
// * @param code - The code of the currency to retrieve (e.g., 'USD', 'EUR')
|
|
165
|
-
// * @returns CurrencyBE | null - The currency or null if not found
|
|
166
|
-
// *
|
|
167
|
-
// * @example
|
|
168
|
-
// * const currency = getCachedCurrencyByCode('USD');
|
|
169
|
-
// * if (currency) {
|
|
170
|
-
// * console.log(currency.label);
|
|
171
|
-
// * }
|
|
172
|
-
// */
|
|
173
|
-
// export const getCachedCurrencyByCode = (code: string): CurrencyBE | null => {
|
|
174
|
-
// if (!code) {
|
|
175
|
-
// return null;
|
|
176
|
-
// }
|
|
177
|
-
// try {
|
|
178
|
-
// const currencies = getCachedCurrenciesSync().items;
|
|
179
|
-
// return currencies.find((curr) => curr.code === code) || null;
|
|
180
|
-
// } catch (error) {
|
|
181
|
-
// console.error('Error getting cached currency by code:', error);
|
|
182
|
-
// return null;
|
|
183
|
-
// }
|
|
184
|
-
// };
|
|
185
|
-
// /**
|
|
186
|
-
// * Utility function to invalidate (remove) currencies from cache
|
|
187
|
-
// * Useful when currencies have been updated and you want to force a refresh
|
|
188
|
-
// *
|
|
189
|
-
// * @example
|
|
190
|
-
// * invalidateCurrenciesCache();
|
|
191
|
-
// * const freshCurrencies = await getCachedCurrencies();
|
|
192
|
-
// */
|
|
193
|
-
// export const invalidateCurrenciesCache = (): void => {
|
|
194
|
-
// try {
|
|
195
|
-
// localStorage.removeItem(LS_KEYS.CURRENCIES);
|
|
196
|
-
// } catch (error) {
|
|
197
|
-
// console.error('Error invalidating currencies cache:', error);
|
|
198
|
-
// }
|
|
199
|
-
// };
|
|
200
|
-
// /**
|
|
201
|
-
// * Utility function to preload currencies into cache
|
|
202
|
-
// * Useful to call on app initialization or login
|
|
203
|
-
// *
|
|
204
|
-
// * @returns Promise<{count:number, items: CurrencyBE[]}> - Array of preloaded currencies
|
|
205
|
-
// *
|
|
206
|
-
// * @example
|
|
207
|
-
// * // On app initialization
|
|
208
|
-
// * await preloadCurrencies();
|
|
209
|
-
// */
|
|
210
|
-
// export const preloadCurrencies = async (): Promise<{
|
|
211
|
-
// count: number;
|
|
212
|
-
// items: CurrencyBE[];
|
|
213
|
-
// }> => {
|
|
214
|
-
// return getCachedCurrencies();
|
|
215
|
-
// };
|
|
216
|
-
// /**
|
|
217
|
-
// * Utility function to check if currencies cache is stale
|
|
218
|
-
// * Returns true if cache is older than 1 week or doesn't exist
|
|
219
|
-
// *
|
|
220
|
-
// * @returns boolean - True if cache is stale or doesn't exist
|
|
221
|
-
// *
|
|
222
|
-
// * @example
|
|
223
|
-
// * if (isCurrenciesCacheStale()) {
|
|
224
|
-
// * await getCachedCurrencies(); // This will fetch fresh data
|
|
225
|
-
// * }
|
|
226
|
-
// */
|
|
227
|
-
// export const isCurrenciesCacheStale = (): boolean => {
|
|
228
|
-
// try {
|
|
229
|
-
// const cachedData = getStorageValue(
|
|
230
|
-
// LS_KEYS.CURRENCIES
|
|
231
|
-
// ) as CachedCurrencies | null;
|
|
232
|
-
// if (!cachedData) {
|
|
233
|
-
// return true;
|
|
234
|
-
// }
|
|
235
|
-
// const currentTime = new Date().getTime();
|
|
236
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
237
|
-
// const ageInMs = currentTime - cachedTime;
|
|
238
|
-
// return ageInMs >= ONE_WEEK_IN_MS;
|
|
239
|
-
// } catch (error) {
|
|
240
|
-
// console.error('Error checking currencies cache staleness:', error);
|
|
241
|
-
// return true;
|
|
242
|
-
// }
|
|
243
|
-
// };
|
|
File without changes
|