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