Fix/accept team invite once signup (#1653)

* Accept team invite once signup

* Add expectations for the team invite

Co-authored-by: Miguel Nieto A <anietom@uninorte.edu.co>
pull/1652/head
Alex van Andel 2022-01-29 18:56:40 +00:00 committed by GitHub
parent 2079199bab
commit 9dc45071c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -53,7 +53,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const hashedPassword = await hashPassword(password);
await prisma.user.upsert({
const user = await prisma.user.upsert({
where: { email: userEmail },
update: {
username,
@ -69,5 +69,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
});
// If user has been invitedTo a team, we accept the membership
if (user.invitedTo) {
await prisma.membership.update({
where: {
userId_teamId: { userId: user.id, teamId: user.invitedTo },
},
data: {
accepted: true,
},
});
}
res.status(201).json({ message: "Created user" });
}

View File

@ -94,11 +94,25 @@ test.describe("Can signup from a team invite", async () => {
const createdUser = await prisma.user.findUnique({
where: { email: testUser.email },
include: {
teams: {
include: {
team: true,
},
},
},
});
// Check that the user was created
expect(createdUser).not.toBeNull();
expect(createdUser?.username).toBe(testUser.validUsername);
expect(createdUser?.password).not.toBeNull();
expect(createdUser?.emailVerified).not.toBeNull();
// Check that the user accepted the team invite
expect(createdUser?.teams).toHaveLength(1);
expect(createdUser?.teams[0].team.name).toBe(team.name);
expect(createdUser?.teams[0].team.slug).toBe(team.slug);
expect(createdUser?.teams[0].role).toBe("MEMBER");
expect(createdUser?.teams[0].accepted).toBe(true);
});
});