Skip to content

Instantly share code, notes, and snippets.

@GonchuB
Last active August 29, 2015 14:02
Show Gist options
  • Save GonchuB/cfa7b641f04ee6286626 to your computer and use it in GitHub Desktop.
Save GonchuB/cfa7b641f04ee6286626 to your computer and use it in GitHub Desktop.
parseResponseSettings
var
_ = require('underscore'),
Utils = {
string: require('../utils/string')
},
whereToValues = {
notification: ['inApp', 'email'],
sharing: ['facebook', 'twitter']
},
categoryValues = {
notification: ['favoriteTour', 'favoriteMedia', 'facebookFriend'],
sharing: ['favoriteArtist', 'favoriteVideo', 'playlistView']
}
function parseResponseSettings (settings, type) {
var parsedSettings = {},
_whereToValues = whereToValues[type];
settings = settings[type];
// Iterate through possible category share values.
_.each(categoryValues[type], function(action) {
// Front-End expects 'notificationFavoriteMedia', and we have just 'favoriteMedia'.
var category = type + Utils.string.capitalize(action);
// Initialize empty category.
parsedSettings[category] = {};
// Find user settings for given category.
var foundSetting = _.findWhere(settings, {category: action});
// Iterate through possible whereToValues to set category to true or false.
_.each(_whereToValues, function(whereToValue) {
// notificationFavoriteMedia.facebook = true if foundSetting.whereTo (array) contains facebook
parsedSettings[category][whereToValue] = !!(foundSetting && _.contains(foundSetting.whereTo, whereToValue));
});
});
return parsedSettings;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment