From f6cd509aa4328da968b72108a78a2babaf695d23 Mon Sep 17 00:00:00 2001 From: Carlos-Mesquita Date: Mon, 6 Jan 2025 20:46:18 +0000 Subject: [PATCH] Removed a non sense entity ownership classroom name check I added a while back, patched the same entity + same name + same admin query --- src/components/ImportSummaries/Classroom.tsx | 48 +---------------- .../Imports/StudentClassroomTransfer.tsx | 51 +++---------------- src/pages/api/groups/controller.ts | 42 --------------- src/pages/api/users/controller.ts | 42 +++------------ 4 files changed, 17 insertions(+), 166 deletions(-) diff --git a/src/components/ImportSummaries/Classroom.tsx b/src/components/ImportSummaries/Classroom.tsx index 215d7d53..c583d727 100644 --- a/src/components/ImportSummaries/Classroom.tsx +++ b/src/components/ImportSummaries/Classroom.tsx @@ -21,7 +21,6 @@ const ClassroomImportSummary: React.FC<{ state: ClassroomTransferState }> = ({ s const [showNotFoundModal, setShowNotFoundModal] = useState(false); const [showOtherEntityModal, setShowOtherEntityModal] = useState(false); const [showAlreadyInClassModal, setShowAlreadyInClassModal] = useState(false); - const [showNotOwnedModal, setShowNotOwnedModal] = useState(false); const [showMismatchesModal, setShowMismatchesModal] = useState(false); const errorCount = state.parsedExcel?.errors ? @@ -114,21 +113,6 @@ const ClassroomImportSummary: React.FC<{ state: ClassroomTransferState }> = ({ s )} - {state.notOwnedClassrooms.length > 0 && ( -
-
- - {`${state.notOwnedClassrooms.length} classroom${state.notOwnedClassrooms.length !== 1 ? 's' : ''} not owned`} -
- -
- )} - {state.duplicatedRows.length > 0 && (
@@ -176,8 +160,7 @@ const ClassroomImportSummary: React.FC<{ state: ClassroomTransferState }> = ({ s
{(state.duplicatedRows.length > 0 || state.userMismatches.length > 0 || errorCount > 0 || - state.notFoundUsers.length > 0 || state.otherEntityUsers.length > 0 || - state.notOwnedClassrooms.length > 0) && ( + state.notFoundUsers.length > 0 || state.otherEntityUsers.length > 0) && (
@@ -202,16 +185,6 @@ const ClassroomImportSummary: React.FC<{ state: ClassroomTransferState }> = ({ s
)} - {state.notOwnedClassrooms.length > 0 && ( -
  • -
    - {state.notOwnedClassrooms.length} classrooms not owned: -
    - {state.notOwnedClassrooms.join(', ')} -
    -
    -
  • - )} {state.duplicatedRows.length > 0 && (
  • @@ -288,25 +261,6 @@ const ClassroomImportSummary: React.FC<{ state: ClassroomTransferState }> = ({ s - setShowNotOwnedModal(false)}> - <> -
    - -

    Classrooms Not Owned

    -
    -
    - {state.notOwnedClassrooms.map(classroom => ( -
    - {classroom} -
    - ))} -
    - -
    - setShowDuplicatesModal(false)}> <>
    diff --git a/src/components/Imports/StudentClassroomTransfer.tsx b/src/components/Imports/StudentClassroomTransfer.tsx index dcbb1157..0e21d8ff 100644 --- a/src/components/Imports/StudentClassroomTransfer.tsx +++ b/src/components/Imports/StudentClassroomTransfer.tsx @@ -4,7 +4,6 @@ import Modal from "../Modal"; import { useFilePicker } from "use-file-picker"; import readXlsxFile from "read-excel-file"; import countryCodes from "country-codes-list"; -import { ExcelUserDuplicatesMap } from "../ImportSummaries/User"; import { UserImport } from "@/interfaces/IUserImport"; import axios from "axios"; import { toast } from "react-toastify"; @@ -47,8 +46,6 @@ export interface ClassroomTransferState { notFoundUsers: UserImport[]; otherEntityUsers: UserImport[]; alreadyInClass: UserImport[]; - notOwnedClassrooms: string[]; - validClassrooms: string[]; } @@ -66,8 +63,6 @@ const StudentClassroomTransfer: React.FC<{ user: User; entities?: EntityWithRole notFoundUsers: [], otherEntityUsers: [], alreadyInClass: [], - notOwnedClassrooms: [], - validClassrooms: [] }) const router = useRouter(); @@ -322,7 +317,8 @@ const StudentClassroomTransfer: React.FC<{ user: User; entities?: EntityWithRole email: info.email, classroom: info.groupName })), - entity + entity, + userId: user.id }); const excludeEmails = new Set([ @@ -361,38 +357,7 @@ const StudentClassroomTransfer: React.FC<{ user: User; entities?: EntityWithRole crossRefUsers(); } - }, [classroomTransferState.imports, user.entities, classroomTransferState.stage, entity]) - - // Stage 3 - Classroom Filter - // - See if there are classrooms with same name but different admin - // - Find which new classrooms need to be created - useEffect(() => { - const crossRefClassrooms = async () => { - const classrooms = Array.from(new Set(classroomTransferState.imports.map((i) => i.groupName))); - - try { - const { data: notOwnedClassroomsSameName } = await axios.post("/api/groups/controller?op=crossRefOwnership", { - userId: user.id, - classrooms - }); - - setClassroomTransferState((prev) => ({ - ...prev, - stage: 3, - notOwnedClassrooms: notOwnedClassroomsSameName, - validClassrooms: Array.from(classrooms).filter( - (name) => !new Set(notOwnedClassroomsSameName).has(name) - ) - })) - } catch (error) { - toast.error("Something went wrong, please try again later!"); - } - }; - if (classroomTransferState.imports.length > 0 && classroomTransferState.stage === 2) { - crossRefClassrooms(); - } - }, [classroomTransferState.imports, classroomTransferState.stage, user.id, entity]) - + }, [classroomTransferState.imports, user.entities, classroomTransferState.stage, entity, user.id]) const clearAndReset = () => { setIsLoading(false); @@ -405,8 +370,6 @@ const StudentClassroomTransfer: React.FC<{ user: User; entities?: EntityWithRole notFoundUsers: [], otherEntityUsers: [], alreadyInClass: [], - notOwnedClassrooms: [], - validClassrooms: [] }); clear(); }; @@ -425,6 +388,8 @@ const StudentClassroomTransfer: React.FC<{ user: User; entities?: EntityWithRole try { setIsLoading(true); + const classrooms = Array.from(new Set(classroomTransferState.imports.map((i) => i.groupName))); + const getIds = async () => { try { const { data: emailIdMap } = await axios.post("/api/users/controller?op=getIds", { @@ -458,7 +423,7 @@ const StudentClassroomTransfer: React.FC<{ user: User; entities?: EntityWithRole }); const { data: existingGroupsMap } = await axios.post("/api/groups/controller?op=existantGroupIds", { - names: classroomTransferState.validClassrooms + names: classrooms }); const groupedUsers = imports.reduce((acc, user) => { @@ -475,7 +440,7 @@ const StudentClassroomTransfer: React.FC<{ user: User; entities?: EntityWithRole const newGroupUsers = Object.fromEntries( Object.entries(groupedUsers) .filter(([groupName]) => - classroomTransferState.validClassrooms.includes(groupName) && + classrooms.includes(groupName) && !existingGroupsMap[groupName] ) ); @@ -496,7 +461,7 @@ const StudentClassroomTransfer: React.FC<{ user: User; entities?: EntityWithRole const allExistingUsers = Object.fromEntries( Object.entries(groupedUsers) .filter(([groupName]) => - !classroomTransferState.validClassrooms.includes(groupName) || + !classrooms.includes(groupName) || existingGroupsMap[groupName] ) ); diff --git a/src/pages/api/groups/controller.ts b/src/pages/api/groups/controller.ts index 6fe67631..f17739f2 100644 --- a/src/pages/api/groups/controller.ts +++ b/src/pages/api/groups/controller.ts @@ -22,9 +22,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { case 'existantGroupIds': res.status(200).json(await existantGroupIds(req.body.names)); break; - case 'crossRefOwnership': - res.status(200).json(await crossRefOwnership(req.body)); - break; case 'getIds': res.status(200).json(await getIds(req.body)); break; @@ -40,45 +37,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { } } -async function crossRefOwnership(body: any): Promise { - const { userId, classrooms, entity } = body; - - const existingClassrooms = await db.collection('groups') - .find({ - name: { $in: classrooms }, - admin: { $ne: userId } - }) - .project({ name: 1, admin: 1, _id: 0 }) - .toArray(); - - if (existingClassrooms.length === 0) { - return []; - } - - const adminUsers = await db.collection('users') - .find({ - id: { $in: existingClassrooms.map(classroom => classroom.admin) } - }) - .project({ id: 1, entities: 1, _id: 0 }) - .toArray(); - - const adminEntitiesMap = new Map( - adminUsers.map(admin => [ - admin.id, - admin.entities?.map((e: any) => e.id) || [] - ]) - ); - - return Array.from(new Set( - existingClassrooms - .filter(classroom => { - const adminEntities = adminEntitiesMap.get(classroom.admin) || []; - return adminEntities.includes(entity); - }) - .map(classroom => classroom.name) - )); -} - async function getIds(body: any): Promise> { const { names, userEmails } = body; diff --git a/src/pages/api/users/controller.ts b/src/pages/api/users/controller.ts index 8bd507e7..7aed0a7f 100644 --- a/src/pages/api/users/controller.ts +++ b/src/pages/api/users/controller.ts @@ -169,8 +169,12 @@ async function entityCheck(body: Record): Promise