| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { ConfigService } |
| 3 | import { MailerService } |
| 4 | |
| 5 | @Injectable() |
| 6 | export class MailService |
| 7 | private readonly logger |
| 8 | /** Auth forms live in the web app, so every link in an email points there. */ |
| 9 | private readonly appUrl |
| 10 | |
| 11 | constructor( |
| 12 | private readonly mailer |
| 13 | config: ConfigService |
| 14 | ) { |
| 15 | this.appUrl = |
| 16 | } |
| 17 | |
| 18 | async sendVerificationEmail( |
| 19 | to: string, |
| 20 | context: { |
| 21 | ): Promise<void |
| 22 | await this.send |
| 23 | } |
| 24 | |
| 25 | async sendVerifyAliasEmail( |
| 26 | to: string, |
| 27 | context: { |
| 28 | ): Promise<void |
| 29 | await this.send |
| 30 | } |
| 31 | |
| 32 | async sendChangeEmailEmail( |
| 33 | to: string, |
| 34 | context: { |
| 35 | ): Promise<void |
| 36 | await this.send |
| 37 | to, |
| 38 | 'Approve your new email address', |
| 39 | 'change-email', |
| 40 | context, |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | async sendResetPasswordEmail( |
| 45 | to: string, |
| 46 | context: { |
| 47 | ): Promise<void |
| 48 | await this.send |
| 49 | } |
| 50 | |
| 51 | async sendOrganizationInvitationEmail( |
| 52 | to: string, |
| 53 | context: { |
| 54 | inviter: string |
| 55 | organization: string |
| 56 | role: string |
| 57 | acceptUrl: string |
| 58 | }, |
| 59 | ): Promise<void |
| 60 | await this.send |
| 61 | to, |
| 62 | `${context.inviter} |
| 63 | 'organization-invitation', |
| 64 | context, |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | async sendRepositoryInvitationEmail( |
| 69 | to: string, |
| 70 | context: { |
| 71 | name?: string |
| 72 | inviter: string |
| 73 | repository: string |
| 74 | role: string |
| 75 | }, |
| 76 | ): Promise<void |
| 77 | await this.send |
| 78 | to, |
| 79 | `${context.inviter} |
| 80 | 'repository-invitation', |
| 81 | { ...context, |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | private async send |
| 86 | to: string, |
| 87 | subject: string, |
| 88 | template: string, |
| 89 | context: Record |
| 90 | ): Promise<void |
| 91 | await this.mailer |
| 92 | to, |
| 93 | subject, |
| 94 | template, |
| 95 | context: { ...context |
| 96 | }); |
| 97 | this.logger.log |
| 98 | } |
| 99 | } |