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