Skip to content

Commit bab134c

Browse files
Merge pull request #4542 from OneCommunityGlobal/development
Frontend Release to Main [4.65]
2 parents a541f0f + 7b50c3f commit bab134c

59 files changed

Lines changed: 3779 additions & 1696 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/actions/badgeManagement.js

Lines changed: 200 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,6 @@ export const fetchAllBadges = (forceRefresh = false) => {
4747
}
4848
};
4949
};
50-
51-
// Return updated badgeCollection
52-
export const returnUpdatedBadgesCollection = (badgeCollection, selectedBadgesId) => {
53-
const personalMaxBadge = '666b78265bca0bcb94080605'; // backend id for Personal Max badge
54-
const badgeMap = new Map(badgeCollection?.map(badge => [badge.badge, badge]));
55-
56-
const currentTs = Date.now();
57-
const currentDate = formatDate();
58-
selectedBadgesId.forEach(originalBadgeId => {
59-
const badgeId = originalBadgeId.replace('assign-badge-', '');
60-
if (badgeMap.has(badgeId)) {
61-
// Update the existing badge record
62-
if (badgeId !== personalMaxBadge) {
63-
const badge = badgeMap.get(badgeId);
64-
badge.count = (badge.count || 0) + 1;
65-
badge.lastModified = currentTs;
66-
badge.earnedDate.push(currentDate);
67-
}
68-
} else {
69-
// Add the new badge record
70-
badgeMap.set(badgeId, {
71-
badge: badgeId,
72-
count: 1,
73-
lastModified: currentTs,
74-
earnedDate: [currentDate],
75-
});
76-
}
77-
});
78-
79-
return Array.from(badgeMap.values());
80-
};
81-
8250
export const gotCloseAlert = () => ({ type: CLOSE_ALERT });
8351

8452
const getBadgeCountSuccess = badgeCount => ({
@@ -167,7 +135,7 @@ export const validateBadges = (firstName, lastName) => {
167135
if (!firstName || !lastName) {
168136
dispatch(
169137
getMessage(
170-
'The Name Find function does not work without entering a name. Nice try though.',
138+
'The Name Find function does not work without entering first and last name. Nice try though.',
171139
'danger',
172140
),
173141
);
@@ -180,7 +148,105 @@ export const validateBadges = (firstName, lastName) => {
180148
};
181149
};
182150

183-
export const assignBadges = (firstName, lastName, selectedBadges) => {
151+
152+
153+
export const returnUpdatedBadgesCollection = (badgeCollection, selectedBadgesId) => {
154+
let newBadgeCollection = Array.from(badgeCollection);
155+
156+
// Object to track updated or newly added badges to prevent duplicates
157+
const updatedOrAddedBadges = {};
158+
159+
selectedBadgesId.forEach(originalBadgeId => {
160+
let badgeId = originalBadgeId;
161+
162+
// Remove "assign-badge-" from badgeId
163+
if (badgeId.includes('assign-badge-')) badgeId = badgeId.replace('assign-badge-', '');
164+
165+
if (!updatedOrAddedBadges[badgeId]) {
166+
// Flag to check if the badge is already in the collection
167+
let included = false;
168+
const currentTs = Date.now();
169+
const currentDate = formatDate();
170+
171+
for (let i = 0; i < newBadgeCollection.length; i+=1) {
172+
const badgeObj = newBadgeCollection[i];
173+
if (badgeId === badgeObj.badge) {
174+
// If the badge is found, increment the count and mark it as included
175+
badgeObj.count = badgeObj.count ? badgeObj.count + 1 : 1;
176+
badgeObj.lastModified = currentTs;
177+
badgeObj.earnedDate.push(currentDate);
178+
included = true;
179+
// Mark this badge ID as updated so it's not added again
180+
updatedOrAddedBadges[badgeId] = true;
181+
break; // Exit loop after finding and updating the badge
182+
}
183+
}
184+
185+
// If the badge was not already in the collection, add it as a new entry
186+
if (!included) {
187+
newBadgeCollection.push({
188+
badge: badgeId,
189+
count: 1,
190+
lastModified: currentTs,
191+
earnedDate: [currentDate],
192+
});
193+
// Mark this badge ID as added
194+
updatedOrAddedBadges[badgeId] = true;
195+
}
196+
}
197+
});
198+
199+
return newBadgeCollection;
200+
};
201+
202+
export const returnUpdatedBadgesCollectionSingleUser = (badgeCollection, selectedBadgesId) => {
203+
let newBadgeCollection = Array.from(badgeCollection);
204+
205+
const updatedOrAddedBadges = {};
206+
207+
selectedBadgesId.forEach(originalBadgeId => {
208+
let badgeId = originalBadgeId;
209+
if (badgeId.includes('assign-badge-')) badgeId = badgeId.replace('assign-badge-', '');
210+
211+
if (!updatedOrAddedBadges[badgeId]) {
212+
let included = false;
213+
const currentTs = Date.now();
214+
const currentDate = formatDate();
215+
216+
newBadgeCollection = newBadgeCollection.map(badgeObj => {
217+
if (badgeId === badgeObj.badge) {
218+
if (!included) {
219+
included = true;
220+
updatedOrAddedBadges[badgeId] = true;
221+
return {
222+
...badgeObj,
223+
count: badgeObj.count ? badgeObj.count + 1 : 1,
224+
lastModified: currentTs,
225+
earnedDate: [...badgeObj.earnedDate, currentDate]
226+
};
227+
}
228+
updatedOrAddedBadges[badgeId] = true;
229+
}
230+
return badgeObj;
231+
});
232+
233+
if (!included) {
234+
newBadgeCollection.push({
235+
badge: badgeId,
236+
count: 1,
237+
lastModified: currentTs,
238+
earnedDate: [currentDate],
239+
});
240+
updatedOrAddedBadges[badgeId] = true;
241+
}
242+
}
243+
});
244+
245+
return newBadgeCollection;
246+
};
247+
248+
249+
export const assignBadgesByUserID = (userId, selectedBadges) => {
184250
return async dispatch => {
185251
if (selectedBadges.length === 0) {
186252
dispatch(
@@ -189,17 +255,13 @@ export const assignBadges = (firstName, lastName, selectedBadges) => {
189255
'danger',
190256
),
191257
);
192-
if (ALERT_DELAY === 0) {
258+
setTimeout(() => {
193259
dispatch(closeAlert());
194-
} else {
195-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY);
196-
}
260+
}, 6000);
197261
return;
198262
}
199263

200-
const userAssigned = `${firstName} ${lastName}`;
201-
202-
const res = await axios.get(ENDPOINTS.USER_PROFILE_BY_NAME(userAssigned));
264+
const res = await axios.get(ENDPOINTS.USER_PROFILE(userId));
203265

204266
if (res.data.length === 0) {
205267
dispatch(
@@ -208,18 +270,22 @@ export const assignBadges = (firstName, lastName, selectedBadges) => {
208270
'danger',
209271
),
210272
);
211-
if (ALERT_DELAY === 0) {
273+
setTimeout(() => {
212274
dispatch(closeAlert());
213-
} else {
214-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY);
215-
}
275+
}, 6000);
216276
return;
217277
}
278+
const { badgeCollection } = res.data;
279+
for (let i = 0; i < badgeCollection.length; i+=1) {
280+
badgeCollection[i].badge = badgeCollection[i].badge._id;
281+
}
218282

219-
const { badgeCollection } = res.data[0];
220-
const userToBeAssignedBadge = res.data[0]._id;
221-
const newBadgeCollection = returnUpdatedBadgesCollection(badgeCollection, selectedBadges);
222-
283+
const userToBeAssignedBadge = res.data._id;
284+
const newBadgeCollection = returnUpdatedBadgesCollectionSingleUser(
285+
badgeCollection,
286+
selectedBadges,
287+
);
288+
// send updated badgeCollection to backend
223289
const url = ENDPOINTS.BADGE_ASSIGN(userToBeAssignedBadge);
224290
try {
225291
await axios.put(url, {
@@ -232,23 +298,19 @@ export const assignBadges = (firstName, lastName, selectedBadges) => {
232298
'success',
233299
),
234300
);
235-
if (ALERT_DELAY === 0) {
301+
setTimeout(() => {
236302
dispatch(closeAlert());
237-
} else {
238-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY);
239-
}
303+
}, 6000);
240304
} catch (e) {
241305
dispatch(getMessage('Oops, something is wrong!', 'danger'));
242-
if (ALERT_DELAY === 0) {
306+
setTimeout(() => {
243307
dispatch(closeAlert());
244-
} else {
245-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY);
246-
}
308+
}, 6000);
247309
}
248310
};
249311
};
250312

251-
export const assignBadgesByUserID = (userId, selectedBadges) => {
313+
export const assignBadgesToMultipleUserID = (userIds, selectedBadges) => {
252314
return async dispatch => {
253315
if (selectedBadges.length === 0) {
254316
dispatch(
@@ -257,45 +319,80 @@ export const assignBadgesByUserID = (userId, selectedBadges) => {
257319
'danger',
258320
),
259321
);
260-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY || 0);
322+
setTimeout(() => {
323+
dispatch(closeAlert());
324+
}, 6000);
261325
return;
262326
}
263327

264328
try {
265-
const res = await axios.get(ENDPOINTS.USER_PROFILE(userId));
266-
const userData = Array.isArray(res.data) ? res.data[0] : res.data;
329+
const response = await axios.post(ENDPOINTS.BADGE_ASSIGN_MULTIPLE, {
330+
userIds,
331+
selectedBadges,
332+
});
267333

268-
if (!userData || !userData._id) {
269-
dispatch(getMessage('User data is incomplete. Cannot assign badges.', 'danger'));
270-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY || 0);
271-
return;
334+
if (response.status === 200) {
335+
dispatch(
336+
getMessage(
337+
"Awesomesauce! You've increased badges and proportionally increased life happiness for multiple users!",
338+
'success',
339+
),
340+
);
341+
} else {
342+
throw new Error('Failed to assign badges');
272343
}
344+
} catch (error) {
345+
dispatch(getMessage('Oops, something went wrong while assigning badges!', 'danger'));
346+
} finally {
347+
setTimeout(() => {
348+
dispatch(closeAlert());
349+
}, 6000);
350+
}
351+
};
352+
};
273353

274-
const badgeCollectionToSend = selectedBadges.map(badgeId => ({
275-
badge: badgeId,
276-
count: 1,
277-
lastModified: Date.now(),
278-
featured: false,
279-
earnedDate: []
280-
}));
281-
282-
await axios.put(ENDPOINTS.BADGE_ASSIGN(userData._id), {
283-
badgeCollection: badgeCollectionToSend,
284-
newBadges: selectedBadges.length
285-
});
286-
354+
export const assignBadges = (firstName, lastName, selectedBadges) => {
355+
return async dispatch => {
356+
if (selectedBadges.length === 0) {
287357
dispatch(
288358
getMessage(
289-
"Awesomesauce! Not only have you increased a person's badges, you've also proportionally increased their life happiness!",
290-
'success',
359+
"Um no, that didn't work. Badge Select Function must include actual selection of badges to work. Better luck next time!",
360+
'danger',
291361
),
292362
);
363+
setTimeout(() => {
364+
dispatch(closeAlert());
365+
}, 6000);
366+
return;
367+
}
293368

294-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY || 0);
295-
} catch (e) {
296-
toast.error('Badge assignment error:', e);
297-
dispatch(getMessage('Oops, something is wrong!', 'danger'));
298-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY || 0);
369+
const userAssigned = `${firstName} ${lastName}`;
370+
371+
try {
372+
const res = await axios.get(ENDPOINTS.USER_PROFILE_BY_NAME(userAssigned));
373+
if (res.data.length === 0) {
374+
dispatch(
375+
getMessage(
376+
"Can't find that user. Step 1 to getting badges: Be in the system. Not in the system? No badges for you!",
377+
'danger',
378+
),
379+
);
380+
setTimeout(() => {
381+
dispatch(closeAlert());
382+
}, 6000);
383+
}
384+
const userToBeAssignedBadge = res.data[0]._id;
385+
await dispatch(assignBadgesByUserID([userToBeAssignedBadge], selectedBadges));
386+
} catch (error) {
387+
dispatch(
388+
getMessage(
389+
'Oops, something is wrong!',
390+
'danger',
391+
),
392+
);
393+
setTimeout(() => {
394+
dispatch(closeAlert());
395+
}, 6000);
299396
}
300397
};
301398
};
@@ -306,30 +403,24 @@ export const sendUpdatedBadgeCollectionReq = async (
306403
selectedBadges,
307404
userToBeAssignedBadge,
308405
) => {
309-
return async dispatch => {
310-
const url = ENDPOINTS.BADGE_ASSIGN(userToBeAssignedBadge);
311-
try {
312-
await axios.put(url, { badgeCollection, newBadges: selectedBadges.length });
313-
dispatch(
314-
getMessage(
315-
"Awesomesauce! Not only have you increased a person's badges, you've also proportionally increased their life happiness!",
316-
'success',
317-
),
318-
);
319-
if (ALERT_DELAY === 0) {
320-
dispatch(closeAlert());
321-
} else {
322-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY);
323-
}
324-
} catch (e) {
325-
dispatch(getMessage('Oops, something is wrong!', 'danger'));
326-
if (ALERT_DELAY === 0) {
327-
dispatch(closeAlert());
328-
} else {
329-
setTimeout(() => dispatch(closeAlert()), ALERT_DELAY);
330-
}
331-
}
332-
};
406+
const url = ENDPOINTS.BADGE_ASSIGN(userToBeAssignedBadge);
407+
try {
408+
await axios.put(url, { badgeCollection, newBadges: selectedBadges.length });
409+
dispatch(
410+
getMessage(
411+
"Awesomesauce! Not only have you increased a person's badges, you've also proportionally increased their life happiness!",
412+
'success',
413+
),
414+
);
415+
setTimeout(() => {
416+
dispatch(closeAlert());
417+
}, 6000);
418+
} catch (e) {
419+
dispatch(getMessage('Oops, something is wrong!', 'danger'));
420+
setTimeout(() => {
421+
dispatch(closeAlert());
422+
}, 6000);
423+
}
333424
};
334425

335426
export const changeBadgesByUserID = (userId, badgeCollection) => {

0 commit comments

Comments
 (0)