<commit_before>/**
 * Created by aleckim on 2018. 2. 2..
 */

var async = require('async');
var express = require('express');
var router = express.Router();
var ControllerPush = require('../../controllers/controllerPush');
var AlertPushController = require('../../controllers/alert.push.controller');

function updatePushInfoList(language, pushList, callback) {
    if (!Array.isArray(pushList)) {
        var err = new Error('Invalid push info list');
        err.statusCode = 403;
        return callback(err);
    }

    async.map(pushList,
        function (pushInfo, callback) {
            try {
                pushInfo.lang = language;
                if (pushInfo.location) {
                    pushInfo.geo = [pushInfo.location.long, pushInfo.location.lat];
                }
                if (pushInfo.source == undefined) {
                    log.error('pushInfo source is undefined');
                    pushInfo.source = "KMA"
                }
                if (pushInfo.category == undefined) {
                    log.error('pushInfo category is undefined');
                    pushInfo.category == 'alarm';
                }

                pushInfo.units = UnitConverter.initUnits(pushInfo.units);

                log.info('pushInfo : '+ JSON.stringify(pushInfo));
            }
            catch (err) {
                return callback (err);
            }
            if (pushInfo.category === 'alarm') {
                var co = new ControllerPush();
                co.updatePushInfo(pushInfo, function (err, result) {
                    callback(err, result);
                });
            }
            else if (pushInfo.category === 'alert') {
                var ca = new AlertPushController();
                ca.updateAlertPush(pushInfo, function (err, result) {
                   callback(err, result) ;
                })
            }
        },
        function (err, results) {
            if (err) {
                err.statusCode = 500;
                return callback(err);
            }
            callback(null, results);
        });
}

router.post('/', function(req, res) {
    log.info('post : '+ JSON.stringify(req.body));
    log.info('accept-language:'+req.headers['accept-language']);

    var language = req.headers['accept-language'] || req.headers['Accept-Language'];

    if (language == undefined) {
        log.warn("accept-language is undefined");
        language = 'en';
    }
    else {
        language = language.split(',')[0];
        if (language) {
            if ( language.indexOf('ko') >= 0 ||
                language.indexOf('en') >= 0 ||
                language.indexOf('ja') >= 0 ||
                language.indexOf('de') >= 0 )
            {
                language = language.substr(0, 2);
            }
        }
        else {
            log.error("Fail to parse language="+req.headers['accept-language']);
            language = 'en';
        }
    }

    var pushList = req.body;
    try {
        pushList.forEach(function (obj) {
           if (!obj.hasOwnProperty('registrationId') ||
               !obj.hasOwnProperty('type') )
           {
               throw new Error('invalid push info registrationId/type');
           }

           if (obj.registrationId.length === 0 ||
               obj.type.length === 0)
           {
               throw new Error('invalid push info registrationId/type');
           }

           if (!obj.hasOwnProperty('location') && !obj.hasOwnProperty('town')) {
               throw new Error('invalid push info location or town is empty');
           }
        });
    }
    catch (err) {
        log.error(err);
        return res.status(403).send(err.message);
    }

    updatePushInfoList(language, pushList, function (err, result) {
        if (err) {
            if (err.statusCode) {
                return res.status(err.statusCode).send(err.message);
            }
            else {
                return res.status(501).send(err.message);
            }
        }
        return res.send(result);
    });
});

module.exports = router;
<commit_msg>TW-174 TW-121 fix error<commit_after>/**
 * Created by aleckim on 2018. 2. 2..
 */

var async = require('async');
var express = require('express');
var router = express.Router();
var ControllerPush = require('../../controllers/controllerPush');
var AlertPushController = require('../../controllers/alert.push.controller');

function updatePushInfoList(language, pushList, callback) {
    if (!Array.isArray(pushList)) {
        var err = new Error('Invalid push info list');
        err.statusCode = 403;
        return callback(err);
    }

    async.map(pushList,
        function (pushInfo, callback) {
            try {
                pushInfo.lang = language;
                if (pushInfo.location) {
                    pushInfo.geo = [pushInfo.location.long, pushInfo.location.lat];
                }
                if (pushInfo.source == undefined) {
                    log.error('pushInfo source is undefined');
                    pushInfo.source = "KMA"
                }
                if (pushInfo.category == undefined) {
                    log.error('pushInfo category is undefined');
                    pushInfo.category = 'alarm';
                }

                pushInfo.units = UnitConverter.initUnits(pushInfo.units);

                log.info('pushInfo : '+ JSON.stringify(pushInfo));
            }
            catch (err) {
                return callback (err);
            }
            if (pushInfo.category === 'alarm') {
                var co = new ControllerPush();
                co.updatePushInfo(pushInfo, function (err, result) {
                    callback(err, result);
                });
            }
            else if (pushInfo.category === 'alert') {
                var ca = new AlertPushController();
                ca.updateAlertPush(pushInfo, function (err, result) {
                   callback(err, result) ;
                })
            }
        },
        function (err, results) {
            if (err) {
                err.statusCode = 500;
                return callback(err);
            }
            callback(null, results);
        });
}

router.post('/', function(req, res) {
    log.info('post : '+ JSON.stringify(req.body));
    log.info('accept-language:'+req.headers['accept-language']);

    var language = req.headers['accept-language'] || req.headers['Accept-Language'];

    if (language == undefined) {
        log.warn("accept-language is undefined");
        language = 'en';
    }
    else {
        language = language.split(',')[0];
        if (language) {
            if ( language.indexOf('ko') >= 0 ||
                language.indexOf('en') >= 0 ||
                language.indexOf('ja') >= 0 ||
                language.indexOf('de') >= 0 )
            {
                language = language.substr(0, 2);
            }
        }
        else {
            log.error("Fail to parse language="+req.headers['accept-language']);
            language = 'en';
        }
    }

    var pushList = req.body;
    try {
        pushList.forEach(function (obj) {
           if (!obj.hasOwnProperty('registrationId') ||
               !obj.hasOwnProperty('type') )
           {
               throw new Error('invalid push info registrationId/type');
           }

           if (obj.registrationId.length === 0 ||
               obj.type.length === 0)
           {
               throw new Error('invalid push info registrationId/type');
           }

           if (!obj.hasOwnProperty('location') && !obj.hasOwnProperty('town')) {
               throw new Error('invalid push info location or town is empty');
           }
        });
    }
    catch (err) {
        log.error(err);
        return res.status(403).send(err.message);
    }

    updatePushInfoList(language, pushList, function (err, result) {
        if (err) {
            if (err.statusCode) {
                return res.status(err.statusCode).send(err.message);
            }
            else {
                return res.status(501).send(err.message);
            }
        }
        return res.send(result);
    });
});

module.exports = router;

{'fraction_non_alphanumeric': 0.13347173037000853, 'fraction_numerical': 0.007326901941629015, 'mean_word_length': 1.5434782608695652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4661862', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2079, 'n_words': 445}