diff --git a/apps/api/src/intra-auth/intra-auth.module.ts b/apps/api/src/intra-auth/intra-auth.module.ts index 004a9fce..03f5b64a 100644 --- a/apps/api/src/intra-auth/intra-auth.module.ts +++ b/apps/api/src/intra-auth/intra-auth.module.ts @@ -1,30 +1,16 @@ +import { MailModule } from '@api/mail/mail.module'; import { UserModule } from '@api/user/user.module'; import { CacheModule } from '@app/common/cache/cache.module'; import { IntraAuth } from '@app/entity/intra-auth/intra-auth.entity'; import { Module } from '@nestjs/common'; -import { ConfigModule, ConfigService } from '@nestjs/config'; +import { ConfigModule } from '@nestjs/config'; import { TypeOrmModule } from '@nestjs/typeorm'; import { IntraAuthController } from './intra-auth.controller'; import { IntraAuthService } from './intra-auth.service'; -import StibeeService from './stibee.service'; @Module({ - imports: [UserModule, CacheModule, TypeOrmModule.forFeature([IntraAuth]), ConfigModule], + imports: [UserModule, CacheModule, TypeOrmModule.forFeature([IntraAuth]), ConfigModule, MailModule], controllers: [IntraAuthController], - providers: [ - IntraAuthService, - { - provide: 'MailService', - inject: [ConfigService], - useFactory: async (config: ConfigService) => { - return new StibeeService(config); - }, - }, - { - provide: 'UnsubscribeStibeeService', - useClass: StibeeService, - }, - ], - exports: [IntraAuthService], + providers: [IntraAuthService], }) export class IntraAuthModule {} diff --git a/apps/api/src/intra-auth/intra-auth.service.ts b/apps/api/src/intra-auth/intra-auth.service.ts index 3d9c181c..0a13eb00 100644 --- a/apps/api/src/intra-auth/intra-auth.service.ts +++ b/apps/api/src/intra-auth/intra-auth.service.ts @@ -1,8 +1,11 @@ import { CADET_ALREADY_EXIST_ERROR_MESSAGE, + EMAIL, NOT_EXIST_TOKEN_ERROR_MESSAGE, SIGNIN_ALREADY_AUTH_ERROR_MESSAGE, } from '@api/intra-auth/intra-auth.constant'; +import { MailService, MAIL_SERVICE_TOKEN } from '@api/mail/mail.service'; +import { UnsubscribeStibeeService, UnsubscribeStibeeServiceToken } from '@api/mail/unsubscribe-stibee.service'; import { UserService } from '@api/user/user.service'; import { CacheService } from '@app/common/cache/cache.service'; import { IntraAuthMailDto } from '@app/common/cache/dto/intra-auth.dto'; @@ -13,15 +16,18 @@ import { ForbiddenException, Inject, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { getCode } from './intra-auth.utils'; -import MailService from './mail.service'; -import UnsubscribeStibeeService from './unsubscribe-stibee.service'; @Injectable() export class IntraAuthService { constructor( - @Inject('MailService') private readonly mailService: MailService, - @Inject('UnsubscribeStibeeService') private readonly unsubscribeStibeeService: UnsubscribeStibeeService, + @Inject(MAIL_SERVICE_TOKEN) + private readonly mailService: MailService, + + @Inject(UnsubscribeStibeeServiceToken) + private readonly unsubscribeStibeeService: UnsubscribeStibeeService, + private readonly userService: UserService, + private readonly cacheService: CacheService, @InjectRepository(IntraAuth) @@ -40,7 +46,7 @@ export class IntraAuthService { await this.cacheService.setIntraAuthMailData(code, intraAuthMailDto); - await this.mailService.send(intraId, code, user.nickname); + await this.mailService.send(`${intraId}@${EMAIL}`, intraId, code, user.nickname); } async getAuth(code: string): Promise { diff --git a/apps/api/src/intra-auth/mail.service.ts b/apps/api/src/intra-auth/mail.service.ts deleted file mode 100644 index 0c2cd323..00000000 --- a/apps/api/src/intra-auth/mail.service.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default interface MailService { - send(name: string, code: string, githubId: string); -} diff --git a/apps/api/src/intra-auth/unsubscribe-stibee.service.ts b/apps/api/src/intra-auth/unsubscribe-stibee.service.ts deleted file mode 100644 index dfd69a52..00000000 --- a/apps/api/src/intra-auth/unsubscribe-stibee.service.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default interface UnsubscribeStibeeService { - unsubscribe(name: string); -} diff --git a/apps/api/src/mail/mail.module.ts b/apps/api/src/mail/mail.module.ts new file mode 100644 index 00000000..5498e0d8 --- /dev/null +++ b/apps/api/src/mail/mail.module.ts @@ -0,0 +1,23 @@ +import { Module } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { MAIL_SERVICE_TOKEN } from './mail.service'; +import StibeeService from './stibee.service'; +import { UnsubscribeStibeeServiceToken } from './unsubscribe-stibee.service'; + +@Module({ + providers: [ + { + provide: MAIL_SERVICE_TOKEN, + inject: [ConfigService], + useFactory: async (config: ConfigService) => { + return new StibeeService(config); + }, + }, + { + provide: UnsubscribeStibeeServiceToken, + useClass: StibeeService, + }, + ], + exports: [MAIL_SERVICE_TOKEN, UnsubscribeStibeeServiceToken], +}) +export class MailModule {} diff --git a/apps/api/src/mail/mail.service.ts b/apps/api/src/mail/mail.service.ts new file mode 100644 index 00000000..6fec9ca0 --- /dev/null +++ b/apps/api/src/mail/mail.service.ts @@ -0,0 +1,5 @@ +export const MAIL_SERVICE_TOKEN = Symbol('MailService'); + +export interface MailService { + send(name: string, email: string, code: string, githubId: string); +} diff --git a/apps/api/src/intra-auth/stibee.service.ts b/apps/api/src/mail/stibee.service.ts similarity index 73% rename from apps/api/src/intra-auth/stibee.service.ts rename to apps/api/src/mail/stibee.service.ts index bc8c2b84..76c915f4 100644 --- a/apps/api/src/intra-auth/stibee.service.ts +++ b/apps/api/src/mail/stibee.service.ts @@ -1,24 +1,29 @@ +import { logger } from '@app/utils/logger'; import { Injectable, InternalServerErrorException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import axios from 'axios'; -import { EMAIL } from './intra-auth.constant'; -import MailService from './mail.service'; -import UnsubscribeStibeeService from './unsubscribe-stibee.service'; +import { MailService } from './mail.service'; +import { UnsubscribeStibeeService } from './unsubscribe-stibee.service'; + @Injectable() export default class StibeeService implements MailService, UnsubscribeStibeeService { constructor(private readonly configService: ConfigService) {} private accessToken = this.configService.get('STIBEE_API_KEY'); - async send(name: string, code: string, githubId: string) { + async send(email: string, name: string, code: string, githubId: string) { await this.subscribe(name); - const url = this.configService.get('STIBEE_MAIL_SEND_URL'); + const url = this.configService.get('STIBEE_MAIL_SEND_URL'); + + await this.mailSend(url, email, name, code, githubId); + } + private async mailSend(url: string, email: string, name: string, code: string, githubId: string) { try { await axios.post( url, { - subscriber: `${name}@${EMAIL}`, + subscriber: email, name, code, githubId, @@ -67,7 +72,7 @@ export default class StibeeService implements MailService, UnsubscribeStibeeServ } private printError(err: any) { - console.error({ status: err.response.status, message: err.response.data }); - console.trace(); + logger.error({ status: err.response.status, message: err.response.data }); + logger.error(err.stack); } } diff --git a/apps/api/src/mail/unsubscribe-stibee.service.ts b/apps/api/src/mail/unsubscribe-stibee.service.ts new file mode 100644 index 00000000..0e055795 --- /dev/null +++ b/apps/api/src/mail/unsubscribe-stibee.service.ts @@ -0,0 +1,5 @@ +export const UnsubscribeStibeeServiceToken = Symbol('UnsubscribeStibee'); + +export interface UnsubscribeStibeeService { + unsubscribe(name: string); +} diff --git a/apps/api/test/e2e/intra-auth.e2e-spec.ts b/apps/api/test/e2e/intra-auth.e2e-spec.ts index daef8535..95ad74cb 100644 --- a/apps/api/test/e2e/intra-auth.e2e-spec.ts +++ b/apps/api/test/e2e/intra-auth.e2e-spec.ts @@ -2,7 +2,9 @@ import { AuthModule } from '@api/auth/auth.module'; import { AuthService } from '@api/auth/auth.service'; import { IntraAuthController } from '@api/intra-auth/intra-auth.controller'; import { IntraAuthService } from '@api/intra-auth/intra-auth.service'; -import StibeeService from '@api/intra-auth/stibee.service'; +import { MAIL_SERVICE_TOKEN } from '@api/mail/mail.service'; +import StibeeService from '@api/mail/stibee.service'; +import { UnsubscribeStibeeServiceToken } from '@api/mail/unsubscribe-stibee.service'; import { UserRepository } from '@api/user/repositories/user.repository'; import { UserModule } from '@api/user/user.module'; import { CacheService } from '@app/common/cache/cache.service'; @@ -42,11 +44,11 @@ describe('IntraAuth', () => { providers: [ IntraAuthService, { - provide: 'MailService', + provide: MAIL_SERVICE_TOKEN, useValue: instance(stibeeService), }, { - provide: 'UnsubscribeStibeeService', + provide: UnsubscribeStibeeServiceToken, useValue: instance(stibeeService), }, {