From 8c7f926bd7c11d2b56dbe859890c229cef87b162 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Fri, 30 Oct 2020 14:29:53 +0530 Subject: [PATCH] :sparkles: Send membership welcome email --- src/modules/memberships/memberships.module.ts | 4 +++- .../memberships/memberships.service.ts | 22 ++++++++++++++++--- src/templates/groups/invitation.md | 9 ++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/templates/groups/invitation.md diff --git a/src/modules/memberships/memberships.module.ts b/src/modules/memberships/memberships.module.ts index 3c7c21542..1f6601e45 100644 --- a/src/modules/memberships/memberships.module.ts +++ b/src/modules/memberships/memberships.module.ts @@ -1,11 +1,13 @@ import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { EmailModule } from '../email/email.module'; import { PrismaModule } from '../prisma/prisma.module'; import { GroupMembershipController } from './memberships-group.controller'; import { UserMembershipController } from './memberships-user.controller'; import { MembershipsService } from './memberships.service'; @Module({ - imports: [PrismaModule], + imports: [PrismaModule, EmailModule, ConfigModule], controllers: [UserMembershipController, GroupMembershipController], providers: [MembershipsService], }) diff --git a/src/modules/memberships/memberships.service.ts b/src/modules/memberships/memberships.service.ts index 07a768b0c..fd760f69d 100644 --- a/src/modules/memberships/memberships.service.ts +++ b/src/modules/memberships/memberships.service.ts @@ -4,6 +4,7 @@ import { Injectable, UnauthorizedException, } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { groupsCreateInput, memberships, @@ -15,17 +16,19 @@ import { import { safeEmail } from 'src/helpers/safe-email'; import { Expose } from 'src/modules/prisma/prisma.interface'; import { AuthService } from '../auth/auth.service'; +import { EmailService } from '../email/email.service'; import { PrismaService } from '../prisma/prisma.service'; -import { UsersService } from '../users/users.service'; import { CreateMembershipInput } from './memberships.interface'; @Injectable() export class MembershipsService { constructor( private prisma: PrismaService, - private users: UsersService, private auth: AuthService, + private email: EmailService, + private configService: ConfigService, ) {} + async getMemberships(params: { skip?: number; take?: number; @@ -147,13 +150,26 @@ export class MembershipsService { }), ); if (!user) user = await this.auth.register(ipAddress, data); - return this.prisma.memberships.create({ + const result = await this.prisma.memberships.create({ data: { role: data.role, group: { connect: { id: groupId } }, user: { connect: { id: user.id } }, }, + include: { group: { select: { name: true } } }, + }); + this.email.send({ + to: `"${user.name}" <${emailSafe}>`, + template: 'groups/invitation', + data: { + name: user.name, + group: result.group.name, + link: `${this.configService.get( + 'frontendUrl', + )}/groups/${groupId}`, + }, }); + return this.prisma.expose(result); } /** Verify whether a group membership can be deleted */ diff --git a/src/templates/groups/invitation.md b/src/templates/groups/invitation.md new file mode 100644 index 000000000..cff5c06bf --- /dev/null +++ b/src/templates/groups/invitation.md @@ -0,0 +1,9 @@ +# Welcome to {{ group }} + +Hi {{name}}, + +You've joined the group **{{ group }}**. You can manage your membership by logging to your account. + +Visit this group + +If you didn't want to be added to {{ group }}, you can log in to your account and leave the group.