{"action":"created","comment":{"url":"https://api.github.com/repos/promisasu/Portal/pulls/comments/61323231","id":61323231,"diff_hunk":"@@ -0,0 +1,120 @@\n+'use strict';\n+\n+/**\n+ * @module controller/handler/trial-csv\n+ */\n+\n+const database = require('../../model');\n+const convertJsonToCsv = require('../helper/convert-json-to-csv');\n+const boom = require('boom');\n+const deduplicate = require('../helper/deduplicate');\n+const configuration = [\n+    {\n+        label: 'Trial Name',\n+        key: 'trialName',\n+        default: ''\n+    },\n+    {\n+        label: 'patient pin',\n+        key: 'pin',\n+        default: ''\n+    },\n+    {\n+        label: 'Date Started',\n+        key: 'dateStarted',\n+        default: ''\n+    },\n+    {\n+        label: 'Date Completed',\n+        key: 'dateCompleted',\n+        default: ''\n+    },\n+    {\n+        label: 'survey name',\n+        key: 'name',\n+        default: ''\n+    },\n+    {\n+        label: 'unique survey id',\n+        key: 'id',\n+        default: ''\n+    },\n+    {\n+        label: 'unique question id',\n+        key: 'questionId',\n+        default: ''\n+    },\n+    {\n+        label: 'question',\n+        key: 'questionText',\n+        default: ''\n+    },\n+    {\n+        label: 'question option',\n+        key: 'optionText',\n+        default: ''\n+    }\n+];\n+\n+/**\n+ * Create a Comma Seperate Value export of the data of all the patient's that are enrolled in a trial.\n+ * @param {Request} request - Hapi request\n+ * @param {Reply} reply - Hapi Reply\n+ * @returns {View} Rendered page\n+ */\n+function trialCSV (request, reply) {\n+    const formatSpecifier = '%a %b %d %Y %T';\n+\n+    database.sequelize.query(\n+        `\n+        SELECT tr.name AS trialName, pa.pin, DATE_FORMAT(pa.dateStarted, ?) AS dateStarted,\n+        DATE_FORMAT( pa.dateCompleted, ?) AS dateCompleted, st.name,\n+        si.id, qt.id AS questionId, qt.questionText, qo.id AS optionId, qo.optionText\n+        FROM trial AS tr\n+        JOIN stage\n+        ON stage.trialId = tr.id\n+        JOIN active_patients AS pa\n+        ON pa.stageId = stage.id\n+        JOIN survey_instance AS si\n+        ON si.patientId = pa.id\n+        JOIN survey_template AS st\n+        ON st.id = si.surveyTemplateId\n+        JOIN join_surveys_and_questions AS jsq\n+        ON jsq.surveyTemplateId = st.id\n+        JOIN question_template AS qt\n+        ON qt.id = jsq.questionTemplateId\n+        JOIN question_option AS qo\n+        ON qo.questionTemplateId = qt.id\n+        JOIN question_result AS qr\n+        ON qr.surveyInstanceId = si.id\n+        AND qr.questionOptionId = qo.id\n+        WHERE tr.id = ?\n+        AND si.state = 'completed'\n+        ORDER BY pa.pin ASC, si.id, jsq.questionOrder, qo.order\n+        `,\n+        {\n+            type: database.sequelize.QueryTypes.SELECT,\n+            replacements: [\n+                formatSpecifier,\n+                formatSpecifier,\n+                request.params.id\n+            ]\n+        }\n+    )\n+    .then((optionsWithAnswers) => {\n+        const property = ['trialName', 'pin', 'name', 'id', 'questionText', 'questionId',","path":"controller/handler/trial-csv.js","position":105,"original_position":105,"commit_id":"d7933409fc6d8f55490beae7b561a9a35b3132f4","original_commit_id":"d7933409fc6d8f55490beae7b561a9a35b3132f4","user":{"login":"ChristianMurphy","id":3107513,"avatar_url":"https://avatars.githubusercontent.com/u/3107513?v=3","gravatar_id":"","url":"https://api.github.com/users/ChristianMurphy","html_url":"https://github.com/ChristianMurphy","followers_url":"https://api.github.com/users/ChristianMurphy/followers","following_url":"https://api.github.com/users/ChristianMurphy/following{/other_user}","gists_url":"https://api.github.com/users/ChristianMurphy/gists{/gist_id}","starred_url":"https://api.github.com/users/ChristianMurphy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ChristianMurphy/subscriptions","organizations_url":"https://api.github.com/users/ChristianMurphy/orgs","repos_url":"https://api.github.com/users/ChristianMurphy/repos","events_url":"https://api.github.com/users/ChristianMurphy/events{/privacy}","received_events_url":"https://api.github.com/users/ChristianMurphy/received_events","type":"User","site_admin":false},"body":"IMO the\r\n``` js\r\nconst property = [\r\n     'TrialName',\r\n     'Pin',\r\n     ...\r\n]\r\n```","created_at":"2016-04-27T19:44:28Z","updated_at":"2016-04-27T19:44:28Z","html_url":"https://github.com/promisasu/Portal/pull/348#discussion_r61323231","pull_request_url":"https://api.github.com/repos/promisasu/Portal/pulls/348","_links":{"self":{"href":"https://api.github.com/repos/promisasu/Portal/pulls/comments/61323231"},"html":{"href":"https://github.com/promisasu/Portal/pull/348#discussion_r61323231"},"pull_request":{"href":"https://api.github.com/repos/promisasu/Portal/pulls/348"}}},"pull_request":{"url":"https://api.github.com/repos/promisasu/Portal/pulls/348","id":66081466,"html_url":"https://github.com/promisasu/Portal/pull/348","diff_url":"https://github.com/promisasu/Portal/pull/348.diff","patch_url":"https://github.com/promisasu/Portal/pull/348.patch","issue_url":"https://api.github.com/repos/promisasu/Portal/issues/348","number":348,"state":"open","locked":false,"title":"Us#82","user":{"login":"prabhanjan2906","id":13915440,"avatar_url":"https://avatars.githubusercontent.com/u/13915440?v=3","gravatar_id":"","url":"https://api.github.com/users/prabhanjan2906","html_url":"https://github.com/prabhanjan2906","followers_url":"https://api.github.com/users/prabhanjan2906/followers","following_url":"https://api.github.com/users/prabhanjan2906/following{/other_user}","gists_url":"https://api.github.com/users/prabhanjan2906/gists{/gist_id}","starred_url":"https://api.github.com/users/prabhanjan2906/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prabhanjan2906/subscriptions","organizations_url":"https://api.github.com/users/prabhanjan2906/orgs","repos_url":"https://api.github.com/users/prabhanjan2906/repos","events_url":"https://api.github.com/users/prabhanjan2906/events{/privacy}","received_events_url":"https://api.github.com/users/prabhanjan2906/received_events","type":"User","site_admin":false},"body":"<!--\r\n1. Ensure Pull Request is targeting the `development` branch\r\n2. Provide the requested information\r\n-->\r\n**Taiga User Story**\r\nTG-82\r\n\r\n**Taiga Task(s)**\r\nTG-119 \r\n\r\n**What did you change?**\r\nIntroduced the retrieval of the data of all the patients that belong into the particular trial into CSV.\r\nChanged files:\r\n1. Added controller/handler/trial-csv.js\r\n2. view/template/trial.hbs - Added a new Button to retrieve the data into CSV.\r\n3. controller/router.js - Added a new route to get the trial id and forward to trial-csv.\r\n\r\n**How can we test?**\r\nIn the Main Dashboard file, goto any trial. You will see a new Button called \"Export Trial Data\". Click on that button and you should see a csv file getting downloaded. This file contains the answers to the questions each patient answered.","created_at":"2016-04-11T23:55:08Z","updated_at":"2016-04-27T19:44:28Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d4021830518c6edda8c594a678b2811b95c8545d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/promisasu/Portal/pulls/348/commits","review_comments_url":"https://api.github.com/repos/promisasu/Portal/pulls/348/comments","review_comment_url":"https://api.github.com/repos/promisasu/Portal/pulls/comments{/number}","comments_url":"https://api.github.com/repos/promisasu/Portal/issues/348/comments","statuses_url":"https://api.github.com/repos/promisasu/Portal/statuses/d7933409fc6d8f55490beae7b561a9a35b3132f4","head":{"label":"promisasu:US#82","ref":"US#82","sha":"d7933409fc6d8f55490beae7b561a9a35b3132f4","user":{"login":"promisasu","id":16406149,"avatar_url":"https://avatars.githubusercontent.com/u/16406149?v=3","gravatar_id":"","url":"https://api.github.com/users/promisasu","html_url":"https://github.com/promisasu","followers_url":"https://api.github.com/users/promisasu/followers","following_url":"https://api.github.com/users/promisasu/following{/other_user}","gists_url":"https://api.github.com/users/promisasu/gists{/gist_id}","starred_url":"https://api.github.com/users/promisasu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/promisasu/subscriptions","organizations_url":"https://api.github.com/users/promisasu/orgs","repos_url":"https://api.github.com/users/promisasu/repos","events_url":"https://api.github.com/users/promisasu/events{/privacy}","received_events_url":"https://api.github.com/users/promisasu/received_events","type":"Organization","site_admin":false},"repo":{"id":42970270,"name":"Portal","full_name":"promisasu/Portal","owner":{"login":"promisasu","id":16406149,"avatar_url":"https://avatars.githubusercontent.com/u/16406149?v=3","gravatar_id":"","url":"https://api.github.com/users/promisasu","html_url":"https://github.com/promisasu","followers_url":"https://api.github.com/users/promisasu/followers","following_url":"https://api.github.com/users/promisasu/following{/other_user}","gists_url":"https://api.github.com/users/promisasu/gists{/gist_id}","starred_url":"https://api.github.com/users/promisasu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/promisasu/subscriptions","organizations_url":"https://api.github.com/users/promisasu/orgs","repos_url":"https://api.github.com/users/promisasu/repos","events_url":"https://api.github.com/users/promisasu/events{/privacy}","received_events_url":"https://api.github.com/users/promisasu/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/promisasu/Portal","description":"A portal to allow clinician to view and manage Promis surveys","fork":false,"url":"https://api.github.com/repos/promisasu/Portal","forks_url":"https://api.github.com/repos/promisasu/Portal/forks","keys_url":"https://api.github.com/repos/promisasu/Portal/keys{/key_id}","collaborators_url":"https://api.github.com/repos/promisasu/Portal/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/promisasu/Portal/teams","hooks_url":"https://api.github.com/repos/promisasu/Portal/hooks","issue_events_url":"https://api.github.com/repos/promisasu/Portal/issues/events{/number}","events_url":"https://api.github.com/repos/promisasu/Portal/events","assignees_url":"https://api.github.com/repos/promisasu/Portal/assignees{/user}","branches_url":"https://api.github.com/repos/promisasu/Portal/branches{/branch}","tags_url":"https://api.github.com/repos/promisasu/Portal/tags","blobs_url":"https://api.github.com/repos/promisasu/Portal/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/promisasu/Portal/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/promisasu/Portal/git/refs{/sha}","trees_url":"https://api.github.com/repos/promisasu/Portal/git/trees{/sha}","statuses_url":"https://api.github.com/repos/promisasu/Portal/statuses/{sha}","languages_url":"https://api.github.com/repos/promisasu/Portal/languages","stargazers_url":"https://api.github.com/repos/promisasu/Portal/stargazers","contributors_url":"https://api.github.com/repos/promisasu/Portal/contributors","subscribers_url":"https://api.github.com/repos/promisasu/Portal/subscribers","subscription_url":"https://api.github.com/repos/promisasu/Portal/subscription","commits_url":"https://api.github.com/repos/promisasu/Portal/commits{/sha}","git_commits_url":"https://api.github.com/repos/promisasu/Portal/git/commits{/sha}","comments_url":"https://api.github.com/repos/promisasu/Portal/comments{/number}","issue_comment_url":"https://api.github.com/repos/promisasu/Portal/issues/comments{/number}","contents_url":"https://api.github.com/repos/promisasu/Portal/contents/{+path}","compare_url":"https://api.github.com/repos/promisasu/Portal/compare/{base}...{head}","merges_url":"https://api.github.com/repos/promisasu/Portal/merges","archive_url":"https://api.github.com/repos/promisasu/Portal/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/promisasu/Portal/downloads","issues_url":"https://api.github.com/repos/promisasu/Portal/issues{/number}","pulls_url":"https://api.github.com/repos/promisasu/Portal/pulls{/number}","milestones_url":"https://api.github.com/repos/promisasu/Portal/milestones{/number}","notifications_url":"https://api.github.com/repos/promisasu/Portal/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/promisasu/Portal/labels{/name}","releases_url":"https://api.github.com/repos/promisasu/Portal/releases{/id}","deployments_url":"https://api.github.com/repos/promisasu/Portal/deployments","created_at":"2015-09-23T01:10:13Z","updated_at":"2016-03-27T05:54:59Z","pushed_at":"2016-04-27T19:30:24Z","git_url":"git://github.com/promisasu/Portal.git","ssh_url":"git@github.com:promisasu/Portal.git","clone_url":"https://github.com/promisasu/Portal.git","svn_url":"https://github.com/promisasu/Portal","homepage":"","size":2277,"stargazers_count":3,"watchers_count":3,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":13,"forks":0,"open_issues":13,"watchers":3,"default_branch":"master"}},"base":{"label":"promisasu:development","ref":"development","sha":"128658ae9c5dc7507de757676172cf8ea6c0423e","user":{"login":"promisasu","id":16406149,"avatar_url":"https://avatars.githubusercontent.com/u/16406149?v=3","gravatar_id":"","url":"https://api.github.com/users/promisasu","html_url":"https://github.com/promisasu","followers_url":"https://api.github.com/users/promisasu/followers","following_url":"https://api.github.com/users/promisasu/following{/other_user}","gists_url":"https://api.github.com/users/promisasu/gists{/gist_id}","starred_url":"https://api.github.com/users/promisasu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/promisasu/subscriptions","organizations_url":"https://api.github.com/users/promisasu/orgs","repos_url":"https://api.github.com/users/promisasu/repos","events_url":"https://api.github.com/users/promisasu/events{/privacy}","received_events_url":"https://api.github.com/users/promisasu/received_events","type":"Organization","site_admin":false},"repo":{"id":42970270,"name":"Portal","full_name":"promisasu/Portal","owner":{"login":"promisasu","id":16406149,"avatar_url":"https://avatars.githubusercontent.com/u/16406149?v=3","gravatar_id":"","url":"https://api.github.com/users/promisasu","html_url":"https://github.com/promisasu","followers_url":"https://api.github.com/users/promisasu/followers","following_url":"https://api.github.com/users/promisasu/following{/other_user}","gists_url":"https://api.github.com/users/promisasu/gists{/gist_id}","starred_url":"https://api.github.com/users/promisasu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/promisasu/subscriptions","organizations_url":"https://api.github.com/users/promisasu/orgs","repos_url":"https://api.github.com/users/promisasu/repos","events_url":"https://api.github.com/users/promisasu/events{/privacy}","received_events_url":"https://api.github.com/users/promisasu/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/promisasu/Portal","description":"A portal to allow clinician to view and manage Promis surveys","fork":false,"url":"https://api.github.com/repos/promisasu/Portal","forks_url":"https://api.github.com/repos/promisasu/Portal/forks","keys_url":"https://api.github.com/repos/promisasu/Portal/keys{/key_id}","collaborators_url":"https://api.github.com/repos/promisasu/Portal/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/promisasu/Portal/teams","hooks_url":"https://api.github.com/repos/promisasu/Portal/hooks","issue_events_url":"https://api.github.com/repos/promisasu/Portal/issues/events{/number}","events_url":"https://api.github.com/repos/promisasu/Portal/events","assignees_url":"https://api.github.com/repos/promisasu/Portal/assignees{/user}","branches_url":"https://api.github.com/repos/promisasu/Portal/branches{/branch}","tags_url":"https://api.github.com/repos/promisasu/Portal/tags","blobs_url":"https://api.github.com/repos/promisasu/Portal/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/promisasu/Portal/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/promisasu/Portal/git/refs{/sha}","trees_url":"https://api.github.com/repos/promisasu/Portal/git/trees{/sha}","statuses_url":"https://api.github.com/repos/promisasu/Portal/statuses/{sha}","languages_url":"https://api.github.com/repos/promisasu/Portal/languages","stargazers_url":"https://api.github.com/repos/promisasu/Portal/stargazers","contributors_url":"https://api.github.com/repos/promisasu/Portal/contributors","subscribers_url":"https://api.github.com/repos/promisasu/Portal/subscribers","subscription_url":"https://api.github.com/repos/promisasu/Portal/subscription","commits_url":"https://api.github.com/repos/promisasu/Portal/commits{/sha}","git_commits_url":"https://api.github.com/repos/promisasu/Portal/git/commits{/sha}","comments_url":"https://api.github.com/repos/promisasu/Portal/comments{/number}","issue_comment_url":"https://api.github.com/repos/promisasu/Portal/issues/comments{/number}","contents_url":"https://api.github.com/repos/promisasu/Portal/contents/{+path}","compare_url":"https://api.github.com/repos/promisasu/Portal/compare/{base}...{head}","merges_url":"https://api.github.com/repos/promisasu/Portal/merges","archive_url":"https://api.github.com/repos/promisasu/Portal/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/promisasu/Portal/downloads","issues_url":"https://api.github.com/repos/promisasu/Portal/issues{/number}","pulls_url":"https://api.github.com/repos/promisasu/Portal/pulls{/number}","milestones_url":"https://api.github.com/repos/promisasu/Portal/milestones{/number}","notifications_url":"https://api.github.com/repos/promisasu/Portal/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/promisasu/Portal/labels{/name}","releases_url":"https://api.github.com/repos/promisasu/Portal/releases{/id}","deployments_url":"https://api.github.com/repos/promisasu/Portal/deployments","created_at":"2015-09-23T01:10:13Z","updated_at":"2016-03-27T05:54:59Z","pushed_at":"2016-04-27T19:30:24Z","git_url":"git://github.com/promisasu/Portal.git","ssh_url":"git@github.com:promisasu/Portal.git","clone_url":"https://github.com/promisasu/Portal.git","svn_url":"https://github.com/promisasu/Portal","homepage":"","size":2277,"stargazers_count":3,"watchers_count":3,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":13,"forks":0,"open_issues":13,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/promisasu/Portal/pulls/348"},"html":{"href":"https://github.com/promisasu/Portal/pull/348"},"issue":{"href":"https://api.github.com/repos/promisasu/Portal/issues/348"},"comments":{"href":"https://api.github.com/repos/promisasu/Portal/issues/348/comments"},"review_comments":{"href":"https://api.github.com/repos/promisasu/Portal/pulls/348/comments"},"review_comment":{"href":"https://api.github.com/repos/promisasu/Portal/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/promisasu/Portal/pulls/348/commits"},"statuses":{"href":"https://api.github.com/repos/promisasu/Portal/statuses/d7933409fc6d8f55490beae7b561a9a35b3132f4"}}}}