Skip to content

Commit

Permalink
Fix incorrect usage of osm.userDetails callback
Browse files Browse the repository at this point in the history
Javascript is not my usual domain so still getting used to how scope
works and working with callbacks. Believe this code is now written as
intended.

As a bonus a response status to the error update request which isn't the
expected 200 now returns before visibly removing the error and adding it
to the closed cache.
  • Loading branch information
kymckay committed Feb 1, 2019
1 parent 03a5c26 commit e9397aa
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions modules/services/improveOSM.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,49 +342,51 @@ export default {
return callback({ message: 'Error update already inflight', status: -2 }, d);
}

var osmUsername = services.osm.userDetails(function(err, user) {
if (err) return '';

return user.display_name;
});

var that = this;
var type = d.error_type;
var payload = {
username: osmUsername
};

// Each error type has different data for identification
if (type === 'ow') {
payload.roadSegments = [ d.identifier ];
} else if (type === 'mr') {
payload.tiles = [ d.identifier ];
} else if (type === 'tr') {
payload.targetIds = [ d.identifier ];
}

// Separate requests required to comment and change status
var url = _impOsmUrls[type] + '/comment';

// Comments don't currently work
// if (d.newComment !== undefined) {
// payload.text = d.newComment;
// }

if (d.newStatus !== d.status) {
payload.status = d.newStatus;
payload.text = 'status changed';
// Payload can only be sent once username is established
services.osm.userDetails(sendPayload);

function sendPayload(err, user) {
if (err) { return callback(err, d); }

var type = d.error_type;
var url = _impOsmUrls[type] + '/comment';
var payload = {
username: user.display_name
};

// Each error type has different data for identification
if (type === 'ow') {
payload.roadSegments = [ d.identifier ];
} else if (type === 'mr') {
payload.tiles = [ d.identifier ];
} else if (type === 'tr') {
payload.targetIds = [ d.identifier ];
}

// Comments don't currently work, if they ever do in future
// it looks as though they require a separate post
// if (d.newComment !== undefined) {
// payload.text = d.newComment;
// }

if (d.newStatus !== d.status) {
payload.status = d.newStatus;
payload.text = 'status changed';
}

_erCache.inflightPost[d.id] = d3_request(url)
.header('Content-Type', 'application/json')
.post(JSON.stringify(payload), function(err) {
delete _erCache.inflightPost[d.id];

if (d.newStatus === 'INVALID') {
that.removeError(d);
} else if (d.newStatus === 'SOLVED') {
that.removeError(d);
// Unsuccessful response status, keep issue open
if (err.status !== 200) { return callback(err, d); }

that.removeError(d);

if (d.newStatus === 'SOLVED') {
//TODO the identifiers are ugly and can't be used frontend, use error position instead?
// or perhaps don't track this at all?
//_erCache.closed[d.error_type + ':' + d.identifier] = true;
Expand Down

0 comments on commit e9397aa

Please sign in to comment.