From 2e9d17272356bd05c40d0478ad47c3e0b2de1aa7 Mon Sep 17 00:00:00 2001 From: Benjamin Pabst Date: Tue, 1 Oct 2024 14:53:16 +0200 Subject: [PATCH 1/5] feat: added method to correct utc date --- libs/accelerator/src/lib/utils/date.utils.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libs/accelerator/src/lib/utils/date.utils.ts b/libs/accelerator/src/lib/utils/date.utils.ts index d5100ee8..bb29aae7 100644 --- a/libs/accelerator/src/lib/utils/date.utils.ts +++ b/libs/accelerator/src/lib/utils/date.utils.ts @@ -1,4 +1,16 @@ export function isValidDate(value: any): value is Date { return value instanceof Date && !isNaN(value as any); } - \ No newline at end of file + + +/** + * This function removes time info from a JS DateTime Object and returns + * the local date as a correct UTC Date + * @param date a date-time Date object + * @returns the date without time / timezone issues + */ +export function getUTCDateWithoutTimezoneIssues(date: Date) { + return new Date( + Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0), + ); +} \ No newline at end of file From 3ce3b3f2aec3874d2f6a1b79ad7c07eb947dfd44 Mon Sep 17 00:00:00 2001 From: Benjamin Pabst Date: Wed, 2 Oct 2024 10:39:58 +0200 Subject: [PATCH 2/5] feat: added search criteria builder --- libs/accelerator/src/lib/utils/date.utils.ts | 11 ++--- .../src/lib/utils/criteria.utils.ts | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 libs/angular-accelerator/src/lib/utils/criteria.utils.ts diff --git a/libs/accelerator/src/lib/utils/date.utils.ts b/libs/accelerator/src/lib/utils/date.utils.ts index bb29aae7..63e798b0 100644 --- a/libs/accelerator/src/lib/utils/date.utils.ts +++ b/libs/accelerator/src/lib/utils/date.utils.ts @@ -1,7 +1,6 @@ export function isValidDate(value: any): value is Date { - return value instanceof Date && !isNaN(value as any); - } - + return value instanceof Date && !isNaN(value as any) +} /** * This function removes time info from a JS DateTime Object and returns @@ -10,7 +9,5 @@ export function isValidDate(value: any): value is Date { * @returns the date without time / timezone issues */ export function getUTCDateWithoutTimezoneIssues(date: Date) { - return new Date( - Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0), - ); -} \ No newline at end of file + return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)) +} diff --git a/libs/angular-accelerator/src/lib/utils/criteria.utils.ts b/libs/angular-accelerator/src/lib/utils/criteria.utils.ts new file mode 100644 index 00000000..e53a7fd2 --- /dev/null +++ b/libs/angular-accelerator/src/lib/utils/criteria.utils.ts @@ -0,0 +1,46 @@ +import { QueryList } from '@angular/core' +import { FormGroup } from '@angular/forms' +import { isValidDate } from '@onecx/accelerator' +import { Calendar } from 'primeng/calendar' + +export type hasShowTimeFunction = (key: string) => boolean +/** + * removeNullValues: whether to remove entries from the search criteria where the value is null + */ +export interface BuildSearchCriteriaParameters { + removeNullValues: boolean +} + +function _hasShowTime(calendars: QueryList, formKey: string): boolean { + return ( + calendars.find((c) => { + return c.name === formKey + })?.showTime === true + ) +} + +/** + * Safely builds the search criteria based on form values + * @param formValues the form values to use + * @param calendars a list of calendars of the form (use `@ViewChildren(Calendar) calendars!: QueryList;`) + * @param parameters {@link BuildSearchCriteriaParameters} to use when building the search criteria + * @returns the search criteria as partial of T (T should be your search criteria type) + */ +export function buildSearchCriteria( + formValues: FormGroup, + calendars: QueryList, + { removeNullValues = false }: BuildSearchCriteriaParameters +) { + return Object.entries(formValues).reduce((acc: Partial, [key, value]) => { + if (value == null && removeNullValues) { + return acc + } + if (isValidDate(value) && !_hasShowTime(calendars, key)) { + value = new Date(Date.UTC(value.getFullYear(), value.getMonth(), value.getDate(), 0, 0, 0)) + } + return { + ...acc, + [key]: value, + } + }, {}) +} From ef754951c1850b31fb495c3fe22fbe6db2d13511 Mon Sep 17 00:00:00 2001 From: Benjamin Pabst Date: Wed, 2 Oct 2024 11:25:50 +0200 Subject: [PATCH 3/5] feat: re-use utc date method --- libs/angular-accelerator/src/lib/utils/criteria.utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/angular-accelerator/src/lib/utils/criteria.utils.ts b/libs/angular-accelerator/src/lib/utils/criteria.utils.ts index e53a7fd2..45830596 100644 --- a/libs/angular-accelerator/src/lib/utils/criteria.utils.ts +++ b/libs/angular-accelerator/src/lib/utils/criteria.utils.ts @@ -1,6 +1,6 @@ import { QueryList } from '@angular/core' import { FormGroup } from '@angular/forms' -import { isValidDate } from '@onecx/accelerator' +import { getUTCDateWithoutTimezoneIssues, isValidDate } from '@onecx/accelerator' import { Calendar } from 'primeng/calendar' export type hasShowTimeFunction = (key: string) => boolean @@ -36,7 +36,7 @@ export function buildSearchCriteria( return acc } if (isValidDate(value) && !_hasShowTime(calendars, key)) { - value = new Date(Date.UTC(value.getFullYear(), value.getMonth(), value.getDate(), 0, 0, 0)) + value = getUTCDateWithoutTimezoneIssues(value); } return { ...acc, From 3f18103822c4bfd027d32c5d4642118ba4474179 Mon Sep 17 00:00:00 2001 From: Benjamin Pabst Date: Fri, 4 Oct 2024 10:53:56 +0200 Subject: [PATCH 4/5] misc: added utils to export --- libs/angular-accelerator/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/angular-accelerator/src/index.ts b/libs/angular-accelerator/src/index.ts index 21f37232..9d8ae9d3 100644 --- a/libs/angular-accelerator/src/index.ts +++ b/libs/angular-accelerator/src/index.ts @@ -61,3 +61,4 @@ export * from './lib/utils/primeicon.utils' export * from './lib/utils/translate.combined.loader' export * from './lib/utils/create-remote-component-translate-loader.utils' export * from './lib/utils/enum-to-dropdown-options.utils' +export * from './lib/utils/criteria.utils' From 03f6e2136716f5047d0bc3963079eb3e35563ef8 Mon Sep 17 00:00:00 2001 From: Benjamin Pabst Date: Fri, 4 Oct 2024 11:35:50 +0200 Subject: [PATCH 5/5] docs: improved docs --- libs/angular-accelerator/src/lib/utils/criteria.utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/angular-accelerator/src/lib/utils/criteria.utils.ts b/libs/angular-accelerator/src/lib/utils/criteria.utils.ts index 45830596..7c9059b2 100644 --- a/libs/angular-accelerator/src/lib/utils/criteria.utils.ts +++ b/libs/angular-accelerator/src/lib/utils/criteria.utils.ts @@ -22,9 +22,9 @@ function _hasShowTime(calendars: QueryList, formKey: string): boolean /** * Safely builds the search criteria based on form values * @param formValues the form values to use - * @param calendars a list of calendars of the form (use `@ViewChildren(Calendar) calendars!: QueryList;`) + * @param calendars a list of primeng calendars of the form (use `@ViewChildren(Calendar) calendars!: QueryList;`) * @param parameters {@link BuildSearchCriteriaParameters} to use when building the search criteria - * @returns the search criteria as partial of T (T should be your search criteria type) + * @returns the search criteria as partial of T (T = type of the search criteria) */ export function buildSearchCriteria( formValues: FormGroup,