From 4210c89a50cf648ebe4de29040c1d9c9b580d446 Mon Sep 17 00:00:00 2001 From: ttpl-rt-217 Date: Fri, 13 Dec 2024 19:21:10 +0530 Subject: [PATCH 1/4] task #2787:- If any new session is added to a center, all the learners at that center should be notified about the new session. --- src/components/SessionCard.tsx | 43 +++++++++++++++++++++++++++++++++- src/hooks/useNotification.tsx | 4 ++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/components/SessionCard.tsx b/src/components/SessionCard.tsx index b8f75cf1..66317d32 100644 --- a/src/components/SessionCard.tsx +++ b/src/components/SessionCard.tsx @@ -14,6 +14,11 @@ import CheckCircleTwoToneIcon from '@mui/icons-material/CheckCircleTwoTone'; import SensorsTwoToneIcon from '@mui/icons-material/SensorsTwoTone'; import CircleTwoToneIcon from '@mui/icons-material/CircleTwoTone'; import { EventStatus } from '@/utils/app.constant'; +import { getMyCohortMemberList } from '@/services/MyClassDetailsService'; +import useNotification from '@/hooks/useNotification'; +import { useRouter } from 'next/router'; +import { Role } from '@/utils/app.constant'; + const SessionsCard: React.FC = ({ data, @@ -41,6 +46,10 @@ const SessionsCard: React.FC = ({ const [showEdit, setShowEdit] = React.useState(false); const [editSession, setEditSession] = React.useState(); const [eventStatus, setEventStatus] = React.useState(''); + const router = useRouter(); + const { cohortId }: any = router.query; + + const { getNotification } = useNotification(); const handleEditSelection = (selection: string) => { setEditSelection(selection); @@ -144,12 +153,44 @@ const SessionsCard: React.FC = ({ setModalOpen(true); }; - const onUpdateClick = () => { + const onUpdateClick = async() => { console.log('update the event'); setUpdateEvent(true); // if (isEventUpdated) { // isEventUpdated(); // } + if (cohortId) { + const filters = { + cohortId, + // role: Role.STUDENT, + // status: [Status.ACTIVE], + }; + + try { + const response = await getMyCohortMemberList({ + // limit: 20, + // page: 0, + filters, + }); + const replacements = { + "{sessionName}": "Home Science" + } + + if (response?.result?.userDetails) { + const deviceId = response?.result?.userDetails + .filter((user: any) => user.role === Role.TEACHER || user.role === Role.STUDENT) + .map((user: any) => user.deviceId) + .filter((id: any) => id !== null); + if (deviceId?.length > 0) { + getNotification(deviceId, "SESSION_UPDATE_NOTIFICATION", replacements); + } else { + console.warn("No valid device IDs found. Skipping notification API call."); + } + } + } catch (error) { + console.error("Error fetching cohort member list:", error); + } + } }; const subject = data?.metadata?.subject; diff --git a/src/hooks/useNotification.tsx b/src/hooks/useNotification.tsx index 7d681d55..23e9adc9 100644 --- a/src/hooks/useNotification.tsx +++ b/src/hooks/useNotification.tsx @@ -2,7 +2,7 @@ import { readUserId, sendCredentialService } from "@/services/NotificationServi import { useCallback } from "react"; const useNotification = () => { - const getNotification = useCallback(async ( deviceId: any, key: any, ) => { + const getNotification = useCallback(async (deviceId: any, key: any, replacements?: any ) => { try { @@ -10,7 +10,7 @@ const useNotification = () => { isQueue: false, context: "USER", key: key, - // replacements: replacements, + replacements: replacements, push: { receipients: deviceId, }, From 29d18a158764a475dfcdcd4fbc53382b51ba1643 Mon Sep 17 00:00:00 2001 From: ttpl-rt-217 Date: Fri, 13 Dec 2024 19:31:38 +0530 Subject: [PATCH 2/4] task #2787:- If any new session is added to a center, all the learners at that center should be notified about the new session --- src/components/SessionCard.tsx | 36 +++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/components/SessionCard.tsx b/src/components/SessionCard.tsx index 66317d32..bf8a777c 100644 --- a/src/components/SessionCard.tsx +++ b/src/components/SessionCard.tsx @@ -62,12 +62,46 @@ const SessionsCard: React.FC = ({ const handleClose = () => setOpen(false); - const onEventDeleted = () => { + const onEventDeleted = async () => { setOpen(false); setEventDeleted(true); if (isEventDeleted) { isEventDeleted(); } + + if (cohortId) { + const filters = { + cohortId, + // role: Role.STUDENT, + // status: [Status.ACTIVE], + }; + + try { + const response = await getMyCohortMemberList({ + // limit: 20, + // page: 0, + filters, + }); + const replacements = { + "{sessionName}": "Home Science" + } + + if (response?.result?.userDetails) { + const deviceId = response?.result?.userDetails + .filter((user: any) => user.role === Role.TEACHER || user.role === Role.STUDENT) + .map((user: any) => user.deviceId) + .filter((id: any) => id !== null); + if (deviceId?.length > 0) { + getNotification(deviceId, "SESSION_DELETION_NOTIFICATION", replacements); + } else { + console.warn("No valid device IDs found. Skipping notification API call."); + } + } + } catch (error) { + console.error("Error fetching cohort member list:", error); + } + } + }; const onEventUpdated = () => { From 20fa1796bf402d43bb13a7cf7f2dfb59bf71b675 Mon Sep 17 00:00:00 2001 From: ttpl-rt-217 Date: Sun, 15 Dec 2024 00:47:46 +0530 Subject: [PATCH 3/4] task #2787:- If any new session is added to a center, all the learners at that center should be notified about the new session --- src/components/SessionCard.tsx | 135 ++++++++++++++++----------------- 1 file changed, 67 insertions(+), 68 deletions(-) diff --git a/src/components/SessionCard.tsx b/src/components/SessionCard.tsx index bf8a777c..6f3e711a 100644 --- a/src/components/SessionCard.tsx +++ b/src/components/SessionCard.tsx @@ -62,6 +62,49 @@ const SessionsCard: React.FC = ({ const handleClose = () => setOpen(false); + + + + + + + + + const handleCohortNotification = async ( + cohortId: string, + notificationType: string, + replacements: Record + ) => { + const filters = { cohortId }; + + try { + const response = await getMyCohortMemberList({ filters }); + + if (response?.result?.userDetails) { + const deviceIds = response.result.userDetails + .filter((user: any) => user.role === Role.TEACHER || user.role === Role.STUDENT) + .map((user: any) => user.deviceId) + .filter((id: any) => id !== null); + + if (deviceIds.length > 0) { + getNotification(deviceIds, notificationType, replacements); + } else { + console.warn("No valid device IDs found. Skipping notification API call."); + } + } + } catch (error) { + console.error(`Error fetching cohort member list for ${notificationType}:`, error); + } + }; + + + + + + + + + const onEventDeleted = async () => { setOpen(false); setEventDeleted(true); @@ -70,38 +113,15 @@ const SessionsCard: React.FC = ({ } if (cohortId) { - const filters = { - cohortId, - // role: Role.STUDENT, - // status: [Status.ACTIVE], + const replacements = { + "{sessionName}": subject && sessionTitle + ? `${toPascalCase(subject)} - ${sessionTitle}` + : subject + ? toPascalCase(subject) + : toPascalCase(sessionTitle) }; - - try { - const response = await getMyCohortMemberList({ - // limit: 20, - // page: 0, - filters, - }); - const replacements = { - "{sessionName}": "Home Science" - } - - if (response?.result?.userDetails) { - const deviceId = response?.result?.userDetails - .filter((user: any) => user.role === Role.TEACHER || user.role === Role.STUDENT) - .map((user: any) => user.deviceId) - .filter((id: any) => id !== null); - if (deviceId?.length > 0) { - getNotification(deviceId, "SESSION_DELETION_NOTIFICATION", replacements); - } else { - console.warn("No valid device IDs found. Skipping notification API call."); - } - } - } catch (error) { - console.error("Error fetching cohort member list:", error); - } - } - + await handleCohortNotification(cohortId, "SESSION_DELETION_NOTIFICATION", replacements); + } }; const onEventUpdated = () => { @@ -187,44 +207,19 @@ const SessionsCard: React.FC = ({ setModalOpen(true); }; - const onUpdateClick = async() => { - console.log('update the event'); + const onUpdateClick = async () => { + console.log("update the event"); setUpdateEvent(true); - // if (isEventUpdated) { - // isEventUpdated(); - // } if (cohortId) { - const filters = { - cohortId, - // role: Role.STUDENT, - // status: [Status.ACTIVE], + const replacements = { + "{sessionName}": subject && sessionTitle + ? `${toPascalCase(subject)} - ${sessionTitle}` + : subject + ? toPascalCase(subject) + : toPascalCase(sessionTitle) }; - - try { - const response = await getMyCohortMemberList({ - // limit: 20, - // page: 0, - filters, - }); - const replacements = { - "{sessionName}": "Home Science" - } - - if (response?.result?.userDetails) { - const deviceId = response?.result?.userDetails - .filter((user: any) => user.role === Role.TEACHER || user.role === Role.STUDENT) - .map((user: any) => user.deviceId) - .filter((id: any) => id !== null); - if (deviceId?.length > 0) { - getNotification(deviceId, "SESSION_UPDATE_NOTIFICATION", replacements); - } else { - console.warn("No valid device IDs found. Skipping notification API call."); - } - } - } catch (error) { - console.error("Error fetching cohort member list:", error); - } - } + await handleCohortNotification(cohortId, "SESSION_UPDATE_NOTIFICATION", replacements); + } }; const subject = data?.metadata?.subject; @@ -261,7 +256,7 @@ const SessionsCard: React.FC = ({ ? `${toPascalCase(subject)} - ${sessionTitle}` : subject ? toPascalCase(subject) - : toPascalCase(sessionTitle)}{' '} + : toPascalCase(sessionTitle)} = ({ From 561a323a8c6c0e469567a5deb818c9afbf0248d8 Mon Sep 17 00:00:00 2001 From: ttpl-rt-217 Date: Sun, 15 Dec 2024 00:52:21 +0530 Subject: [PATCH 4/4] task #2787:- If any new session is added to a center, all the learners at that center should be notified about the new session --- src/components/SessionCard.tsx | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/components/SessionCard.tsx b/src/components/SessionCard.tsx index 6f3e711a..b1f68897 100644 --- a/src/components/SessionCard.tsx +++ b/src/components/SessionCard.tsx @@ -62,14 +62,6 @@ const SessionsCard: React.FC = ({ const handleClose = () => setOpen(false); - - - - - - - - const handleCohortNotification = async ( cohortId: string, notificationType: string, @@ -96,15 +88,7 @@ const SessionsCard: React.FC = ({ console.error(`Error fetching cohort member list for ${notificationType}:`, error); } }; - - - - - - - - - + const onEventDeleted = async () => { setOpen(false); setEventDeleted(true);