Built motion from commit 3594e56.|0.0.120
[motion.git] / server / models / mail_room.js
index 170ed3b..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', {
@@ -21,19 +28,48 @@ module.exports = function(sequelize, DataTypes) {
                        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() {
-                               return this.getDataValue('tags') ? this.getDataValue('tags').split(';').pop() : [];
+                               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);
@@ -42,15 +78,24 @@ module.exports = function(sequelize, DataTypes) {
                disposition: {
                        type: DataTypes.STRING
                },
-               deletedAt: {
+               ParentId: {
+                       type: DataTypes.INTEGER
+               },
+               arrivedAt: {
                        type: DataTypes.DATE
                },
+               closedAt: {
+                       type: DataTypes.DATE
+               }
        }, {
                tableName: 'mail_rooms',
                paranoid: true,
                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'