Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Documentation and fix User and Org Fields #6

Merged
merged 4 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@ This example will generate the following HTML inside the app:
* {{ticket.requester.name}}
* {{ticket.requester.email}}
* {{ticket.requester.externalId}}
* {{ticket.requester.firstname}}
* {{ticket.requester.lastname}}
* {{ticket.requester.firstName}}
* {{ticket.requester.lastName}}
* {{ticket.requester.user_fields.YYY}} = custom user fields can be used
* {{ticket.assignee.user.id}}
* {{ticket.assignee.user.name}}
* {{ticket.assignee.user.email}}
* {{ticket.assignee.user.externalId}}
* {{ticket.assignee.user.firstname}}
* {{ticket.assignee.user.lastname}}
* {{ticket.assignee.user.firstName}}
* {{ticket.assignee.user.lastName}}
* {{ticket.assignee.group.id}}
* {{ticket.assignee.group.name}}
* {{ticket.custom_field_XXXXXXX}} // XXXXXXX = custom field id
* {{ticket.organization.organization_fields.XXXXXXX}} // XXXXXXX = Field key, default is field name
* {{currentUser.id}}
* {{current_user.name}}
* {{current_user.email}}
* {{current_user.externalId}}
* {{current_user.firstname}}
* {{current_user.lastname}}
* {{currentUser.name}}
* {{currentUser.email}}
* {{currentUser.externalId}}
* {{currentUser.firstName}}
* {{currentUser.lastName}}

### Making changes

Expand Down
16 changes: 16 additions & 0 deletions src/javascripts/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,19 @@ export function getTicketData(ticketId) {
dataType: 'json'
}
}

export function getUserData(userId) {
return {
url: `/api/v2/users/${userId}.json`,
type: 'GET',
dataType: 'json'
}
}

export function getOrganizationData(orgId) {
return {
url: `/api/v2/organizations/${orgId}.json`,
type: 'GET',
dataType: 'json'
}
}
63 changes: 20 additions & 43 deletions src/javascripts/modules/context.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import client from '../lib/client';
import { getTicketData } from "../lib/api";
import { getUserData } from "../lib/api";
import { getOrganizationData } from "../lib/api";

const TEMPLATE_OPTIONS = { interpolate: /\{\{(.+?)\}\}/g };

/**
* Parses a Zendesk users first and last name
* from their Full name
*
* TODO: Docz
*
* @param {Object} user - A Zendesk User Object
*/
export function parseFirstLastName(user) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had this twice; this one being exported but not used anywhere else.

const [first_name = '', last_name = ''] = (user.name || '').split(' ');

return {
...user,
first_name,
last_name,
};
}

/**
* TODO: JS DOcs
* @param {*} settings - blah
Expand All @@ -36,7 +20,7 @@ export function getUrisFromSettings({ uri_templates }) {
*/
export function buildTemplatesFromContext(uris, context) {
return _.map(uris, uri => {
mike.url = _.template(uri.url, TEMPLATE_OPTIONS)(context)
uri.url = _.template(uri.url, TEMPLATE_OPTIONS)(context)
uri.title = _.template(uri.title, TEMPLATE_OPTIONS)(context)

return uri;
Expand All @@ -57,47 +41,35 @@ function assignTicketFields(ticket, ticketFields) {
return ticketCopy;
}

/**
* TODO: JS DOcs
* @param {*} user
*/
function parseFirstLastName(user) {
const [first_name = '', last_name = ''] = (user.name || '').split(' ');
async function processUserObject(user) {
const [firstName = '', lastName = ''] = (user.name || '').split(' ');
const { user: {user_fields}} = await client.request(getUserData(user.id));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Destructuring a nested object.


return {
...user,
first_name,
last_name,
firstName,
lastName,
user_fields
};
}

/**
* TODO: JS DOcs
*/
async function getContext() {
function buildContext(ticket, currentUser) {
async function buildContext(ticket, currentUser) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async in an async doesn't read great. I wonder if we should pull buildContext out and place it above, like the processUserObject function. Although only the getContext function uses them, it might help readability.

let context = {};
context.ticket = ticket;

if (ticket.requester.id) {
context.ticket.requester = parseFirstLastName(ticket.requester);

/*
// TODO: Look into organizations
// this should be ticket.requester.organization_id
if (context.ticket.requester.organization_id) {
context.ticket.organization = _.find(data.organizations, org => {
return org.id = context.ticket.requester.organization_id;
});
}
*/
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per this issue, I believe the organization fields are referred to incorrectly. The below code refers to the ticket's organization versus one of the requester organizations.

context.ticket.requester = await processUserObject(ticket.requester);
}

if (ticket.assignee.id) {
context.ticket.assignee.user = parseFirstLastName(ticket.assignee);
if (ticket.assignee.user.id) {
context.ticket.assignee.user = await processUserObject(ticket.assignee.user);
}

context.current_user = parseFirstLastName(currentUser);
context.currentUser = await processUserObject(currentUser);

return context;
};
Expand All @@ -106,9 +78,14 @@ async function getContext() {
let { ticket } = await client.get('ticket');
const ticketFields = await client.request(getTicketData(ticket.id));

if (ticket.organization) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a conundrum. I checked both ZAFCient and Zendesk API for all the organizations, and of the 5 Organization objects returned, none of them had data in the organization_fields object. Furthermore, our User objects and Ticket object do not have organization data. I'm not sure why, or how this gets assigned. Either way, to protect ourselves from this, I have a null check for ticket.organization before trying to get the organization fields.

const { organization } = await client.request(getOrganizationData(ticket.organization.id));
ticket.organization.organization_fields = organization.organization_fields;
}

ticket = assignTicketFields(ticket, ticketFields);

return buildContext(ticket, currentUser)
return await buildContext(ticket, currentUser)
}

export default getContext;