<commit_before>'use strict';
var fs = require('fs');
var async = require('async');
var request = require("request");
var utils = require('../../utils/utils.js');

var colName = 'cicd';
var envColName = 'environment';
var servicesColName = 'services';
var daemonsColName = 'daemons';

var BL = {

	model: null,

	/**
	 * Function that gets CD config
	 * @param  {Object}             config
	 * @param  {Request Object}     req
	 * @param  {Function}           cb
	 *
	 */
	getConfig: function (config, req, helpers, cb) {
		var opts = { collection: colName, conditions: { "type": "cd" } };
		BL.model.findEntry(req.soajs, opts, function (error, record) {
			utils.checkErrorReturn(req.soajs, cb, { config: config, error: error, code: 600 }, function () {
				async.mapValues(record, function (oneRecordEntry, key, callback) {
					if (['_id', 'type'].indexOf(key) !== -1 || typeof(oneRecordEntry) !== 'object') {
						return callback(null, oneRecordEntry);
					}

					return utils.normalizeKeyValues(oneRecordEntry, config.tokens.dotToken, config.tokens.dotValue, callback);
				}, function (error, updatedRecords) {
					if(updatedRecords[process.env.SOAJS_ENV.toUpperCase()]){
						delete updatedRecords[process.env.SOAJS_ENV.toUpperCase()];
					}
					return cb(null, updatedRecords || {});
				});
			});
		});
	},

	/**
	 * Function that saves CD config
	 * @param  {Object}             config
	 * @param  {Request Object}     req
	 * @param  {Function}           cb
	 *
	 */
	saveConfig: function (config, req, helpers, soajsUtils, cb) {
		var opts = { collection: colName, conditions: { "type": "cd" } };
		BL.model.findEntry(req.soajs, opts, function (error, record) {
			utils.checkErrorReturn(req.soajs, cb, { config: config, error: error, code: 600 }, function () {

				//normalize service name, replace "." with __dot__
				if (req.soajs.inputmaskData.config.serviceName) {
					var regex = new RegExp(config.tokens.dotRegexString, 'g');
					if (req.soajs.inputmaskData.config.serviceName.match(regex)) {
						req.soajs.inputmaskData.config.serviceName = req.soajs.inputmaskData.config.serviceName.replace(regex, config.tokens.dotToken);
					}
				}

				if (!record) {
					record = { type: 'cd' };
				}

				if (!record[req.soajs.inputmaskData.config.env.toUpperCase()]) {
					record[req.soajs.inputmaskData.config.env.toUpperCase()] = {};
				}
				
				if (!record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName]) {
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName] = {};
				}

				if(req.soajs.inputmaskData.config.default){
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName] = req.soajs.inputmaskData.config.default;
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName].type = 'custom';
				}
				else if (req.soajs.inputmaskData.config.version) {
					var version = req.soajs.inputmaskData.config.version.v;
					delete req.soajs.inputmaskData.config.version.v;
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName][version] = req.soajs.inputmaskData.config.version;
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName].type = 'service';
				}
				
				if(req.soajs.inputmaskData.config.version && req.soajs.inputmaskData.config.version.options && req.soajs.inputmaskData.config.version.options.custom && req.soajs.inputmaskData.config.version.options.custom.type === 'daemon'){
					var groupName = req.soajs.inputmaskData.config.version.options.custom.daemonGroup;
					
					var temp = soajsUtils.cloneObj(record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName][version]);
					
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName][version] = {};
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName][version][groupName] = temp;
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName].type = 'daemon';
				}

				if(record[process.env.SOAJS_ENV.toUpperCase()]){
					delete record[process.env.SOAJS_ENV.toUpperCase()];
				}
				<commit_msg>DASHBOARD-263: completed API BL<commit_after>'use strict';
var fs = require('fs');
var async = require('async');
var request = require("request");
var utils = require('../../utils/utils.js');

var colName = 'cicd';
var envColName = 'environment';
var servicesColName = 'services';
var daemonsColName = 'daemons';

var BL = {

	model: null,

	/**
	 * Function that gets CD config
	 * @param  {Object}             config
	 * @param  {Request Object}     req
	 * @param  {Function}           cb
	 *
	 */
	getConfig: function (config, req, helpers, cb) {
		var opts = { collection: colName, conditions: { "type": "cd" } };
		BL.model.findEntry(req.soajs, opts, function (error, record) {
			utils.checkErrorReturn(req.soajs, cb, { config: config, error: error, code: 600 }, function () {
				async.mapValues(record, function (oneRecordEntry, key, callback) {
					if (['_id', 'type'].indexOf(key) !== -1 || typeof(oneRecordEntry) !== 'object') {
						return callback(null, oneRecordEntry);
					}

					return utils.normalizeKeyValues(oneRecordEntry, config.tokens.dotToken, config.tokens.dotValue, callback);
				}, function (error, updatedRecords) {
					if(updatedRecords[process.env.SOAJS_ENV.toUpperCase()]){
						delete updatedRecords[process.env.SOAJS_ENV.toUpperCase()];
					}
					return cb(null, updatedRecords || {});
				});
			});
		});
	},

	/**
	 * Function that saves CD config
	 * @param  {Object}             config
	 * @param  {Request Object}     req
	 * @param  {Function}           cb
	 *
	 */
	saveConfig: function (config, req, helpers, soajsUtils, cb) {
		var opts = { collection: colName, conditions: { "type": "cd" } };
		BL.model.findEntry(req.soajs, opts, function (error, record) {
			utils.checkErrorReturn(req.soajs, cb, { config: config, error: error, code: 600 }, function () {

				//normalize service name, replace "." with __dot__
				if (req.soajs.inputmaskData.config.serviceName) {
					var regex = new RegExp(config.tokens.dotRegexString, 'g');
					if (req.soajs.inputmaskData.config.serviceName.match(regex)) {
						req.soajs.inputmaskData.config.serviceName = req.soajs.inputmaskData.config.serviceName.replace(regex, config.tokens.dotToken);
					}
				}

				if (!record) {
					record = { type: 'cd' };
				}

				if (!record[req.soajs.inputmaskData.config.env.toUpperCase()]) {
					record[req.soajs.inputmaskData.config.env.toUpperCase()] = {};
				}
				
				if (!record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName]) {
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName] = {};
				}

				if(req.soajs.inputmaskData.config.default){
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName] = req.soajs.inputmaskData.config.default;
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName].type = 'custom';
				}
				else if(req.soajs.inputmaskData.config.version && req.soajs.inputmaskData.config.version.options && req.soajs.inputmaskData.config.version.options.custom && req.soajs.inputmaskData.config.version.options.custom.type === 'daemon'){
					var version = req.soajs.inputmaskData.config.version.v;
					delete req.soajs.inputmaskData.config.version.v;
					
					var groupName = req.soajs.inputmaskData.config.version.options.custom.daemonGroup;
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName].type = 'daemon';
					
					if(!record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName][version]){
						record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName][version] = {};
					}
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName][version][groupName] = req.soajs.inputmaskData.config.version;
				}
				else if (req.soajs.inputmaskData.config.version) {
					var version = req.soajs.inputmaskData.config.version.v;
					delete req.soajs.inputmaskData.config.version.v;
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName][version] = req.soajs.inputmaskData.config.version;
					record[req.soajs.inputmaskData.config.env.toUpperCase()][req.soajs.inputmaskData.config.serviceName].type = 'service';
				}

				if(record[process.env.SOAJS_ENV.toUpperCase()]){
					delete record[process.env.SOAJS_ENV.toUpperCase()];
				}
				
{'fraction_non_alphanumeric': 0.15337217118530136, 'fraction_numerical': 0.0019045485099708716, 'mean_word_length': 3.8834792122538295, 'pattern_counts': {'":': 4, '<': 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': '2172770', 'n_tokens_mistral': 3466, 'n_tokens_neox': 2995, 'n_words': 423}