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