UNPKG

gpii-universal

Version:

Cross platform, core components of the GPII personalization infrastructure.

61 lines (50 loc) 2.5 kB
/*! GPII Preferences Server PUT Handler Copyright 2014 Raising the Floor - International Copyright 2018 OCAD University Licensed under the New BSD license. You may not use this file except in compliance with this License. The research leading to these results has received funding from the European Union's Seventh Framework Programme (FP7/2007-2013) under grant agreement no. 289016. You may obtain a copy of the License at https://github.com/GPII/universal/blob/master/LICENSE.txt */ "use strict"; var fluid = require("infusion"), gpii = fluid.registerNamespace("gpii"); require("../../solutionsRegistry/src/js/sr-validation-middleware.js"); fluid.defaults("gpii.preferencesServer.put.handler", { gradeNames: ["gpii.universal.solutionsRegistry.requestHandlers.preferences"], invokers: { handleRequest: { funcName: "gpii.preferencesServer.post.handler.handlePreferences", args: ["{preferencesServer}", "{preferencesService}.getPreferencesByGpiiKey", "{that}", "{that}.req.params.gpiiKey", "{that}.req.body", "{that}.req.query.view", "{that}.req.query.merge"] } } }); gpii.preferencesServer.post.handler.handlePreferences = function (preferencesServer, getPreferencesByGpiiKeyFunc, request, gpiiKey, preferences, view, merge) { var gpiiKeyPromise = getPreferencesByGpiiKeyFunc(gpiiKey); // GPII-3721: if GPII key already exists, update its preferences. Otherwise, create the GPII key and its prefs safe. gpiiKeyPromise.then(function () { merge = (merge === "true"); // convert string to boolean var prefsPromise = preferencesServer.updatePreferences(gpiiKey, preferences, view, merge); prefsPromise.then(request.events.onSuccess.fire, function (error) { gpii.preferencesServer.post.handler.fireError(request, error); }); }, function (error) { if (error.errorCode === gpii.preferencesServer.errors.missingGpiiKey.errorCode) { var prefsPromise = preferencesServer.createPreferences(preferences, view, gpiiKey); prefsPromise.then(request.events.onSuccess.fire, request.events.onError.fire); } else { gpii.preferencesServer.post.handler.fireError(request, error); } }); }; gpii.preferencesServer.post.handler.fireError = function (request, error) { request.events.onError.fire({ isError: true, errorCode: error.errorCode, message: error.message, statusCode: 404 }); };