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