Built motion from commit 1038d87.|0.0.141
[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   }, {
82     tableName: 'sms_rooms',
83     associate: function(models) {
84       // BINDING
85       SmsRoom.hasMany(models.SmsMessage);
86       SmsRoom.hasMany(models.SmsRoomStatus, {
87         plural: 'SmsRoomStatuses'
88       });
89       SmsRoom.belongsToMany(models.User, {
90         through: 'user_has_sms_rooms'
91       });
92       SmsRoom.belongsTo(models.SmsAccount);
93       SmsRoom.addScope('default', {
94         order: [
95           ['createdAt', 'DESC']
96         ],
97         include: [{
98           model: models.SmsAccount
99         }, {
100           model: models.SmsMessage,
101           include: [{
102             model: models.User,
103             attributes: ['id', 'name', 'fullname', 'email']
104           }]
105         }, {
106           model: models.User,
107           attributes: ['id', 'name', 'fullname', 'email']
108         }]
109       });
110       SmsRoom.addScope('agent', function(id) {
111         return {
112           include: [{
113             model: models.User,
114             where: {
115               id: id
116             }
117           }]
118         }
119       });
120     }
121
122
123   });
124   return SmsRoom;
125 };