I need to get the data using locationid or default in the below nodejs code, the code works, but how can I reduce the complexity? If day is selected, retrieve the data of the day loop by using the values given if locationid is null or not.
if (location_id == '') {
if (filter_by === 'Day') {
//12 hrs in a day
graph_data = graph_data_of_day;
query = {
created_at: {
gt: current_date,
},
ref: headerData ? .ref,
};
} else if (filter_by === 'Week') {
// 7 days a week
graph_data = graph_data_of_weeks;
query = {
created_at: {
gte: startofweek_date,
lte: endofweek_date,
},
ref: headerData ? .ref,
};
} else if (filter_by === 'Month') {
// 4 weeks in a month
graph_data = graph_data_of_months;
query = {
created_at: {
gte: startOfMonth_date,
lte: endOfMonth_date,
},
ref: headerData ? .ref,
};
} else if (filter_by === 'Year') {
// 12 months_for_year in a year
graph_data = graph_data_of_year;
query = {
created_at: {
gte: startOfYear_date,
lte: endOftheYear_date,
},
ref: headerData ? .ref,
};
} else if (filter_by === 'custom') {
graph_data = [1, 2, 3, 4, 5, 7, 3, 12, 4, 2, 5, 6];
query = {
created_at: {
gte: custom_start_date,
lt: custom_end_date,
},
};
}
} else {
//if there is location id
if (filter_by === 'Day') {
//12 hrs in a day
graph_data = graph_data_of_day;
query = {
created_at: {
gt: current_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
} else if (filter_by === 'Week') {
// 7 days a week
graph_data = graph_data_of_weeks;
query = {
created_at: {
gte: startofweek_date,
lte: endofweek_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
} else if (filter_by === 'Month') {
// 4 weeks in a month
graph_data = graph_data_of_months;
query = {
created_at: {
gte: startOfMonth_date,
lte: endOfMonth_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
} else if (filter_by === 'Year') {
// 12 months_for_year in a year
graph_data = graph_data_of_year;
query = {
created_at: {
gte: startOfYear_date,
lte: endOftheYear_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
} else if (filter_by === 'custom') {
graph_data = [1, 2, 3, 4, 5, 7, 3, 12, 4, 2, 5, 6];
query = {
created_at: {
gte: custom_start_date,
lt: custom_end_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
}
}
>Solution :
To reduce the complexity of your code, you can use a single if statement to check the value of the filter_by variable, and then use a switch statement to handle the different possible values of filter_by. This will allow you to avoid repeating the same code multiple times, which will make your code more concise and easier to read.
Here is an example of how you could implement this:
if (filter_by === 'Day' || filter_by === 'Week' || filter_by === 'Month' || filter_by === 'Year' || filter_by === 'custom') {
// Handle different values of "filter_by" using a switch statement
switch (filter_by) {
case 'Day':
// 12 hrs in a day
graph_data = graph_data_of_day;
query = {
created_at: {
gt: current_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
break;
case 'Week':
// 7 days a week
graph_data = graph_data_of_weeks;
query = {
created_at: {
gte: startofweek_date,
lte: endofweek_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
break;
case 'Month':
// 4 weeks in a month
graph_data = graph_data_of_months;
query = {
created_at: {
gte: startOfMonth_date,
lte: endOfMonth_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
break;
case 'Year':
// 12 months_for_year in a year
graph_data = graph_data_of_year;
query = {
created_at: {
gte: startOfYear_date,
lte: endOftheYear_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
break;
case 'custom':
graph_data = [1, 2, 3, 4, 5, 7, 3, 12, 4, 2, 5, 6];
query = {
created_at: {
gte: custom_start_date,
lt: custom_end_date,
},
ref: headerData ? .ref,
location_id: location_id,
};
break;
}
}
This code uses a single if statement to check if the filter_by variable is one of the expected values, and then a switch statement to handle the different cases. The location_id property is added to the query object in each case, so you don’t need to repeat the same code twice, as you were doing in your original code.