{"action":"created","comment":{"url":"https://api.github.com/repos/findOctave/cortex/pulls/comments/226875391","pull_request_review_id":166796763,"id":226875391,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIyNjg3NTM5MQ==","diff_hunk":"@@ -125,44 +139,155 @@ export const chargeFromAggregate = (\n })\n \n /**\n- * Given a rollup of appt data, finds the uncharged appts and returns Stripe\n+ * Given a rollup of appt data, finds the chargable appts and returns Stripe\n  * charge objects for them.\n  *\n- * @param {Array<Object>} aggregations data from `getApptDataInPastWeek`\n+ * @param {Array<Object>} apptsWithBillables data from `getApptsWithBillables`\n  * @return {Array<Object>} Array of Stripe charge objects\n  */\n-export const apptDataToChargeables = (aggregations, emailToDiscountSum) =>\n-  _.compact(\n-    aggregations.map(aggregate => {\n-      const { appt, charges } = aggregate\n-      if (\n-        (!appt.patient_arrived && !appt.did_not_arrive) || // Attendance status is unmarked\n-        !aggregate.billableItem || // Free appointment like Consult\n-        apptAlreadyCharged(appt, charges) // Already charged appt\n-      ) {\n-        return null\n-      } else return chargeFromAggregate(aggregate, emailToDiscountSum)\n+export const addChargesToChargableAppts = async apptsWithBillables => {\n+  const emailToDiscountSum = await getEmailToDiscountSum()\n+  return _.compact(\n+    apptsWithBillables.map(appt => {\n+      if (isChargableAppt(appt)) {\n+        appt.charge = buildCharge(appt, emailToDiscountSum)\n+        return appt\n+      } else return null\n     })\n   )\n+}\n+\n+/** ** Network Data Methods ****/\n \n /**\n- * Map Lead emailAddress to sumCouponDiscounts\n+ *  Get Leads from Airtable and, for each record, map emailAddress to sumCouponDiscounts\n+\n+ *  @return {Object} emailAddress -> sumCouponDiscounts\n  */\n-export const buildEmailToDiscountSum = async () => {\n+export const getEmailToDiscountSum = async () => {\n   const leads = await at.find({ table: 'leads' })\n   return _.mapValues(_.keyBy(leads, 'emailAddress'), 'sumCouponDiscounts')\n }\n \n-export default async () => {\n-  const apptData = await getApptDataInPastWeek()\n-  const emailToDiscountSum = await buildEmailToDiscountSum()\n-  const charges = apptDataToChargeables(apptData, emailToDiscountSum)\n-  await Promise.all(\n-    charges.map(async (charge, i) => {\n-      const { patient, apptType, appt } = _.find(\n-        apptData,\n-        ({ appt }) => appt.id === Number(charge.metadata.appointmentId)\n+/**\n+ *  For each Cliniko appointment, add the following related records:\n+ *  Cliniko Pracitioner\n+ *  Cliniko Patient\n+ *  Cliniko Appointment Type\n+ *  Cliniko Billable Item\n+ *  Stripe Customer\n+ *  Stripe Charges - all charges (both satisfied and unsatisfied)\n+ */\n+export const addBillableDataToAppts = async appts => {\n+  return Promise.all(\n+    appts.map(async appt => {\n+      const [practitioner, patient, apptType] = await Promise.all([\n+        cliniko.find({ url: appt.practitioner.links.self }),\n+        cliniko.find({ url: appt.patient.links.self }),\n+        cliniko.find({ url: appt.appointment_type.links.self })\n+      ])\n+      const billableItem = apptType.billable_item\n+        ? await cliniko.find({\n+          url: apptType.billable_item.links.self\n+        })\n+        : null\n+      // Ideally we'd fetch Auth0 by the patient's ID, but it's not possible.\n+      // https://community.auth0.com/t/how-to-search-for-users-by-using-app-metadata/8688\n+      const [authUser] = await auth0.getUsersByEmail(patient.email)\n+      const stripeCustomer = await stripe.customers.retrieve(\n+        authUser.user_metadata.customerId\n       )\n+      const { data: historicalCharges } = await stripe.charges.list({\n+        customer: stripeCustomer.id\n+      })\n+      return {\n+        appt,\n+        apptType,\n+        billableItem,\n+        historicalCharges,\n+        stripeCustomer,\n+        patient,\n+        practitioner\n+      }\n+    })\n+  )\n+}\n+\n+/**\n+ *  Given a list of Cliniko Group Appointments, get the Attendee join records and\n+ *  format each GroupAppointment record so that it contains the necessary\n+ *  Appointment fields\n+ *\n+ *  Schemas:","path":"apps/jobs/lib/charge-sessions.js","position":300,"original_position":300,"commit_id":"50d9a442895f63d77da266c921c676df1b3e4b6b","original_commit_id":"50d9a442895f63d77da266c921c676df1b3e4b6b","user":{"login":"craigspaeth","id":555859,"node_id":"MDQ6VXNlcjU1NTg1OQ==","avatar_url":"https://avatars1.githubusercontent.com/u/555859?v=4","gravatar_id":"","url":"https://api.github.com/users/craigspaeth","html_url":"https://github.com/craigspaeth","followers_url":"https://api.github.com/users/craigspaeth/followers","following_url":"https://api.github.com/users/craigspaeth/following{/other_user}","gists_url":"https://api.github.com/users/craigspaeth/gists{/gist_id}","starred_url":"https://api.github.com/users/craigspaeth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/craigspaeth/subscriptions","organizations_url":"https://api.github.com/users/craigspaeth/orgs","repos_url":"https://api.github.com/users/craigspaeth/repos","events_url":"https://api.github.com/users/craigspaeth/events{/privacy}","received_events_url":"https://api.github.com/users/craigspaeth/received_events","type":"User","site_admin":false},"body":"👍 nice comments here.","created_at":"2018-10-21T19:41:54Z","updated_at":"2018-10-21T19:53:12Z","html_url":"https://github.com/findOctave/cortex/pull/283#discussion_r226875391","pull_request_url":"https://api.github.com/repos/findOctave/cortex/pulls/283","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/findOctave/cortex/pulls/comments/226875391"},"html":{"href":"https://github.com/findOctave/cortex/pull/283#discussion_r226875391"},"pull_request":{"href":"https://api.github.com/repos/findOctave/cortex/pulls/283"}}},"pull_request":{"url":"https://api.github.com/repos/findOctave/cortex/pulls/283","id":224320461,"node_id":"MDExOlB1bGxSZXF1ZXN0MjI0MzIwNDYx","html_url":"https://github.com/findOctave/cortex/pull/283","diff_url":"https://github.com/findOctave/cortex/pull/283.diff","patch_url":"https://github.com/findOctave/cortex/pull/283.patch","issue_url":"https://api.github.com/repos/findOctave/cortex/issues/283","number":283,"state":"open","locked":false,"title":"Billing group appts","user":{"login":"lionsdwarf","id":10677428,"node_id":"MDQ6VXNlcjEwNjc3NDI4","avatar_url":"https://avatars1.githubusercontent.com/u/10677428?v=4","gravatar_id":"","url":"https://api.github.com/users/lionsdwarf","html_url":"https://github.com/lionsdwarf","followers_url":"https://api.github.com/users/lionsdwarf/followers","following_url":"https://api.github.com/users/lionsdwarf/following{/other_user}","gists_url":"https://api.github.com/users/lionsdwarf/gists{/gist_id}","starred_url":"https://api.github.com/users/lionsdwarf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lionsdwarf/subscriptions","organizations_url":"https://api.github.com/users/lionsdwarf/orgs","repos_url":"https://api.github.com/users/lionsdwarf/repos","events_url":"https://api.github.com/users/lionsdwarf/events{/privacy}","received_events_url":"https://api.github.com/users/lionsdwarf/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-10-19T15:09:51Z","updated_at":"2018-10-21T19:53:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e18b3496caa0a3d9db152ac630b53bf1d8702a2b","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/findOctave/cortex/pulls/283/commits","review_comments_url":"https://api.github.com/repos/findOctave/cortex/pulls/283/comments","review_comment_url":"https://api.github.com/repos/findOctave/cortex/pulls/comments{/number}","comments_url":"https://api.github.com/repos/findOctave/cortex/issues/283/comments","statuses_url":"https://api.github.com/repos/findOctave/cortex/statuses/50d9a442895f63d77da266c921c676df1b3e4b6b","head":{"label":"lionsdwarf:billing-group-appts","ref":"billing-group-appts","sha":"50d9a442895f63d77da266c921c676df1b3e4b6b","user":{"login":"lionsdwarf","id":10677428,"node_id":"MDQ6VXNlcjEwNjc3NDI4","avatar_url":"https://avatars1.githubusercontent.com/u/10677428?v=4","gravatar_id":"","url":"https://api.github.com/users/lionsdwarf","html_url":"https://github.com/lionsdwarf","followers_url":"https://api.github.com/users/lionsdwarf/followers","following_url":"https://api.github.com/users/lionsdwarf/following{/other_user}","gists_url":"https://api.github.com/users/lionsdwarf/gists{/gist_id}","starred_url":"https://api.github.com/users/lionsdwarf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lionsdwarf/subscriptions","organizations_url":"https://api.github.com/users/lionsdwarf/orgs","repos_url":"https://api.github.com/users/lionsdwarf/repos","events_url":"https://api.github.com/users/lionsdwarf/events{/privacy}","received_events_url":"https://api.github.com/users/lionsdwarf/received_events","type":"User","site_admin":false},"repo":{"id":148494916,"node_id":"MDEwOlJlcG9zaXRvcnkxNDg0OTQ5MTY=","name":"cortex","full_name":"lionsdwarf/cortex","private":false,"owner":{"login":"lionsdwarf","id":10677428,"node_id":"MDQ6VXNlcjEwNjc3NDI4","avatar_url":"https://avatars1.githubusercontent.com/u/10677428?v=4","gravatar_id":"","url":"https://api.github.com/users/lionsdwarf","html_url":"https://github.com/lionsdwarf","followers_url":"https://api.github.com/users/lionsdwarf/followers","following_url":"https://api.github.com/users/lionsdwarf/following{/other_user}","gists_url":"https://api.github.com/users/lionsdwarf/gists{/gist_id}","starred_url":"https://api.github.com/users/lionsdwarf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lionsdwarf/subscriptions","organizations_url":"https://api.github.com/users/lionsdwarf/orgs","repos_url":"https://api.github.com/users/lionsdwarf/repos","events_url":"https://api.github.com/users/lionsdwarf/events{/privacy}","received_events_url":"https://api.github.com/users/lionsdwarf/received_events","type":"User","site_admin":false},"html_url":"https://github.com/lionsdwarf/cortex","description":"The main Octave repo","fork":true,"url":"https://api.github.com/repos/lionsdwarf/cortex","forks_url":"https://api.github.com/repos/lionsdwarf/cortex/forks","keys_url":"https://api.github.com/repos/lionsdwarf/cortex/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lionsdwarf/cortex/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lionsdwarf/cortex/teams","hooks_url":"https://api.github.com/repos/lionsdwarf/cortex/hooks","issue_events_url":"https://api.github.com/repos/lionsdwarf/cortex/issues/events{/number}","events_url":"https://api.github.com/repos/lionsdwarf/cortex/events","assignees_url":"https://api.github.com/repos/lionsdwarf/cortex/assignees{/user}","branches_url":"https://api.github.com/repos/lionsdwarf/cortex/branches{/branch}","tags_url":"https://api.github.com/repos/lionsdwarf/cortex/tags","blobs_url":"https://api.github.com/repos/lionsdwarf/cortex/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lionsdwarf/cortex/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lionsdwarf/cortex/git/refs{/sha}","trees_url":"https://api.github.com/repos/lionsdwarf/cortex/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lionsdwarf/cortex/statuses/{sha}","languages_url":"https://api.github.com/repos/lionsdwarf/cortex/languages","stargazers_url":"https://api.github.com/repos/lionsdwarf/cortex/stargazers","contributors_url":"https://api.github.com/repos/lionsdwarf/cortex/contributors","subscribers_url":"https://api.github.com/repos/lionsdwarf/cortex/subscribers","subscription_url":"https://api.github.com/repos/lionsdwarf/cortex/subscription","commits_url":"https://api.github.com/repos/lionsdwarf/cortex/commits{/sha}","git_commits_url":"https://api.github.com/repos/lionsdwarf/cortex/git/commits{/sha}","comments_url":"https://api.github.com/repos/lionsdwarf/cortex/comments{/number}","issue_comment_url":"https://api.github.com/repos/lionsdwarf/cortex/issues/comments{/number}","contents_url":"https://api.github.com/repos/lionsdwarf/cortex/contents/{+path}","compare_url":"https://api.github.com/repos/lionsdwarf/cortex/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lionsdwarf/cortex/merges","archive_url":"https://api.github.com/repos/lionsdwarf/cortex/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lionsdwarf/cortex/downloads","issues_url":"https://api.github.com/repos/lionsdwarf/cortex/issues{/number}","pulls_url":"https://api.github.com/repos/lionsdwarf/cortex/pulls{/number}","milestones_url":"https://api.github.com/repos/lionsdwarf/cortex/milestones{/number}","notifications_url":"https://api.github.com/repos/lionsdwarf/cortex/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lionsdwarf/cortex/labels{/name}","releases_url":"https://api.github.com/repos/lionsdwarf/cortex/releases{/id}","deployments_url":"https://api.github.com/repos/lionsdwarf/cortex/deployments","created_at":"2018-09-12T14:39:11Z","updated_at":"2018-09-12T14:39:13Z","pushed_at":"2018-10-19T15:09:30Z","git_url":"git://github.com/lionsdwarf/cortex.git","ssh_url":"git@github.com:lionsdwarf/cortex.git","clone_url":"https://github.com/lionsdwarf/cortex.git","svn_url":"https://github.com/lionsdwarf/cortex","homepage":"https://octave-cortex-staging.herokuapp.com/","size":5282,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"findOctave:master","ref":"master","sha":"264a70e07bd1541cead172cea9257d2efb97cc91","user":{"login":"findOctave","id":40005440,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQwMDA1NDQw","avatar_url":"https://avatars2.githubusercontent.com/u/40005440?v=4","gravatar_id":"","url":"https://api.github.com/users/findOctave","html_url":"https://github.com/findOctave","followers_url":"https://api.github.com/users/findOctave/followers","following_url":"https://api.github.com/users/findOctave/following{/other_user}","gists_url":"https://api.github.com/users/findOctave/gists{/gist_id}","starred_url":"https://api.github.com/users/findOctave/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/findOctave/subscriptions","organizations_url":"https://api.github.com/users/findOctave/orgs","repos_url":"https://api.github.com/users/findOctave/repos","events_url":"https://api.github.com/users/findOctave/events{/privacy}","received_events_url":"https://api.github.com/users/findOctave/received_events","type":"Organization","site_admin":false},"repo":{"id":136378049,"node_id":"MDEwOlJlcG9zaXRvcnkxMzYzNzgwNDk=","name":"cortex","full_name":"findOctave/cortex","private":false,"owner":{"login":"findOctave","id":40005440,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQwMDA1NDQw","avatar_url":"https://avatars2.githubusercontent.com/u/40005440?v=4","gravatar_id":"","url":"https://api.github.com/users/findOctave","html_url":"https://github.com/findOctave","followers_url":"https://api.github.com/users/findOctave/followers","following_url":"https://api.github.com/users/findOctave/following{/other_user}","gists_url":"https://api.github.com/users/findOctave/gists{/gist_id}","starred_url":"https://api.github.com/users/findOctave/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/findOctave/subscriptions","organizations_url":"https://api.github.com/users/findOctave/orgs","repos_url":"https://api.github.com/users/findOctave/repos","events_url":"https://api.github.com/users/findOctave/events{/privacy}","received_events_url":"https://api.github.com/users/findOctave/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/findOctave/cortex","description":"The main Octave repo","fork":false,"url":"https://api.github.com/repos/findOctave/cortex","forks_url":"https://api.github.com/repos/findOctave/cortex/forks","keys_url":"https://api.github.com/repos/findOctave/cortex/keys{/key_id}","collaborators_url":"https://api.github.com/repos/findOctave/cortex/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/findOctave/cortex/teams","hooks_url":"https://api.github.com/repos/findOctave/cortex/hooks","issue_events_url":"https://api.github.com/repos/findOctave/cortex/issues/events{/number}","events_url":"https://api.github.com/repos/findOctave/cortex/events","assignees_url":"https://api.github.com/repos/findOctave/cortex/assignees{/user}","branches_url":"https://api.github.com/repos/findOctave/cortex/branches{/branch}","tags_url":"https://api.github.com/repos/findOctave/cortex/tags","blobs_url":"https://api.github.com/repos/findOctave/cortex/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/findOctave/cortex/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/findOctave/cortex/git/refs{/sha}","trees_url":"https://api.github.com/repos/findOctave/cortex/git/trees{/sha}","statuses_url":"https://api.github.com/repos/findOctave/cortex/statuses/{sha}","languages_url":"https://api.github.com/repos/findOctave/cortex/languages","stargazers_url":"https://api.github.com/repos/findOctave/cortex/stargazers","contributors_url":"https://api.github.com/repos/findOctave/cortex/contributors","subscribers_url":"https://api.github.com/repos/findOctave/cortex/subscribers","subscription_url":"https://api.github.com/repos/findOctave/cortex/subscription","commits_url":"https://api.github.com/repos/findOctave/cortex/commits{/sha}","git_commits_url":"https://api.github.com/repos/findOctave/cortex/git/commits{/sha}","comments_url":"https://api.github.com/repos/findOctave/cortex/comments{/number}","issue_comment_url":"https://api.github.com/repos/findOctave/cortex/issues/comments{/number}","contents_url":"https://api.github.com/repos/findOctave/cortex/contents/{+path}","compare_url":"https://api.github.com/repos/findOctave/cortex/compare/{base}...{head}","merges_url":"https://api.github.com/repos/findOctave/cortex/merges","archive_url":"https://api.github.com/repos/findOctave/cortex/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/findOctave/cortex/downloads","issues_url":"https://api.github.com/repos/findOctave/cortex/issues{/number}","pulls_url":"https://api.github.com/repos/findOctave/cortex/pulls{/number}","milestones_url":"https://api.github.com/repos/findOctave/cortex/milestones{/number}","notifications_url":"https://api.github.com/repos/findOctave/cortex/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/findOctave/cortex/labels{/name}","releases_url":"https://api.github.com/repos/findOctave/cortex/releases{/id}","deployments_url":"https://api.github.com/repos/findOctave/cortex/deployments","created_at":"2018-06-06T19:40:44Z","updated_at":"2018-10-21T15:52:43Z","pushed_at":"2018-10-21T16:13:07Z","git_url":"git://github.com/findOctave/cortex.git","ssh_url":"git@github.com:findOctave/cortex.git","clone_url":"https://github.com/findOctave/cortex.git","svn_url":"https://github.com/findOctave/cortex","homepage":"https://octave-cortex-staging.herokuapp.com/","size":5299,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"archived":false,"open_issues_count":2,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":5,"open_issues":2,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/findOctave/cortex/pulls/283"},"html":{"href":"https://github.com/findOctave/cortex/pull/283"},"issue":{"href":"https://api.github.com/repos/findOctave/cortex/issues/283"},"comments":{"href":"https://api.github.com/repos/findOctave/cortex/issues/283/comments"},"review_comments":{"href":"https://api.github.com/repos/findOctave/cortex/pulls/283/comments"},"review_comment":{"href":"https://api.github.com/repos/findOctave/cortex/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/findOctave/cortex/pulls/283/commits"},"statuses":{"href":"https://api.github.com/repos/findOctave/cortex/statuses/50d9a442895f63d77da266c921c676df1b3e4b6b"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"craigspaeth"}}