generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ORV2-1992 Limit date range to 31 days for reports (#1230)
- Loading branch information
1 parent
f5fa0ab
commit 749e294
Showing
10 changed files
with
243 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
ValidationArguments, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
} from 'class-validator'; | ||
import { differenceBetween, getDuration } from '../helper/date-time.helper'; | ||
import { MaxDifferenceType } from '../interface/duration-difference.interface'; | ||
/** | ||
* The constraint implementation for checking if a datetime is after | ||
* another datetime and within the allowable difference. | ||
*/ | ||
@ValidatorConstraint({ name: 'DateRange', async: false }) | ||
export class DateRangeConstraint<T> implements ValidatorConstraintInterface { | ||
validate(toDateTime: string, args: ValidationArguments) { | ||
// Some destructuring | ||
const { constraints, object } = args; | ||
const [ | ||
propertyToCompareAgainst, | ||
{ | ||
difference: { maxDiff, unit }, | ||
rounding, | ||
}, | ||
] = constraints as [string, MaxDifferenceType]; | ||
const fromDateTime = (object as T)[propertyToCompareAgainst] as string; | ||
|
||
const difference = differenceBetween(fromDateTime, toDateTime, unit); | ||
|
||
// Transform the rounding duration to a unit specified by difference | ||
// to allow direct comparison. | ||
const roundingDuration = getDuration(rounding).as(unit); | ||
return difference > 0 && difference <= maxDiff + roundingDuration; | ||
} | ||
|
||
defaultMessage({ property, constraints }: ValidationArguments) { | ||
const [ | ||
propertyToCompareAgainst, | ||
{ | ||
difference: { maxDiff, unit }, | ||
}, | ||
] = constraints as [string, MaxDifferenceType]; | ||
return ( | ||
`${property} must be after ${propertyToCompareAgainst}.` + | ||
`Max difference allowed is ${maxDiff} ${unit}.` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ValidationOptions, registerDecorator } from 'class-validator'; | ||
import { DateRangeConstraint } from '../constraint/date-range.constraint'; | ||
import { MaxDifferenceType } from '../interface/duration-difference.interface'; | ||
|
||
/** | ||
* Decorator that validates if a date time property is after | ||
* another property. | ||
* | ||
* @param propertyToCompareAgainst The name of the property to compare against | ||
* @param validationOptions Validation options, if any. | ||
* @returns A function that validates the input to the class. | ||
*/ | ||
export function IsDateTimeAfter<T>( | ||
propertyToCompareAgainst: string, | ||
additionalConstraints?: MaxDifferenceType, | ||
validationOptions?: ValidationOptions, | ||
) { | ||
return function (object: object, propertyName: string) { | ||
registerDecorator({ | ||
name: 'IsDateTimeAfter', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
constraints: [propertyToCompareAgainst, additionalConstraints], | ||
options: validationOptions, | ||
async: false, | ||
validator: DateRangeConstraint<T>, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
vehicles/src/common/interface/duration-difference.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { DurationUnitType } from 'dayjs/plugin/duration'; | ||
|
||
/** | ||
* The type to specify a max allowable to difference between two datetimes. | ||
*/ | ||
export type DurationDifference = { | ||
/** | ||
* The unit of comparison. | ||
*/ | ||
unit: DurationUnitType; | ||
/** | ||
* The maximum allowable difference. | ||
*/ | ||
maxDiff: number; | ||
}; | ||
|
||
/** | ||
* The type to define values for a max allowable difference between two datetimes. | ||
*/ | ||
export type MaxDifferenceType = { | ||
/** | ||
* The max allowable difference between two datetimes. | ||
*/ | ||
difference: DurationDifference; | ||
/** | ||
* The allowable rounding difference between two datetimes. | ||
* Optional. If not given, the value given in `difference` field | ||
* will be the hard limit. | ||
*/ | ||
rounding?: DurationDifference; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters