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