diff --git a/server/src/auth/v0/auth-v0.service.ts b/server/src/auth/v0/auth-v0.service.ts index 6c60ffb1..d4612eab 100644 --- a/server/src/auth/v0/auth-v0.service.ts +++ b/server/src/auth/v0/auth-v0.service.ts @@ -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); } \ No newline at end of file