Skip to content

Instantly share code, notes, and snippets.

@svizzari
Created February 10, 2016 01:15
Show Gist options
  • Save svizzari/5b794ebb40a787a23108 to your computer and use it in GitHub Desktop.
Save svizzari/5b794ebb40a787a23108 to your computer and use it in GitHub Desktop.
(function() {
with( ZendeskApps.AppScope.create() ) {
var source = (function() {
return {
fieldsOnError: [],
requests: {
fetchUser: function() {
return {
url: helpers.fmt('/api/v2/users/%@.json?include=groups,organizations',
this.currentUser().id()),
dataType: 'json',
type: 'GET'
};
}
},
events: {
'app.activated' : 'onAppActivated',
'fetchUser.done' : 'onFetchUserDone',
'ticket.save' : 'onTicketSave',
'*.changed' : 'onFieldChanged'
},
onAppActivated: function(app) {
this.ajax('fetchUser');
},
onFetchUserDone: function(data) {
this.data = data;
this.onFieldChanged();
},
onTicketSave: function() {
var fieldsOnError = this.validateRequiredFields();
if (!_.isEmpty(fieldsOnError)) {
return this.I18n.t('invalid_fields', { fields: this.fieldsLabel(fieldsOnError).join(',') });
}
return true;
},
onFieldChanged: function() {
if (!this.data) return;
_.defer(this.handleFields.bind(this));
},
handleFields: function() {
this.handleHiddenFields();
this.handleReadOnlyFields();
},
validateRequiredFields: function() {
return _.filter(this.requiredFields(), function(field) {
return !this.fieldIsValid(field);
}, this);
},
handleHiddenFields: function() {
this.hiddenFields().forEach(function(field) {
this.applyActionOnField(field, 'hide');
}, this);
},
handleReadOnlyFields: function() {
this.readOnlyFields().forEach(function(field) {
this.applyActionOnField(field, 'disable');
}, this);
},
applyActionOnField: function(field, action) {
var splittedField = field.split('.'),
fieldName = splittedField[0],
optionValue = splittedField[1],
ticketField = this.ticketFields(fieldName);
if (!ticketField) { return false; }
if (optionValue && ticketField.options()) {
var option = _.find(ticketField.options(), function(opt) {
return opt.value() == optionValue;
});
if (option) {
option[action]();
}
} else {
ticketField[action]();
}
},
requiredFields: _.memoize(function() {
return this.fields('required_fields');
}),
hiddenFields: _.memoize(function() {
return this.fields('hidden_fields');
}),
readOnlyFields: _.memoize(function() {
return this.fields('readonly_fields');
}),
fields: function(type) {
if (this.currentUserIsWithlistedFor(type))
return [];
return this.splittedSetting(type);
},
currentUserIsWithlistedFor: function(type) {
return _.any([
this.currentUserIsWhitelistedByTagFor(type),
this.currentUserIsWhitelistedByGroupFor(type),
this.currentUserIsWhitelistedByOrganizationFor(type)
]);
},
currentUserIsWhitelistedByTagFor: function(type) {
var tags = this.splittedSetting(type + '_whitelist_tags');
return this.deepContains(this.data.user.tags, tags);
},
currentUserIsWhitelistedByGroupFor: function(type) {
var group_ids = this.splittedSetting(type + '_whitelist_group_ids'),
current_group_ids = _.map(this.data.groups, function(group) {
return String(group.id);
});
return this.deepContains(current_group_ids, group_ids);
},
currentUserIsWhitelistedByOrganizationFor: function(type) {
var organization_ids = this.splittedSetting(type + '_whitelist_organization_ids'),
current_organization_ids = _.map(this.data.organizations, function(organization) {
return String(organization.id);
});
return this.deepContains(current_organization_ids, organization_ids);
},
//list and values should be Arrays
deepContains: function(list, values) {
var flattened_contains = _.inject(values, function(memo, value) {
memo.push(_.contains(list, value));
return memo;
}, []);
return _.any(flattened_contains);
},
splittedSetting: function(name) {
return _.compact((this.setting(name) || '').split(','));
},
fieldIsValid: function(field) {
var value = _.clone(this.containerContext().ticket[field]);
// field is present and is empty
if (this.ticketFields(field) &&
(_.isEmpty(value) || value == '-' ||
(field == "type" && value == "ticket"))) {
return false;
}
return true;
},
fieldsLabel: function(fields) {
return _.map(fields, function(field) {
var tf = this.ticketFields(field),
label = this.ticketFields(field) && this.ticketFields(field).label();
if (label) {
return label;
} else {
return field;
}
}, this);
}
};
}());
;
var app = ZendeskApps.defineApp(source)
.reopenClass({
location: ["ticket_sidebar","new_ticket_sidebar"],
noTemplate: true
})
.reopen({
assetUrlPrefix: "/api/v2/apps/44024/assets/",
appClassName: "app-44024",
author: {
name: "Zendesk Services",
email: "services@zendesk.com"
},
translations: {"app":{"parameters":{"required_fields":{"label":"Required Fields","helpText":"List of required ticket fields separated by commas. Example: status,custom_field_1234567"},"required_fields_whitelist_tags":{"label":"Required Fields whitelist tags","helpText":"List of tags separated by commas. Example: admin,super_agent"},"required_fields_whitelist_group_ids":{"label":"Required Fields whitelist groups","helpText":"List of group ids separated by commas. Example: 231231321,34345456"},"required_fields_whitelist_organization_ids":{"label":"Required Fields whitelist organizations","helpText":"List of organization ids separated by commas. Example: 231231321,34345456"},"hidden_fields":{"label":"Hidden Fields","helpText":"List of hidden ticket fields separated by commas. Example: status,custom_field_1234567"},"hidden_fields_whitelist_tags":{"label":"Hidden Fields whitelist tags","helpText":"List of tags separated by commas. Example: admin,super_agent"},"hidden_fields_whitelist_group_ids":{"label":"Hidden Fields whitelist groups","helpText":"List of group ids separated by commas. Example: 231231321,34345456"},"hidden_fields_whitelist_organization_ids":{"label":"Hidden Fields whitelist organizations","helpText":"List of organization ids separated by commas. Example: 231231321,34345456"},"readonly_fields":{"label":"Readonly Fields","helpText":"List of read only ticket fields separated by commas. Example: status,custom_field_1234567"},"readonly_fields_whitelist_tags":{"label":"Readonly Fields whitelist tags","helpText":"List of tags separated by commas. Example: admin,super_agent"},"readonly_fields_whitelist_group_ids":{"label":"Readonly Fields whitelist groups","helpText":"List of group ids separated by commas. Example: 231231321,34345456"},"readonly_fields_whitelist_organization_ids":{"label":"Readonly Fields whitelist organizations","helpText":"List of organization ids separated by commas. Example: 231231321,34345456"}}},"invalid_fields":"The following fields cannot be empty: <strong>{{fields}}</strong>"},
templates: {},
frameworkVersion: "1.0"
});
ZendeskApps["Ticket Restrictions"] = app;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment