-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.service.ts
94 lines (74 loc) · 2.71 KB
/
user.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { Repository } from 'typeorm';
import { JwtService } from '@nestjs/jwt';
import { KakaoauthService } from 'src/kakaoauth/kakaoauth.service';
import { MbtiService } from 'src/mbti/mbti.service';
import { CharacterService } from 'src/character/character.service';
@Injectable()
export class UserService {
constructor(
@InjectRepository(User) private repository: Repository<User>,
private readonly jwtService: JwtService,
private readonly mbtiService: MbtiService,
private readonly characterService: CharacterService,
private readonly kakaoauthService: KakaoauthService
) { }
async createToken(claimPlain): Promise<{ accessToken: string }> {
return {
accessToken: await this.jwtService.signAsync(JSON.stringify(claimPlain)),
};
}
async join(kAccessToken: string) {
const kakaoProfile = await this.kakaoauthService.getProile(kAccessToken);
const userProfile = await this.repository.findOne({
where: { uid: kakaoProfile.id },
select: { user_mbti: true, user_name: true, uid: true }
})
if (userProfile) return await this.createToken(userProfile);
await this.repository.createQueryBuilder().insert().into(User).values([
{ uid: kakaoProfile.id, user_name: kakaoProfile.properties.nickname }
]).execute();
return await this.createToken(await this.profile(kakaoProfile.id))
}
async verifyToken(token: string) {
try {
return this.jwtService.verify(token);
} catch (e) {
throw new UnauthorizedException();
}
}
logout() {
return 'logout service'
}
users() {
return this.repository.createQueryBuilder('user_info').getMany();
}
async profile(uid: number) {
return await this.repository.findOneOrFail({
where: { uid: uid },
select: {
user_mbti: true, user_name: true, uid: true, character_id: true,
}
})
}
async createMBTI(token: string, mbti: string) {
this.verifyToken(token)
const uid = this.jwtService.decode(token)['uid']
await this.repository.update({ uid }, { user_mbti: mbti })
return await this.createToken(await this.profile(uid))
}
async getMatch(mbti: string) {
return this.mbtiService.getMatch(mbti)
}
async createTarget(token: string, character_id: number) {
this.verifyToken(token)
const uid = this.jwtService.decode(token)['uid']
await this.repository.update({ uid }, { character_id })
return this.characterService.characterById(character_id)
}
async getCharacterById(character_id: number) {
return this.characterService.characterById(character_id)
}
}