ENCOA-217: Adapted the invite system to now work based on Entities instead of Users/Groups
This commit is contained in:
@@ -9,7 +9,8 @@ type Data = {
|
||||
};
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
|
||||
await db.collection("users").updateMany({}, {$set: {entities: []}});
|
||||
// await db.collection("users").updateMany({}, {$set: {entities: []}});
|
||||
await db.collection("invites").deleteMany({});
|
||||
|
||||
res.status(200).json({name: "John Doe"});
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import { CorporateUser, Group, User } from "@/interfaces/user";
|
||||
import { v4 } from "uuid";
|
||||
import { sendEmail } from "@/email";
|
||||
import { updateExpiryDateOnGroup } from "@/utils/groups.be";
|
||||
import { addUserToEntity, getEntity, getEntityWithRoles } from "@/utils/entities.be";
|
||||
import { findBy } from "@/utils";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
@@ -19,72 +21,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
|
||||
async function addToInviterGroup(user: User, invitedBy: User) {
|
||||
const invitedByGroups = await db.collection("groups").find<Group>({ admin: invitedBy.id }).toArray();
|
||||
const typeGroupName = user.type === "student" ? "Students" : user.type === "teacher" ? "Teachers" : undefined;
|
||||
|
||||
if (typeGroupName) {
|
||||
const typeGroup: Group = invitedByGroups.find((g) => g.name === typeGroupName) || {
|
||||
id: v4(),
|
||||
admin: invitedBy.id,
|
||||
name: typeGroupName,
|
||||
participants: [],
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
await db.collection("groups").updateOne(
|
||||
{ id: typeGroup.id },
|
||||
{
|
||||
$set: {
|
||||
...typeGroup,
|
||||
participants: [...typeGroup.participants.filter((x) => x !== user.id), user.id],
|
||||
},
|
||||
},
|
||||
{ upsert: true }
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
const invitationsGroup: Group = invitedByGroups.find((g) => g.name === "Invited") || {
|
||||
id: v4(),
|
||||
admin: invitedBy.id,
|
||||
name: "Invited",
|
||||
participants: [],
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
await db.collection("groups").updateOne(
|
||||
{ id: invitationsGroup.id },
|
||||
{
|
||||
$set: {
|
||||
...invitationsGroup,
|
||||
participants: [...invitationsGroup.participants.filter((x) => x !== user.id), user.id],
|
||||
}
|
||||
},
|
||||
{ upsert: true }
|
||||
);
|
||||
}
|
||||
|
||||
async function deleteFromPreviousCorporateGroups(user: User, invitedBy: User) {
|
||||
const corporatesRef = await db.collection("users").find<CorporateUser>({ type: "corporate" }).toArray();
|
||||
const corporates = corporatesRef.filter((x) => x.id !== invitedBy.id);
|
||||
|
||||
const userGroups = await db.collection("groups").find<Group>({
|
||||
participants: user.id
|
||||
}).toArray();
|
||||
|
||||
const corporateGroups = userGroups.filter((x) => corporates.map((c) => c.id).includes(x.admin));
|
||||
await Promise.all(
|
||||
corporateGroups.map(async (group) => {
|
||||
await db.collection("groups").updateOne(
|
||||
{ id: group.id },
|
||||
{ $set: { participants: group.participants.filter((x) => x !== user.id) } },
|
||||
{ upsert: true }
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
@@ -102,10 +38,11 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const invitedBy = await db.collection("users").findOne<User>({ id: invite.from});
|
||||
if (!invitedBy) return res.status(404).json({ ok: false });
|
||||
|
||||
await updateExpiryDateOnGroup(invite.to, invite.from);
|
||||
const inviteEntity = await getEntityWithRoles(invite.entity)
|
||||
if (!inviteEntity) return res.status(404).json({ ok: false });
|
||||
|
||||
if (invitedBy.type === "corporate") await deleteFromPreviousCorporateGroups(req.session.user, invitedBy);
|
||||
await addToInviterGroup(req.session.user, invitedBy);
|
||||
const defaultRole = findBy(inviteEntity.roles, 'isDefault', true)!
|
||||
await addUserToEntity(invite.to, inviteEntity.id, defaultRole.id)
|
||||
|
||||
try {
|
||||
await sendEmail(
|
||||
|
||||
@@ -8,6 +8,8 @@ import client from "@/lib/mongodb";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
import { Entity } from "@/interfaces/entity";
|
||||
import { getEntity } from "@/utils/entities.be";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
@@ -36,20 +38,19 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const invited = await db.collection("users").findOne<User>({ id: body.to});
|
||||
if (!invited) return res.status(404).json({ok: false});
|
||||
|
||||
const invitedBy = await db.collection("users").findOne<User>({ id: body.from});
|
||||
if (!invitedBy) return res.status(404).json({ok: false});
|
||||
const entity = await getEntity(body.entity)
|
||||
if (!entity) return res.status(404).json({ok: false});
|
||||
|
||||
try {
|
||||
await sendEmail(
|
||||
"receivedInvite",
|
||||
{
|
||||
name: invited.name,
|
||||
corporateName:
|
||||
invitedBy.type === "corporate" ? invitedBy.corporateInformation?.companyInformation?.name || invitedBy.name : invitedBy.name,
|
||||
entity: entity.label,
|
||||
environment: process.env.ENVIRONMENT,
|
||||
},
|
||||
[invited.email],
|
||||
"You have been invited to a group!",
|
||||
"You have been invited to an entity!",
|
||||
);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
|
||||
Reference in New Issue
Block a user