From 1777332d1bbd2b84a46ae34f7592c2a2ccd545e6 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Sun, 15 Nov 2020 22:08:46 +0530 Subject: [PATCH] :recycle: Auto-join groups based on email address --- src/modules/auth/auth.service.ts | 34 +++++++++++++++++-- .../memberships/memberships.service.ts | 2 +- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 73afee8a3..2ad603455 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -394,7 +394,37 @@ export class AuthService { const result = await this.prisma.emails.update({ where: { id }, data: { isVerified: true }, + include: { user: true }, + }); + const groupsToJoin = await this.prisma.groups.findMany({ + where: { + autoJoinDomain: true, + domains: { + some: { isVerified: true, domain: result.emailSafe.split('@')[1] }, + }, + }, + select: { id: true, name: true }, }); + for await (const group of groupsToJoin) { + await this.prisma.memberships.create({ + data: { + user: { connect: { id: result.user.id } }, + group: { connect: { id: group.id } }, + role: 'MEMBER', + }, + }); + this.email.send({ + to: `"${result.user.name}" <${result.email}>`, + template: 'groups/invitation', + data: { + name: result.user.name, + group: group.name, + link: `${this.configService.get('frontendUrl')}/groups/${ + group.id + }`, + }, + }); + } return this.prisma.expose(result); } @@ -442,7 +472,7 @@ export class AuthService { .join(', ') || 'Unknown location'; if (user.prefersEmail) this.email.send({ - to: `"${user.name}" <${user.prefersEmail.emailSafe}>`, + to: `"${user.name}" <${user.prefersEmail.email}>`, template: 'auth/used-backup-code', data: { name: user.name, @@ -566,7 +596,7 @@ export class AuthService { .join(', ') || 'Unknown location'; if (user.prefersEmail) this.email.send({ - to: `"${user.name}" <${user.prefersEmail.emailSafe}>`, + to: `"${user.name}" <${user.prefersEmail.email}>`, template: 'auth/approve-subnets', data: { name: user.name, diff --git a/src/modules/memberships/memberships.service.ts b/src/modules/memberships/memberships.service.ts index bedcd8e22..f8a6f397c 100644 --- a/src/modules/memberships/memberships.service.ts +++ b/src/modules/memberships/memberships.service.ts @@ -170,7 +170,7 @@ export class MembershipsService { include: { group: { select: { name: true } } }, }); this.email.send({ - to: `"${user.name}" <${emailSafe}>`, + to: `"${user.name}" <${data.email}>`, template: 'groups/invitation', data: { name: user.name,