Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ORM     = require('sequelize');
  2. var orm     = require('./orm');
  3. var async   = require('async');
  4. var jwt = require('jsonwebtoken');
  5. // var bcrypt  = require('bcrypt');
  6. var crypto  = require('crypto');
  7. var entities = {};
  8.  
  9. var salt = crypto.randomBytes(16).toString('hex');
  10.  
  11. // User
  12. entities.User = orm.define('User',
  13.   {
  14.     email: {
  15.         type: ORM.STRING,
  16.         allowNull: false
  17.     },
  18.     password: {
  19.       type: ORM.VIRTUAL,
  20.       set: function (val) {
  21.         this.setDataValue('password', val); // Remember to set the data value, otherwise it won't be validated
  22.         this.setDataValue('passwd_hash', crypto.pbkdf2Sync(val, salt, 1000, 64).toString('hex'));
  23.       },
  24.     },
  25.     passwd_hash: {
  26.         type: ORM.STRING,
  27.         allowNull: true
  28.     },
  29.     first_name: {
  30.         type: ORM.STRING,
  31.         allowNull: true
  32.     },
  33.     last_name: {
  34.         type: ORM.STRING,
  35.         allowNull: true
  36.     },
  37.     role: {
  38.         type: ORM.ENUM,
  39.         values: [
  40.             'blocked',
  41.             'client',
  42.             'developer',
  43.             'manager',
  44.             'admin'
  45.         ],
  46.         defaultValue: 'developer',
  47.         allowNull: false
  48.     },
  49.     auth_token: {
  50.         type: ORM.STRING,
  51.         allowNull: true
  52.     },
  53.     auth_token_web: {
  54.         type: ORM.STRING,
  55.         allowNull: true
  56.     },
  57.     office_loc_token: {
  58.         type: ORM.STRING,
  59.         allowNull: true
  60.     }
  61.   },
  62.   {
  63.     instanceMethods: {
  64.       generateJwt: function() {
  65.         var expiry = new Date();
  66.         expiry.setDate(expiry.getDate() + 7);
  67.         return jwt.sign({
  68.           _id : this.id,
  69.           email: this.email,
  70.           role: this.role,
  71.           exp: parseInt(expiry.getTime() / 1000),
  72.         }, "MY_SECRET");
  73.       },
  74.       validatePassword: function(password) {
  75.         var hash = crypto.pbkdf2Sync(password, salt, 1000, 64).toString('hex');
  76.         return this.passwd_hash === hash;
  77.       }
  78.     },
  79.     tableName: 'user',
  80.     name: {
  81.         singular: 'User',
  82.         plural: 'Users'
  83.     },
  84.     indexes: [
  85.         {
  86.             unique: true,
  87.             fields: [
  88.                 'email'
  89.             ]
  90.         },
  91.         {
  92.             unique: true,
  93.             fields: [
  94.                 'auth_token'
  95.             ]
  96.         },
  97.         {
  98.             unique: true,
  99.             fields: [
  100.                 'auth_token_web'
  101.             ]
  102.         },
  103.         {
  104.             unique: true,
  105.             fields: [
  106.                 'office_loc_token'
  107.             ]
  108.         }
  109.     ]
  110. });
  111.  
  112. // Project
  113. entities.Project = orm.define('Project', {
  114.     name: {
  115.         type: ORM.STRING,
  116.         allowNull: false
  117.     },
  118.     description: {
  119.         type: ORM.STRING,
  120.         allowNull: true
  121.     },
  122.     type: {
  123.         type: ORM.ENUM,
  124.         values: [
  125.             'internal',
  126.             'external'
  127.         ],
  128.         defaultValue: 'internal',
  129.         allowNull: false
  130.     },
  131.     status: {
  132.         type: ORM.ENUM,
  133.         values: [
  134.             'open',
  135.             'close'
  136.         ],
  137.         defaultValue: 'open',
  138.         allowNull: false
  139.     }
  140. }, {
  141.     tableName: 'project',
  142.     name: {
  143.         singular: 'Project',
  144.         plural: 'Projects'
  145.     }
  146. });
  147.  
  148. // Session
  149. entities.Session = orm.define('Session', {
  150.     description: {
  151.         type: ORM.STRING,
  152.         allowNull: true
  153.     },
  154.     start_time: {
  155.         type: ORM.INTEGER,
  156.         allowNull: false
  157.     },
  158.     stop_time: {
  159.         type: ORM.INTEGER,
  160.         allowNull: false
  161.     },
  162.     status: {
  163.         type: ORM.ENUM,
  164.         values: [
  165.             'started',
  166.             'stopped'
  167.         ],
  168.         defaultValue: 'started',
  169.         allowNull: false
  170.     }
  171. }, {
  172.     tableName: 'tracking_session',
  173.     name: {
  174.         singular: 'Session',
  175.         plural: 'Sessions'
  176.     }
  177. });
  178.  
  179. // Stat
  180. entities.Stat = orm.define('Stat', {
  181.     creation_time: {
  182.         type: ORM.INTEGER,
  183.         allowNull: false
  184.     },
  185.     creation_day: {
  186.         type: ORM.TEXT,
  187.         allowNull: false
  188.     },
  189.     save_time: {
  190.         type: ORM.INTEGER,
  191.         allowNull: true
  192.     },
  193.     work_time: {
  194.         type: ORM.INTEGER,
  195.         allowNull: true
  196.     },
  197.     image: {
  198.         type: ORM.STRING,
  199.         allowNull: true
  200.     },
  201.     sources: {
  202.         type: ORM.TEXT,
  203.         allowNull: true
  204.     },
  205.     keyboard: {
  206.         type: ORM.INTEGER,
  207.         allowNull: true
  208.     },
  209.     mouse: {
  210.         type: ORM.INTEGER,
  211.         allowNull: true
  212.     },
  213.     token: {
  214.         type: ORM.STRING,
  215.         allowNull: true
  216.     },
  217.     mode: {
  218.         type: ORM.ENUM,
  219.         values: [
  220.             'online',
  221.             'offline'
  222.         ],
  223.         defaultValue: 'online',
  224.         allowNull: false
  225.     },
  226.     status: {
  227.         type: ORM.ENUM,
  228.         values: [
  229.             'pending',
  230.             'completed',
  231.             'rejected'
  232.         ],
  233.         defaultValue: 'pending',
  234.         allowNull: false
  235.     }
  236. }, {
  237.     tableName: 'stat',
  238.     name: {
  239.         singular: 'Stat',
  240.         plural: 'Stats'
  241.     },
  242.     indexes: [
  243.         {
  244.             unique: true,
  245.             fields: [
  246.                 'token'
  247.             ]
  248.         }
  249.     ]
  250. });
  251.  
  252. /* *************************************************** Relations **************************************************** */
  253. // User <===> Project
  254. entities.User.belongsToMany(entities.Project, {
  255.     through: 'user_many_to_many_project',
  256.     foreignKey: 'user_id'
  257. });
  258. entities.Project.belongsToMany(entities.User, {
  259.     through: 'user_many_to_many_project',
  260.     foreignKey: 'project_id'
  261. });
  262.  
  263. // Project <=== Session
  264. entities.Project.hasMany(entities.Session, {
  265.     foreignKey: 'project_id',
  266.     onDelete: 'CASCADE',
  267.     onUpdate: 'CASCADE'
  268. });
  269. entities.Session.belongsTo(entities.Project, {
  270.     foreignKey: 'project_id',
  271.     onDelete: 'CASCADE',
  272.     onUpdate: 'CASCADE'
  273. });
  274.  
  275. // User <=== Session
  276. entities.User.hasMany(entities.Session, {
  277.     foreignKey: 'user_id',
  278.     onDelete: 'CASCADE',
  279.     onUpdate: 'CASCADE'
  280. });
  281. entities.Session.belongsTo(entities.User, {
  282.     foreignKey: 'user_id',
  283.     onDelete: 'CASCADE',
  284.     onUpdate: 'CASCADE'
  285. });
  286.  
  287. // Session <=== Stat
  288. entities.Session.hasMany(entities.Stat, {
  289.     foreignKey: 'tracking_session_id',
  290.     onDelete: 'CASCADE',
  291.     onUpdate: 'CASCADE'
  292. });
  293. entities.Stat.belongsTo(entities.Session, {
  294.     foreignKey: 'tracking_session_id',
  295.     onDelete: 'CASCADE',
  296.     onUpdate: 'CASCADE'
  297. });
  298.  
  299. // Schema update
  300. orm.sync({
  301.      force: true
  302. }).then(function () {
  303.     async.waterfall([
  304.         // ...
  305.         function (cb) {
  306.             entities.User.create({
  307.                 email: 'ivan@test.test',
  308.                 password: 'test',
  309.                 first_name: 'First Name',
  310.                 last_name: 'Last Name',
  311.                 office_loc_token: 'ivan',
  312.                 role: 'developer'
  313.             }).then(function (user) {
  314.                 cb(null, user);
  315.             });
  316.         }
  317.     ], function (err, user) {
  318.         entities.Project.create({
  319.             name: 'Project Name 1',
  320.             description: 'Description 1',
  321.             status: 'open'
  322.         }).then(function (project) {
  323.             user.addProject(project);
  324.         });
  325.     });
  326.  
  327.     async.waterfall([
  328.         // ...
  329.         function (cb) {
  330.             entities.User.create({
  331.                 email: 'sanya@test.test',
  332.                 password: 'test',
  333.                 first_name: 'First Name',
  334.                 last_name: 'Last Name',
  335.                 office_loc_token: 'sanya',
  336.                 role: 'developer'
  337.             }).then(function (user) {
  338.                 cb(null, user);
  339.             });
  340.         }
  341.     ], function (err, user) {
  342.         entities.Project.create({
  343.             name: 'Project Name 2',
  344.             description: 'Description 1',
  345.             status: 'open'
  346.         }).then(function (project) {
  347.             user.addProject(project);
  348.         });
  349.     });
  350.  
  351.     async.waterfall([
  352.         // ...
  353.         function (cb) {
  354.             entities.User.create({
  355.                 email: 'tolya@test.test',
  356.                 password: 'test',
  357.                 first_name: 'First Name',
  358.                 last_name: 'Last Name',
  359.                 office_loc_token: 'tolya',
  360.                 role: 'developer'
  361.             }).then(function (user) {
  362.                 cb(null, user);
  363.             });
  364.         }
  365.     ], function (err, user) {
  366.         entities.Project.create({
  367.             name: 'Project Name 3',
  368.             description: 'Description 1',
  369.             status: 'open'
  370.         }).then(function (project) {
  371.             user.addProject(project);
  372.         });
  373.     });
  374.  
  375.     async.waterfall([
  376.         // ...
  377.         function (cb) {
  378.             entities.User.create({
  379.                 email: 'evgen@test.test',
  380.                 password: 'test',
  381.                 first_name: 'First Name',
  382.                 last_name: 'Last Name',
  383.                 office_loc_token: 'evgen',
  384.                 role: 'developer'
  385.             }).then(function (user) {
  386.                 cb(null, user);
  387.             });
  388.         }
  389.     ], function (err, user) {
  390.         entities.Project.create({
  391.             name: 'Project Name 4',
  392.             description: 'Description 1',
  393.             status: 'open'
  394.         }).then(function (project) {
  395.             user.addProject(project);
  396.         });
  397.     });
  398.  
  399.  
  400.     async.waterfall([
  401.         // 1)
  402.         function (cb) {
  403.             entities.User.create({
  404.                 email: 'oleg.krasno@faceit-team.com',
  405.                 password: 'D1K2MYgtS',
  406.                 first_name: 'Олег',
  407.                 last_name: 'Красно',
  408.                 office_loc_token: 'oleg.krasno',
  409.                 role: 'developer'
  410.             }).then(function (user) {
  411.                 cb(null, user);
  412.             });
  413.         }
  414.     ], function (err, user) {
  415.         entities.Project.create({
  416.             name: 'Default Project',
  417.             description: 'Красно Олег',
  418.             status: 'open'
  419.         }).then(function (project) {
  420.             user.addProject(project);
  421.         });
  422.     });
  423.  
  424.     async.waterfall([
  425.         // 2)
  426.         function (cb) {
  427.             entities.User.create({
  428.                 email: 'alexey.macko@faceit-team.com',
  429.                 password: 'Pnd25GdI4',
  430.                 first_name: 'Алексей',
  431.                 last_name: 'Мацько',
  432.                 office_loc_token: 'alexey.macko',
  433.                 role: 'developer'
  434.             }).then(function (user) {
  435.                 cb(null, user);
  436.             });
  437.         }
  438.     ], function (err, user) {
  439.         entities.Project.create({
  440.             name: 'Default Project',
  441.             description: 'Мацько Алексей',
  442.             status: 'open'
  443.         }).then(function (project) {
  444.             user.addProject(project);
  445.         });
  446.     });
  447.  
  448.     async.waterfall([
  449.         // 3)
  450.         function (cb) {
  451.             entities.User.create({
  452.                 email: 'maksim.shabelnik@faceit-team.com',
  453.                 password: 'WmrWfO3Ky',
  454.                 first_name: 'Максим',
  455.                 last_name: 'Шабельник',
  456.                 office_loc_token: 'maksim.shabelnik',
  457.                 role: 'developer'
  458.             }).then(function (user) {
  459.                 cb(null, user);
  460.             });
  461.         }
  462.     ], function (err, user) {
  463.         entities.Project.create({
  464.             name: 'Default Project',
  465.             description: 'Шабельник Максим',
  466.             status: 'open'
  467.         }).then(function (project) {
  468.             user.addProject(project);
  469.         });
  470.     });
  471.  
  472.     async.waterfall([
  473.         // 4)
  474.         function (cb) {
  475.             entities.User.create({
  476.                 email: 'vladimir.stepanov@faceit-team.com',
  477.                 password: 'Ssn0xidR5',
  478.                 first_name: 'Владимир',
  479.                 last_name: 'Степанов',
  480.                 office_loc_token: 'vladimir.stepanov',
  481.                 role: 'developer'
  482.             }).then(function (user) {
  483.                 cb(null, user);
  484.             });
  485.         }
  486.     ], function (err, user) {
  487.         entities.Project.create({
  488.             name: 'Default Project',
  489.             description: 'Степанов Владимир',
  490.             status: 'open'
  491.         }).then(function (project) {
  492.             user.addProject(project);
  493.         });
  494.     });
  495.  
  496.     async.waterfall([
  497.         // 5)
  498.         function (cb) {
  499.             entities.User.create({
  500.                 email: 'gleb.zaycev@faceit-team.com',
  501.                 password: 'KFYZAN3zJ',
  502.                 first_name: 'Глеб',
  503.                 last_name: 'Зайцев',
  504.                 office_loc_token: 'gleb.zaycev',
  505.                 role: 'developer'
  506.             }).then(function (user) {
  507.                 cb(null, user);
  508.             });
  509.         }
  510.     ], function (err, user) {
  511.         entities.Project.create({
  512.             name: 'Default Project',
  513.             description: 'Зайцев Глеб',
  514.             status: 'open'
  515.         }).then(function (project) {
  516.             user.addProject(project);
  517.         });
  518.     });
  519.  
  520.     async.waterfall([
  521.         // 6)
  522.         function (cb) {
  523.             entities.User.create({
  524.                 email: 'ivan.abakumov@faceit-team.com',
  525.                 password: 'zPFSoYu55',
  526.                 first_name: 'Иван',
  527.                 last_name: 'Абакумов',
  528.                 office_loc_token: 'ivan.abakumov',
  529.                 role: 'developer'
  530.             }).then(function (user) {
  531.                 cb(null, user);
  532.             });
  533.         }
  534.     ], function (err, user) {
  535.         entities.Project.create({
  536.             name: 'Default Project',
  537.             description: 'Абакумов Иван',
  538.             status: 'open'
  539.         }).then(function (project) {
  540.             user.addProject(project);
  541.         });
  542.     });
  543.  
  544.     async.waterfall([
  545.         // 7)
  546.         function (cb) {
  547.             entities.User.create({
  548.                 email: 'alexandr.skripko@faceit-team.com',
  549.                 password: 'fzXBE2Qrq',
  550.                 first_name: 'Александр',
  551.                 last_name: 'Скрыпко',
  552.                 office_loc_token: 'alexandr.skripko',
  553.                 role: 'developer'
  554.             }).then(function (user) {
  555.                 cb(null, user);
  556.             });
  557.         }
  558.     ], function (err, user) {
  559.         entities.Project.create({
  560.             name: 'Default Project',
  561.             description: 'Скрыпко Александр',
  562.             status: 'open'
  563.         }).then(function (project) {
  564.             user.addProject(project);
  565.         });
  566.     });
  567. });
  568.  
  569. module.exports = entities;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement