bc0cfd8ce0ef5a500bee3d5418f81377bac09eea
[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                 openReason: DataTypes.STRING,
82                 ParentId: {
83                         type: DataTypes.INTEGER
84                 },
85                 arrivedAt: {
86                         type: DataTypes.DATE
87                 },
88                 closedAt: {
89                         type: DataTypes.DATE
90                 }
91         }, {
92                 tableName: 'mail_rooms',
93                 paranoid: true,
94                 associate: function(models) {
95                         // BINDING
96                         MailRoom.hasMany(models.MailMessage);
97                         MailRoom.hasMany(models.MailRoomStatus, {
98                                 plural: 'MailRoomStatuses'
99                         });
100                         MailRoom.belongsTo(models.MailAccount);
101                         MailRoom.belongsToMany(models.User, {
102                                 through: 'user_has_mail_rooms'
103                         });
104                         // SCOPES
105                         MailRoom.addScope('default', {
106                                 order: [
107                                         ['createdAt', 'DESC']
108                                 ],
109                                 include: [{
110                                         model: models.MailAccount
111                                 }, {
112                                         model: models.MailMessage,
113                                         include: [{
114                                                 model: models.MailAttachment
115                                         }, {
116                                                 model: models.User,
117                                                 attributes: ['id', 'name', 'fullname', 'email', 'userpic']
118                                         }]
119                                 }, {
120                                         model: models.User,
121                                         attributes: ['id', 'name', 'fullname', 'email', 'userpic']
122                                 }]
123                         });
124                 }
125         });
126
127         return MailRoom;
128 };