1
1
import { forwardRef , Inject , Injectable } from '@nestjs/common' ;
2
2
import {
3
+ CalendarDate ,
3
4
CreationFailed ,
4
5
ID ,
5
6
InputException ,
6
7
NotFoundException ,
7
8
ObjectView ,
9
+ Range ,
10
+ RangeException ,
8
11
ReadAfterCreationFailed ,
9
12
ServerException ,
10
13
Session ,
@@ -18,6 +21,7 @@ import {
18
21
Logger ,
19
22
ResourceLoader ,
20
23
} from '~/core' ;
24
+ import { AnyChangesOf } from '~/core/database/changes' ;
21
25
import { Privileges } from '../authorization' ;
22
26
import { FileService } from '../file' ;
23
27
import { PartnerService } from '../partner' ;
@@ -61,6 +65,8 @@ export class PartnershipService {
61
65
) : Promise < Partnership > {
62
66
const { projectId, partnerId } = input ;
63
67
68
+ PartnershipDateRangeException . throwIfInvalid ( input ) ;
69
+
64
70
const isFirstPartnership = await this . repo . isFirstPartnership (
65
71
projectId ,
66
72
changeset ,
@@ -190,6 +196,8 @@ export class PartnershipService {
190
196
this . privileges . for ( session , Partnership , object ) . verifyChanges ( changes ) ;
191
197
const { mou, agreement, ...simpleChanges } = changes ;
192
198
199
+ PartnershipDateRangeException . throwIfInvalid ( existing , changes ) ;
200
+
193
201
if ( changes . primary ) {
194
202
await this . repo . removePrimaryFromOtherPartnerships ( input . id ) ;
195
203
}
@@ -297,3 +305,34 @@ export class PartnershipService {
297
305
}
298
306
}
299
307
}
308
+
309
+ class PartnershipDateRangeException extends RangeException {
310
+ static throwIfInvalid (
311
+ current : Partial <
312
+ Pick < UnsecuredDto < Partnership > , 'mouStartOverride' | 'mouEndOverride' >
313
+ > ,
314
+ changes : AnyChangesOf < Partnership > = { } ,
315
+ ) {
316
+ const start =
317
+ changes . mouStartOverride !== undefined
318
+ ? changes . mouStartOverride
319
+ : current . mouStartOverride ;
320
+ const end =
321
+ changes . mouEndOverride !== undefined
322
+ ? changes . mouEndOverride
323
+ : current . mouEndOverride ;
324
+ if ( start && end && start > end ) {
325
+ const field =
326
+ changes . mouEndOverride !== undefined
327
+ ? 'partnership.mouEndOverride'
328
+ : 'partnership.mouStartOverride' ;
329
+ throw new PartnershipDateRangeException ( { start, end } , field ) ;
330
+ }
331
+ }
332
+
333
+ constructor ( readonly value : Range < CalendarDate > , readonly field : string ) {
334
+ const message =
335
+ "Partnership's MOU start date must be before the MOU end date" ;
336
+ super ( { message, field } ) ;
337
+ }
338
+ }
0 commit comments