Built motion from commit fa8c70b.|0.0.144
[motion.git] / server / models / sms_room.js
1 const moment = require('moment');
2
3
4 module.exports = function(sequelize, DataTypes) {
5   var SmsRoom = sequelize.define('SmsRoom', {
6     contact: {
7       type: DataTypes.STRING,
8       validate: {
9         is: /^[\+]?[0-9]+$/
10       }
11     },
12     smsIn: {
13       type: DataTypes.INTEGER,
14       defaultValue: 0
15     },
16     smsOut: {
17       type: DataTypes.INTEGER,
18       defaultValue: 0
19     },
20     status: {
21       type: DataTypes.ENUM('NEW', 'OPEN', 'PENDING', 'CLOSED'),
22       defaultValue: 'NEW',
23       set: function(status) {
24         this.setDataValue('status', status);
25
26         switch (status) {
27           case 'NEW':
28             this.setDataValue('arrivedAt', moment().format('YYYY-MM-DD HH:mm:ss'));
29             break;
30           case 'CLOSED':
31             this.setDataValue('closedAt', moment().format('YYYY-MM-DD HH:mm:ss'));
32             break;
33           default:
34
35         }
36       }
37     },
38     lastEvent: {
39       type: DataTypes.ENUM('SENDING', 'SENT', 'RECEIVED', 'FAILED', 'NOTE')
40     },
41     lastEventAt: {
42       type: DataTypes.DATE
43     },
44     waiting: {
45       type: DataTypes.BOOLEAN,
46       defaultValue: false
47     },
48     tags: {
49       type: DataTypes.TEXT,
50       get: function() {
51         var tags;
52         if (this.getDataValue('tags')) {
53           tags = this.getDataValue('tags').split(';');
54           tags.pop();
55         } else {
56           tags = [];
57         }
58         return tags;
59       },
60       set: function(val) {
61         this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
62       }
63     },
64     disposition: {
65       type: DataTypes.STRING
66     },
67     openReason: DataTypes.STRING,
68     arrivedAt: {
69       type: DataTypes.DATE
70     },
71     closedAt: {
72       type: DataTypes.DATE
73     },
74     ParentId: {
75       type: DataTypes.INTEGER
76     },
77     voiceSource: {
78       type: DataTypes.BOOLEAN,
79       defaultValue: false
80     },
81     account: {
82       type: DataTypes.STRING
83     }
84   }, {
85     tableName: 'sms_rooms',
86     associate: function(models) {
87       // BINDING
88       SmsRoom.hasMany(models.SmsMessage);
89       SmsRoom.hasMany(models.SmsRoomStatus, {
90         plural: 'SmsRoomStatuses'
91       });
92       SmsRoom.belongsToMany(models.User, {
93         through: 'user_has_sms_rooms'
94       });
95       SmsRoom.belongsTo(models.SmsAccount);
96       SmsRoom.addScope('default', {
97         order: [
98           ['createdAt', 'DESC']
99         ],
100         include: [{
101           model: models.SmsAccount
102         }, {
103           model: models.SmsMessage,
104           include: [{
105             model: models.User,
106             attributes: ['id', 'name', 'fullname', 'email']
107           }]
108         }, {
109           model: models.User,
110           attributes: ['id', 'name', 'fullname', 'email']
111         }]
112       });
113       SmsRoom.addScope('agent', function(id) {
114         return {
115           include: [{
116             model: models.User,
117             where: {
118               id: id
119             }
120           }]
121         }
122       });
123     }
124
125
126   });
127   return SmsRoom;
128 };