Built motion from commit ae98c6c.|0.0.149
[motion.git] / server / models / user.js
index 5a6b165..4341c9c 100644 (file)
@@ -3,6 +3,7 @@
 var crypto = require('crypto');
 var md5 = require('md5');
 var _ = require('lodash');
+var moment = require('moment');
 
 module.exports = function(sequelize, DataTypes) {
   var User = sequelize.define('User', {
@@ -10,21 +11,27 @@ module.exports = function(sequelize, DataTypes) {
       type: DataTypes.STRING,
       unique: true,
       validate: {
-        notEmpty: true
+        notEmpty: true,
+        is: /^[A-Za-z0-9\.\_]+$/i
       },
       set: function(name) {
         this.setDataValue('name', name);
         this.setDataValue('defaultuser', name);
       }
     },
+    fullname: {
+      type: DataTypes.STRING,
+      allowNull: false,
+      unique: true
+    },
     email: {
       type: DataTypes.STRING,
       unique: true,
+      isEmail: true,
       set: function(email) {
         if (email) {
           this.setDataValue('email', email.toLowerCase());
         }
-
       },
       defaultValue: null
     },
@@ -79,67 +86,226 @@ module.exports = function(sequelize, DataTypes) {
     country: {
       type: DataTypes.STRING
     },
-    facebookUserId: {
-      type: DataTypes.INTEGER
+    online: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
     },
-    twitterUserId: {
-      type: DataTypes.INTEGER
+    lastLoginAt: {
+      type: DataTypes.DATE
     },
-    twitterKey: {
-      type: DataTypes.STRING
+    status: {
+      type: DataTypes.STRING,
+      defaultValue: 'UNKNOWN'
     },
-    twitterSecret: {
-      type: DataTypes.STRING
+    statusAt: {
+      type: DataTypes.DATE
     },
-    github: {
-      type: DataTypes.STRING
+    queueStatus: {
+      type: DataTypes.STRING,
+      defaultValue: 'complete'
+    },
+    queueStatusAt: {
+      type: DataTypes.DATE
     },
-    openId: {
+    lastQueue: {
       type: DataTypes.STRING
     },
-    online: {
+    voicePause: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false,
+      // set: function(voicePause) {
+      //   this.setDataValue('voicePause', voicePause);
+      //   if (voicePause) {
+      //     this.setDataValue('queueStatus', 'paused');
+      //     this.setDataValue('queueStatusAt', moment().format("YYYY-MM-DD HH:mm:ss"));
+      //   } else {
+      //     this.setDataValue('queueStatus', 'complete');
+      //     this.setDataValue('queueStatusAt', moment().format("YYYY-MM-DD HH:mm:ss"));
+      //   }
+      // }
+    },
+    chatPause: {
       type: DataTypes.BOOLEAN,
       defaultValue: false
     },
-    lastLoginAt: {
+    mailPause: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    faxPause: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    smsPause: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    openchannelPause: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    pauseType: {
+      type: DataTypes.STRING,
+      defaultValue: 'Default Pause'
+    },
+    lastPauseAt: {
       type: DataTypes.DATE
     },
-    ipaddr: {
+    chatCapacity: {
+      type: DataTypes.INTEGER,
+      defaultValue: 0
+    },
+    mailCapacity: {
+      type: DataTypes.INTEGER,
+      defaultValue: 0
+    },
+    faxCapacity: {
+      type: DataTypes.INTEGER,
+      defaultValue: 0
+    },
+    smsCapacity: {
+      type: DataTypes.INTEGER,
+      defaultValue: 0
+    },
+    openchannelCapacity: {
+      type: DataTypes.INTEGER,
+      defaultValue: 0
+    },
+    phoneBarAutoAnswer: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    phoneBarEnableSettings: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: true
+    },
+    phoneBarUnconditionalNumber: {
       type: DataTypes.STRING,
-      allowNull: true,
+      get: function() {
+        if (this.getDataValue('phoneBarUnconditional')) {
+          return this.getDataValue('phoneBarUnconditionalNumber');
+        }
+        return null;
+      }
+    },
+    phoneBarNoReplyNumber: {
+      type: DataTypes.STRING,
+      get: function() {
+        if (this.getDataValue('phoneBarNoReply')) {
+          return this.getDataValue('phoneBarNoReplyNumber');
+        }
+        return null;
+      }
+    },
+    phoneBarBusyNumber: {
+      type: DataTypes.STRING,
+      get: function() {
+        if (this.getDataValue('phoneBarBusy')) {
+          return this.getDataValue('phoneBarBusyNumber');
+        }
+        return null;
+      }
+    },
+    phoneBarUnconditional: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
     },
-    port: {
+    phoneBarNoReply: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    phoneBarBusy: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    phoneBarListenPort: {
       type: DataTypes.INTEGER(5),
-      allowNull: true,
+      defaultValue: 5060
     },
-    regseconds: {
-      type: DataTypes.INTEGER(11),
-      allowNull: true,
+    phoneBarECTail: {
+      type: DataTypes.INTEGER(5),
+      defaultValue: 200
     },
-    defaultuser: {
+    phoneBarExpires: {
+      type: DataTypes.INTEGER(5),
+      defaultValue: 120
+    },
+    phoneBarNameServer: {
       type: DataTypes.STRING,
-      allowNull: true,
+      allowNull: true
+    },
+    phoneBarStunServer: {
+      type: DataTypes.STRING,
+      allowNull: true
+    },
+    phoneBarVADEnabled: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    phoneBarNoUDP: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    phoneBarNoTCP: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: true
+    },
+    phoneBarLogLevel: {
+      type: DataTypes.INTEGER(5),
+      defaultValue: 1
+    },
+    phoneBarPublishEnabled: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    phoneBarRemoteControl: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    phoneBarRemoteControlPort: {
+      type: DataTypes.INTEGER,
+      defaultValue: 9888
+    },
+    chanspy: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
     },
-    fullcontact: {
+    description: {
       type: DataTypes.STRING,
       allowNull: true,
     },
-    regserver: {
+    host: {
       type: DataTypes.STRING,
       allowNull: true,
+      defaultValue: 'dynamic'
     },
-    useragent: {
+    ipaddr: { //REALTIME ASTERISK
       type: DataTypes.STRING,
       allowNull: true,
     },
-    lastms: {
+    port: { //REALTIME ASTERISK
+      type: DataTypes.INTEGER(5),
+      allowNull: true,
+    },
+    regseconds: { //REALTIME ASTERISK
       type: DataTypes.INTEGER(11),
       allowNull: true,
     },
-    host: {
+    fullcontact: { //REALTIME ASTERISK
       type: DataTypes.STRING,
       allowNull: true,
-      defaultValue: 'dynamic'
+    },
+    regserver: { //REALTIME ASTERISK
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
+    useragent: { //REALTIME ASTERISK
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
+    lastms: { //REALTIME ASTERISK
+      type: DataTypes.INTEGER(11),
+      allowNull: true,
     },
     type: {
       type: DataTypes.ENUM('friend', 'user', 'peer'),
@@ -151,6 +317,13 @@ module.exports = function(sequelize, DataTypes) {
       allowNull: true,
       defaultValue: 'from-sip'
     },
+    callingpres: {
+      type: DataTypes.ENUM('ALLOWED_NOT_SCREENED',
+        'ALLOWED_PASSED_SCREEN', 'ALLOWED_FAILED_SCREEN', 'ALLOWED',
+        'PROHIB_NOT_SCREENED', 'PROHIB_PASSED_SCREEN',
+        'PROHIB_FAILED_SCREEN', 'PROHIB'),
+      allowNull: true,
+    },
     permit: {
       type: DataTypes.STRING,
       allowNull: true,
@@ -165,7 +338,7 @@ module.exports = function(sequelize, DataTypes) {
     },
     md5secret: {
       type: DataTypes.STRING,
-      allowNull: true
+      allowNull: true,
     },
     remotesecret: {
       type: DataTypes.STRING,
@@ -183,10 +356,23 @@ module.exports = function(sequelize, DataTypes) {
       defaultValue: 'rfc2833'
     },
     directmedia: {
-      type: DataTypes.ENUM('yes', 'no', 'nonat', 'update'),
+      type: DataTypes.ENUM('yes', 'no', 'nonat', 'update', 'outgoing'),
       allowNull: true,
       defaultValue: 'no'
     },
+    directrtpsetup: {
+      type: DataTypes.ENUM('yes', 'no'),
+      allowNull: true,
+      defaultValue: 'no'
+    },
+    directmediapermit: {
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
+    directmediadeny: {
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
     nat: {
       type: DataTypes.STRING,
       allowNull: true,
@@ -196,15 +382,27 @@ module.exports = function(sequelize, DataTypes) {
       type: DataTypes.STRING,
       allowNull: true,
     },
+    namedcallgroup: { //We are in named call groups engineering,sales,netgroup,protgroup
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
     pickupgroup: {
       type: DataTypes.STRING,
       allowNull: true,
     },
+    namedpickupgroup: { //We can do call pick-p for named call group sales
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
     language: {
       type: DataTypes.STRING,
       allowNull: true,
       defaultValue: 'en'
     },
+    tonezone: {
+      type: DataTypes.STRING,
+      allowNull: true
+    },
     disallow: {
       type: DataTypes.STRING,
       allowNull: true,
@@ -215,6 +413,10 @@ module.exports = function(sequelize, DataTypes) {
       allowNull: true,
       defaultValue: 'alaw;ulaw;gsm'
     },
+    autoframing: {
+      type: DataTypes.ENUM('yes', 'no'),
+      allowNull: true,
+    },
     insecure: {
       type: DataTypes.STRING,
       allowNull: true,
@@ -225,6 +427,11 @@ module.exports = function(sequelize, DataTypes) {
       allowNull: true,
       defaultValue: 'no'
     },
+    trust_id_outbound: {
+      type: DataTypes.ENUM('yes', 'no'),
+      allowNull: true,
+      defaultValue: 'no'
+    },
     progressinband: {
       type: DataTypes.ENUM('yes', 'no', 'never'),
       allowNull: true,
@@ -271,10 +478,27 @@ module.exports = function(sequelize, DataTypes) {
       type: DataTypes.ENUM('yes', 'no'),
       allowNull: true,
     },
-    videosupport: {
+    allowtransfer: {
       type: DataTypes.ENUM('yes', 'no'),
       allowNull: true,
     },
+    ignoresdpversion: {
+      type: DataTypes.ENUM('yes', 'no'),
+      allowNull: true,
+    },
+    subscribecontext: {
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
+    template: {
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
+    videosupport: {
+      type: DataTypes.ENUM('yes', 'no', 'always'),
+      allowNull: true,
+      defaultValue: 'no'
+    },
     maxcallbitrate: {
       type: DataTypes.INTEGER(11),
       allowNull: true,
@@ -287,21 +511,22 @@ module.exports = function(sequelize, DataTypes) {
       type: DataTypes.STRING,
       allowNull: true,
     },
-    "session-timers": {
+    session_timers: {
       type: DataTypes.ENUM('accept', 'refuse', 'originate'),
       allowNull: true,
     },
-    "session-expires": {
+    session_expires: {
       type: DataTypes.INTEGER(11),
       allowNull: true,
     },
-    "session-minse": {
+    session_minse: {
       type: DataTypes.INTEGER(11),
       allowNull: true,
     },
-    "session-refresher": {
+    session_refresher: {
       type: DataTypes.ENUM('uac', 'uas'),
       allowNull: true,
+      defaultValue: 'uas'
     },
     t38pt_usertpsource: {
       type: DataTypes.STRING,
@@ -324,15 +549,27 @@ module.exports = function(sequelize, DataTypes) {
       allowNull: true,
       defaultValue: 'yes'
     },
+    keepalive: {
+      type: DataTypes.INTEGER(11),
+      allowNull: true,
+    },
     defaultip: {
       type: DataTypes.STRING,
       allowNull: true,
     },
-    rtptimeout: {
+    defaultuser: {
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
+    rtptimeout: { // Terminate call if 60 seconds of no RTP or RTCP activity on the audio channel  when we're not on hold.
+      type: DataTypes.INTEGER(11),
+      allowNull: true,
+    },
+    rtpholdtimeout: { // Terminate call if 300 seconds of no RTP or RTCP activity on the audio channel when we're on hold (must be > rtptimeout)
       type: DataTypes.INTEGER(11),
       allowNull: true,
     },
-    rtpholdtimeout: {
+    rtpkeepalive: { // Send keepalives in the RTP stream to keep NAT open (default is off - zero)
       type: DataTypes.INTEGER(11),
       allowNull: true,
     },
@@ -361,10 +598,6 @@ module.exports = function(sequelize, DataTypes) {
       type: DataTypes.INTEGER(11),
       allowNull: true,
     },
-    constantssrc: {
-      type: DataTypes.ENUM('yes', 'no'),
-      allowNull: true,
-    },
     contactpermit: {
       type: DataTypes.STRING,
       allowNull: true,
@@ -373,128 +606,142 @@ module.exports = function(sequelize, DataTypes) {
       type: DataTypes.STRING,
       allowNull: true,
     },
-    usereqphone: {
+    contactacl: {
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
+    unsolicited_mailbox: {
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
+    use_q850_reason: {
+      type: DataTypes.STRING,
+      allowNull: true,
+    },
+    maxforwards: {
+      type: DataTypes.INTEGER(11),
+      allowNull: true,
+    },
+    encryption: {
       type: DataTypes.ENUM('yes', 'no'),
       allowNull: true,
       defaultValue: 'no'
     },
-    textsupport: {
+    avpf: {
       type: DataTypes.ENUM('yes', 'no'),
-      allowNull: true,
+      allowNull: true
     },
-    faxdetect: {
+    force_avp: {
       type: DataTypes.ENUM('yes', 'no'),
-      allowNull: true,
+      allowNull: true
     },
-    buggymwi: {
+    icesupport: {
       type: DataTypes.ENUM('yes', 'no'),
-      allowNull: true,
+      allowNull: true
     },
-    auth: {
-      type: DataTypes.STRING,
-      allowNull: true,
+    dtlsenable: {
+      type: DataTypes.ENUM('yes', 'no'),
+      allowNull: true
     },
-    fullname: {
-      type: DataTypes.STRING,
+    dtlsverify: {
+      type: DataTypes.ENUM('yes', 'no', 'fingerprint', 'certificate'),
+      allowNull: true
+    },
+    dtlsrekey: {
+      type: DataTypes.INTEGER(11),
       allowNull: true,
     },
-    trunkname: {
+    dtlscertfile: {
       type: DataTypes.STRING,
       allowNull: true,
     },
-    cid_number: {
+    dtlsprivatekey: {
       type: DataTypes.STRING,
       allowNull: true,
     },
-    callingpres: {
-      type: DataTypes.ENUM('ALLOWED_NOT_SCREENED',
-        'ALLOWED_PASSED_SCREEN', 'ALLOWED_FAILED_SCREEN', 'ALLOWED',
-        'PROHIB_NOT_SCREENED', 'PROHIB_PASSED_SCREEN',
-        'PROHIB_FAILED_SCREEN', 'PROHIB'),
+    dtlscipher: {
+      type: DataTypes.STRING,
       allowNull: true,
     },
-    mohinterpret: {
+    dtlscafile: {
       type: DataTypes.STRING,
       allowNull: true,
     },
-    mohsuggest: {
+    dtlscapath: {
       type: DataTypes.STRING,
       allowNull: true,
     },
-    parkinglot: {
+    dtlssetup: {
+      type: DataTypes.ENUM('active', 'passive', 'actpass'),
+      allowNull: true
+    },
+    dtlsfingerprint: {
       type: DataTypes.STRING,
       allowNull: true,
     },
-    hasvoicemail: {
+    usereqphone: { //This provider requires ";user=phone" on URI
       type: DataTypes.ENUM('yes', 'no'),
       allowNull: true,
+      defaultValue: 'no'
     },
-    subscribemwi: {
-      type: DataTypes.ENUM('yes', 'no'),
+    recordonfeature: { //Feature to use when INFO with Record: on is received.
+      type: DataTypes.STRING,
       allowNull: true,
     },
-    vmexten: {
+    recordofffeature: { //Feature to use when INFO with Record: off is received.
       type: DataTypes.STRING,
       allowNull: true,
     },
-    description: {
-      type: DataTypes.STRING,
+    call_limit: {
+      type: DataTypes.INTEGER(11),
       allowNull: true,
+      defaultValue: 10
     },
-    autoframing: {
+    registertrying: { //Send a 100 Trying when the device registers.
       type: DataTypes.ENUM('yes', 'no'),
       allowNull: true,
     },
-    limitonpeers: {
+    subscribemwi: { //Only send notifications if this phone subscribes for mailbox notification
       type: DataTypes.ENUM('yes', 'no'),
       allowNull: true,
-      defaultValue: 'yes'
     },
-    rtpkeepalive: {
-      type: DataTypes.INTEGER(11),
+    vmexten: { // dialplan extension to reach mailbox. defaults to global vmexten which defaults to "asterisk"
+      type: DataTypes.STRING,
       allowNull: true,
     },
-    "call-limit": {
-      type: DataTypes.INTEGER(11),
+    mohinterpret: { // This option specifies a preference for which music on hold class this channel should listen to when put on hold
+      type: DataTypes.STRING,
       allowNull: true,
-      defaultValue: null
     },
-    g726nonstandard: {
-      type: DataTypes.ENUM('yes', 'no'),
+    mohsuggest: { //  This option specifies which music on hold class to suggest to the peer channel when this channel places the peer on hold.
+      type: DataTypes.STRING,
       allowNull: true,
     },
-    ignoresdpversion: {
-      type: DataTypes.ENUM('yes', 'no'),
+    parkinglot: {
+      type: DataTypes.STRING,
       allowNull: true,
     },
-    allowtransfer: {
-      type: DataTypes.ENUM('yes', 'no'),
+    canreinvite: {
+      type: DataTypes.ENUM('yes', 'no', 'nonat', 'update', 'update,nonat'),
       allowNull: true,
+      defaultValue: 'no'
     },
-    dynamic: {
-      type: DataTypes.ENUM('yes', 'no'),
-      allowNull: true,
+    loginInPause: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
     },
-    encryption: {
-      type: DataTypes.ENUM('yes', 'no'),
-      allowNull: true,
-      defaultValue: 'no'
+    resetPasswordToken: {
+      type: DataTypes.STRING
     },
-    registry: {
-      type: DataTypes.STRING,
-      allowNull: true,
+    resetPasswordExpires: {
+      type: DataTypes.DATE
+    },
+    phoneBarEnableRecording: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
     }
-
   }, {
     tableName: 'users',
-    getterMethods: {
-      // profile: function() {
-      //   return {
-      //     name: this.name,
-      //     role: this.role
-      //   }
-      // }
-    },
     instanceMethods: {
       /**
        * Authenticate - check if the passwords are the same
@@ -543,35 +790,327 @@ module.exports = function(sequelize, DataTypes) {
     associate: function(models) {
       // BELOGNS TO MANY
       User.hasMany(models.ChatMessage);
+      User.hasMany(models.MailMessage);
       User.hasMany(models.Contact);
+      User.hasMany(models.Action);
       User.belongsToMany(models.Module, {
-        through: 'user_has_modules'
+        through: models.UserHasModule,
+        required: false
       });
       User.belongsToMany(models.Channel, {
         through: 'user_has_channels'
       });
+      User.belongsToMany(models.MailRoom, {
+        through: 'user_has_mail_rooms'
+      });
+      User.belongsToMany(models.SmsRoom, {
+        through: 'user_has_sms_rooms'
+      });
+      User.belongsToMany(models.OpenchannelRoom, {
+        through: 'user_has_openchannel_rooms'
+      });
+      User.belongsToMany(models.FaxRoom, {
+        through: 'user_has_fax_rooms'
+      });
       User.belongsToMany(models.Team, {
         through: models.UserHasTeam
       });
-      User.belongsToMany(models.ChatRoom, {
-        through: models.UserHasChatRoom
+      User.belongsToMany(models.VoiceQueue, {
+        through: models.UserHasVoiceQueue,
+        required: false
+      });
+      User.belongsToMany(models.VoiceQueue, {
+        through: models.UserHasVoiceQueuePermit,
+        as: 'PVoiceQueues'
       });
       User.belongsToMany(models.MailQueue, {
-        through: models.UserHasMailQueue
+        through: models.UserHasMailQueue,
+        required: false
+      });
+      User.belongsToMany(models.SmsQueue, {
+        through: models.UserHasSmsQueue,
+        required: false
+      });
+      User.belongsToMany(models.SmsQueue, {
+        through: models.UserHasSmsQueuePermit,
+        as: 'PSmsQueues'
+      });
+      User.belongsToMany(models.OpenchannelQueue, {
+        through: models.UserHasOpenchannelQueue,
+        required: false
+      });
+      User.belongsToMany(models.OpenchannelQueue, {
+        through: models.UserHasOpenchannelQueuePermit,
+        as: 'POpenchannelQueues'
+      });
+      User.belongsToMany(models.MailQueue, {
+        through: models.UserHasMailQueuePermit,
+        as: 'PMailQueues'
+      });
+      User.belongsToMany(models.FaxQueue, {
+        through: models.UserHasFaxQueue,
+        required: false
+      });
+      User.belongsToMany(models.FaxQueue, {
+        through: models.UserHasFaxQueuePermit,
+        as: 'PFaxQueues'
       });
       User.belongsToMany(models.ChatQueue, {
-        through: models.UserHasChatQueue
+        through: models.UserHasChatQueue,
+        required: false
       });
-      User.belongsToMany(models.VoiceQueue, {
-        through: models.UserHasVoiceQueue
+      User.belongsToMany(models.ChatQueue, {
+        through: models.UserHasChatQueuePermit,
+        as: 'PChatQueues'
+      });
+      User.belongsToMany(models.ChatRoom, {
+        through: models.UserHasChatRoom
+      });
+      User.belongsToMany(models.List, {
+        through: models.UserHasList
       });
       User.hasMany(models.VoiceExtension, {
         foreignKey: 'UserId',
         as: 'UserExtensions',
-        onDelete: 'cascade'
+        onDelete: 'cascade',
+        hooks: true
+      });
+
+      // SCOPES MANAGEMENT
+      User.addScope('all', {
+        attributes: ['id',
+          'name',
+          'email',
+          'internal',
+          'fullname',
+          'role',
+          'online',
+          'userpic',
+          'accountcode',
+          'transport',
+          'host',
+          'role',
+          'nat',
+          'type',
+          'allow',
+          'lastLoginAt',
+          'ipaddr',
+          'fullcontact',
+          'port',
+          'lastms',
+          'description'
+        ]
+      });
+
+      // SCOPES MANAGEMENT
+      User.addScope('user', {
+        where: {
+          role: {
+            $in: ['admin', 'user']
+          },
+        },
+        attributes: ['id',
+          'name',
+          'email',
+          'internal',
+          'fullname',
+          'role',
+          'userpic',
+          'accountcode',
+          'transport',
+          'host',
+          'role',
+          'nat',
+          'type',
+          'allow',
+          'lastLoginAt',
+          'ipaddr',
+          'fullcontact',
+          'port',
+          'lastms',
+          'description',
+        ]
+      });
+      User.addScope('agent', {
+        where: {
+          role: 'agent'
+        },
+        attributes: ['id',
+          'name',
+          'email',
+          'internal',
+          'fullname',
+          'role',
+          'userpic',
+          'accountcode',
+          'transport',
+          'host',
+          'nat',
+          'type',
+          'allow',
+          'chatCapacity',
+          'mailCapacity',
+          'faxCapacity',
+          'smsCapacity',
+          'openchannelCapacity',
+          'online',
+          'lastLoginAt',
+          'phoneBarAutoAnswer',
+          'phoneBarEnableSettings',
+          'phoneBarUnconditional',
+          'phoneBarNoReply',
+          'phoneBarBusy',
+          'phoneBarUnconditionalNumber',
+          'phoneBarNoReplyNumber',
+          'phoneBarBusyNumber',
+          'phoneBarListenPort',
+          'phoneBarExpires',
+          'phoneBarRemoteControl',
+          'phoneBarRemoteControlPort',
+          'phoneBarEnableRecording',
+          'chanspy',
+          'voicePause',
+          'mailPause',
+          'faxPause',
+          'chatPause',
+          'smsPause',
+          'openchannelPause',
+          'pauseType',
+          'lastPauseAt',
+          'status',
+          'statusAt',
+          'queueStatus',
+          'queueStatusAt',
+          'lastQueue',
+          'useragent',
+          'ipaddr',
+          'fullcontact',
+          'port',
+          'lastms',
+          'description',
+          'loginInPause'
+        ]
+      });
+      User.addScope('telephone', {
+        where: {
+          role: 'telephone'
+        }
+      });
+      User.addScope('queues', {
+        include: [models.VoiceQueue, models.ChatQueue, models.MailQueue,
+          models.FaxQueue, models.SmsQueue, models.OpenchannelQueue
+        ]
       });
 
+      User.addScope('checkPauseStatus', function(query) {
+        var scope = {
+          where: {}
+        };
+        if (query.voicePause) {
+          scope.where.voicePause = (query.voicePause === 'true') ?
+            true : false;
+          delete query.voicePause;
+        } else if (query.faxPause) {
+          scope.where.faxPause = (query.faxPause === 'true') ? true :
+            false;
+          delete query.faxPause;
+        } else if (query.chatPause) {
+          scope.where.chatPause = (query.chatPause === 'true') ?
+            true : false;
+          delete query.chatPause;
+        } else if (query.mailPause) {
+          scope.where.mailPause = (query.mailPause === 'true') ?
+            true : false;
+          delete query.mailPause;
+        } else if (query.smsPause) {
+          scope.where.smsPause = (query.smsPause === 'true') ?
+            true : false;
+          delete query.smsPause;
+        } else if (query.openchannelPause) {
+          scope.where.openchannelPause = (query.openchannelPause === 'true') ?
+            true : false;
+          delete query.openchannelPause;
+        }
+        return scope;
+      });
+      User.addScope('checkOnlineStatus', function(query) {
+        var scope = {
+          where: {}
+        };
+        if (query.online) {
+          scope.where.online = (query.online === 'true') ? true :
+            false;
+          delete query.online;
+        }
+        return scope;
+      });
+      User.addScope('checkSipStatus', function(query) {
+        var scope = {
+          where: {}
+        };
+        if (query.status) {
+          scope.where.status = query.status;
+          delete query.status;
+        }
+        return scope;
+      });
+      User.addScope('checkQueueStatus', function(query) {
+        var scope = {
+          where: {}
+        };
+        if (query.queueStatus) {
+          scope.where.queueStatus = query.queueStatus;
+          delete query.queueStatus;
+        }
+        return scope;
+      });
 
+      User.addScope('me', function(userId) {
+        var scope = {
+          where: {
+            id: userId
+          },
+          attributes: ['id', 'fullname', 'name', 'email', 'role', 'userpic', 'lastLoginAt', 'voicePause',
+            'mailPause',
+            'chatPause',
+            'faxPause',
+            'smsPause',
+            'openchannelPause',
+            'lastPauseAt',
+            'pauseType',
+            'phoneBarRemoteControl',
+            'phoneBarRemoteControlPort',
+            'loginInPause'
+          ],
+          include: [{
+            model: models.Module,
+            include: [{
+              model: models.Module,
+              as: 'SubModules',
+              required: false,
+              include: [{
+                all: true
+              }]
+            }]
+          }, {
+            model: models.VoiceQueue,
+            as: 'PVoiceQueues'
+          }, {
+            model: models.ChatQueue,
+            as: 'PChatQueues'
+          }, {
+            model: models.MailQueue,
+            as: 'PMailQueues'
+          }, {
+            model: models.FaxQueue,
+            as: 'PFaxQueues'
+          }, {
+            model: models.SmsQueue,
+            as: 'PSmsQueues'
+          }]
+        };
+        return scope;
+      });
     }
   });