From e2662584b53133cba5067eb02cc062a0fbb93b00 Mon Sep 17 00:00:00 2001 From: Steven Maguire Date: Thu, 6 Oct 2016 14:10:01 -0500 Subject: [PATCH] Add support for organization search results --- CHANGELOG.md | 18 ++++++++++ README.md | 4 +-- app.js | 92 ++++++++++++++++++++++++++++++++++++++++----------- manifest.json | 2 +- 4 files changed, 93 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 083834c..727f4b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,24 @@ All Notable changes to `chargify-zendesk` will be documented in this file +## 1.2.0 - 2016-10-06 + +### Added +- Support for Organization Sidebar based on space delimited domains attached to the given organization +- Support for displaying subscriptions associated with a given user or ticket requester's organizations + +### Deprecated +- Nothing + +### Fixed +- Nothing + +### Removed +- Nothing + +### Security +- Nothing + ## 1.1.1 - 2016-10-04 ### Added diff --git a/README.md b/README.md index 3bf2502..a0b3b8d 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,9 @@ Access your Chargify subscription data from within Zendesk. ## Usage -After the app has been successfully installed and enabled, it will show up in the right pane of the User and Ticket views within Zendesk. +After the app has been successfully installed and enabled, it will show up in the right pane of the User, Ticket, and Organization views within Zendesk. -Upon initial load of the User Profile or Ticket, the app will search for customer records in your Chargify account associated with the User's primary email address or Ticket Requestors email address. +Upon initial load of the User Profile, Ticket, or Organization, the app will search for customer records in your Chargify account associated with the User's primary email address, Ticket Requestors email address, or Organization email domains (prepend `@` sign before searching). When no customer records are found in Chargify, then the UI will display an alert. diff --git a/app.js b/app.js index 6486942..a4dcd6d 100644 --- a/app.js +++ b/app.js @@ -21,9 +21,9 @@ dataType: 'json' }; }, - fetchCustomersByEmail: function(email) { + fetchCustomersByQuery: function(query) { return { - url: 'https://'+this.settings.subdomain+'.chargify.com/customers.json?q='+email, + url: 'https://'+this.settings.subdomain+'.chargify.com/customers.json?q='+query, type:'GET', username: this.settings.api_key, password: 'x', @@ -138,6 +138,37 @@ return; }, + getCustomerOrganizationDomains: function () { + var organization, user; + + if (this.currentLocation() === 'ticket_sidebar') { + user = this.ticket().requester(); + } else if (this.currentLocation() === 'user_sidebar') { + user = this.user(); + } else if (this.currentLocation() === 'organization_sidebar') { + organization = this.organization(); + } + + if (organization) { + return organization.domains().split(' '); + } + + if (user) { + var organizations = user.organizations(), + domains = []; + + organizations.map(function (organization) { + organization.domains().split(' ').map(function (domain) { + domains.push(domain); + }); + }); + + return domains; + } + + return; + }, + getDomainFromURL: function(baseURI) { var regexResult = this.resources.DOMAIN_PATTERN.exec(baseURI); return regexResult[0]; @@ -207,28 +238,49 @@ var app = this; var pageData = { title: app.I18n.t('searchPage.title'), - customers: [] + customers: [], + expected_queries: [], + queries_completed: 0 }; app.switchTo('loading'); + + var displayResults = function () { + if (pageData.customers.length == 1) { + app.showCustomer(e, {customerId: pageData.customers[0].id}); + } else { + app.switchTo('searchPage', pageData); + } + }; + + var aggregateResults = function (data) { + pageData.queries_completed++; + app.cacheChargifyCustomerSearch(data); + for (var i = data.length - 1; i >= 0; i--) { + pageData.customers.push( + app.buildCustomerFromChargifyCustomer(data[i]) + ); + } + if (pageData.queries_completed >= pageData.expected_queries.length) { + displayResults(); + } + }; + var email = app.getCustomerEmail(); if (email) { - app.ajax('fetchCustomersByEmail', email) - .done(function (data) { - this.cacheChargifyCustomerSearch(data); - if (data.length == 1) { - var customer = app.buildCustomerFromChargifyCustomer(data[0]); - this.showCustomer(e, {customerId: customer.id}); - } else { - for (var i = data.length - 1; i >= 0; i--) { - pageData.customers.push( - app.buildCustomerFromChargifyCustomer(data[i]) - ); - } - app.switchTo('searchPage', pageData); - } - }); - } else { - app.switchTo('searchPage', pageData); + pageData.expected_queries.push(email); + app.ajax('fetchCustomersByQuery', email).done(aggregateResults); + } + + var organizationDomains = app.getCustomerOrganizationDomains(); + if (organizationDomains) { + organizationDomains.map(function (domain) { + pageData.expected_queries.push(domain); + app.ajax('fetchCustomersByQuery', '@' + domain).done(aggregateResults); + }); + } + + if (pageData.expected_queries === 0) { + displayResults(); } } }, diff --git a/manifest.json b/manifest.json index 89170d9..7be3bc8 100644 --- a/manifest.json +++ b/manifest.json @@ -7,7 +7,7 @@ }, "defaultLocale": "en", "private": true, - "location": ["ticket_sidebar","user_sidebar"], + "location": ["organization_sidebar","ticket_sidebar","user_sidebar"], "version": "1.0", "frameworkVersion": "1.0", "parameters": [