Built motion from commit 3594e56.|0.0.120
[motion.git] / server / models / fax_room.js
index 112966e..0e488e7 100644 (file)
  * Fax Room Model
  */
 
-var crypto = require('crypto');
+const crypto = require('crypto');
+const moment = require('moment');
 
 module.exports = function(sequelize, DataTypes) {
 
   var FaxRoom = sequelize.define('FaxRoom', {
-    roomId: DataTypes.STRING,
-    // subject: DataTypes.STRING,
     from: DataTypes.STRING,
-    status: DataTypes.ENUM('NEW', 'OPEN', 'PENDING', 'SOLVED', 'CLOSED')
+    account: DataTypes.STRING,
+    faxIn: {
+      type: DataTypes.INTEGER,
+      defaultValue: 0
+    },
+    faxOut: {
+      type: DataTypes.INTEGER,
+      defaultValue: 0
+    },
+    status: {
+      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')
+    },
+    lastEventAt: {
+      type: DataTypes.DATE
+    },
+    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);
+      }
+    },
+    waiting: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    },
+    disposition: {
+      type: DataTypes.STRING
+    },
+    arrivedAt: {
+      type: DataTypes.DATE
+    },
+    closedAt: {
+      type: DataTypes.DATE
+    }
   }, {
     tableName: 'fax_rooms',
+    paranoid: true,
     associate: function(models) {
       // hasMany relations
       FaxRoom.hasMany(models.FaxMessage);
+      FaxRoom.hasMany(models.FaxRoomStatus, {
+        plural: 'MailRoomStatuses'
+      });
       FaxRoom.belongsTo(models.FaxAccount);
-      FaxRoom.belongsTo(models.User);
+      FaxRoom.belongsToMany(models.User, {
+        through: 'user_has_fax_rooms'
+      });
+      // SCOPES
+      FaxRoom.addScope('default', {
+        order: [
+          ['createdAt', 'DESC']
+        ],
+        include: [{
+          model: models.FaxAccount
+        }, {
+          model: models.FaxMessage,
+          include: [{
+            model: models.FaxAttachment
+          }, {
+            model: models.User,
+            attributes: ['id', 'name', 'fullname', 'email']
+          }]
+        }, {
+          model: models.User,
+          attributes: ['id', 'name', 'fullname', 'email']
+        }]
+      });
     }
   });