Built motion from commit 3594e56.|0.0.120
[motion.git] / server / models / mail_room.js
index ea4e337..4057c0a 100644 (file)
  * Chat Website Model
  */
 
-module.exports = function(sequelize, DataTypes) {
+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', {
                subject: DataTypes.STRING,
                from: DataTypes.STRING,
+               account: DataTypes.STRING,
                attachment: {
                        type: DataTypes.BOOLEAN,
                        defaultValue: false
                },
+               mailIn: {
+                       type: DataTypes.INTEGER,
+                       defaultValue: 0
+               },
+               mailOut: {
+                       type: DataTypes.INTEGER,
+                       defaultValue: 0
+               },
                status: {
-                       type: DataTypes.ENUM('NEW', 'OPEN', 'CLOSED'),
-                       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('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',
+               paranoid: true,
                associate: function(models) {
+                       // BINDING
                        MailRoom.hasMany(models.MailMessage);
                        MailRoom.hasMany(models.MailRoomStatus, {
-                               as: 'States'
+                               plural: 'MailRoomStatuses'
                        });
                        MailRoom.belongsTo(models.MailAccount);
-                       MailRoom.belongsTo(models.User);
+                       MailRoom.belongsToMany(models.User, {
+                               through: 'user_has_mail_rooms'
+                       });
+                       // SCOPES
+                       MailRoom.addScope('default', {
+                               order: [
+                                       ['createdAt', 'DESC']
+                               ],
+                               include: [{
+                                       model: models.MailAccount
+                               }, {
+                                       model: models.MailMessage,
+                                       include: [{
+                                               model: models.MailAttachment
+                                       }, {
+                                               model: models.User,
+                                               attributes: ['id', 'name', 'fullname', 'email', 'userpic']
+                                       }]
+                               }, {
+                                       model: models.User,
+                                       attributes: ['id', 'name', 'fullname', 'email', 'userpic']
+                               }]
+                       });
                }
        });