| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { ConfigService } |
| 3 | import { MailerService } |
| 4 | |
| 5 | export type ThreadTemplate |
| 6 | | 'thread-opened' |
| 7 | | 'thread-comment' |
| 8 | | 'thread-review' |
| 9 | | 'thread-assigned' |
| 10 | | 'thread-state' |
| 11 | |
| 12 | @Injectable() |
| 13 | export class MailService |
| 14 | private readonly logger |
| 15 | /** Auth forms live in the web app, so every link in an email points there. */ |
| 16 | private readonly appUrl |
| 17 | |
| 18 | constructor( |
| 19 | private readonly mailer |
| 20 | config: ConfigService |
| 21 | ) { |
| 22 | this.appUrl = |
| 23 | } |
| 24 | |
| 25 | async sendVerificationEmail( |
| 26 | to: string, |
| 27 | context: { |
| 28 | ): Promise<void |
| 29 | await this.send |
| 30 | } |
| 31 | |
| 32 | async sendVerifyAliasEmail( |
| 33 | to: string, |
| 34 | context: { |
| 35 | ): Promise<void |
| 36 | await this.send |
| 37 | } |
| 38 | |
| 39 | async sendChangeEmailEmail( |
| 40 | to: string, |
| 41 | context: { |
| 42 | ): Promise<void |
| 43 | await this.send |
| 44 | to, |
| 45 | 'Approve your new email address', |
| 46 | 'change-email', |
| 47 | context, |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | async sendResetPasswordEmail( |
| 52 | to: string, |
| 53 | context: { |
| 54 | ): Promise<void |
| 55 | await this.send |
| 56 | } |
| 57 | |
| 58 | async sendOrganizationInvitationEmail( |
| 59 | to: string, |
| 60 | context: { |
| 61 | inviter: string |
| 62 | organization: string |
| 63 | role: string |
| 64 | acceptUrl: string |
| 65 | }, |
| 66 | ): Promise<void |
| 67 | await this.send |
| 68 | to, |
| 69 | `${context.inviter} |
| 70 | 'organization-invitation', |
| 71 | context, |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | async sendRepositoryInvitationEmail( |
| 76 | to: string, |
| 77 | context: { |
| 78 | name?: string |
| 79 | inviter: string |
| 80 | repository: string |
| 81 | role: string |
| 82 | }, |
| 83 | ): Promise<void |
| 84 | await this.send |
| 85 | to, |
| 86 | `${context.inviter} |
| 87 | 'repository-invitation', |
| 88 | { ...context, |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** Every email about one issue or pull request names the same thread in `References`, so mail clients file them as one conversation. */ |
| 93 | async sendThreadEmail( |
| 94 | to: string, |
| 95 | { |
| 96 | template, |
| 97 | threadId, |
| 98 | subject, |
| 99 | context, |
| 100 | }: { |
| 101 | template: ThreadTemplate |
| 102 | threadId: string |
| 103 | subject: string |
| 104 | context: Record |
| 105 | }, |
| 106 | ): Promise<void |
| 107 | await this.send |
| 108 | references: `<${threadId |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | private async send |
| 113 | to: string, |
| 114 | subject: string, |
| 115 | template: string, |
| 116 | context: Record |
| 117 | options: { |
| 118 | ): Promise<void |
| 119 | await this.mailer |
| 120 | to, |
| 121 | subject, |
| 122 | template, |
| 123 | context: { ...context |
| 124 | ...options, |
| 125 | }); |
| 126 | this.logger.log |
| 127 | } |
| 128 | } |