1
1
import { forwardRef , Inject , Injectable } from '@nestjs/common' ;
2
2
import { Many } from '@seedcompany/common' ;
3
3
import {
4
+ CalendarDate ,
4
5
ClientException ,
5
6
CreationFailed ,
6
7
EnhancedResource ,
@@ -10,6 +11,8 @@ import {
10
11
many ,
11
12
NotFoundException ,
12
13
ObjectView ,
14
+ Range ,
15
+ RangeException ,
13
16
ReadAfterCreationFailed ,
14
17
Role ,
15
18
SecuredList ,
@@ -27,6 +30,7 @@ import {
27
30
Logger ,
28
31
} from '~/core' ;
29
32
import { Transactional } from '~/core/database' ;
33
+ import { AnyChangesOf } from '~/core/database/changes' ;
30
34
import { Privileges } from '../authorization' ;
31
35
import { withoutScope } from '../authorization/dto' ;
32
36
import { BudgetService } from '../budget' ;
@@ -101,6 +105,7 @@ export class ProjectService {
101
105
input : CreateProject ,
102
106
session : Session ,
103
107
) : Promise < UnsecuredDto < Project > > {
108
+ ProjectDateRangeException . throwIfInvalid ( input ) ;
104
109
if ( input . type !== ProjectType . Internship && input . sensitivity ) {
105
110
throw new InputException (
106
111
'Can only set sensitivity on Internship Projects' ,
@@ -290,6 +295,8 @@ export class ProjectService {
290
295
return await this . readOneUnsecured ( input . id , session , changeset ) ;
291
296
}
292
297
298
+ ProjectDateRangeException . throwIfInvalid ( currentProject , changes ) ;
299
+
293
300
let updated = currentProject ;
294
301
if ( changedStep ) {
295
302
await this . workflow . executeTransitionLegacy (
@@ -611,3 +618,23 @@ export class ProjectService {
611
618
) ;
612
619
}
613
620
}
621
+
622
+ class ProjectDateRangeException extends RangeException {
623
+ static throwIfInvalid (
624
+ current : Partial < Pick < UnsecuredDto < Project > , 'mouStart' | 'mouEnd' > > ,
625
+ changes : AnyChangesOf < Project > = { } ,
626
+ ) {
627
+ const start =
628
+ changes . mouStart !== undefined ? changes . mouStart : current . mouStart ;
629
+ const end = changes . mouEnd !== undefined ? changes . mouEnd : current . mouEnd ;
630
+ if ( start && end && start > end ) {
631
+ const field = changes . mouEnd ? 'project.mouEnd' : 'project.mouStart' ;
632
+ throw new ProjectDateRangeException ( { start, end } , field ) ;
633
+ }
634
+ }
635
+
636
+ constructor ( readonly value : Range < CalendarDate > , readonly field : string ) {
637
+ const message = "Project's MOU start date must be before the MOU end date" ;
638
+ super ( { message, field } ) ;
639
+ }
640
+ }
0 commit comments