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