Skip to content

Commit

Permalink
Merge pull request #179 from opendatakit/cxlt/patch-collect-date-tz
Browse files Browse the repository at this point in the history
patch collect datetime tz offset for odata
  • Loading branch information
issa-tseng authored Feb 12, 2019
2 parents 3677def + 8c18677 commit 6442a06
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/data/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,15 @@ const submissionToOData = (fields, table, submission, options = {}) => new Promi
if (fieldPtr != null) {
if (getBranchState(fieldStack, table) === 1) {
const dataPtr = ptr(dataStack);
const { name, type } = fieldPtr;
// we have a value and a place to put it. preprocess it if necessary and write.
if ((fieldPtr.type === 'structure') || (fieldPtr.type === 'repeat')) {
if ((type === 'structure') || (type === 'repeat')) {
// do nothing.
} else if (fieldPtr.type === 'int') {
dataPtr[fieldPtr.name] = parseInt(text, 10);
} else if (fieldPtr.type === 'decimal') {
dataPtr[fieldPtr.name] = parseFloat(text);
} else if (fieldPtr.type === 'geopoint') {
} else if (type === 'int') {
dataPtr[name] = parseInt(text, 10);
} else if (type === 'decimal') {
dataPtr[name] = parseFloat(text);
} else if (type === 'geopoint') {
// all formats require this parsing/formulation:
const [ lat, lon, altitude ] = text.split(/\s+/g).map(parseFloat);
if ((lat == null) || (lon == null)) return;
Expand All @@ -222,13 +223,18 @@ const submissionToOData = (fields, table, submission, options = {}) => new Promi
if ((altitude != null) && !Number.isNaN(altitude)) coordinates.push(altitude);

if (options.wkt === true) // well-known text format:
dataPtr[fieldPtr.name] = `POINT (${coordinates.join(' ')})`;
dataPtr[name] = `POINT (${coordinates.join(' ')})`;
else // geojson is the default:
dataPtr[fieldPtr.name] = { type: 'Point', coordinates };
dataPtr[name] = { type: 'Point', coordinates };
} else if (type === 'dateTime') {
// patch a case where jr/collect outputs eg +07 as the tz, but most
// specs require +07:00
const trimmed = text.trim();
dataPtr[name] = /[+-]\d\d$/.test(trimmed) ? trimmed + ':00' : trimmed;
} else {
// we have to account for multiple text events for the same field,
// since for whatever reason entities decode into their own text events.
dataPtr[fieldPtr.name] = (dataPtr[fieldPtr.name] || '') + text;
dataPtr[name] = (dataPtr[name] || '') + text;
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/data/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ describe('submissionToOData', () => {
decimal: { name: 'decimal', type: 'decimal' },
geopoint: { name: 'geopoint', type: 'geopoint' },
geopointNoAlt: { name: 'geopointNoAlt', type: 'geopoint' },
dateTime: { name: 'dateTime', type: 'dateTime' },
dateTimeWhitespace: { name: 'dateTimeWhitespace', type: 'dateTime' },
dateTimeCorrect: { name: 'dateTimeCorrect', type: 'dateTime' },
text: { name: 'text', type: 'text' },
other: { name: 'other', type: 'other' }
};
Expand All @@ -58,6 +61,11 @@ describe('submissionToOData', () => {
<decimal>3.14</decimal>
<geopoint>4.8 15.16 23.42</geopoint>
<geopointNoAlt>11.38 -11.38</geopointNoAlt>
<dateTime>2019-01-01T00:00:00.000-08</dateTime>
<dateTimeWhitespace>
2019-01-01T00:00:00.000-08
</dateTimeWhitespace>
<dateTimeCorrect>2019-01-01T00:00:00.000-08:00</dateTimeCorrect>
<text>hello</text>
<other>what could it be?</other>
</data>`);
Expand All @@ -69,6 +77,9 @@ describe('submissionToOData', () => {
decimal: 3.14,
geopoint: { type: 'Point', coordinates: [ 15.16, 4.8, 23.42 ] },
geopointNoAlt: { type: 'Point', coordinates: [ -11.38, 11.38 ] },
dateTime: '2019-01-01T00:00:00.000-08:00',
dateTimeWhitespace: '2019-01-01T00:00:00.000-08:00',
dateTimeCorrect: '2019-01-01T00:00:00.000-08:00',
text: 'hello',
other: 'what could it be?'
}]);
Expand Down

0 comments on commit 6442a06

Please sign in to comment.