1
0
mirror of https://github.com/immich-app/immich.git synced 2025-08-09 23:17:29 +02:00

fix(server): correct person birth date across timezones (#11369)

* fix(server): correct person birth date across timezones

* fix test

* update e2e tests

* use Optional decorator
This commit is contained in:
Michel Heusschen
2024-07-30 01:52:04 +02:00
committed by GitHub
parent ebc71e428d
commit 434bcec5cc
7 changed files with 132 additions and 18 deletions

View File

@@ -16,11 +16,15 @@ import {
IsOptional,
IsString,
IsUUID,
ValidateBy,
ValidateIf,
ValidationOptions,
buildMessage,
isDateString,
maxDate,
} from 'class-validator';
import { CronJob } from 'cron';
import { DateTime } from 'luxon';
import sanitize from 'sanitize-filename';
@Injectable()
@@ -165,3 +169,46 @@ export const isValidInteger = (value: number, options: { min?: number; max?: num
const { min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER } = options;
return Number.isInteger(value) && value >= min && value <= max;
};
export function isDateStringFormat(value: unknown, format: string) {
if (typeof value !== 'string') {
return false;
}
return DateTime.fromFormat(value, format, { zone: 'utc' }).isValid;
}
export function IsDateStringFormat(format: string, validationOptions?: ValidationOptions) {
return ValidateBy(
{
name: 'isDateStringFormat',
constraints: [format],
validator: {
validate(value: unknown) {
return isDateStringFormat(value, format);
},
defaultMessage: () => `$property must be a string in the format ${format}`,
},
},
validationOptions,
);
}
export function MaxDateString(date: Date | (() => Date), validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy(
{
name: 'maxDateString',
constraints: [date],
validator: {
validate: (value, args) => {
const date = DateTime.fromISO(value, { zone: 'utc' }).toJSDate();
return maxDate(date, args?.constraints[0]);
},
defaultMessage: buildMessage(
(eachPrefix) => 'maximal allowed date for ' + eachPrefix + '$property is $constraint1',
validationOptions,
),
},
},
validationOptions,
);
}