From 3968cab22d05e06fbcbad6a76a150a195af8b8b0 Mon Sep 17 00:00:00 2001 From: Kacper Podpora Date: Thu, 12 Dec 2024 09:35:12 +0100 Subject: [PATCH] fix: datetime isNot filter (#1158) --- ...multiple-datetime-cell-value-filter.adapter.ts | 15 +++++++++------ .../datetime-cell-value-filter.adapter.ts | 9 ++++++--- .../datetime-cell-value-filter.adapter.ts | 6 +++--- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/apps/nestjs-backend/src/db-provider/filter-query/postgres/cell-value-filter/multiple-value/multiple-datetime-cell-value-filter.adapter.ts b/apps/nestjs-backend/src/db-provider/filter-query/postgres/cell-value-filter/multiple-value/multiple-datetime-cell-value-filter.adapter.ts index 945a60573a..2ff630c4ce 100644 --- a/apps/nestjs-backend/src/db-provider/filter-query/postgres/cell-value-filter/multiple-value/multiple-datetime-cell-value-filter.adapter.ts +++ b/apps/nestjs-backend/src/db-provider/filter-query/postgres/cell-value-filter/multiple-value/multiple-datetime-cell-value-filter.adapter.ts @@ -27,12 +27,15 @@ export class MultipleDatetimeCellValueFilterAdapter extends CellValueFilterPostg const { options } = this.field; const dateTimeRange = this.getFilterDateTimeRange(options as IDateFieldOptions, value); - builderClient - .whereRaw( - `NOT ??::jsonb @\\? '$[*] \\? (@ >= "${dateTimeRange[0]}" && @ <= "${dateTimeRange[1]}")'`, - [this.tableColumnRef] - ) - .orWhereNull(this.tableColumnRef); + builderClient.where((builder) => { + builder + .whereRaw( + `NOT ??::jsonb @\\? '$[*] \\? (@ >= "${dateTimeRange[0]}" && @ <= "${dateTimeRange[1]}")'`, + [this.tableColumnRef] + ) + .orWhereNull(this.tableColumnRef); + }); + return builderClient; } diff --git a/apps/nestjs-backend/src/db-provider/filter-query/postgres/cell-value-filter/single-value/datetime-cell-value-filter.adapter.ts b/apps/nestjs-backend/src/db-provider/filter-query/postgres/cell-value-filter/single-value/datetime-cell-value-filter.adapter.ts index 313d06c3f7..cf819812e3 100644 --- a/apps/nestjs-backend/src/db-provider/filter-query/postgres/cell-value-filter/single-value/datetime-cell-value-filter.adapter.ts +++ b/apps/nestjs-backend/src/db-provider/filter-query/postgres/cell-value-filter/single-value/datetime-cell-value-filter.adapter.ts @@ -24,9 +24,12 @@ export class DatetimeCellValueFilterAdapter extends CellValueFilterPostgres { const { options } = this.field; const dateTimeRange = this.getFilterDateTimeRange(options as IDateFieldOptions, value); - builderClient - .whereNotBetween(this.tableColumnRef, dateTimeRange) - .orWhereNull(this.tableColumnRef); + + // Wrap conditions in a nested `.where()` to ensure proper SQL grouping with parentheses, + // generating `WHERE ("data" NOT BETWEEN ... OR "data" IS NULL) AND other_query`. + builderClient.where((builder) => { + builder.whereNotBetween(this.tableColumnRef, dateTimeRange).orWhereNull(this.tableColumnRef); + }); return builderClient; } diff --git a/apps/nestjs-backend/src/db-provider/filter-query/sqlite/cell-value-filter/single-value/datetime-cell-value-filter.adapter.ts b/apps/nestjs-backend/src/db-provider/filter-query/sqlite/cell-value-filter/single-value/datetime-cell-value-filter.adapter.ts index eaa4dbea46..e44b67c954 100644 --- a/apps/nestjs-backend/src/db-provider/filter-query/sqlite/cell-value-filter/single-value/datetime-cell-value-filter.adapter.ts +++ b/apps/nestjs-backend/src/db-provider/filter-query/sqlite/cell-value-filter/single-value/datetime-cell-value-filter.adapter.ts @@ -24,9 +24,9 @@ export class DatetimeCellValueFilterAdapter extends CellValueFilterSqlite { const { options } = this.field; const dateTimeRange = this.getFilterDateTimeRange(options as IDateFieldOptions, value); - builderClient - .whereNotBetween(this.tableColumnRef, dateTimeRange) - .orWhereNull(this.tableColumnRef); + builderClient.where((builder) => { + builder.whereNotBetween(this.tableColumnRef, dateTimeRange).orWhereNull(this.tableColumnRef); + }); return builderClient; }