Built motion from commit e2ff5cd.|0.0.92
[motion.git] / server / models / user.js
1 'use strict';
2
3 var crypto = require('crypto');
4 var md5 = require('md5');
5 var _ = require('lodash');
6 var moment = require('moment');
7 var VoiceQueue = require('.').VoiceQueue;
8
9 module.exports = function(sequelize, DataTypes) {
10   var User = sequelize.define('User', {
11     name: {
12       type: DataTypes.STRING,
13       unique: true,
14       validate: {
15         notEmpty: true,
16         is: /^[A-Za-z0-9\.\_]+$/i
17       },
18       set: function(name) {
19         this.setDataValue('name', name);
20         this.setDataValue('defaultuser', name);
21       }
22     },
23     fullname: {
24       type: DataTypes.STRING,
25       allowNull: false,
26     },
27     email: {
28       type: DataTypes.STRING,
29       unique: true,
30       isEmail: true,
31       set: function(email) {
32         if (email) {
33           this.setDataValue('email', email.toLowerCase());
34         }
35       },
36       defaultValue: null
37     },
38     role: {
39       type: DataTypes.ENUM('admin', 'user', 'agent', 'telephone')
40     },
41     password: {
42       type: DataTypes.STRING,
43       allowNull: false,
44       validate: {
45         notEmpty: true
46       },
47       set: function(password) {
48         this.salt = this.makeSalt();
49         this.setDataValue('password', this.encryptPassword(password));
50         this.setDataValue('md5secret', this.md5Password(this.name +
51           ':asterisk:' + password));
52       }
53     },
54     provider: {
55       type: DataTypes.STRING,
56       defaultValue: 'local'
57     },
58     internal: {
59       type: DataTypes.INTEGER(11),
60       unique: true,
61       set: function(internal) {
62         this.setDataValue('internal', internal);
63         this.setDataValue('accountcode', internal);
64       }
65     },
66     salt: {
67       type: DataTypes.STRING
68     },
69     phone: {
70       type: DataTypes.STRING
71     },
72     mobile: {
73       type: DataTypes.STRING
74     },
75     address: {
76       type: DataTypes.STRING
77     },
78     zipcode: {
79       type: DataTypes.STRING
80     },
81     userpic: {
82       type: DataTypes.STRING
83     },
84     city: {
85       type: DataTypes.STRING
86     },
87     country: {
88       type: DataTypes.STRING
89     },
90     online: {
91       type: DataTypes.BOOLEAN,
92       defaultValue: false
93     },
94     lastLoginAt: {
95       type: DataTypes.DATE
96     },
97     status: {
98       type: DataTypes.STRING,
99       defaultValue: 'UNKNOWN'
100     },
101     statusAt: {
102       type: DataTypes.DATE
103     },
104     queueStatus: {
105       type: DataTypes.STRING,
106       defaultValue: 'complete'
107     },
108     queueStatusAt: {
109       type: DataTypes.DATE
110     },
111     lastQueue: {
112       type: DataTypes.STRING
113     },
114     voicePause: {
115       type: DataTypes.BOOLEAN,
116       defaultValue: false,
117       set: function(voicePause) {
118         this.setDataValue('voicePause', voicePause);
119         if (voicePause) {
120           this.setDataValue('queueStatus', 'paused');
121           this.setDataValue('queueStatusAt', moment().format(
122             "YYYY-MM-DD HH:mm:ss"));
123         } else {
124           this.setDataValue('queueStatus', 'complete');
125           this.setDataValue('queueStatusAt', moment().format(
126             "YYYY-MM-DD HH:mm:ss"));
127         }
128       }
129     },
130     chatPause: {
131       type: DataTypes.BOOLEAN,
132       defaultValue: false
133     },
134     mailPause: {
135       type: DataTypes.BOOLEAN,
136       defaultValue: false
137     },
138     faxPause: {
139       type: DataTypes.BOOLEAN,
140       defaultValue: false
141     },
142     smsPause: {
143       type: DataTypes.BOOLEAN,
144       defaultValue: false
145     },
146     pauseType: {
147       type: DataTypes.STRING,
148       defaultValue: 'Default Pause'
149     },
150     lastPauseAt: {
151       type: DataTypes.DATE
152     },
153     chatCapacity: {
154       type: DataTypes.INTEGER,
155       defaultValue: 0
156     },
157     mailCapacity: {
158       type: DataTypes.INTEGER,
159       defaultValue: 0
160     },
161     faxCapacity: {
162       type: DataTypes.INTEGER,
163       defaultValue: 0
164     },
165     smsCapacity: {
166       type: DataTypes.INTEGER,
167       defaultValue: 0
168     },
169     phoneBarAutoAnswer: {
170       type: DataTypes.BOOLEAN,
171       defaultValue: false
172     },
173     phoneBarEnableSettings: {
174       type: DataTypes.BOOLEAN,
175       defaultValue: true
176     },
177     phoneBarUnconditionalNumber: {
178       type: DataTypes.STRING,
179       get: function() {
180         if (this.getDataValue('phoneBarUnconditional')) {
181           return this.getDataValue('phoneBarUnconditionalNumber');
182         }
183         return null;
184       }
185     },
186     phoneBarNoReplyNumber: {
187       type: DataTypes.STRING,
188       get: function() {
189         if (this.getDataValue('phoneBarNoReply')) {
190           return this.getDataValue('phoneBarNoReplyNumber');
191         }
192         return null;
193       }
194     },
195     phoneBarBusyNumber: {
196       type: DataTypes.STRING,
197       get: function() {
198         if (this.getDataValue('phoneBarBusy')) {
199           return this.getDataValue('phoneBarBusyNumber');
200         }
201         return null;
202       }
203     },
204     phoneBarUnconditional: {
205       type: DataTypes.BOOLEAN,
206       defaultValue: false
207     },
208     phoneBarNoReply: {
209       type: DataTypes.BOOLEAN,
210       defaultValue: false
211     },
212     phoneBarBusy: {
213       type: DataTypes.BOOLEAN,
214       defaultValue: false
215     },
216     phoneBarListenPort: {
217       type: DataTypes.INTEGER(5),
218       defaultValue: 5060
219     },
220     phoneBarECTail: {
221       type: DataTypes.INTEGER(5),
222       defaultValue: 200
223     },
224     phoneBarExpires: {
225       type: DataTypes.INTEGER(5),
226       defaultValue: 3600
227     },
228     phoneBarNameServer: {
229       type: DataTypes.STRING,
230       allowNull: true
231     },
232     phoneBarStunServer: {
233       type: DataTypes.STRING,
234       allowNull: true
235     },
236     phoneBarVADEnabled: {
237       type: DataTypes.BOOLEAN,
238       defaultValue: true
239     },
240     phoneBarNoUDP: {
241       type: DataTypes.BOOLEAN,
242       defaultValue: false
243     },
244     phoneBarNoTCP: {
245       type: DataTypes.BOOLEAN,
246       defaultValue: true
247     },
248     phoneBarLogLevel: {
249       type: DataTypes.INTEGER(5),
250       defaultValue: 1
251     },
252     phoneBarPublishEnabled: {
253       type: DataTypes.BOOLEAN,
254       defaultValue: false
255     },
256     chanspy: {
257       type: DataTypes.BOOLEAN,
258       defaultValue: false
259     },
260     description: {
261       type: DataTypes.STRING,
262       allowNull: true,
263     },
264     ipaddr: { //REALTIME ASTERISK
265       type: DataTypes.STRING,
266       allowNull: true,
267     },
268     port: { //REALTIME ASTERISK
269       type: DataTypes.INTEGER(5),
270       allowNull: true,
271     },
272     regseconds: { //REALTIME ASTERISK
273       type: DataTypes.INTEGER(11),
274       allowNull: true,
275     },
276     fullcontact: { //REALTIME ASTERISK
277       type: DataTypes.STRING,
278       allowNull: true,
279     },
280     regserver: { //REALTIME ASTERISK
281       type: DataTypes.STRING,
282       allowNull: true,
283     },
284     useragent: { //REALTIME ASTERISK
285       type: DataTypes.STRING,
286       allowNull: true,
287     },
288     lastms: { //REALTIME ASTERISK
289       type: DataTypes.INTEGER(11),
290       allowNull: true,
291     },
292     type: {
293       type: DataTypes.ENUM('friend', 'user', 'peer'),
294       allowNull: true,
295       defaultValue: 'friend'
296     },
297     context: {
298       type: DataTypes.STRING,
299       allowNull: true,
300       defaultValue: 'from-sip'
301     },
302     callingpres: {
303       type: DataTypes.ENUM('ALLOWED_NOT_SCREENED',
304         'ALLOWED_PASSED_SCREEN', 'ALLOWED_FAILED_SCREEN', 'ALLOWED',
305         'PROHIB_NOT_SCREENED', 'PROHIB_PASSED_SCREEN',
306         'PROHIB_FAILED_SCREEN', 'PROHIB'),
307       allowNull: true,
308     },
309     permit: {
310       type: DataTypes.STRING,
311       allowNull: true,
312     },
313     deny: {
314       type: DataTypes.STRING,
315       allowNull: true,
316     },
317     secret: {
318       type: DataTypes.STRING,
319       allowNull: true,
320     },
321     md5secret: {
322       type: DataTypes.STRING,
323       allowNull: true,
324     },
325     remotesecret: {
326       type: DataTypes.STRING,
327       allowNull: true,
328     },
329     transport: {
330       type: DataTypes.STRING,
331       allowNull: true,
332       defaultValue: 'udp'
333     },
334     dtmfmode: {
335       type: DataTypes.ENUM('rfc2833', 'info', 'shortinfo', 'inband',
336         'auto'),
337       allowNull: true,
338       defaultValue: 'rfc2833'
339     },
340     directmedia: {
341       type: DataTypes.ENUM('yes', 'no', 'nonat', 'update', 'outgoing'),
342       allowNull: true,
343       defaultValue: 'no'
344     },
345     directrtpsetup: {
346       type: DataTypes.ENUM('yes', 'no'),
347       allowNull: true,
348       defaultValue: 'no'
349     },
350     directmediapermit: {
351       type: DataTypes.STRING,
352       allowNull: true,
353     },
354     directmediadeny: {
355       type: DataTypes.STRING,
356       allowNull: true,
357     },
358     nat: {
359       type: DataTypes.STRING,
360       allowNull: true,
361       defaultValue: 'force_rport,comedia'
362     },
363     callgroup: {
364       type: DataTypes.STRING,
365       allowNull: true,
366     },
367     namedcallgroup: { //We are in named call groups engineering,sales,netgroup,protgroup
368       type: DataTypes.STRING,
369       allowNull: true,
370     },
371     pickupgroup: {
372       type: DataTypes.STRING,
373       allowNull: true,
374     },
375     namedpickupgroup: { //We can do call pick-p for named call group sales
376       type: DataTypes.STRING,
377       allowNull: true,
378     },
379     language: {
380       type: DataTypes.STRING,
381       allowNull: true,
382       defaultValue: 'en'
383     },
384     tonezone: {
385       type: DataTypes.STRING,
386       allowNull: true
387     },
388     allow: {
389       type: DataTypes.STRING,
390       allowNull: true,
391       defaultValue: 'alaw;ulaw;gsm'
392     },
393     disallow: {
394       type: DataTypes.STRING,
395       allowNull: true,
396       defaultValue: null
397     },
398     autoframing: {
399       type: DataTypes.ENUM('yes', 'no'),
400       allowNull: true,
401     },
402     insecure: {
403       type: DataTypes.STRING,
404       allowNull: true,
405       defaultValue: 'port,invite'
406     },
407     trustrpid: {
408       type: DataTypes.ENUM('yes', 'no'),
409       allowNull: true,
410       defaultValue: 'no'
411     },
412     trust_id_outbound: {
413       type: DataTypes.ENUM('yes', 'no'),
414       allowNull: true,
415       defaultValue: 'no'
416     },
417     progressinband: {
418       type: DataTypes.ENUM('yes', 'no', 'never'),
419       allowNull: true,
420     },
421     promiscredir: {
422       type: DataTypes.ENUM('yes', 'no'),
423       allowNull: true,
424     },
425     useclientcode: {
426       type: DataTypes.ENUM('yes', 'no'),
427       allowNull: true,
428     },
429     accountcode: {
430       type: DataTypes.INTEGER(11),
431       allowNull: true,
432     },
433     setvar: {
434       type: DataTypes.STRING,
435       allowNull: true,
436     },
437     callerid: {
438       type: DataTypes.STRING,
439       allowNull: true,
440       defaultValue: '"" <>'
441     },
442     amaflags: {
443       type: DataTypes.STRING,
444       allowNull: true,
445     },
446     callcounter: {
447       type: DataTypes.ENUM('yes', 'no'),
448       allowNull: true,
449       defaultValue: 'yes'
450     },
451     busylevel: {
452       type: DataTypes.INTEGER(11),
453       allowNull: true,
454     },
455     allowoverlap: {
456       type: DataTypes.ENUM('yes', 'no'),
457       allowNull: true,
458     },
459     allowsubscribe: {
460       type: DataTypes.ENUM('yes', 'no'),
461       allowNull: true,
462     },
463     allowtransfer: {
464       type: DataTypes.ENUM('yes', 'no'),
465       allowNull: true,
466     },
467     ignoresdpversion: {
468       type: DataTypes.ENUM('yes', 'no'),
469       allowNull: true,
470     },
471     subscribecontext: {
472       type: DataTypes.STRING,
473       allowNull: true,
474     },
475     template: {
476       type: DataTypes.STRING,
477       allowNull: true,
478     },
479     videosupport: {
480       type: DataTypes.ENUM('yes', 'no', 'always'),
481       allowNull: true,
482       defaultValue: 'no'
483     },
484     maxcallbitrate: {
485       type: DataTypes.INTEGER(11),
486       allowNull: true,
487     },
488     rfc2833compensate: {
489       type: DataTypes.ENUM('yes', 'no'),
490       allowNull: true,
491     },
492     mailbox: {
493       type: DataTypes.STRING,
494       allowNull: true,
495     },
496     session_timers: {
497       type: DataTypes.ENUM('accept', 'refuse', 'originate'),
498       allowNull: true,
499     },
500     session_expires: {
501       type: DataTypes.INTEGER(11),
502       allowNull: true,
503     },
504     session_minse: {
505       type: DataTypes.INTEGER(11),
506       allowNull: true,
507     },
508     session_refresher: {
509       type: DataTypes.ENUM('uac', 'uas'),
510       allowNull: true,
511       defaultValue: 'uas'
512     },
513     t38pt_usertpsource: {
514       type: DataTypes.STRING,
515       allowNull: true,
516     },
517     regexten: {
518       type: DataTypes.STRING,
519       allowNull: true,
520     },
521     fromdomain: {
522       type: DataTypes.STRING,
523       allowNull: true,
524     },
525     fromuser: {
526       type: DataTypes.STRING,
527       allowNull: true,
528     },
529     host: {
530       type: DataTypes.STRING,
531       allowNull: true,
532       defaultValue: 'dynamic'
533     },
534     qualify: {
535       type: DataTypes.ENUM('yes', 'no'),
536       allowNull: true,
537       defaultValue: 'yes'
538     },
539     keepalive: {
540       type: DataTypes.INTEGER(11),
541       allowNull: true,
542     },
543     defaultip: {
544       type: DataTypes.STRING,
545       allowNull: true,
546     },
547     defaultuser: {
548       type: DataTypes.STRING,
549       allowNull: true,
550     },
551     rtptimeout: { // Terminate call if 60 seconds of no RTP or RTCP activity on the audio channel  when we're not on hold.
552       type: DataTypes.INTEGER(11),
553       allowNull: true,
554     },
555     rtpholdtimeout: { // Terminate call if 300 seconds of no RTP or RTCP activity on the audio channel when we're on hold (must be > rtptimeout)
556       type: DataTypes.INTEGER(11),
557       allowNull: true,
558     },
559     rtpkeepalive: { // Send keepalives in the RTP stream to keep NAT open (default is off - zero)
560       type: DataTypes.INTEGER(11),
561       allowNull: true,
562     },
563     sendrpid: {
564       type: DataTypes.ENUM('yes', 'no'),
565       allowNull: true,
566       defaultValue: 'no'
567     },
568     outboundproxy: {
569       type: DataTypes.STRING,
570       allowNull: true,
571     },
572     callbackextension: {
573       type: DataTypes.STRING,
574       allowNull: true,
575     },
576     timert1: {
577       type: DataTypes.INTEGER(11),
578       allowNull: true,
579     },
580     timerb: {
581       type: DataTypes.INTEGER(11),
582       allowNull: true,
583     },
584     qualifyfreq: {
585       type: DataTypes.INTEGER(11),
586       allowNull: true,
587     },
588     contactpermit: {
589       type: DataTypes.STRING,
590       allowNull: true,
591     },
592     contactdeny: {
593       type: DataTypes.STRING,
594       allowNull: true,
595     },
596     contactacl: {
597       type: DataTypes.STRING,
598       allowNull: true,
599     },
600     unsolicited_mailbox: {
601       type: DataTypes.STRING,
602       allowNull: true,
603     },
604     use_q850_reason: {
605       type: DataTypes.STRING,
606       allowNull: true,
607     },
608     maxforwards: {
609       type: DataTypes.INTEGER(11),
610       allowNull: true,
611     },
612     encryption: {
613       type: DataTypes.ENUM('yes', 'no'),
614       allowNull: true,
615       defaultValue: 'no'
616     },
617     avpf: {
618       type: DataTypes.ENUM('yes', 'no'),
619       allowNull: true
620     },
621     force_avp: {
622       type: DataTypes.ENUM('yes', 'no'),
623       allowNull: true
624     },
625     icesupport: {
626       type: DataTypes.ENUM('yes', 'no'),
627       allowNull: true
628     },
629     dtlsenable: {
630       type: DataTypes.ENUM('yes', 'no'),
631       allowNull: true
632     },
633     dtlsverify: {
634       type: DataTypes.ENUM('yes', 'no', 'fingerprint', 'certificate'),
635       allowNull: true
636     },
637     dtlsrekey: {
638       type: DataTypes.INTEGER(11),
639       allowNull: true,
640     },
641     dtlscertfile: {
642       type: DataTypes.STRING,
643       allowNull: true,
644     },
645     dtlsprivatekey: {
646       type: DataTypes.STRING,
647       allowNull: true,
648     },
649     dtlscipher: {
650       type: DataTypes.STRING,
651       allowNull: true,
652     },
653     dtlscafile: {
654       type: DataTypes.STRING,
655       allowNull: true,
656     },
657     dtlscapath: {
658       type: DataTypes.STRING,
659       allowNull: true,
660     },
661     dtlssetup: {
662       type: DataTypes.ENUM('active', 'passive', 'actpass'),
663       allowNull: true
664     },
665     dtlsfingerprint: {
666       type: DataTypes.STRING,
667       allowNull: true,
668     },
669     usereqphone: { //This provider requires ";user=phone" on URI
670       type: DataTypes.ENUM('yes', 'no'),
671       allowNull: true,
672       defaultValue: 'no'
673     },
674     recordonfeature: { //Feature to use when INFO with Record: on is received.
675       type: DataTypes.STRING,
676       allowNull: true,
677     },
678     recordofffeature: { //Feature to use when INFO with Record: off is received.
679       type: DataTypes.STRING,
680       allowNull: true,
681     },
682     call_limit: {
683       type: DataTypes.INTEGER(11),
684       allowNull: true,
685       defaultValue: null
686     },
687     registertrying: { //Send a 100 Trying when the device registers.
688       type: DataTypes.ENUM('yes', 'no'),
689       allowNull: true,
690     },
691     subscribemwi: { //Only send notifications if this phone subscribes for mailbox notification
692       type: DataTypes.ENUM('yes', 'no'),
693       allowNull: true,
694     },
695     vmexten: { // dialplan extension to reach mailbox. defaults to global vmexten which defaults to "asterisk"
696       type: DataTypes.STRING,
697       allowNull: true,
698     },
699     mohinterpret: { // This option specifies a preference for which music on hold class this channel should listen to when put on hold
700       type: DataTypes.STRING,
701       allowNull: true,
702     },
703     mohsuggest: { //  This option specifies which music on hold class to suggest to the peer channel when this channel places the peer on hold.
704       type: DataTypes.STRING,
705       allowNull: true,
706     },
707     parkinglot: {
708       type: DataTypes.STRING,
709       allowNull: true,
710     },
711     canreinvite: {
712       type: DataTypes.ENUM('yes', 'no', 'nonat', 'update', 'update,nonat'),
713       allowNull: true,
714       defaultValue: 'no'
715     },
716   }, {
717     tableName: 'users',
718     instanceMethods: {
719       /**
720        * Authenticate - check if the passwords are the same
721        *
722        * @param {String} plainText
723        *        {function} callBack
724        * @api public
725        */
726       authenticate: function(plainText) {
727         return this.encryptPassword(plainText) === this.password;
728       },
729       /**
730        * Make salt
731        *
732        * @return {String}
733        * @api public
734        */
735       makeSalt: function() {
736         return crypto.randomBytes(16).toString('base64');
737       },
738       /**
739        * Encrypt password
740        *
741        * @param {String} password
742        * @return {String}
743        * @api public
744        */
745       encryptPassword: function(password) {
746         if (!password || !this.salt) return '';
747         var salt = new Buffer(this.salt, 'base64');
748         return crypto.pbkdf2Sync(password, salt, 10000, 64).toString(
749           'base64');
750       },
751       /**
752        * md5 password
753        *
754        * @param {String} password
755        * @return {String}
756        * @api public
757        */
758       md5Password: function(password) {
759         if (!password) return '';
760         return md5(password);
761       }
762     },
763     associate: function(models) {
764       // BELOGNS TO MANY
765       User.hasMany(models.ChatMessage);
766       User.hasMany(models.MailMessage);
767       User.hasMany(models.Contact);
768       User.hasMany(models.Action);
769       User.belongsToMany(models.Module, {
770         through: models.UserHasModule,
771         required: false
772       });
773       User.belongsToMany(models.Channel, {
774         through: 'user_has_channels'
775       });
776       User.belongsToMany(models.MailRoom, {
777         through: 'user_has_mail_rooms'
778       });
779       User.belongsToMany(models.SmsRoom, {
780         through: 'user_has_sms_rooms'
781       });
782       User.belongsToMany(models.Team, {
783         through: models.UserHasTeam
784       });
785       User.belongsToMany(models.VoiceQueue, {
786         through: models.UserHasVoiceQueue,
787         required: false
788       });
789       User.belongsToMany(models.VoiceQueue, {
790         through: models.UserHasVoiceQueuePermit,
791         as: 'PVoiceQueues'
792       });
793       User.belongsToMany(models.MailQueue, {
794         through: models.UserHasMailQueue,
795         required: false
796       });
797       User.belongsToMany(models.SmsQueue, {
798         through: models.UserHasSmsQueue,
799         required: false
800       });
801       User.belongsToMany(models.SmsQueue, {
802         through: models.UserHasSmsQueuePermit,
803         as: 'PSmsQueues'
804       });
805
806       User.belongsToMany(models.MailQueue, {
807         through: models.UserHasMailQueuePermit,
808         as: 'PMailQueues'
809       });
810       User.belongsToMany(models.FaxQueue, {
811         through: models.UserHasFaxQueue,
812         required: false
813       });
814       User.belongsToMany(models.FaxQueue, {
815         through: models.UserHasFaxQueuePermit,
816         as: 'PFaxQueues'
817       });
818       User.belongsToMany(models.ChatQueue, {
819         through: models.UserHasChatQueue,
820         required: false
821       });
822       User.belongsToMany(models.ChatQueue, {
823         through: models.UserHasChatQueuePermit,
824         as: 'PChatQueues'
825       });
826       User.belongsToMany(models.ChatRoom, {
827         through: models.UserHasChatRoom
828       });
829       User.belongsToMany(models.List, {
830         through: models.UserHasList
831       });
832       User.hasMany(models.VoiceExtension, {
833         foreignKey: 'UserId',
834         as: 'UserExtensions',
835         onDelete: 'cascade',
836         hooks: true
837       });
838
839       // SCOPES MANAGEMENT
840       User.addScope('all', {
841         attributes: ['id',
842           'name',
843           'email',
844           'internal',
845           'fullname',
846           'role',
847           'online',
848           'userpic',
849           'accountcode',
850           'transport',
851           'host',
852           'role',
853           'nat',
854           'type',
855           'allow',
856           'lastLoginAt',
857           'ipaddr',
858           'fullcontact',
859           'port',
860           'lastms',
861           'description'
862         ]
863       });
864
865       // SCOPES MANAGEMENT
866       User.addScope('user', {
867         where: {
868           role: {
869             $in: ['admin', 'user']
870           },
871         },
872         attributes: ['id',
873           'name',
874           'email',
875           'internal',
876           'fullname',
877           'role',
878           'userpic',
879           'accountcode',
880           'transport',
881           'host',
882           'role',
883           'nat',
884           'type',
885           'allow',
886           'lastLoginAt',
887           'ipaddr',
888           'fullcontact',
889           'port',
890           'lastms',
891           'description'
892         ]
893       });
894       User.addScope('agent', {
895         where: {
896           role: 'agent'
897         },
898         attributes: ['id',
899           'name',
900           'email',
901           'internal',
902           'fullname',
903           'role',
904           'userpic',
905           'accountcode',
906           'transport',
907           'host',
908           'nat',
909           'type',
910           'allow',
911           'chatCapacity',
912           'mailCapacity',
913           'faxCapacity',
914           'smsCapacity',
915           'online',
916           'lastLoginAt',
917           'phoneBarAutoAnswer',
918           'phoneBarEnableSettings',
919           'phoneBarUnconditional',
920           'phoneBarNoReply',
921           'phoneBarBusy',
922           'phoneBarUnconditionalNumber',
923           'phoneBarNoReplyNumber',
924           'phoneBarBusyNumber',
925           'phoneBarListenPort',
926           'chanspy',
927           'voicePause',
928           'mailPause',
929           'faxPause',
930           'chatPause',
931           'smsPause',
932           'pauseType',
933           'lastPauseAt',
934           'status',
935           'statusAt',
936           'queueStatus',
937           'queueStatusAt',
938           'lastQueue',
939           'useragent',
940           'ipaddr',
941           'fullcontact',
942           'port',
943           'lastms',
944           'description'
945         ]
946       });
947       User.addScope('telephone', {
948         where: {
949           role: 'telephone'
950         }
951       });
952       User.addScope('queues', {
953         include: [models.VoiceQueue, models.ChatQueue, models.MailQueue,
954           models.FaxQueue, models.SmsQueue
955         ]
956       });
957
958       User.addScope('checkPauseStatus', function(query) {
959         var scope = {
960           where: {}
961         };
962         if (query.voicePause) {
963           scope.where.voicePause = (query.voicePause === 'true') ?
964             true : false;
965           delete query.voicePause;
966         } else if (query.faxPause) {
967           scope.where.faxPause = (query.faxPause === 'true') ? true :
968             false;
969           delete query.faxPause;
970         } else if (query.chatPause) {
971           scope.where.chatPause = (query.chatPause === 'true') ?
972             true : false;
973           delete query.chatPause;
974         } else if (query.mailPause) {
975           scope.where.mailPause = (query.mailPause === 'true') ?
976             true : false;
977           delete query.mailPause;
978         } else if (query.smsPause) {
979           scope.where.smsPause = (query.smsPause === 'true') ?
980             true : false;
981           delete query.smsPause;
982         }
983         return scope;
984       });
985       User.addScope('checkOnlineStatus', function(query) {
986         var scope = {
987           where: {}
988         };
989         if (query.online) {
990           scope.where.online = (query.online === 'true') ? true :
991             false;
992           delete query.online;
993         }
994         return scope;
995       });
996       User.addScope('checkSipStatus', function(query) {
997         var scope = {
998           where: {}
999         };
1000         if (query.status) {
1001           scope.where.status = query.status;
1002           delete query.status;
1003         }
1004         return scope;
1005       });
1006       User.addScope('checkQueueStatus', function(query) {
1007         var scope = {
1008           where: {}
1009         };
1010         if (query.queueStatus) {
1011           scope.where.queueStatus = query.queueStatus;
1012           delete query.queueStatus;
1013         }
1014         return scope;
1015       });
1016     }
1017   });
1018
1019   return User;
1020 }