@@ -3,7 +3,7 @@ import { AnnalsService } from './annals.service';
3
3
import { UeService } from '../ue.service' ;
4
4
import { Response as ExpressResponse } from 'express' ;
5
5
import { UUIDParam } from '../../app.pipe' ;
6
- import { GetUser , RequireUserType } from '../../auth/decorator' ;
6
+ import { GetUser , RequireApiPermission } from '../../auth/decorator' ;
7
7
import { AppException , ERROR_CODE } from '../../exceptions' ;
8
8
import { FileSize , MulterWithMime , UploadRoute , UserFile } from '../../upload.interceptor' ;
9
9
import { CommentStatus } from '../comments/interfaces/comment.interface' ;
@@ -26,7 +26,7 @@ export class AnnalsController {
26
26
constructor ( readonly annalsService : AnnalsService , readonly ueService : UeService ) { }
27
27
28
28
@Get ( )
29
- @RequireUserType ( 'STUDENT' , 'FORMER_STUDENT ')
29
+ @RequireApiPermission ( 'API_SEE_ANNALS ')
30
30
@ApiOperation ( { description : 'Get the list of annals of a UE.' } )
31
31
@ApiOkResponse ( { type : UeAnnalResDto , isArray : true } )
32
32
@ApiAppErrorResponse ( ERROR_CODE . NO_SUCH_UE , 'Thrown when there is no UE with code `ueCode`.' )
@@ -36,14 +36,14 @@ export class AnnalsController {
36
36
@GetPermissions ( ) permissions : PermissionManager ,
37
37
) : Promise < UeAnnalResDto [ ] > {
38
38
if ( ! ( await this . ueService . doesUeExist ( ueCode ) ) ) throw new AppException ( ERROR_CODE . NO_SUCH_UE , ueCode ) ;
39
- return this . annalsService . getUeAnnalsList ( user , ueCode , permissions . can ( Permission . API_MODERATE_ANNAL ) ) ;
39
+ return this . annalsService . getUeAnnalsList ( user , ueCode , permissions . can ( Permission . API_MODERATE_ANNALS ) ) ;
40
40
}
41
41
42
42
@Post ( )
43
- @RequireUserType ( 'STUDENT ')
43
+ @RequireApiPermission ( 'API_UPLOAD_ANNALS ')
44
44
@ApiOperation ( {
45
45
description :
46
- 'Create an annal. User must have done the UE, or have the permission `annalUploader `. Metadata of the annal will be created, but the file will not actually exist. To upload the file, see `PUT /v1/ue/annals/:annalId`.' ,
46
+ 'Create an annal. User must have done the UE, or have the permission `API_MODERATE_ANNALS `. Metadata of the annal will be created, but the file will not actually exist. To upload the file, see `PUT /v1/ue/annals/:annalId`.' ,
47
47
} )
48
48
@ApiOkResponse ( { type : UeAnnalResDto } )
49
49
@ApiAppErrorResponse ( ERROR_CODE . NO_SUCH_UE , 'Thrown when there is no UE with code `ueCode`.' )
@@ -53,28 +53,30 @@ export class AnnalsController {
53
53
)
54
54
@ApiAppErrorResponse (
55
55
ERROR_CODE . NOT_DONE_UE_IN_SEMESTER ,
56
- 'User has not done the UE and is not an `annalUploader `, and thus cannot upload an annal for this UE.' ,
56
+ 'User has not done the UE and is not an `API_MODERATE_ANNALS `, and thus cannot upload an annal for this UE.' ,
57
57
)
58
58
async createUeAnnal (
59
59
@Body ( ) { ueCode, semester, typeId, ueof } : CreateAnnalReqDto ,
60
60
@GetUser ( ) user : User ,
61
61
@GetPermissions ( ) permissions : PermissionManager ,
62
62
) : Promise < UeAnnalResDto > {
63
- if ( ueof && permissions . can ( Permission . API_UPLOAD_ANNAL ) )
64
- throw new AppException ( ERROR_CODE . FORBIDDEN_NOT_ENOUGH_API_PERMISSIONS , Permission . API_UPLOAD_ANNAL ) ;
63
+ if ( ueof && ! permissions . can ( Permission . API_MODERATE_ANNALS ) )
64
+ throw new AppException ( ERROR_CODE . FORBIDDEN_NOT_ENOUGH_API_PERMISSIONS , Permission . API_MODERATE_ANNALS ) ;
65
65
if ( ! ueof && ! ( await this . ueService . doesUeExist ( ueCode ) ) ) throw new AppException ( ERROR_CODE . NO_SUCH_UE , ueCode ) ;
66
66
if ( ueof && ! ( await this . ueService . doesUeofExist ( ueof ) ) ) throw new AppException ( ERROR_CODE . NO_SUCH_UEOF , ueof ) ;
67
67
if ( ! ( await this . annalsService . doesAnnalTypeExist ( typeId ) ) ) throw new AppException ( ERROR_CODE . NO_SUCH_ANNAL_TYPE ) ;
68
68
if (
69
69
! ( await this . ueService . hasUserAttended ( ueCode , user . id , semester ) ) &&
70
- ! permissions . can ( Permission . API_UPLOAD_ANNAL )
70
+ ! permissions . can ( Permission . API_MODERATE_ANNALS )
71
71
)
72
72
throw new AppException ( ERROR_CODE . NOT_DONE_UE_IN_SEMESTER , ueCode , semester ) ;
73
+ if ( ! ( await this . ueService . didUeHappenAtSemester ( ueCode , semester ) ) )
74
+ throw new AppException ( ERROR_CODE . NO_SUCH_UE_AT_SEMESTER , ueCode , semester ) ;
73
75
return this . annalsService . createAnnalFile ( user , { ueCode, semester, typeId, ueof } ) ;
74
76
}
75
77
76
78
@Get ( 'metadata' )
77
- @RequireUserType ( 'STUDENT' , 'FORMER_STUDENT ')
79
+ @RequireApiPermission ( 'API_SEE_ANNALS ')
78
80
@ApiOperation ( {
79
81
description :
80
82
'Get generic information about annals for a particular UE. User must have already done this UE, or be an `annalUploader`.' ,
@@ -92,13 +94,13 @@ export class AnnalsController {
92
94
@GetPermissions ( ) permissions : PermissionManager ,
93
95
) : Promise < UeAnnalMetadataResDto > {
94
96
if ( ! ( await this . ueService . doesUeExist ( ueCode ) ) ) throw new AppException ( ERROR_CODE . NO_SUCH_UE , ueCode ) ;
95
- if ( ! ( await this . ueService . hasUserAttended ( ueCode , user . id ) ) && ! permissions . can ( Permission . API_UPLOAD_ANNAL ) )
97
+ if ( ! ( await this . ueService . hasUserAttended ( ueCode , user . id ) ) && ! permissions . can ( Permission . API_UPLOAD_ANNALS ) )
96
98
throw new AppException ( ERROR_CODE . NOT_ALREADY_DONE_UE ) ;
97
- return this . annalsService . getUeAnnalMetadata ( user , ueCode , permissions . can ( Permission . API_UPLOAD_ANNAL ) ) ;
99
+ return this . annalsService . getUeAnnalMetadata ( user , ueCode , permissions . can ( Permission . API_UPLOAD_ANNALS ) ) ;
98
100
}
99
101
100
102
@Put ( ':annalId' )
101
- @RequireUserType ( 'STUDENT ')
103
+ @RequireApiPermission ( 'API_UPLOAD_ANNALS ')
102
104
@UploadRoute ( 'file' )
103
105
@ApiOperation ( {
104
106
description :
@@ -125,15 +127,15 @@ export class AnnalsController {
125
127
if ( ! ( await this . annalsService . isUeAnnalSender ( user . id , annalId ) ) )
126
128
throw new AppException ( ERROR_CODE . NOT_ANNAL_SENDER ) ;
127
129
if (
128
- ( await this . annalsService . getUeAnnal ( annalId , user . id , permissions . can ( Permission . API_MODERATE_ANNAL ) ) ) . status !==
129
- CommentStatus . PROCESSING
130
+ ( await this . annalsService . getUeAnnal ( annalId , user . id , permissions . can ( Permission . API_MODERATE_ANNALS ) ) )
131
+ . status !== CommentStatus . PROCESSING
130
132
)
131
133
throw new AppException ( ERROR_CODE . ANNAL_ALREADY_UPLOADED ) ;
132
134
return this . annalsService . uploadAnnalFile ( await file , annalId , rotate ) ;
133
135
}
134
136
135
137
@Get ( ':annalId' )
136
- @RequireUserType ( 'STUDENT' , 'FORMER_STUDENT ')
138
+ @RequireApiPermission ( 'API_SEE_ANNALS ')
137
139
@ApiOperation ( { description : 'Get the file linked to a specific annal.' } )
138
140
@ApiOkResponse ( { description : 'The file is sent back.' } )
139
141
@ApiAppErrorResponse (
@@ -146,12 +148,14 @@ export class AnnalsController {
146
148
@Response ( ) response : ExpressResponse ,
147
149
@GetPermissions ( ) permissions : PermissionManager ,
148
150
) {
149
- if ( ! ( await this . annalsService . isAnnalAccessible ( user . id , annalId , permissions . can ( Permission . API_MODERATE_ANNAL ) ) ) )
151
+ if (
152
+ ! ( await this . annalsService . isAnnalAccessible ( user . id , annalId , permissions . can ( Permission . API_MODERATE_ANNALS ) ) )
153
+ )
150
154
throw new AppException ( ERROR_CODE . NO_SUCH_ANNAL , annalId ) ;
151
155
const annalFile = await this . annalsService . getUeAnnalFile (
152
156
annalId ,
153
157
user . id ,
154
- permissions . can ( Permission . API_MODERATE_ANNAL ) ,
158
+ permissions . can ( Permission . API_MODERATE_ANNALS ) ,
155
159
) ;
156
160
if ( ! annalFile ) throw new AppException ( ERROR_CODE . NO_SUCH_ANNAL , annalId ) ;
157
161
response . setHeader ( 'Content-Type' , 'application/pdf' ) ;
@@ -163,7 +167,7 @@ export class AnnalsController {
163
167
}
164
168
165
169
@Patch ( ':annalId' )
166
- @RequireUserType ( 'STUDENT' , 'FORMER_STUDENT ')
170
+ @RequireApiPermission ( 'API_UPLOAD_ANNALS ')
167
171
@ApiOperation ( {
168
172
description :
169
173
'Modify the metadata of an annal. User must be the original sender of the annal, or be an `annalModerator`.' ,
@@ -177,18 +181,20 @@ export class AnnalsController {
177
181
@GetUser ( ) user : User ,
178
182
@GetPermissions ( ) permissions : PermissionManager ,
179
183
) : Promise < UeAnnalResDto > {
180
- if ( ! ( await this . annalsService . isAnnalAccessible ( user . id , annalId , permissions . can ( Permission . API_MODERATE_ANNAL ) ) ) )
184
+ if (
185
+ ! ( await this . annalsService . isAnnalAccessible ( user . id , annalId , permissions . can ( Permission . API_MODERATE_ANNALS ) ) )
186
+ )
181
187
throw new AppException ( ERROR_CODE . NO_SUCH_ANNAL , annalId ) ;
182
188
if (
183
189
! ( await this . annalsService . isUeAnnalSender ( user . id , annalId ) ) &&
184
- ! permissions . can ( Permission . API_MODERATE_ANNAL )
190
+ ! permissions . can ( Permission . API_MODERATE_ANNALS )
185
191
)
186
192
throw new AppException ( ERROR_CODE . NOT_ANNAL_SENDER ) ;
187
193
return this . annalsService . updateAnnalMetadata ( annalId , body ) ;
188
194
}
189
195
190
196
@Delete ( ':annalId' )
191
- @RequireUserType ( 'STUDENT' , 'FORMER_STUDENT ')
197
+ @RequireApiPermission ( 'API_UPLOAD_ANNALS ')
192
198
@ApiOperation ( {
193
199
description :
194
200
'Delete an annal. The file attached to the annal will not actually be deleted. User must be the original sender of the annal, or be an `annalModerator`.' ,
@@ -201,11 +207,13 @@ export class AnnalsController {
201
207
@GetUser ( ) user : User ,
202
208
@GetPermissions ( ) permissions : PermissionManager ,
203
209
) : Promise < UeAnnalResDto > {
204
- if ( ! ( await this . annalsService . isAnnalAccessible ( user . id , annalId , permissions . can ( Permission . API_MODERATE_ANNAL ) ) ) )
210
+ if (
211
+ ! ( await this . annalsService . isAnnalAccessible ( user . id , annalId , permissions . can ( Permission . API_MODERATE_ANNALS ) ) )
212
+ )
205
213
throw new AppException ( ERROR_CODE . NO_SUCH_ANNAL , annalId ) ;
206
214
if (
207
215
! ( await this . annalsService . isUeAnnalSender ( user . id , annalId ) ) &&
208
- ! permissions . can ( Permission . API_MODERATE_ANNAL )
216
+ ! permissions . can ( Permission . API_MODERATE_ANNALS )
209
217
)
210
218
throw new AppException ( ERROR_CODE . NOT_ANNAL_SENDER ) ;
211
219
return this . annalsService . deleteAnnal ( annalId ) ;
0 commit comments