<commit_before>var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var repository = require('./js/repository.js');
var secretGenerator = require('./js/secret-generator.js');
var mailSender = require('./js/mail-sender.js');

var port = process.env.PORT || 5000;
var router = express.Router();
var jsonParser = bodyParser.json();

router.get('/', function (req, res) {
  res.json({message: 'Velkommen til Fotballvarsel for Skeid.no' });
});

router.route('/teams')
    .get(function(req, res) {
      repository.findAllTeams(function(error, teams) {
        res.json(teams);
      });
    });

router.route('/newsecret')
    .post(jsonParser, function(req, res) {
        var email = req.body.email;
        var secret = secretGenerator.newSecret();

        if (!email) {
            return res.json({
                status: "error"
            });
        }

        repository.findSubscriberByEmail(email, function(error, subscriber) {
            if (subscriber) {
                console.log("Issuing new secret for %s", email);
                subscriber.secret = secret;
                updateSubscriberAndSendEmail(subscriber, res);
            } else {
                console.log("Registering new subscriber %s", email);
                var newSubscriber = {
                    email: email,
                    secret: secret,
                    teams: []
                };
                updateSubscriberAndSendEmail(newSubscriber, res);
            }
        });
    });

var updateSubscriberAndSendEmail = function(subscriber, res) {
    repository.insertOrUpdateSubscriber(subscriber.email, subscriber, function(error, result) {
        mailSender.newSecret(subscriber, function(error, result) {
            if (error) {
                console.log("Error sending email: %s", JSON.stringify(error));
                res.json({
                    email: subscriber.email,
                    status: "error"
                });
            } else {
                res.json({
                    email: subscriber.email,
                    status: "ok"
                });
            }
        });
    })
};

router.route('/subscribers/:email')
    .get(function(req, res) {
      console.log("Getting subscriber " + req.params.email);
      repository.findSubscriberByEmail(req.params.email, function(err, subscriber) {
        if (err) {
          console.log("Error in repo: " + err);
          res.send(err);
        }
        res.json(subscriber || {});
      });
    })
    .post(jsonParser, function(req, res) {
        var email = req.params.email;
        console.log("Inserting subscriber %s as %s", email, JSON.stringify(req.body));
        repository.insertOrUpdateSubscriber(email, req.body, function(err, status) {
            if (err) {
                console.log("Error in repo: " + err);
                res.send(err);
            }
            console.log("Inserted %s", email);
            res.json(status);
        })
    }).put(jsonParser, function(req, res) {
        var email = req.params.email;
        console.log("Updating subscriber %s to %s", email, JSON.stringify(req.body));<commit_msg>Removing option to REST POST new subscribers. Creating new subscribers must be done using /newsecret.<commit_after>var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var repository = require('./js/repository.js');
var secretGenerator = require('./js/secret-generator.js');
var mailSender = require('./js/mail-sender.js');

var port = process.env.PORT || 5000;
var router = express.Router();
var jsonParser = bodyParser.json();

router.get('/', function (req, res) {
  res.json({message: 'Velkommen til Fotballvarsel for Skeid.no' });
});

router.route('/teams')
    .get(function(req, res) {
      repository.findAllTeams(function(error, teams) {
        res.json(teams);
      });
    });

router.route('/newsecret')
    .post(jsonParser, function(req, res) {
        var email = req.body.email;
        var secret = secretGenerator.newSecret();

        if (!email) {
            return res.json({
                status: "error"
            });
        }

        repository.findSubscriberByEmail(email, function(error, subscriber) {
            if (subscriber) {
                console.log("Issuing new secret for %s", email);
                subscriber.secret = secret;
                updateSubscriberAndSendEmail(subscriber, res);
            } else {
                console.log("Registering new subscriber %s", email);
                var newSubscriber = {
                    email: email,
                    secret: secret,
                    teams: []
                };
                updateSubscriberAndSendEmail(newSubscriber, res);
            }
        });
    });

var updateSubscriberAndSendEmail = function(subscriber, res) {
    repository.insertOrUpdateSubscriber(subscriber.email, subscriber, function(error, result) {
        mailSender.newSecret(subscriber, function(error, result) {
            if (error) {
                console.log("Error sending email: %s", JSON.stringify(error));
                res.json({
                    email: subscriber.email,
                    status: "error"
                });
            } else {
                res.json({
                    email: subscriber.email,
                    status: "ok"
                });
            }
        });
    })
};

router.route('/subscribers/:email')
    .get(function(req, res) {
      console.log("Getting subscriber " + req.params.email);
      repository.findSubscriberByEmail(req.params.email, function(err, subscriber) {
        if (err) {
          console.log("Error in repo: " + err);
          res.send(err);
        }
        res.json(subscriber || {});
      });
    })
    .put(jsonParser, function(req, res) {
        var email = req.params.email;
        console.log("Updating subscriber %s to %s", email, JSON.stringify(req.body));
{'fraction_non_alphanumeric': 0.13445799231668615, 'fraction_numerical': 0.0013362284950726574, 'mean_word_length': 1.9352941176470588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3647776', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1596, 'n_words': 339}