var _ = require('lodash');
var openfisca = require('../lib/openfisca');
var openfiscaTest = require('../lib/openfisca/test');
var Situation = require('mongoose').model('Situation');

exports.situation = function(req, res, next, id) {
    Situation.findById(id, function(err, situation) {
        if (err) return next(err);
        if (! situation) return res.sendStatus(404);

        req.situation = situation;
        next();
    });
};

exports.validateAccess = function(req, res, next) {
    var situation = req.situation;
    if (req.situation.status === 'test' || req.situation.status === 'investigation' || !situation.token || req.cookies['situation_' + situation.id] === situation.token) return next();
    res.status(403).send({ error: 'You do not have access to this situation.' });
};

exports.show = function(req, res) {
    res.send(req.situation);
};

exports.create = function(req, res, next) {
    if (req.body._id) return res.status(403).send({ error: 'You can‘t provide _id when saving a situation. _id will be generated automatically.' });

    return Situation.create(_.omit(req.body, 'status', 'token'), function(err, persistedSituation) {
        if (err) return next(err);

        res.cookie('situation_' + persistedSituation.id, persistedSituation.token, { maxAge: 7 * 24 * 3600 * 1000, httpOnly: true });
        res.send(persistedSituation);
    });
};

exports.openfiscaResponse = function(req, res, next) {
    return openfisca.calculate(req.situation, function(err, result) {
        if (err) return next(Object.assign(err, { _id: req.situation._id }));

        res.send(Object.assign(result, { _id: req.situation._id }));
    });
};

exports.openfiscaTrace = function(req, res, next) {
    return openfisca.trace(req.situation, function(err, result) {
        if (err) return next(Object.assign(err, { _id: req.situation._id }));

        res.send(Object.assign(result, { _id: req.situation._id }));
    });
};

exports.openfiscaRequest = function(req, res) {
    res.send(openfisca.buildOpenFiscaRequest(req.situation));
};

exports.openfiscaRequestFromLegacy = function(req, res) {
    res.send(openfisca.buildOpenFiscaRequestFromLegacySituation(req.situation));
};

var DETAILS_DEFAULT_ATTRIBUTES = {
    absolute_error_margin: 10,
};

// Attributes are sorted as they should appear in the YAML test file
var DETAILS_ATTRIBUTES = [
    'name',
    'description',
    'extension',
    'absolute_error_margin',
    'relative_error_margin',
    'output_variables',
];

exports.openfiscaTest = function(req, res) {
    var details = _.assign({}, DETAILS_DEFAULT_ATTRIBUTES,
        _.pick(req.body, DETAILS_ATTRIBUTES)
    );
    if (! details.name || ! details.description || ! details.output_variables) {
        return res.status(403).send({ error: 'You must provide a name, description and output_variables.' });
    }

    var situation = req.situation.toObject ? req.situation.toObject() : req.situation;
    res.type('yaml').send(openfiscaTest.generateYAMLTest(details, situation));
};

@@ -176,16 +176,50 @@
 ion');%0A%0A
+var cookiePrefix = 'situation_';%0A%0A
 exports.
@@ -673,28 +673,28 @@
 cookies%5B
-'situation_'
+cookiePrefix
  + situa
@@ -889,24 +889,498 @@
 ation);%0A%7D;%0A%0A
+function clearCookies(req, res) %7B%0A    var limit = 10;%0A%0A    var keys = Object.keys(req.cookies);%0A    var situationCookies = _.filter(keys, function(k) %7B return k.startsWith(cookiePrefix); %7D);%0A    situationCookies.sort();%0A%0A    if (situationCookies.length-limit%3E=0) %7B%0A        var cookieToClear = situationCookies.slice(0, situationCookies.length-limit);%0A        cookieToClear.forEach(function(name) %7B%0A            res.clearCookie(name, %7B httpOnly: true %7D);%0A        %7D);%0A    %7D%0A%7D%0A%0A
 exports.crea
@@ -1710,31 +1710,64 @@
     
-res.cookie('situation_'
+clearCookies(req, res);%0A%0A        res.cookie(cookiePrefix
  + p

Supprime des cookies de situations