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

Commit

Permalink
♻️ Auto-join groups based on email address
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 15, 2020
1 parent d0a4ace commit 1777332
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('frontendUrl')}/groups/${
group.id
}`,
},
});
}
return this.prisma.expose<emails>(result);
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/memberships/memberships.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1777332

Please sign in to comment.