All files / universal.klown/gpii/node_modules/preferencesServer/src preferencesPutHandler.js

100% Statements 15/15
100% Branches 2/2
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                                  1x 1x   1x                                                     1x 9x 9x   3x 2x   1x 1x   2x       1x   8x 8x 8x      
/*!
GPII Preferences Server PUT Handler
 
Copyright 2014 Raising the Floor - International
 
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");
 
fluid.defaults("gpii.preferencesServer.put.handler", {
    gradeNames: ["kettle.request.http"],
    invokers: {
        handleRequest: {
            funcName: "gpii.preferencesServer.put.handleRequest",
            args: [ "{request}", "{preferencesServer}", "{that}.events.onGetRawPreferences"]
        },
        onGetRawPreferences: {
            funcName: "gpii.preferencesServer.put.onGetRawPreferences",
            args: [ "{preferencesServer}", "{preferencesServer}.ontologyHandler",
                 "{request}", "{arguments}.0", "{that}.events.onPutRawPreferences" ]
        },
        onPutRawPreferences: {
            func: "{preferencesServer}.setPrefsReturnHandler",
            args: [ "{arguments}.0", "{request}" ]
        }
    },
    events: {
        onGetRawPreferences: null,
        onPutRawPreferences: null
    },
    listeners: {
        onGetRawPreferences: "{that}.onGetRawPreferences",
        onPutRawPreferences: "{that}.onPutRawPreferences"
    }
});
 
gpii.preferencesServer.put.handleRequest = function (request, preferencesServer, event) {
    var promise = preferencesServer.getRawPreferences(request.req.params.userToken);
    promise.then(event.fire, function (data) {
        // If error stems from preference set not being found, continue putting the error set. Else pass on error
        if (data.statusCode === 404) {
            data = {};
        } else {
            request.events.onError.fire(data);
            return;
        }
        event.fire(data);
    });
};
 
gpii.preferencesServer.put.onGetRawPreferences = function (preferencesServer, ontologyHandler, request, rawPrefs, event) {
    // get the view of the given preferences (in body), update raw prefernces set and PUT it
    var prefsView = gpii.preferencesServer.getRequestView(request);
    rawPrefs = ontologyHandler.addPrefsToRawPrefs(request.req.body, prefsView, rawPrefs);
    preferencesServer.setRawPreferences(event, rawPrefs, request.req.params.userToken);
};