Skip to content

Commit

Permalink
BugFix: Make Environments Case Insensitive in Observations Import (#1448
Browse files Browse the repository at this point in the history
)

fix case sensitivity of environments & environment validate sql
  • Loading branch information
mauberti-bc authored Dec 7, 2024
1 parent 662ae2f commit c8dfb81
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,19 @@ export class ObservationSubCountEnvironmentRepository extends BaseRepository {
'environment_qualitative.environment_qualitative_id'
);

const searchConditions = [];

for (const searchTerm of searchTerms) {
queryBuilder
.where('environment_qualitative.name', 'ILIKE', `%${searchTerm}%`)
.orWhere('environment_qualitative.description', 'ILIKE', `%${searchTerm}%`);
searchConditions.push(
knex.raw('environment_qualitative.name ILIKE ? OR environment_qualitative.description ILIKE ?', [
`%${searchTerm}%`,
`%${searchTerm}%`
])
);
}

if (searchConditions.length > 0) {
queryBuilder.whereRaw(searchConditions.join(' OR '));
}

queryBuilder.groupBy(
Expand All @@ -381,7 +390,9 @@ export class ObservationSubCountEnvironmentRepository extends BaseRepository {
async findQuantitativeEnvironmentTypeDefinitions(
searchTerms: string[]
): Promise<QuantitativeEnvironmentTypeDefinition[]> {
const queryBuilder = getKnex()
const knex = getKnex();

const queryBuilder = knex
.select(
'environment_quantitative.environment_quantitative_id',
'environment_quantitative.name',
Expand All @@ -392,10 +403,19 @@ export class ObservationSubCountEnvironmentRepository extends BaseRepository {
)
.from('environment_quantitative');

const searchConditions = [];

for (const searchTerm of searchTerms) {
queryBuilder
.where('environment_quantitative.name', 'ILIKE', `%${searchTerm}%`)
.orWhere('environment_quantitative.description', 'ILIKE', `%${searchTerm}%`);
searchConditions.push(
knex.raw('environment_quantitative.name ILIKE ? OR environment_quantitative.description ILIKE ?', [
`%${searchTerm}%`,
`%${searchTerm}%`
])
);
}

if (searchConditions.length > 0) {
queryBuilder.whereRaw(searchConditions.join(' OR '));
}

const response = await this.connection.knex(queryBuilder, QuantitativeEnvironmentTypeDefinition);
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/observation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ export class ObservationService extends DBService {

// if environment is qualitative, find the option id
if (isEnvironmentQualitativeTypeDefinition(environment)) {
const foundOption = environment.options.find((option) => option.name === String(rowData));
const foundOption = environment.options.find((option) => option.name === String(rowData).toLowerCase());

if (!foundOption) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('environment-column-utils', () => {
{
environment_qualitative_id: '22-123-456',
environment_qualitative_option_id: '33-123-456',
name: 'Low',
name: 'Low'.toLowerCase(),
description: 'Low'
}
]
Expand All @@ -207,7 +207,7 @@ describe('environment-column-utils', () => {
{
environment_qualitative_id: '44-123-456',
environment_qualitative_option_id: '55-123-456',
name: 'North',
name: 'North'.toLowerCase(),
description: 'North'
}
]
Expand Down Expand Up @@ -270,6 +270,7 @@ describe('environment-column-utils', () => {
value: 100
}
];

const environmentNameTypeDefinitionMap: EnvironmentNameTypeDefinitionMap = new Map([
[
'Wind Speed',
Expand All @@ -281,7 +282,8 @@ describe('environment-column-utils', () => {
{
environment_qualitative_id: '22-123-456',
environment_qualitative_option_id: '33-123-456',
name: 'Low',
// Name is made lowercase at this point in the code
name: 'low',
description: 'Low'
}
]
Expand Down Expand Up @@ -330,7 +332,8 @@ describe('environment-column-utils', () => {
{
environment_qualitative_id: '22-123-456',
environment_qualitative_option_id: '33-123-456',
name: 'Low',
// Name is made lowercase at this point in the code
name: 'low',
description: 'Low'
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ export function getEnvironmentColumnsTypeDefinitionMap(
const qualitativeEnvironment = environmentTypeDefinitions.qualitative_environments.find(
(item) => item.name.toLowerCase() === columnName.toLowerCase()
);

if (qualitativeEnvironment) {
// Lowercase the options for comparison
qualitativeEnvironment.options = qualitativeEnvironment.options.map((option) => ({
...option,
name: option.name.toLowerCase()
}));
columnNameDefinitionMap.set(columnName, qualitativeEnvironment);
continue;
}
Expand Down Expand Up @@ -104,7 +110,7 @@ export function validateEnvironments(

if (isEnvironmentQualitativeTypeDefinition(environmentDefinition)) {
return isQualitativeValueValid(
String(environmentToValidate.value),
String(environmentToValidate.value).toLowerCase(),
environmentDefinition.options.map((option) => option.name)
);
}
Expand Down

0 comments on commit c8dfb81

Please sign in to comment.