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

FIO-6966: Fixes missing address component data in csv exported submissions #1750

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 10 additions & 5 deletions src/export/exporters/CSVExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,21 @@ class CSVExporter extends Exporter {

// If a component has multiple parts, pick what we want.
if (component.type === 'address') {
const getAddressComponentValue = (component, value = {}) => {
if (component.enableManualMode && value.address) {
return value.address;
}
return value;
};

items.push({
rename: (label) => `${label}.formatted`,
preprocessor: (value) => {
if (_.isArray(value)) {
return value;
}

const address = (value && value.address) || value || {};
const address = getAddressComponentValue(component, value);

// OpenStreetMap || Azure || Google
// eslint-disable-next-line max-len
Expand All @@ -81,7 +88,7 @@ class CSVExporter extends Exporter {
return value;
}

const address = (value && value.address) || value || {};
const address = getAddressComponentValue(component, value);

// OpenStreetMap || Azure || Google
return address.lat || _.get(address, 'position.lat') || _.get(address, 'geometry.location.lat') || '';
Expand All @@ -94,7 +101,7 @@ class CSVExporter extends Exporter {
return value;
}

const address = (value && value.address) || value || {};
const address = getAddressComponentValue(component, value);

// OpenStreetMap || Azure || Google
return address.lon || _.get(address, 'position.lon') || _.get(address, 'geometry.location.lng') || '';
Expand Down Expand Up @@ -423,7 +430,6 @@ class CSVExporter extends Exporter {
res.status(400).send(err.message || err);
}
}
/* eslint-enable max-statements */

/**
* Start the CSV export by creating the headers.
Expand Down Expand Up @@ -496,7 +502,6 @@ class CSVExporter extends Exporter {
const updatedSubmission = {};
const result = this.fields.map((column) => {
const componentData = _.get(submission.data, column.path);

// If the path had no results and the component specifies a path, check for a datagrid component
if (_.isUndefined(componentData) && column.path.includes('.')) {
let parts = column.path.split('.');
Expand Down
57 changes: 48 additions & 9 deletions test/export/CSVExporter/CSVExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,30 @@ module.exports = function(app, template, hook) {
done(error);
}

const addressLatWithMode = getComponentValue(result.text, 'addressWithMode.lat', 0);
const addressLngWithMode = getComponentValue(result.text, 'addressWithMode.lng', 0);
const addressNameWithMode = getComponentValue(result.text, 'addressWithMode.formatted', 0);

const addressLat = getComponentValue(result.text, 'address.lat', 0);
const addressLng = getComponentValue(result.text, 'address.lng', 0);
const addressName = getComponentValue(result.text, 'address.formatted', 0);

const expectedAddressLat = '"35.68696"';
const expectedAddressLng = '"139.74946"';
const expectedAddressName = '"Tokyo, Kanto"';
const expectedAddressLat = '"40.71305"';
const expectedAddressLng = '"`-74.00723"';
const expectedAddressName = '"New York, NY"';

const expectedAddressLatWithMode = '"47.37319"';
const expectedAddressLngWithMode = '"`-120.4237"';
const expectedAddressNameWithMode = '"Washington"';

assert.strictEqual(addressLat, expectedAddressLat);
assert.strictEqual(addressLng, expectedAddressLng);
assert.strictEqual(addressName, expectedAddressName);

assert.strictEqual(addressLatWithMode, expectedAddressLatWithMode);
assert.strictEqual(addressLngWithMode, expectedAddressLngWithMode);
assert.strictEqual(addressNameWithMode, expectedAddressNameWithMode);

done();
});
});
Expand All @@ -241,17 +254,30 @@ module.exports = function(app, template, hook) {
done(error);
}

const addressLatWithMode = getComponentValue(result.text, 'addressWithMode.lat', 0);
const addressLngWithMode = getComponentValue(result.text, 'addressWithMode.lng', 0);
const addressNameWithMode = getComponentValue(result.text, 'addressWithMode.formatted', 0);

const addressLat = getComponentValue(result.text, 'address.lat', 0);
const addressLng = getComponentValue(result.text, 'address.lng', 0);
const addressName = getComponentValue(result.text, 'address.formatted', 0);

const expectedAddressLat = '"35.6761919"';
const expectedAddressLng = '"139.6503106"';
const expectedAddressName = '"Tokyo, Japan"';
const expectedAddressLat = '"40.7127753"';
const expectedAddressLng = '"`-74.0059728"';
const expectedAddressName = '"New York, NY, USA"';

const expectedAddressLatWithMode = '"47.7510741"';
const expectedAddressLngWithMode = '"`-120.7401386"';
const expectedAddressNameWithMode = '"Washington, USA"';

assert.strictEqual(addressLat, expectedAddressLat);
assert.strictEqual(addressLng, expectedAddressLng);
assert.strictEqual(addressName, expectedAddressName);

assert.strictEqual(addressLatWithMode, expectedAddressLatWithMode);
assert.strictEqual(addressLngWithMode, expectedAddressLngWithMode);
assert.strictEqual(addressNameWithMode, expectedAddressNameWithMode);

done();
});
});
Expand All @@ -273,17 +299,30 @@ module.exports = function(app, template, hook) {
done(error);
}

const addressLatWithMode = getComponentValue(result.text, 'addressWithMode.lat', 0);
const addressLngWithMode = getComponentValue(result.text, 'addressWithMode.lng', 0);
const addressNameWithMode = getComponentValue(result.text, 'addressWithMode.formatted', 0);

const addressLat = getComponentValue(result.text, 'address.lat', 0);
const addressLng = getComponentValue(result.text, 'address.lng', 0);
const addressName = getComponentValue(result.text, 'address.formatted', 0);

const expectedAddressLat = '"35.6840574"';
const expectedAddressLng = '"139.7744912"';
const expectedAddressName = '"Tokyo, Japan"';
const expectedAddressLat = '"40.7127281"';
const expectedAddressLng = '"`-74.0060152"';
const expectedAddressName = '"New York, United States"';

const expectedAddressLatWithMode = '"47.2868352"';
const expectedAddressLngWithMode = '"`-120.212613"';
const expectedAddressNameWithMode = '"Washington, United States"';

assert.strictEqual(addressLat, expectedAddressLat);
assert.strictEqual(addressLng, expectedAddressLng);
assert.strictEqual(addressName, expectedAddressName);

assert.strictEqual(addressLatWithMode, expectedAddressLatWithMode);
assert.strictEqual(addressLngWithMode, expectedAddressLngWithMode);
assert.strictEqual(addressNameWithMode, expectedAddressNameWithMode);

done();
});
});
Expand Down
Loading
Loading