From 9dc45071c5221aeeff18d56c98f9d1c977e34b4c Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Sat, 29 Jan 2022 18:56:40 +0000 Subject: [PATCH] Fix/accept team invite once signup (#1653) * Accept team invite once signup * Add expectations for the team invite Co-authored-by: Miguel Nieto A --- pages/api/auth/signup.ts | 14 +++++++++++++- playwright/auth/auth-index.test.ts | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pages/api/auth/signup.ts b/pages/api/auth/signup.ts index 5c92ddf1d1..ed43e98e95 100644 --- a/pages/api/auth/signup.ts +++ b/pages/api/auth/signup.ts @@ -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" }); } diff --git a/playwright/auth/auth-index.test.ts b/playwright/auth/auth-index.test.ts index 674a1d2970..6bcb61aac6 100644 --- a/playwright/auth/auth-index.test.ts +++ b/playwright/auth/auth-index.test.ts @@ -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); }); });