Merge pull request #6128 from ToolJet/bug_fix/handle_eq_with_null

bug-fix: 🐛 - handle when a filter exist for equal & null
This commit is contained in:
Shubham Gupta 2023-05-10 18:18:26 +05:30 committed by GitHub
commit 6e5c2ca7c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 0 deletions

View file

@ -2,6 +2,7 @@ import { tooljetDatabaseService } from '@/_services';
import { isEmpty } from 'lodash';
import PostgrestQueryBuilder from '@/_helpers/postgrestQueryBuilder';
import { resolveReferences } from '@/_helpers/utils';
import { hasEqualWithNull } from './util';
export const tooljetDbOperations = {
perform,
@ -55,6 +56,15 @@ function buildPostgrestQuery(filters) {
async function listRows(queryOptions, organizationId, currentState) {
let query = [];
const resolvedOptions = resolveReferences(queryOptions, currentState);
if (hasEqualWithNull(resolvedOptions, 'list_rows')) {
return {
status: 'failed',
statusText: 'failed',
message: 'Null value comparison not allowed, To check null values Please use IS operator instead.',
description: 'Is operator should be used with null value comparision.',
data: {},
};
}
const { table_name: tableName, list_rows: listRows } = resolvedOptions;
if (!isEmpty(listRows)) {
@ -91,6 +101,15 @@ async function createRow(queryOptions, organizationId, currentState) {
async function updateRows(queryOptions, organizationId, currentState) {
const resolvedOptions = resolveReferences(queryOptions, currentState);
if (hasEqualWithNull(resolvedOptions, 'update_rows')) {
return {
status: 'failed',
statusText: 'failed',
message: 'Null value comparison not allowed, To check null values Please use IS operator instead.',
description: 'Is operator should be used with null value comparision.',
data: {},
};
}
const { table_name: tableName, update_rows: updateRows } = resolvedOptions;
const { where_filters: whereFilters, columns } = updateRows;
@ -108,6 +127,15 @@ async function updateRows(queryOptions, organizationId, currentState) {
async function deleteRows(queryOptions, organizationId, currentState) {
const resolvedOptions = resolveReferences(queryOptions, currentState);
if (hasEqualWithNull(resolvedOptions, 'delete_rows')) {
return {
status: 'failed',
statusText: 'failed',
message: 'Null value comparison not allowed, To check null values Please use IS operator instead.',
description: 'Is operator should be used with null value comparision.',
data: {},
};
}
const { table_name: tableName, delete_rows: deleteRows = { whereFilters: {} } } = resolvedOptions;
const { where_filters: whereFilters, limit = 1 } = deleteRows;

View file

@ -0,0 +1,36 @@
import { get } from 'lodash';
/**
* Checks if the queryOptions object contains a filter with an 'eq' (equal) operator and a value equal to '{{null}}'.
*
* @function hasEqualWithNull
* @param {Object} queryOptions - The query options object to check for the presence of the specified filter.
* @property {Object} queryOptions.list_rows.where_filters - An object containing the filters to be checked.
* @returns {boolean} - Returns true if the specified filter is found, false otherwise.
*
* @example
* const queryOptions = {
* list_rows: {
* where_filters: {
* filter1: {
* operator: 'eq',
* value: '{{null}}',
* },
* },
* },
* };
*
* const result = hasEqualWithNull(queryOptions); // true
*/
export const hasEqualWithNull = (queryOptions, operation) => {
const filters = get(queryOptions, `${operation}.where_filters`);
if (filters) {
const filterKeys = Object.keys(filters);
for (let i = 0; i < filterKeys.length; i++) {
const filter = filters[filterKeys[i]];
if (filter.operator !== 'is' && filter.value === null) {
return true;
}
}
}
return false;
};