-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2.1.3
- Loading branch information
Showing
4 changed files
with
74 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,80 @@ | ||
import { | ||
HttpException, | ||
HttpStatus, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { PlaygroundService } from 'src/internal-api/playground/playground.service'; | ||
import { UserActivity } from 'src/entity/user/interface/user-activity.interface'; | ||
import { AuthV0TokenDto } from './dto/auth-v0-token.dto'; | ||
import { UserRepository } from 'src/entity/user/user.repository'; | ||
import { UserPart } from 'src/entity/user/enum/user-part.enum'; | ||
import {HttpException, HttpStatus, Injectable, UnauthorizedException,} from '@nestjs/common'; | ||
import {InjectRepository} from '@nestjs/typeorm'; | ||
import {JwtService} from '@nestjs/jwt'; | ||
import {PlaygroundService} from 'src/internal-api/playground/playground.service'; | ||
import {UserActivity} from 'src/entity/user/interface/user-activity.interface'; | ||
import {AuthV0TokenDto} from './dto/auth-v0-token.dto'; | ||
import {UserRepository} from 'src/entity/user/user.repository'; | ||
import {UserPart} from 'src/entity/user/enum/user-part.enum'; | ||
|
||
@Injectable() | ||
export class AuthV0Service { | ||
constructor( | ||
@InjectRepository(UserRepository) | ||
private userRepository: UserRepository, | ||
|
||
private playgroundService: PlaygroundService, | ||
private jwtService: JwtService, | ||
) {} | ||
constructor( | ||
@InjectRepository(UserRepository) | ||
private userRepository: UserRepository, | ||
private playgroundService: PlaygroundService, | ||
private jwtService: JwtService, | ||
) { | ||
} | ||
|
||
async loginUser(authTokenDTO: AuthV0TokenDto) { | ||
const { authToken } = authTokenDTO; | ||
async loginUser(authTokenDTO: AuthV0TokenDto) { | ||
const {authToken} = authTokenDTO; | ||
|
||
try { | ||
const { id, name, profileImage } = await this.playgroundService.getUser( | ||
authToken, | ||
); | ||
const user = await this.userRepository.getUserByOrgId(id); | ||
const userId: number = await (async () => { | ||
if (!user) { | ||
const newUser = await this.userRepository.save({ | ||
orgId: id, | ||
name, | ||
profileImage, | ||
}); | ||
try { | ||
const {id, name, profileImage} = await this.playgroundService.getUser(authToken); | ||
|
||
return newUser.id; | ||
} | ||
let user = await this.userRepository.getUserByOrgId(id); | ||
|
||
return user.id; | ||
})(); | ||
if (!user) { | ||
user = await this.userRepository.save({ | ||
orgId: id, | ||
name, | ||
profileImage, | ||
}); | ||
} | ||
|
||
const playgroundUserProfile = await this.playgroundService.getUserProfile( | ||
authToken, | ||
); | ||
const playgroundUserProfile = await this.playgroundService.getUserProfile(authToken); | ||
const playgroundUserActivities = await this.playgroundService.getUserActivities(authToken, user.orgId); | ||
|
||
|
||
const playgroundUserActivities = await this.playgroundService.getUserActivities( | ||
authToken, user.orgId | ||
); | ||
const activities: UserActivity[] = playgroundUserActivities[0].activities.flatMap((activity) => { | ||
const [generationString, partString] = activity.cardinalInfo.split(','); | ||
const generation = parseInt(generationString); | ||
const partKey = getKeyByValue(UserPart, partString); | ||
const part = UserPart[partKey]; | ||
|
||
const activities: UserActivity[] = playgroundUserActivities[0].activities.flatMap((activity) => { | ||
const [generationString, partString] = activity.cardinalInfo.split(','); | ||
const generation = parseInt(generationString); | ||
const partKey = getKeyByValue(UserPart, partString); | ||
const part = UserPart[partKey]; | ||
return { | ||
generation: generation, | ||
part: part, | ||
}; | ||
}); | ||
|
||
return { | ||
generation: generation, | ||
part: part | ||
}; | ||
}); | ||
|
||
const phone = playgroundUserProfile.phone | ||
? playgroundUserProfile.phone | ||
: null; | ||
const phone = playgroundUserProfile.phone | ||
? playgroundUserProfile.phone | ||
: null; | ||
|
||
await this.userRepository.update( | ||
{ id: userId }, | ||
{ activities, profileImage, name, phone }, | ||
); | ||
await this.userRepository.update( | ||
{id: user.id}, | ||
{activities, profileImage, name, phone}, | ||
); | ||
|
||
const payload = { name, id: userId }; | ||
const accessToken = this.jwtService.sign(payload); | ||
const payload = {name, id: user.id}; | ||
const accessToken = this.jwtService.sign(payload); | ||
|
||
return { accessToken }; | ||
} catch (error) { | ||
console.log(error); | ||
if (error.response?.status === HttpStatus.UNAUTHORIZED) { | ||
throw new UnauthorizedException({ message: '유효하지 않은 토큰' }); | ||
} | ||
return {accessToken}; | ||
} catch (error) { | ||
console.log(error); | ||
if (error.response?.status === HttpStatus.UNAUTHORIZED) { | ||
throw new UnauthorizedException({message: '유효하지 않은 토큰'}); | ||
} | ||
|
||
throw new HttpException( | ||
{ message: '로그인 서버 에러' }, | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
); | ||
throw new HttpException( | ||
{message: '로그인 서버 에러'}, | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function getKeyByValue(object: any, value: string) { | ||
return Object.keys(object).find(key => object[key] === value); | ||
return Object.keys(object).find(key => object[key] === value); | ||
} |