From 6d35750784efe3c745c5a18f3e4a01f809d1e062 Mon Sep 17 00:00:00 2001 From: sean-brydon <55134778+sean-brydon@users.noreply.github.com> Date: Tue, 11 Jul 2023 09:30:45 +0100 Subject: [PATCH] feat: Auto accept on org verify (#10037) Co-authored-by: Leo Giovanetti --- .../organizations/adminVerify.handler.ts | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/packages/trpc/server/routers/viewer/organizations/adminVerify.handler.ts b/packages/trpc/server/routers/viewer/organizations/adminVerify.handler.ts index c3f0359b7e..e773658449 100644 --- a/packages/trpc/server/routers/viewer/organizations/adminVerify.handler.ts +++ b/packages/trpc/server/routers/viewer/organizations/adminVerify.handler.ts @@ -57,5 +57,69 @@ export const adminVerifyHandler = async ({ input }: AdminVerifyOptions) => { }, }); - return { ok: true, message: "Verified Organization" }; + const foundUsersWithMatchingEmailDomain = await prisma.user.findMany({ + where: { + email: { + endsWith: acceptedEmailDomain, + }, + teams: { + some: { + teamId: input.orgId, + }, + }, + }, + select: { + id: true, + email: true, + }, + }); + + const userIds = foundUsersWithMatchingEmailDomain.map((user) => user.id); + const userEmails = foundUsersWithMatchingEmailDomain.map((user) => user.email); + + await prisma.$transaction([ + prisma.membership.updateMany({ + where: { + userId: { + in: userIds, + }, + OR: [ + { + team: { + parentId: input.orgId, + }, + }, + { + teamId: input.orgId, + }, + ], + }, + data: { + accepted: true, + }, + }), + prisma.user.updateMany({ + where: { + id: { + in: userIds, + }, + }, + data: { + organizationId: input.orgId, + }, + }), + prisma.verificationToken.deleteMany({ + where: { + identifier: { + in: userEmails, + }, + teamId: input.orgId, + }, + }), + ]); + + return { + ok: true, + message: `Verified Organization - Auto accepted all members ending in ${acceptedEmailDomain}`, + }; };