{"action":"created","comment":{"url":"https://api.github.com/repos/fxbox/calendar-server/pulls/comments/70279439","id":70279439,"diff_hunk":"@@ -13,15 +14,33 @@ function hidePrivateData(item) {\n \n router.post('/', function(req, res, next) {\n   const family = req.user.family;\n-  subscriptions.create(family, req.body).then((id) => {\n-    debug('Subscription #%s has been created in database', id);\n-\n-    return subscriptions.show(family, id);\n-  }).then((subscription) => {\n-    res\n-      .status(201)\n-      .location(`${req.baseUrl}/${subscription.id}`)\n-      .send(hidePrivateData(subscription));\n+\n+  const endpoint = req.body.subscription.endpoint;\n+  subscriptions.findByEndpoint(family, endpoint).then((subscription) => {\n+    // This endpoint is already in the DB\n+    throw new DuplicateEndpointError( // will generate a status \"409\"\n+      'duplicate_endpoint',\n+      `A subscription with endpoint \"${endpoint}\" is already registered.`,\n+      hidePrivateData(subscription)\n+    );\n+  },\n+  (err) => {\n+    if (!(err instanceof NotFoundError)) {\n+      throw err;\n+    }\n+\n+    // This endpoint does not exist yet, let's create it\n+\n+    return subscriptions.create(family, req.body).then((id) => {\n+      debug('Subscription #%s has been created in database', id);\n+\n+      return subscriptions.show(family, id);\n+    }).then((subscription) => {\n+      res\n+        .status(201)\n+        .location(`${req.baseUrl}/${subscription.id}`)\n+        .send(hidePrivateData(subscription));\n+    });","path":"routes/subscriptions.js","position":47,"original_position":47,"commit_id":"000bc4b89d0a10d720ada3a7fc670b6d7885229c","original_commit_id":"000bc4b89d0a10d720ada3a7fc670b6d7885229c","user":{"login":"julienw","id":454175,"avatar_url":"https://avatars.githubusercontent.com/u/454175?v=3","gravatar_id":"","url":"https://api.github.com/users/julienw","html_url":"https://github.com/julienw","followers_url":"https://api.github.com/users/julienw/followers","following_url":"https://api.github.com/users/julienw/following{/other_user}","gists_url":"https://api.github.com/users/julienw/gists{/gist_id}","starred_url":"https://api.github.com/users/julienw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/julienw/subscriptions","organizations_url":"https://api.github.com/users/julienw/orgs","repos_url":"https://api.github.com/users/julienw/repos","events_url":"https://api.github.com/users/julienw/events{/privacy}","received_events_url":"https://api.github.com/users/julienw/received_events","type":"User","site_admin":false},"body":"here we start having some complexity that would justify a separate \"service\" layer :)","created_at":"2016-07-11T15:25:05Z","updated_at":"2016-07-11T15:25:05Z","html_url":"https://github.com/fxbox/calendar-server/pull/25#discussion_r70279439","pull_request_url":"https://api.github.com/repos/fxbox/calendar-server/pulls/25","_links":{"self":{"href":"https://api.github.com/repos/fxbox/calendar-server/pulls/comments/70279439"},"html":{"href":"https://github.com/fxbox/calendar-server/pull/25#discussion_r70279439"},"pull_request":{"href":"https://api.github.com/repos/fxbox/calendar-server/pulls/25"}}},"pull_request":{"url":"https://api.github.com/repos/fxbox/calendar-server/pulls/25","id":76972930,"html_url":"https://github.com/fxbox/calendar-server/pull/25","diff_url":"https://github.com/fxbox/calendar-server/pull/25.diff","patch_url":"https://github.com/fxbox/calendar-server/pull/25.patch","issue_url":"https://api.github.com/repos/fxbox/calendar-server/issues/25","number":25,"state":"open","locked":false,"title":"Check a subscription's endpoint unicity before adding it","user":{"login":"julienw","id":454175,"avatar_url":"https://avatars.githubusercontent.com/u/454175?v=3","gravatar_id":"","url":"https://api.github.com/users/julienw","html_url":"https://github.com/julienw","followers_url":"https://api.github.com/users/julienw/followers","following_url":"https://api.github.com/users/julienw/following{/other_user}","gists_url":"https://api.github.com/users/julienw/gists{/gist_id}","starred_url":"https://api.github.com/users/julienw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/julienw/subscriptions","organizations_url":"https://api.github.com/users/julienw/orgs","repos_url":"https://api.github.com/users/julienw/repos","events_url":"https://api.github.com/users/julienw/events{/privacy}","received_events_url":"https://api.github.com/users/julienw/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-07-11T15:23:22Z","updated_at":"2016-07-11T15:25:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"363072f167694ff16fb70307f61122d3ab70757e","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/fxbox/calendar-server/pulls/25/commits","review_comments_url":"https://api.github.com/repos/fxbox/calendar-server/pulls/25/comments","review_comment_url":"https://api.github.com/repos/fxbox/calendar-server/pulls/comments{/number}","comments_url":"https://api.github.com/repos/fxbox/calendar-server/issues/25/comments","statuses_url":"https://api.github.com/repos/fxbox/calendar-server/statuses/000bc4b89d0a10d720ada3a7fc670b6d7885229c","head":{"label":"julienw:check-subscriptions-post-unicity","ref":"check-subscriptions-post-unicity","sha":"000bc4b89d0a10d720ada3a7fc670b6d7885229c","user":{"login":"julienw","id":454175,"avatar_url":"https://avatars.githubusercontent.com/u/454175?v=3","gravatar_id":"","url":"https://api.github.com/users/julienw","html_url":"https://github.com/julienw","followers_url":"https://api.github.com/users/julienw/followers","following_url":"https://api.github.com/users/julienw/following{/other_user}","gists_url":"https://api.github.com/users/julienw/gists{/gist_id}","starred_url":"https://api.github.com/users/julienw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/julienw/subscriptions","organizations_url":"https://api.github.com/users/julienw/orgs","repos_url":"https://api.github.com/users/julienw/repos","events_url":"https://api.github.com/users/julienw/events{/privacy}","received_events_url":"https://api.github.com/users/julienw/received_events","type":"User","site_admin":false},"repo":{"id":61894864,"name":"calendar-server","full_name":"julienw/calendar-server","owner":{"login":"julienw","id":454175,"avatar_url":"https://avatars.githubusercontent.com/u/454175?v=3","gravatar_id":"","url":"https://api.github.com/users/julienw","html_url":"https://github.com/julienw","followers_url":"https://api.github.com/users/julienw/followers","following_url":"https://api.github.com/users/julienw/following{/other_user}","gists_url":"https://api.github.com/users/julienw/gists{/gist_id}","starred_url":"https://api.github.com/users/julienw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/julienw/subscriptions","organizations_url":"https://api.github.com/users/julienw/orgs","repos_url":"https://api.github.com/users/julienw/repos","events_url":"https://api.github.com/users/julienw/events{/privacy}","received_events_url":"https://api.github.com/users/julienw/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/julienw/calendar-server","description":"This is the server for https://github.com/fxbox/calendar","fork":true,"url":"https://api.github.com/repos/julienw/calendar-server","forks_url":"https://api.github.com/repos/julienw/calendar-server/forks","keys_url":"https://api.github.com/repos/julienw/calendar-server/keys{/key_id}","collaborators_url":"https://api.github.com/repos/julienw/calendar-server/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/julienw/calendar-server/teams","hooks_url":"https://api.github.com/repos/julienw/calendar-server/hooks","issue_events_url":"https://api.github.com/repos/julienw/calendar-server/issues/events{/number}","events_url":"https://api.github.com/repos/julienw/calendar-server/events","assignees_url":"https://api.github.com/repos/julienw/calendar-server/assignees{/user}","branches_url":"https://api.github.com/repos/julienw/calendar-server/branches{/branch}","tags_url":"https://api.github.com/repos/julienw/calendar-server/tags","blobs_url":"https://api.github.com/repos/julienw/calendar-server/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/julienw/calendar-server/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/julienw/calendar-server/git/refs{/sha}","trees_url":"https://api.github.com/repos/julienw/calendar-server/git/trees{/sha}","statuses_url":"https://api.github.com/repos/julienw/calendar-server/statuses/{sha}","languages_url":"https://api.github.com/repos/julienw/calendar-server/languages","stargazers_url":"https://api.github.com/repos/julienw/calendar-server/stargazers","contributors_url":"https://api.github.com/repos/julienw/calendar-server/contributors","subscribers_url":"https://api.github.com/repos/julienw/calendar-server/subscribers","subscription_url":"https://api.github.com/repos/julienw/calendar-server/subscription","commits_url":"https://api.github.com/repos/julienw/calendar-server/commits{/sha}","git_commits_url":"https://api.github.com/repos/julienw/calendar-server/git/commits{/sha}","comments_url":"https://api.github.com/repos/julienw/calendar-server/comments{/number}","issue_comment_url":"https://api.github.com/repos/julienw/calendar-server/issues/comments{/number}","contents_url":"https://api.github.com/repos/julienw/calendar-server/contents/{+path}","compare_url":"https://api.github.com/repos/julienw/calendar-server/compare/{base}...{head}","merges_url":"https://api.github.com/repos/julienw/calendar-server/merges","archive_url":"https://api.github.com/repos/julienw/calendar-server/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/julienw/calendar-server/downloads","issues_url":"https://api.github.com/repos/julienw/calendar-server/issues{/number}","pulls_url":"https://api.github.com/repos/julienw/calendar-server/pulls{/number}","milestones_url":"https://api.github.com/repos/julienw/calendar-server/milestones{/number}","notifications_url":"https://api.github.com/repos/julienw/calendar-server/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/julienw/calendar-server/labels{/name}","releases_url":"https://api.github.com/repos/julienw/calendar-server/releases{/id}","deployments_url":"https://api.github.com/repos/julienw/calendar-server/deployments","created_at":"2016-06-24T15:42:37Z","updated_at":"2016-06-24T15:42:39Z","pushed_at":"2016-07-11T15:23:09Z","git_url":"git://github.com/julienw/calendar-server.git","ssh_url":"git@github.com:julienw/calendar-server.git","clone_url":"https://github.com/julienw/calendar-server.git","svn_url":"https://github.com/julienw/calendar-server","homepage":null,"size":81,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"fxbox:master","ref":"master","sha":"d45d42365a968045a6ba1a87331e3f9c9680f729","user":{"login":"fxbox","id":17052474,"avatar_url":"https://avatars.githubusercontent.com/u/17052474?v=3","gravatar_id":"","url":"https://api.github.com/users/fxbox","html_url":"https://github.com/fxbox","followers_url":"https://api.github.com/users/fxbox/followers","following_url":"https://api.github.com/users/fxbox/following{/other_user}","gists_url":"https://api.github.com/users/fxbox/gists{/gist_id}","starred_url":"https://api.github.com/users/fxbox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fxbox/subscriptions","organizations_url":"https://api.github.com/users/fxbox/orgs","repos_url":"https://api.github.com/users/fxbox/repos","events_url":"https://api.github.com/users/fxbox/events{/privacy}","received_events_url":"https://api.github.com/users/fxbox/received_events","type":"Organization","site_admin":false},"repo":{"id":61565794,"name":"calendar-server","full_name":"fxbox/calendar-server","owner":{"login":"fxbox","id":17052474,"avatar_url":"https://avatars.githubusercontent.com/u/17052474?v=3","gravatar_id":"","url":"https://api.github.com/users/fxbox","html_url":"https://github.com/fxbox","followers_url":"https://api.github.com/users/fxbox/followers","following_url":"https://api.github.com/users/fxbox/following{/other_user}","gists_url":"https://api.github.com/users/fxbox/gists{/gist_id}","starred_url":"https://api.github.com/users/fxbox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fxbox/subscriptions","organizations_url":"https://api.github.com/users/fxbox/orgs","repos_url":"https://api.github.com/users/fxbox/repos","events_url":"https://api.github.com/users/fxbox/events{/privacy}","received_events_url":"https://api.github.com/users/fxbox/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/fxbox/calendar-server","description":"This is the server for https://github.com/fxbox/calendar","fork":false,"url":"https://api.github.com/repos/fxbox/calendar-server","forks_url":"https://api.github.com/repos/fxbox/calendar-server/forks","keys_url":"https://api.github.com/repos/fxbox/calendar-server/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fxbox/calendar-server/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fxbox/calendar-server/teams","hooks_url":"https://api.github.com/repos/fxbox/calendar-server/hooks","issue_events_url":"https://api.github.com/repos/fxbox/calendar-server/issues/events{/number}","events_url":"https://api.github.com/repos/fxbox/calendar-server/events","assignees_url":"https://api.github.com/repos/fxbox/calendar-server/assignees{/user}","branches_url":"https://api.github.com/repos/fxbox/calendar-server/branches{/branch}","tags_url":"https://api.github.com/repos/fxbox/calendar-server/tags","blobs_url":"https://api.github.com/repos/fxbox/calendar-server/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fxbox/calendar-server/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fxbox/calendar-server/git/refs{/sha}","trees_url":"https://api.github.com/repos/fxbox/calendar-server/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fxbox/calendar-server/statuses/{sha}","languages_url":"https://api.github.com/repos/fxbox/calendar-server/languages","stargazers_url":"https://api.github.com/repos/fxbox/calendar-server/stargazers","contributors_url":"https://api.github.com/repos/fxbox/calendar-server/contributors","subscribers_url":"https://api.github.com/repos/fxbox/calendar-server/subscribers","subscription_url":"https://api.github.com/repos/fxbox/calendar-server/subscription","commits_url":"https://api.github.com/repos/fxbox/calendar-server/commits{/sha}","git_commits_url":"https://api.github.com/repos/fxbox/calendar-server/git/commits{/sha}","comments_url":"https://api.github.com/repos/fxbox/calendar-server/comments{/number}","issue_comment_url":"https://api.github.com/repos/fxbox/calendar-server/issues/comments{/number}","contents_url":"https://api.github.com/repos/fxbox/calendar-server/contents/{+path}","compare_url":"https://api.github.com/repos/fxbox/calendar-server/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fxbox/calendar-server/merges","archive_url":"https://api.github.com/repos/fxbox/calendar-server/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fxbox/calendar-server/downloads","issues_url":"https://api.github.com/repos/fxbox/calendar-server/issues{/number}","pulls_url":"https://api.github.com/repos/fxbox/calendar-server/pulls{/number}","milestones_url":"https://api.github.com/repos/fxbox/calendar-server/milestones{/number}","notifications_url":"https://api.github.com/repos/fxbox/calendar-server/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fxbox/calendar-server/labels{/name}","releases_url":"https://api.github.com/repos/fxbox/calendar-server/releases{/id}","deployments_url":"https://api.github.com/repos/fxbox/calendar-server/deployments","created_at":"2016-06-20T17:10:15Z","updated_at":"2016-06-20T17:10:37Z","pushed_at":"2016-07-11T15:23:22Z","git_url":"git://github.com/fxbox/calendar-server.git","ssh_url":"git@github.com:fxbox/calendar-server.git","clone_url":"https://github.com/fxbox/calendar-server.git","svn_url":"https://github.com/fxbox/calendar-server","homepage":null,"size":103,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/fxbox/calendar-server/pulls/25"},"html":{"href":"https://github.com/fxbox/calendar-server/pull/25"},"issue":{"href":"https://api.github.com/repos/fxbox/calendar-server/issues/25"},"comments":{"href":"https://api.github.com/repos/fxbox/calendar-server/issues/25/comments"},"review_comments":{"href":"https://api.github.com/repos/fxbox/calendar-server/pulls/25/comments"},"review_comment":{"href":"https://api.github.com/repos/fxbox/calendar-server/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/fxbox/calendar-server/pulls/25/commits"},"statuses":{"href":"https://api.github.com/repos/fxbox/calendar-server/statuses/000bc4b89d0a10d720ada3a7fc670b6d7885229c"}}}}
{"actor":{"display_login":"julienw"}}