Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually backport 2.11 Bug fix for checking Date Field presence (#211) to 2.10 #247

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 63 additions & 43 deletions server/routes/utils/dataReportHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,50 +135,54 @@ export const getOpenSearchData = (
keys = dateField.split('.');
const dateValue = data._source[dateField];
const fieldDateValue = fields[dateField];
// if its not a nested date field
if (keys.length === 1) {
// if conditions to determine if the date field's value is an array or a string
if (typeof dateValue === 'string') {
data._source[keys] = moment(dateValue).format(dateFormat);
} else if (
fieldDateValue.length !== 0 &&
fieldDateValue instanceof Array
) {
fieldDateValue.forEach((element, index) => {
data._source[keys][index] = moment(element).format(dateFormat);
});
} else {
data._source[keys] = [];
}
// else to cover cases with nested date fields
} else {
let keyElement = keys.shift();
// if conditions to determine if the date field's value is an array or a string
if (typeof fieldDateValue === 'string') {
keys.push(moment(fieldDateValue).format(dateFormat));
} else if (
fieldDateValue.length !== 0 &&
fieldDateValue instanceof Array
) {
let tempArray: string[] = [];
fieldDateValue.forEach((index) => {
tempArray.push(moment(index).format(dateFormat));
});
keys.push(tempArray);
} else {
keys.push([]);
}
const nestedJSON = arrayToNestedJSON(keys);
let keyLength = Object.keys(data._source);
// to check if the nested field have anyother keys apart from date field
if (tempKeyElement.includes(keyElement) || keyLength.length > 1) {
data._source[keyElement] = {
...data._source[keyElement],
...nestedJSON,
};
const isDateFieldPresent = isKeyPresent(data._source, dateField);

if (isDateFieldPresent) {
// if its not a nested date field
if (keys.length === 1) {
// if conditions to determine if the date field's value is an array or a string
if (typeof dateValue === 'string') {
data._source[keys] = moment(dateValue).format(dateFormat);
} else if (
fieldDateValue.length !== 0 &&
fieldDateValue instanceof Array
) {
fieldDateValue.forEach((element, index) => {
data._source[keys][index] = moment(element).format(dateFormat);
});
} else {
data._source[keys] = [];
}
// else to cover cases with nested date fields
} else {
data._source[keyElement] = nestedJSON;
tempKeyElement.push(keyElement);
let keyElement = keys.shift();
// if conditions to determine if the date field's value is an array or a string
if (typeof fieldDateValue === 'string') {
keys.push(moment(fieldDateValue).format(dateFormat));
} else if (
fieldDateValue.length !== 0 &&
fieldDateValue instanceof Array
) {
let tempArray: string[] = [];
fieldDateValue.forEach((index) => {
tempArray.push(moment(index).format(dateFormat));
});
keys.push(tempArray);
} else {
keys.push([]);
}
const nestedJSON = arrayToNestedJSON(keys);
let keyLength = Object.keys(data._source);
// to check if the nested field have anyother keys apart from date field
if (tempKeyElement.includes(keyElement) || keyLength.length > 1) {
data._source[keyElement] = {
...data._source[keyElement],
...nestedJSON,
};
} else {
data._source[keyElement] = nestedJSON;
tempKeyElement.push(keyElement);
}
}
}
}
Expand Down Expand Up @@ -277,6 +281,22 @@ function arrayToNestedJSON(arr: string[]) {
}
}


function isKeyPresent(data: any, key: string): boolean {
if (typeof data === 'object' && data !== null) {
if (key in data) {
return true;
}
for (const value of Object.values(data)) {
if (isKeyPresent(value, key)) {
return true;
}
}
}
return false;
}


const addDocValueFields = (report: any, requestBody: any) => {
const docValues = [];
for (const dateType of report._source.dateFields) {
Expand Down
Loading