From 2d0be6faec8f4a6a4927ce2a64bd43493d4c40e6 Mon Sep 17 00:00:00 2001 From: DarkPhoenix2704 Date: Tue, 20 Feb 2024 07:16:16 +0000 Subject: [PATCH] fix(nc-gui): some more imporvements in showing correct date time --- packages/nocodb/src/db/conditionV2.ts | 82 ++++++++++++++++++++------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/packages/nocodb/src/db/conditionV2.ts b/packages/nocodb/src/db/conditionV2.ts index 532af44654..926b00afd7 100644 --- a/packages/nocodb/src/db/conditionV2.ts +++ b/packages/nocodb/src/db/conditionV2.ts @@ -725,7 +725,7 @@ const parseConditionV2 = async ( ].includes(column.uidt) ) { if (qb.client.config.client === 'pg') { - // todo: enbale back if group by date required custom implementation + // todo: enable back if group by date required custom implementation // if ((filter as any).groupby) // qb = qb.where(knex.raw('??::timestamp = ?', [field, val])); // else @@ -899,11 +899,21 @@ const parseConditionV2 = async ( case 'gt': { const gt_op = customWhereClause ? '<' : '>'; - qb = qb.where(field, gt_op, val); - if (column.uidt === UITypes.Rating) { - // unset rating is considered as NULL - if (gt_op === '<' && val > 0) { - qb = qb.orWhereNull(field); + // If the column is a datetime and the client is pg and the value has a timezone offset at the end + // then we need to convert the value to timestamptz before comparing + if ( + column.uidt === UITypes.DateTime && + qb.client.config.client === 'pg' && + val.match(/[+-]\d{2}:\d{2}$/) + ) { + qb.where(field, gt_op, knex.raw('?::timestamptz', [val])); + } else { + qb = qb.where(field, gt_op, val); + if (column.uidt === UITypes.Rating) { + // unset rating is considered as NULL + if (gt_op === '<' && val > 0) { + qb = qb.orWhereNull(field); + } } } } @@ -912,11 +922,21 @@ const parseConditionV2 = async ( case 'gte': { const ge_op = customWhereClause ? '<=' : '>='; - qb = qb.where(field, ge_op, val); - if (column.uidt === UITypes.Rating) { - // unset rating is considered as NULL - if (ge_op === '<=' || (ge_op === '>=' && val === 0)) { - qb = qb.orWhereNull(field); + // If the column is a datetime and the client is pg and the value has a timezone offset at the end + // then we need to convert the value to timestamptz before comparing + if ( + column.uidt === UITypes.DateTime && + qb.client.config.client === 'pg' && + val.match(/[+-]\d{2}:\d{2}$/) + ) { + qb.where(field, ge_op, knex.raw('?::timestamptz', [val])); + } else { + qb = qb.where(field, ge_op, val); + if (column.uidt === UITypes.Rating) { + // unset rating is considered as NULL + if (ge_op === '<=' || (ge_op === '>=' && val === 0)) { + qb = qb.orWhereNull(field); + } } } } @@ -924,11 +944,21 @@ const parseConditionV2 = async ( case 'lt': { const lt_op = customWhereClause ? '>' : '<'; - qb = qb.where(field, lt_op, val); - if (column.uidt === UITypes.Rating) { - // unset number is considered as NULL - if (lt_op === '<' && val > 0) { - qb = qb.orWhereNull(field); + // If the column is a datetime and the client is pg and the value has a timezone offset at the end + // then we need to convert the value to timestamptz before comparing + if ( + column.uidt === UITypes.DateTime && + qb.client.config.client === 'pg' && + val.match(/[+-]\d{2}:\d{2}$/) + ) { + qb.where(field, lt_op, knex.raw('?::timestamptz', [val])); + } else { + qb = qb.where(field, lt_op, val); + if (column.uidt === UITypes.Rating) { + // unset number is considered as NULL + if (lt_op === '<' && val > 0) { + qb = qb.orWhereNull(field); + } } } } @@ -938,11 +968,21 @@ const parseConditionV2 = async ( case 'lte': { const le_op = customWhereClause ? '>=' : '<='; - qb = qb.where(field, le_op, val); - if (column.uidt === UITypes.Rating) { - // unset number is considered as NULL - if (le_op === '<=' || (le_op === '>=' && val === 0)) { - qb = qb.orWhereNull(field); + // If the column is a datetime and the client is pg and the value has a timezone offset at the end + // then we need to convert the value to timestamptz before comparing + if ( + column.uidt === UITypes.DateTime && + qb.client.config.client === 'pg' && + val.match(/[+-]\d{2}:\d{2}$/) + ) { + qb.where(field, le_op, knex.raw('?::timestamptz', [val])); + } else { + qb = qb.where(field, le_op, val); + if (column.uidt === UITypes.Rating) { + // unset number is considered as NULL + if (le_op === '<=' || (le_op === '>=' && val === 0)) { + qb = qb.orWhereNull(field); + } } } }