Skip to content

Commit

Permalink
Update Documentation and fix User and Org Fields (#6)
Browse files Browse the repository at this point in the history
* add user_fields and org_fields, camelCase first and last name
  • Loading branch information
physik932 authored Sep 24, 2019
1 parent 0369740 commit 1bd166b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
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'
}
}
61 changes: 21 additions & 40 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) {
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 @@ -58,46 +42,38 @@ function assignTicketFields(ticket, ticketFields) {
}

/**
* TODO: JS DOcs
* 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));

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) {
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;
});
}
*/
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 +82,14 @@ async function getContext() {
let { ticket } = await client.get('ticket');
const ticketFields = await client.request(getTicketData(ticket.id));

if (ticket.organization) {
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;

0 comments on commit 1bd166b

Please sign in to comment.