{"action":"created","comment":{"url":"https://api.github.com/repos/medic/medic-api/pulls/comments/56258058","id":56258058,"diff_hunk":"@@ -0,0 +1,361 @@\n+var async = require('async'),\n+    _ = require('underscore'),\n+    db = require('../db');\n+\n+var getType = function(user, admins) {\n+  if (user.roles && user.roles.length) {\n+    return user.roles[0];\n+  }\n+  return admins[user.name] ? 'admin' : 'unknown';\n+};\n+\n+var getDoc = function(id, docs) {\n+  return _.findWhere(docs, { _id: id });\n+};\n+\n+var getDocID = function(doc) {\n+  if (_.isString(doc)) {\n+    return doc;\n+  }\n+  if (_.isObject(doc)) {\n+    return doc._id;\n+  }\n+};\n+\n+var getAllUserSettings = function(callback) {\n+  var opts = {\n+    include_docs: true,\n+    key: ['user-settings']\n+  };\n+  db.medic.view('medic', 'doc_by_type', opts, function(err, results) {\n+    if (err) {\n+      return callback(err);\n+    }\n+    callback(null, _.map(results.rows, function(row) {\n+      return row.doc;\n+    }));\n+  });\n+};\n+\n+var getAllUsers = function(callback) {\n+  db._users.list({include_docs: true}, function(err, results) {\n+    callback(err, results.rows);\n+  });\n+};\n+\n+var getFacilities = function(callback) {\n+  db.medic.view('medic', 'facilities', {include_docs: true}, function(err, results) {\n+    if (err) {\n+      return callback(err);\n+    }\n+    callback(null, _.map(results.rows, function(row) {\n+      return row.doc;\n+    }));\n+  });\n+};\n+\n+var isAPlace = function(place) {\n+  return [\n+    'district_hospital',\n+    'health_center',\n+    'clinic'\n+  ].indexOf(place.type) !== -1;\n+};\n+\n+var getPlace = function(id, callback) {\n+  db.medic.get(id, function(err, place) {\n+    if (err) {\n+      console.error('Failed to find place.');\n+      return callback(err);\n+    }\n+    if (!isAPlace(place)) {\n+      return callback(new Error('Wrong type, this is not a place.'));\n+    }\n+    callback(null, place);\n+  });\n+};\n+\n+var createUser = function(data, response, callback) {\n+  response = response || {};\n+  var id = createID(data.username),\n+      user = getUserUpdates(id, data);\n+  db._users.insert(user, id, function(err, body) {\n+    if (err) {\n+      console.error('Failed to create user.');\n+      return callback(err);\n+    }\n+    response.user = {\n+      id: body.id,\n+      rev: body.rev\n+    };\n+    callback(null, data, response);\n+  });\n+};\n+\n+/*\n+ * Warning: not doing validation of the contact data against a form yet.  The\n+ * form is user defined in settings so being liberal with what gets saved to\n+ * the database. Ideally CouchDB could validate a given object against a form\n+ * in validate_doc_update.\n+ */\n+var createContact = function(data, response, callback) {\n+  response = response || {};\n+  db.medic.insert(data.contact, function(err, body) {\n+    if (err) {\n+      console.error('Failed to create contact.');\n+      return callback(err);\n+    }\n+    // save contact id for user settings\n+    data.contact = body.id;\n+    response.contact = {\n+      id: body.id,\n+      rev: body.rev\n+    };\n+    callback(null, data, response);\n+  });\n+};\n+\n+var createUserSettings = function(data, response, callback) {\n+  response = response || {};\n+  var settings = getSettingsUpdates(data);\n+  db.medic.insert(settings, createID(data.username), function(err, body) {\n+    if (err) {\n+      console.error('Failed to create user settings.');\n+      return callback(err);\n+    }\n+    response['user-settings'] = {\n+      id: body.id,\n+      rev: body.rev\n+    };\n+    callback(null, data, response);\n+  });\n+};\n+\n+var hasParent = function(facility, id) {\n+  // do not modify facility\n+  var p = facility.parent;\n+  if (facility._id === id) {\n+    return true;\n+  }","path":"controllers/users.js","position":null,"original_position":139,"commit_id":"e941a12286a1657f0f9c18604f101218e843c161","original_commit_id":"0333b414723afb1798adfdfaf55d2fe3d79f0112","user":{"login":"mandric","id":25641,"avatar_url":"https://avatars.githubusercontent.com/u/25641?v=3","gravatar_id":"","url":"https://api.github.com/users/mandric","html_url":"https://github.com/mandric","followers_url":"https://api.github.com/users/mandric/followers","following_url":"https://api.github.com/users/mandric/following{/other_user}","gists_url":"https://api.github.com/users/mandric/gists{/gist_id}","starred_url":"https://api.github.com/users/mandric/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mandric/subscriptions","organizations_url":"https://api.github.com/users/mandric/orgs","repos_url":"https://api.github.com/users/mandric/repos","events_url":"https://api.github.com/users/mandric/events{/privacy}","received_events_url":"https://api.github.com/users/mandric/received_events","type":"User","site_admin":false},"body":":beers: ","created_at":"2016-03-15T23:19:15Z","updated_at":"2016-03-15T23:19:15Z","html_url":"https://github.com/medic/medic-api/pull/49#discussion_r56258058","pull_request_url":"https://api.github.com/repos/medic/medic-api/pulls/49","_links":{"self":{"href":"https://api.github.com/repos/medic/medic-api/pulls/comments/56258058"},"html":{"href":"https://github.com/medic/medic-api/pull/49#discussion_r56258058"},"pull_request":{"href":"https://api.github.com/repos/medic/medic-api/pulls/49"}}},"pull_request":{"url":"https://api.github.com/repos/medic/medic-api/pulls/49","id":62815659,"html_url":"https://github.com/medic/medic-api/pull/49","diff_url":"https://github.com/medic/medic-api/pull/49.diff","patch_url":"https://github.com/medic/medic-api/pull/49.patch","issue_url":"https://api.github.com/repos/medic/medic-api/issues/49","number":49,"state":"open","locked":false,"title":"users api refactor","user":{"login":"mandric","id":25641,"avatar_url":"https://avatars.githubusercontent.com/u/25641?v=3","gravatar_id":"","url":"https://api.github.com/users/mandric","html_url":"https://github.com/mandric","followers_url":"https://api.github.com/users/mandric/followers","following_url":"https://api.github.com/users/mandric/following{/other_user}","gists_url":"https://api.github.com/users/mandric/gists{/gist_id}","starred_url":"https://api.github.com/users/mandric/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mandric/subscriptions","organizations_url":"https://api.github.com/users/mandric/orgs","repos_url":"https://api.github.com/users/mandric/repos","events_url":"https://api.github.com/users/mandric/events{/privacy}","received_events_url":"https://api.github.com/users/mandric/received_events","type":"User","site_admin":false},"body":"Issue medic/medic-webapp#2046","created_at":"2016-03-14T20:54:20Z","updated_at":"2016-03-15T23:19:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"cd09b989f66d3b611face907035f6e4d864cdf7d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/medic/medic-api/pulls/49/commits","review_comments_url":"https://api.github.com/repos/medic/medic-api/pulls/49/comments","review_comment_url":"https://api.github.com/repos/medic/medic-api/pulls/comments{/number}","comments_url":"https://api.github.com/repos/medic/medic-api/issues/49/comments","statuses_url":"https://api.github.com/repos/medic/medic-api/statuses/e941a12286a1657f0f9c18604f101218e843c161","head":{"label":"medic:2046-users-api","ref":"2046-users-api","sha":"e941a12286a1657f0f9c18604f101218e843c161","user":{"login":"medic","id":474424,"avatar_url":"https://avatars.githubusercontent.com/u/474424?v=3","gravatar_id":"","url":"https://api.github.com/users/medic","html_url":"https://github.com/medic","followers_url":"https://api.github.com/users/medic/followers","following_url":"https://api.github.com/users/medic/following{/other_user}","gists_url":"https://api.github.com/users/medic/gists{/gist_id}","starred_url":"https://api.github.com/users/medic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/medic/subscriptions","organizations_url":"https://api.github.com/users/medic/orgs","repos_url":"https://api.github.com/users/medic/repos","events_url":"https://api.github.com/users/medic/events{/privacy}","received_events_url":"https://api.github.com/users/medic/received_events","type":"Organization","site_admin":false},"repo":{"id":18199775,"name":"medic-api","full_name":"medic/medic-api","owner":{"login":"medic","id":474424,"avatar_url":"https://avatars.githubusercontent.com/u/474424?v=3","gravatar_id":"","url":"https://api.github.com/users/medic","html_url":"https://github.com/medic","followers_url":"https://api.github.com/users/medic/followers","following_url":"https://api.github.com/users/medic/following{/other_user}","gists_url":"https://api.github.com/users/medic/gists{/gist_id}","starred_url":"https://api.github.com/users/medic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/medic/subscriptions","organizations_url":"https://api.github.com/users/medic/orgs","repos_url":"https://api.github.com/users/medic/repos","events_url":"https://api.github.com/users/medic/events{/privacy}","received_events_url":"https://api.github.com/users/medic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/medic/medic-api","description":"Node server to support medic-webapp","fork":false,"url":"https://api.github.com/repos/medic/medic-api","forks_url":"https://api.github.com/repos/medic/medic-api/forks","keys_url":"https://api.github.com/repos/medic/medic-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/medic/medic-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/medic/medic-api/teams","hooks_url":"https://api.github.com/repos/medic/medic-api/hooks","issue_events_url":"https://api.github.com/repos/medic/medic-api/issues/events{/number}","events_url":"https://api.github.com/repos/medic/medic-api/events","assignees_url":"https://api.github.com/repos/medic/medic-api/assignees{/user}","branches_url":"https://api.github.com/repos/medic/medic-api/branches{/branch}","tags_url":"https://api.github.com/repos/medic/medic-api/tags","blobs_url":"https://api.github.com/repos/medic/medic-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/medic/medic-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/medic/medic-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/medic/medic-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/medic/medic-api/statuses/{sha}","languages_url":"https://api.github.com/repos/medic/medic-api/languages","stargazers_url":"https://api.github.com/repos/medic/medic-api/stargazers","contributors_url":"https://api.github.com/repos/medic/medic-api/contributors","subscribers_url":"https://api.github.com/repos/medic/medic-api/subscribers","subscription_url":"https://api.github.com/repos/medic/medic-api/subscription","commits_url":"https://api.github.com/repos/medic/medic-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/medic/medic-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/medic/medic-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/medic/medic-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/medic/medic-api/contents/{+path}","compare_url":"https://api.github.com/repos/medic/medic-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/medic/medic-api/merges","archive_url":"https://api.github.com/repos/medic/medic-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/medic/medic-api/downloads","issues_url":"https://api.github.com/repos/medic/medic-api/issues{/number}","pulls_url":"https://api.github.com/repos/medic/medic-api/pulls{/number}","milestones_url":"https://api.github.com/repos/medic/medic-api/milestones{/number}","notifications_url":"https://api.github.com/repos/medic/medic-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/medic/medic-api/labels{/name}","releases_url":"https://api.github.com/repos/medic/medic-api/releases{/id}","deployments_url":"https://api.github.com/repos/medic/medic-api/deployments","created_at":"2014-03-28T03:20:43Z","updated_at":"2016-01-12T16:42:13Z","pushed_at":"2016-03-15T23:18:52Z","git_url":"git://github.com/medic/medic-api.git","ssh_url":"git@github.com:medic/medic-api.git","clone_url":"https://github.com/medic/medic-api.git","svn_url":"https://github.com/medic/medic-api","homepage":"","size":628,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":5,"forks":4,"open_issues":5,"watchers":1,"default_branch":"develop"}},"base":{"label":"medic:develop","ref":"develop","sha":"c1b5eeb09ee6e809566409bfee3499a55ba761b1","user":{"login":"medic","id":474424,"avatar_url":"https://avatars.githubusercontent.com/u/474424?v=3","gravatar_id":"","url":"https://api.github.com/users/medic","html_url":"https://github.com/medic","followers_url":"https://api.github.com/users/medic/followers","following_url":"https://api.github.com/users/medic/following{/other_user}","gists_url":"https://api.github.com/users/medic/gists{/gist_id}","starred_url":"https://api.github.com/users/medic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/medic/subscriptions","organizations_url":"https://api.github.com/users/medic/orgs","repos_url":"https://api.github.com/users/medic/repos","events_url":"https://api.github.com/users/medic/events{/privacy}","received_events_url":"https://api.github.com/users/medic/received_events","type":"Organization","site_admin":false},"repo":{"id":18199775,"name":"medic-api","full_name":"medic/medic-api","owner":{"login":"medic","id":474424,"avatar_url":"https://avatars.githubusercontent.com/u/474424?v=3","gravatar_id":"","url":"https://api.github.com/users/medic","html_url":"https://github.com/medic","followers_url":"https://api.github.com/users/medic/followers","following_url":"https://api.github.com/users/medic/following{/other_user}","gists_url":"https://api.github.com/users/medic/gists{/gist_id}","starred_url":"https://api.github.com/users/medic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/medic/subscriptions","organizations_url":"https://api.github.com/users/medic/orgs","repos_url":"https://api.github.com/users/medic/repos","events_url":"https://api.github.com/users/medic/events{/privacy}","received_events_url":"https://api.github.com/users/medic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/medic/medic-api","description":"Node server to support medic-webapp","fork":false,"url":"https://api.github.com/repos/medic/medic-api","forks_url":"https://api.github.com/repos/medic/medic-api/forks","keys_url":"https://api.github.com/repos/medic/medic-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/medic/medic-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/medic/medic-api/teams","hooks_url":"https://api.github.com/repos/medic/medic-api/hooks","issue_events_url":"https://api.github.com/repos/medic/medic-api/issues/events{/number}","events_url":"https://api.github.com/repos/medic/medic-api/events","assignees_url":"https://api.github.com/repos/medic/medic-api/assignees{/user}","branches_url":"https://api.github.com/repos/medic/medic-api/branches{/branch}","tags_url":"https://api.github.com/repos/medic/medic-api/tags","blobs_url":"https://api.github.com/repos/medic/medic-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/medic/medic-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/medic/medic-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/medic/medic-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/medic/medic-api/statuses/{sha}","languages_url":"https://api.github.com/repos/medic/medic-api/languages","stargazers_url":"https://api.github.com/repos/medic/medic-api/stargazers","contributors_url":"https://api.github.com/repos/medic/medic-api/contributors","subscribers_url":"https://api.github.com/repos/medic/medic-api/subscribers","subscription_url":"https://api.github.com/repos/medic/medic-api/subscription","commits_url":"https://api.github.com/repos/medic/medic-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/medic/medic-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/medic/medic-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/medic/medic-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/medic/medic-api/contents/{+path}","compare_url":"https://api.github.com/repos/medic/medic-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/medic/medic-api/merges","archive_url":"https://api.github.com/repos/medic/medic-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/medic/medic-api/downloads","issues_url":"https://api.github.com/repos/medic/medic-api/issues{/number}","pulls_url":"https://api.github.com/repos/medic/medic-api/pulls{/number}","milestones_url":"https://api.github.com/repos/medic/medic-api/milestones{/number}","notifications_url":"https://api.github.com/repos/medic/medic-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/medic/medic-api/labels{/name}","releases_url":"https://api.github.com/repos/medic/medic-api/releases{/id}","deployments_url":"https://api.github.com/repos/medic/medic-api/deployments","created_at":"2014-03-28T03:20:43Z","updated_at":"2016-01-12T16:42:13Z","pushed_at":"2016-03-15T23:18:52Z","git_url":"git://github.com/medic/medic-api.git","ssh_url":"git@github.com:medic/medic-api.git","clone_url":"https://github.com/medic/medic-api.git","svn_url":"https://github.com/medic/medic-api","homepage":"","size":628,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":5,"forks":4,"open_issues":5,"watchers":1,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/medic/medic-api/pulls/49"},"html":{"href":"https://github.com/medic/medic-api/pull/49"},"issue":{"href":"https://api.github.com/repos/medic/medic-api/issues/49"},"comments":{"href":"https://api.github.com/repos/medic/medic-api/issues/49/comments"},"review_comments":{"href":"https://api.github.com/repos/medic/medic-api/pulls/49/comments"},"review_comment":{"href":"https://api.github.com/repos/medic/medic-api/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/medic/medic-api/pulls/49/commits"},"statuses":{"href":"https://api.github.com/repos/medic/medic-api/statuses/e941a12286a1657f0f9c18604f101218e843c161"}}}}