convertSearchBy method
Implementation
Map<String, dynamic> convertSearchBy(Map<String, dynamic> body) {
Map<String, dynamic> searchBy = {};
body.forEach((key, value) {
// Remove spaces if key is 'fullname'
if (key == 'employee:fullname' && value is String || key == 'fullname' && value is String) {
value = value.replaceAll(' ', '');
}
// Divide key based on ':'
List<String> parts = key.split(':');
Map<String, dynamic> currentLevel = searchBy;
for (int i = 0; i < parts.length; i++) {
// Handle boolean values
if (value is bool) {
if (i == parts.length - 1) {
currentLevel[parts[i]] = value;
}
}
// Handle DateTimeRange values
else if (value is DateTimeRange) {
if (i == parts.length - 1) {
currentLevel[parts[i]] = {
'gte': "${value.start..toUtc().toIso8601String()}",
'lte': value.end.add(const Duration(hours: 23, minutes: 59, seconds: 59)).toUtc().toIso8601String(),
};
}
}
// Handle other types of values
else {
if (i == parts.length - 1) {
// Se il valore è un Map con gte/lte (range di date già processato), usalo direttamente
if (value is Map<String, dynamic> && (value.containsKey('gte') || value.containsKey('lte'))) {
currentLevel[parts[i]] = value;
} else if (value is Map && (value.containsKey('gte') || value.containsKey('lte'))) {
currentLevel[parts[i]] = value;
}
// If the final key is 'id', use direct search, otherwise use 'contains'
else if (parts[i].contains("Id")) {
if (parts[i].contains("Ids")) {
currentLevel[parts[i]] = {'has': value};
} else {
currentLevel[parts[i]] = value;
}
} else {
currentLevel[parts[i]] = {'contains': value, 'mode': 'insensitive'};
}
} else {
// Create a new nesting level if it does not already exist
if (currentLevel[parts[i]] == null) {
currentLevel[parts[i]] = <String, dynamic>{};
}
currentLevel = currentLevel[parts[i]] as Map<String, dynamic>;
}
}
}
});
return searchBy;
}