Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
🐛 Make sure user has a verified email
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Sep 3, 2020
1 parent 63577fa commit 17f0756
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/_staart/rest/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,11 @@ export const inviteMemberToGroup = async (

const group = await getGroupById(groupId);
if (!group) throw new Error(ORGANIZATION_NOT_FOUND);

/**
* Only users with emails on verified domain can join this group
* Make sure that the provided email ends with the domain
*/
if (group.onlyAllowDomain) {
const emailDomain = newMemberEmail.split("@")[1];
try {
Expand All @@ -725,8 +730,11 @@ export const inviteMemberToGroup = async (
let userExists = false;
let createdUserId: number;

/**
* Check if a user with the email already exists
*/
const checkUser = await prisma.users.findMany({
where: { emails: { some: { email: newMemberEmail } } },
where: { emails: { some: { email: newMemberEmail, isVerified: true } } },
take: 1,
});
if (checkUser.length) {
Expand All @@ -746,6 +754,7 @@ export const inviteMemberToGroup = async (
).length !== 0;
createdUserId = newUser.id;
if (isMemberAlready) throw new Error(USER_IS_MEMBER_ALREADY);

await prisma.memberships.create({
data: {
user: { connect: { id: newUser.id } },
Expand All @@ -771,6 +780,13 @@ export const inviteMemberToGroup = async (
? (await getUserById(userId))?.name ?? "Someone"
: "Someone";
const userDetails = await getUserById(createdUserId);
await prisma.memberships.create({
data: {
user: { connect: { id: createdUserId } },
group: { connect: { id: groupId } },
role,
},
});
mail({
to: newMemberEmail,
template: Templates.INVITED_TO_TEAM,
Expand Down

0 comments on commit 17f0756

Please sign in to comment.