Skip to content

Commit

Permalink
Merge pull request #21 from mash-up-kr/refactor/user-respository
Browse files Browse the repository at this point in the history
Refactor: User모듈 레포지토리 레이어 분리
  • Loading branch information
J-Hoplin authored Jun 29, 2024
2 parents 56d440e + cb76b14 commit e56d114
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ConfigModule } from '@nestjs/config';
import { DatabaseModule } from '@src/infrastructure';
import { AiModule } from './infrastructure/ai/ai.module';
import { UsersModule } from './modules/users/users.module';
import { AuthModule } from './modules/auth/auth.module';
import { FoldersModule } from './modules/folders/folders.module';
import { LinksModule } from './modules/links/links.module';
import { PostsModule } from './modules/posts/posts.module';
Expand All @@ -22,6 +23,7 @@ import { PostsModule } from './modules/posts/posts.module';
DatabaseModule,
AiModule,
UsersModule,
AuthModule,
FoldersModule,
LinksModule,
PostsModule,
Expand Down
14 changes: 14 additions & 0 deletions src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { JwtModule } from '@nestjs/jwt';

@Module({
imports: [
JwtModule.register({
global: true,
}),
],
providers: [AuthService],
exports: [AuthService],
})
export class AuthModule {}
20 changes: 20 additions & 0 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { JwtPayload } from '@src/common/types/type';

@Injectable()
export class AuthService {
constructor(
private readonly jwt: JwtService,
private readonly config: ConfigService,
) {}

async issueAccessToken(payload: JwtPayload): Promise<string> {
const token = await this.jwt.signAsync(payload, {
secret: this.config.get<string>('JWT_SECRET'),
expiresIn: this.config.get<string>('JWT_EXPIRE_TIME'),
});
return token;
}
}
9 changes: 4 additions & 5 deletions src/modules/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { MongooseModule } from '@nestjs/mongoose';
import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy } from './guards/strategy';
import { User, UserSchema } from '@src/infrastructure';
import { UsersRepository } from './users.repository';
import { AuthModule } from '../auth/auth.module';

@Module({
imports: [
AuthModule,
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
JwtModule.register({
global: true,
}),
],
controllers: [UsersController],
providers: [UsersService, JwtStrategy],
providers: [UsersService, UsersRepository, JwtStrategy],
})
export class UsersModule {}
26 changes: 26 additions & 0 deletions src/modules/users/users.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { User } from '@src/infrastructure';
import { Model } from 'mongoose';

@Injectable()
export class UsersRepository {
constructor(
@InjectModel(User.name) private readonly userModel: Model<User>,
) {}

async findOrCreate(deviceToken: string) {
const user = await this.userModel
.findOne({
deviceToken: deviceToken,
})
.lean();
if (user) {
return user;
}
const newUser = await this.userModel.create({
deviceToken: deviceToken,
});
return newUser;
}
}
42 changes: 9 additions & 33 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,25 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateUserDto } from './dto';
import { JwtService } from '@nestjs/jwt';
import { JwtPayload } from 'src/common/types/type';
import { ConfigService } from '@nestjs/config';
import { User } from '@src/infrastructure';
import { UsersRepository } from './users.repository';
import { AuthService } from '../auth/auth.service';

@Injectable()
export class UsersService {
constructor(
@InjectModel(User.name) private readonly userModel: Model<User>,
private readonly jwt: JwtService,
private readonly config: ConfigService,
private readonly userRepository: UsersRepository,
private readonly authService: AuthService,
) {}

async createUser(dto: CreateUserDto): Promise<string> {
const user = await this.userModel
.findOneAndUpdate(
{
deviceToken: dto.deviceToken,
},
{
$set: {
deviceToken: dto.deviceToken,
},
},
{
new: true,
upsert: true,
},
)
.lean();
// 새로운 user의 ID
const user = await this.userRepository.findOrCreate(dto.deviceToken);
// JWT Token Payload
const tokenPayload: JwtPayload = {
id: user._id.toString(),
};
const token = await this.issueAccessToken(tokenPayload);
return token;
}

private async issueAccessToken(payload: JwtPayload) {
const token = await this.jwt.signAsync(payload, {
secret: this.config.get<string>('JWT_SECRET'),
expiresIn: this.config.get<string>('JWT_EXPIRE_TIME'),
});
// JWT Token 발급
const token = await this.authService.issueAccessToken(tokenPayload);
return token;
}
}

0 comments on commit e56d114

Please sign in to comment.