Skip to content

Commit

Permalink
Add support for organization search results
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmaguire committed Oct 6, 2016
1 parent d23e941 commit e266258
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 23 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
92 changes: 72 additions & 20 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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();
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit e266258

Please sign in to comment.