module.exports = function () {

  let mongo = require('../modules/database');
  let email = require('../modules/email');
  let user = require('../models/user');
  let collections = require('../config/collections');

  const collection = collections.WEBSITES;

  /**
   * Find domain by name and token.
   * Token could be client or server, so we should pass @param type
   *
   * @param token
   * @param name
   */
  let get = function(token, name) {

    return mongo.findOne(collection, {
      token: token,
      name: name
    });

  };

  let getByUser = function (user) {

    return mongo.find(collection, {user: user._id.toString()});

  };

  /**
   * Return true if application with name specified is not exists
   */
  let checkName = function(domain) {

    return mongo.findOne(collection, {
      'name': domain
    })
      .then(function (result) {
        return !result;
      });
  };

  /**
   * Add new domain name and client and server tokens to DB
   */
  let add = function (app_name, token, user) {

    return mongo.updateOne('users', {_id: mongo.ObjectId(user._id)}, {$push: {domains: app_name}}).then(function () {

      return mongo.insertOne(collection, {
          'name': app_name,
          'token': token,
          'user': user._id.toString()
        }
      )
        .then(function (result) {
          if (result) {
            email.init();
            email.send(
              {name: 'CodeX Hawk', email: 'codex.ifmo@yandex.ru'},
              user.email,
              'Your token',
              'Your access token: ' + token,
              '');
            return true;
          }
          else {
            return false;
          }
        });
    })

  };


  return {
    get: get,
    checkName: checkName,
    add: add,
    getByUser: getByUser
  }

}();

@@ -852,32 +852,33 @@
 tion (result) %7B%0A
+%0A
         return !
@@ -885,16 +885,17 @@
 result;%0A
+%0A
       %7D)
@@ -896,16 +896,17 @@
     %7D);%0A
+%0A
   %7D;%0A%0A  
@@ -1189,26 +1189,24 @@
 , %7B%0A        
-  
 'name': app_
@@ -1211,18 +1211,16 @@
 p_name,%0A
-  
         
@@ -1243,18 +1243,16 @@
         
-  
 'user': 
@@ -1271,32 +1271,23 @@
 tring()%0A
-  
       %7D
-%0A      
 )%0A      
@@ -1306,32 +1306,33 @@
 tion (result) %7B%0A
+%0A
           if (re
@@ -1339,16 +1339,17 @@
 sult) %7B%0A
+%0A
         
@@ -1394,75 +1394,8 @@
 nd(%0A
-              %7Bname: 'CodeX Hawk', email: 'codex.ifmo@yandex.ru'%7D,%0A
     
@@ -1430,21 +1430,28 @@
         
-'Your
+app_name + '
  token',
@@ -1470,12 +1470,18 @@
    '
-Your
+Here is an
  acc
@@ -1489,18 +1489,47 @@
 ss token
-:
  
+for domain ' + app_name + ':%5Cn
 ' + toke
@@ -1575,16 +1575,17 @@
 n true;%0A
+%0A
         
@@ -1591,26 +1591,16 @@
    %7D
-%0A         
  else %7B%0A
     
@@ -1595,16 +1595,17 @@
  else %7B%0A
+%0A
         
@@ -1622,16 +1622,17 @@
  false;%0A
+%0A
         
@@ -1635,16 +1635,17 @@
       %7D%0A
+%0A
         
@@ -1648,22 +1648,24 @@
     %7D);%0A
+%0A
     %7D)
+;
 %0A%0A  %7D;%0A%0A
@@ -1758,15 +1758,16 @@
 User%0A  %7D
+;
 %0A%0A%7D();%0A

Fix email send function