Removed a non sense entity ownership classroom name check I added a while back, patched the same entity + same name + same admin query

This commit is contained in:
Carlos-Mesquita
2025-01-06 20:46:18 +00:00
parent bc89f4b9ce
commit f6cd509aa4
4 changed files with 17 additions and 166 deletions

View File

@@ -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<string[]> {
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<Record<string, string>> {
const { names, userEmails } = body;

View File

@@ -169,8 +169,12 @@ async function entityCheck(body: Record<string, any>): Promise<Array<{
}
async function crossRefClassrooms(body: any) {
const { sets, entity } = body as { sets: { email: string, classroom: string }[], entity: string };
const { sets, entity, userId } = body as {
sets: { email: string, classroom: string }[],
entity: string,
userId: string
};
const pipeline = [
// Match users with the provided emails
{
@@ -189,6 +193,8 @@ async function crossRefClassrooms(body: any) {
$expr: {
$and: [
{ $in: ['$$userId', '$participants'] },
{ $eq: ['$admin', userId] },
{ $eq: ['$entity', entity] },
{
$let: {
vars: {
@@ -210,38 +216,6 @@ async function crossRefClassrooms(body: any) {
]
}
}
},
// Lookup admin's entities
{
$lookup: {
from: 'users',
let: { adminId: '$admin' },
pipeline: [
{
$match: {
$expr: { $eq: ['$id', '$$adminId'] }
}
}
],
as: 'adminInfo'
}
},
// Filter where admin has the target entity
{
$match: {
$expr: {
$in: [
entity,
{
$map: {
input: { $arrayElemAt: ['$adminInfo.entities', 0] },
as: 'entityObj',
in: '$$entityObj.id'
}
}
]
}
}
}
],
as: 'matchingGroups'