Skip to content

Commit 909cc22

Browse files
committed
Verify complete partnership date override ranges are valid
1 parent a5bf2f9 commit 909cc22

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/components/partnership/partnership.service.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { forwardRef, Inject, Injectable } from '@nestjs/common';
22
import {
3+
CalendarDate,
34
CreationFailed,
45
ID,
56
InputException,
67
NotFoundException,
78
ObjectView,
9+
Range,
10+
RangeException,
811
ReadAfterCreationFailed,
912
ServerException,
1013
Session,
@@ -18,6 +21,7 @@ import {
1821
Logger,
1922
ResourceLoader,
2023
} from '~/core';
24+
import { AnyChangesOf } from '~/core/database/changes';
2125
import { Privileges } from '../authorization';
2226
import { FileService } from '../file';
2327
import { PartnerService } from '../partner';
@@ -61,6 +65,8 @@ export class PartnershipService {
6165
): Promise<Partnership> {
6266
const { projectId, partnerId } = input;
6367

68+
PartnershipDateRangeException.throwIfInvalid(input);
69+
6470
const isFirstPartnership = await this.repo.isFirstPartnership(
6571
projectId,
6672
changeset,
@@ -190,6 +196,8 @@ export class PartnershipService {
190196
this.privileges.for(session, Partnership, object).verifyChanges(changes);
191197
const { mou, agreement, ...simpleChanges } = changes;
192198

199+
PartnershipDateRangeException.throwIfInvalid(existing, changes);
200+
193201
if (changes.primary) {
194202
await this.repo.removePrimaryFromOtherPartnerships(input.id);
195203
}
@@ -297,3 +305,34 @@ export class PartnershipService {
297305
}
298306
}
299307
}
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

Comments
 (0)