Built motion from commit 3594e56.|0.0.120
[motion.git] / server / models / mail_room.js
index 8e8e945..4057c0a 100644 (file)
@@ -2,6 +2,13 @@
  * Chat Website Model
  */
 
+const moment = require('moment');
+const satuses = {
+       NEW: ['OPEN', 'CLOSED'],
+       OPEN: ['PENDING', 'CLOSED'],
+       PENDING: ['OPEN', 'CLOSED'],
+       CLOSED: []
+};
 
 module.exports = function(sequelize, DataTypes) {
        var MailRoom = sequelize.define('MailRoom', {
@@ -12,12 +19,73 @@ module.exports = function(sequelize, DataTypes) {
                        type: DataTypes.BOOLEAN,
                        defaultValue: false
                },
+               mailIn: {
+                       type: DataTypes.INTEGER,
+                       defaultValue: 0
+               },
+               mailOut: {
+                       type: DataTypes.INTEGER,
+                       defaultValue: 0
+               },
                status: {
-                       type: DataTypes.ENUM('NEW', 'OPEN', 'CLOSED', 'UNMANAGED'),
-                       defaultValue: 'NEW'
+                       type: DataTypes.ENUM('NEW', 'OPEN', 'PENDING', 'CLOSED'),
+                       defaultValue: 'NEW',
+                       set: function(status) {
+                               this.setDataValue('status', status);
+
+                               switch (status) {
+                                       case 'NEW':
+                                               this.setDataValue('arrivedAt', moment().format('YYYY-MM-DD HH:mm:ss'));
+                                               break;
+                                       case 'CLOSED':
+                                               this.setDataValue('closedAt', moment().format('YYYY-MM-DD HH:mm:ss'));
+                                               break;
+                                       default:
+
+                               }
+                       }
                },
                lastEvent: {
-                       type: DataTypes.ENUM('SENT', 'SENDING', 'RECEIVED', 'FAILED', 'NOTE')
+                       type: DataTypes.ENUM('SENDING', 'SENT', 'RECEIVED', 'FAILED', 'NOTE')
+               },
+               lastEventAt: {
+                       type: DataTypes.DATE
+               },
+               deleted: {
+                       type: DataTypes.BOOLEAN,
+                       defaultValue: false
+               },
+               waiting: {
+                       type: DataTypes.BOOLEAN,
+                       defaultValue: false
+               },
+               tags: {
+                       type: DataTypes.TEXT,
+                       get: function() {
+                               var tags;
+                               if (this.getDataValue('tags')) {
+                                       tags = this.getDataValue('tags').split(';');
+                                       tags.pop();
+                               } else {
+                                       tags = [];
+                               }
+                               return tags;
+                       },
+                       set: function(val) {
+                               this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
+                       }
+               },
+               disposition: {
+                       type: DataTypes.STRING
+               },
+               ParentId: {
+                       type: DataTypes.INTEGER
+               },
+               arrivedAt: {
+                       type: DataTypes.DATE
+               },
+               closedAt: {
+                       type: DataTypes.DATE
                }
        }, {
                tableName: 'mail_rooms',
@@ -25,6 +93,9 @@ module.exports = function(sequelize, DataTypes) {
                associate: function(models) {
                        // BINDING
                        MailRoom.hasMany(models.MailMessage);
+                       MailRoom.hasMany(models.MailRoomStatus, {
+                               plural: 'MailRoomStatuses'
+                       });
                        MailRoom.belongsTo(models.MailAccount);
                        MailRoom.belongsToMany(models.User, {
                                through: 'user_has_mail_rooms'
@@ -42,11 +113,11 @@ module.exports = function(sequelize, DataTypes) {
                                                model: models.MailAttachment
                                        }, {
                                                model: models.User,
-                                               attributes: ['id', 'name', 'fullname', 'email']
+                                               attributes: ['id', 'name', 'fullname', 'email', 'userpic']
                                        }]
                                }, {
                                        model: models.User,
-                                       attributes: ['id', 'name', 'fullname', 'email']
+                                       attributes: ['id', 'name', 'fullname', 'email', 'userpic']
                                }]
                        });
                }