5
5
Injectable ,
6
6
NotFoundException ,
7
7
Req ,
8
+ UnauthorizedException ,
8
9
} from '@nestjs/common' ;
9
10
import { PreFollow , UserRequest } from '../types/custom-type' ;
10
11
import { Department , Tag , UserTag } from './department.entity' ;
@@ -33,7 +34,7 @@ export class DepartmentService {
33
34
@Req ( ) req : UserRequest ,
34
35
id : number ,
35
36
) : Promise < Department > {
36
- const department : Department = await Department . findOne ( id , {
37
+ const department : Department | undefined = await Department . findOne ( id , {
37
38
relations : [ 'tags' ] ,
38
39
} ) ;
39
40
if ( ! department ) {
@@ -44,8 +45,12 @@ export class DepartmentService {
44
45
return department ;
45
46
}
46
47
47
- async getFollow ( department : Department , user : User ) : Promise < string [ ] > {
48
+ async getFollow (
49
+ department : Department ,
50
+ user : User | undefined ,
51
+ ) : Promise < string [ ] > {
48
52
user = await User . findOne ( user ) ;
53
+ if ( ! user ) throw new UnauthorizedException ( ) ;
49
54
const tags : Tag [ ] = await Tag . find ( {
50
55
department,
51
56
} ) ;
@@ -65,23 +70,24 @@ export class DepartmentService {
65
70
id : number ,
66
71
followData : FollowDto ,
67
72
) : Promise < PreFollow > {
68
- const department : Department = await Department . findOne ( id , {
73
+ const department : Department | undefined = await Department . findOne ( id , {
69
74
relations : [ 'tags' ] ,
70
75
} ) ;
71
76
if ( ! department ) {
72
77
throw new NotFoundException ( 'There is no department with the id' ) ;
73
78
}
74
79
75
- const tag : Tag = department . tags . find (
80
+ const tag : Tag | undefined = department . tags . find (
76
81
( tag ) => tag . name === followData . follow ,
77
82
) ;
78
83
if ( ! tag ) {
79
84
throw new BadRequestException (
80
85
`There is no tag with the given name: ${ followData . follow } ` ,
81
86
) ;
82
87
}
83
- const user : User = await User . findOne ( req . user ) ;
84
- const userTag : UserTag = await UserTag . findOne ( {
88
+ const user : User | undefined = await User . findOne ( req . user ) ;
89
+ if ( ! user ) throw new UnauthorizedException ( ) ;
90
+ const userTag : UserTag | undefined = await UserTag . findOne ( {
85
91
user,
86
92
tag,
87
93
} ) ;
@@ -95,7 +101,7 @@ export class DepartmentService {
95
101
}
96
102
97
103
async createFollow (
98
- @ Req ( ) req ,
104
+ req : UserRequest ,
99
105
id : number ,
100
106
followData : FollowDto ,
101
107
) : Promise < Department > {
@@ -120,7 +126,7 @@ export class DepartmentService {
120
126
}
121
127
122
128
async deleteFollow (
123
- @ Req ( ) req ,
129
+ req : UserRequest ,
124
130
id : number ,
125
131
followData : FollowDto ,
126
132
) : Promise < Department > {
0 commit comments