ENCOA-310
This commit is contained in:
@@ -19,6 +19,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
else if (req.method === 'POST') {
|
||||
switch (op) {
|
||||
case 'existantGroupIds':
|
||||
res.status(200).json(await existantGroupIds(req.body.names));
|
||||
break;
|
||||
case 'crossRefOwnership':
|
||||
res.status(200).json(await crossRefOwnership(req.body));
|
||||
break;
|
||||
@@ -41,7 +44,7 @@ async function crossRefOwnership(body: any): Promise<string[]> {
|
||||
const { userId, classrooms, entity } = body;
|
||||
|
||||
const existingClassrooms = await db.collection('groups')
|
||||
.find({
|
||||
.find({
|
||||
name: { $in: classrooms },
|
||||
admin: { $ne: userId }
|
||||
})
|
||||
@@ -61,7 +64,7 @@ async function crossRefOwnership(body: any): Promise<string[]> {
|
||||
|
||||
const adminEntitiesMap = new Map(
|
||||
adminUsers.map(admin => [
|
||||
admin.id,
|
||||
admin.id,
|
||||
admin.entities?.map((e: any) => e.id) || []
|
||||
])
|
||||
);
|
||||
@@ -104,7 +107,6 @@ async function getIds(body: any): Promise<Record<string, string>> {
|
||||
|
||||
async function deletePriorEntitiesGroups(body: any) {
|
||||
const { ids, entity } = body;
|
||||
|
||||
if (!Array.isArray(ids) || ids.length === 0 || !entity) {
|
||||
return;
|
||||
}
|
||||
@@ -123,8 +125,33 @@ async function deletePriorEntitiesGroups(body: any) {
|
||||
return;
|
||||
}
|
||||
|
||||
db.collection('groups').updateMany(
|
||||
const affectedGroups = await db.collection('groups')
|
||||
.find({ participants: { $in: toDeleteUserIds } })
|
||||
.project({ id: 1, _id: 0 })
|
||||
.toArray();
|
||||
|
||||
await db.collection('groups').updateMany(
|
||||
{ participants: { $in: toDeleteUserIds } },
|
||||
{ $pull: { participants: { $in: toDeleteUserIds } } } as any
|
||||
);
|
||||
|
||||
// delete groups that were updated and have no participants
|
||||
await db.collection('groups').deleteMany({
|
||||
id: { $in: affectedGroups.map(g => g.id) },
|
||||
participants: { $size: 0 }
|
||||
});
|
||||
}
|
||||
|
||||
async function existantGroupIds(names: string[]) {
|
||||
const existingGroups = await db.collection('groups')
|
||||
.find({
|
||||
name: { $in: names }
|
||||
})
|
||||
.project({ id: 1, name: 1, _id: 0 })
|
||||
.toArray();
|
||||
|
||||
return existingGroups.reduce((acc, group) => {
|
||||
acc[group.name] = group.id;
|
||||
return acc;
|
||||
}, {} as Record<string, string>);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user