Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: mail module #294

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions apps/api/src/intra-auth/intra-auth.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
16 changes: 11 additions & 5 deletions apps/api/src/intra-auth/intra-auth.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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)
Expand All @@ -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<void | never> {
Expand Down
3 changes: 0 additions & 3 deletions apps/api/src/intra-auth/mail.service.ts

This file was deleted.

3 changes: 0 additions & 3 deletions apps/api/src/intra-auth/unsubscribe-stibee.service.ts

This file was deleted.

23 changes: 23 additions & 0 deletions apps/api/src/mail/mail.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
5 changes: 5 additions & 0 deletions apps/api/src/mail/mail.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const MAIL_SERVICE_TOKEN = Symbol('MailService');

export interface MailService {
send(name: string, email: string, code: string, githubId: string);
}
Original file line number Diff line number Diff line change
@@ -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<string>('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<string>('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,
Expand Down Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions apps/api/src/mail/unsubscribe-stibee.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const UnsubscribeStibeeServiceToken = Symbol('UnsubscribeStibee');

export interface UnsubscribeStibeeService {
unsubscribe(name: string);
}
8 changes: 5 additions & 3 deletions apps/api/test/e2e/intra-auth.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -42,11 +44,11 @@ describe('IntraAuth', () => {
providers: [
IntraAuthService,
{
provide: 'MailService',
provide: MAIL_SERVICE_TOKEN,
useValue: instance(stibeeService),
},
{
provide: 'UnsubscribeStibeeService',
provide: UnsubscribeStibeeServiceToken,
useValue: instance(stibeeService),
},
{
Expand Down