Built motion from commit 2239aeb.|0.0.113
[motion.git] / server / models / sms_room.js
index 9600743..48ad3f0 100644 (file)
@@ -2,18 +2,50 @@
 
 module.exports = function(sequelize, DataTypes) {
   var SmsRoom = sequelize.define('SmsRoom', {
-    from: DataTypes.INTEGER,
+    from: {
+      type: DataTypes.STRING,
+      validate: {
+        is: /^[\+]?[0-9]+$/
+      }
+    },
     status: {
-      type: DataTypes.ENUM('NEW', 'OPEN', 'CLOSED'),
+      type: DataTypes.ENUM('NEW', 'OPEN', 'CLOSED', 'UNMANAGED'),
       defaultValue: 'NEW'
+    },
+    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
+    },
+    voiceSource: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
     }
   }, {
     tableName: 'sms_rooms',
     associate: function(models) {
       // BINDING
       SmsRoom.hasMany(models.SmsMessage);
-      SmsRoom.belongsTo(models.User);
-
+      SmsRoom.belongsToMany(models.User, {
+        through: 'user_has_sms_rooms'
+      });
       SmsRoom.belongsTo(models.SmsAccount);
       SmsRoom.addScope('default', {
         order: [
@@ -32,6 +64,16 @@ module.exports = function(sequelize, DataTypes) {
           attributes: ['id', 'name', 'fullname', 'email']
         }]
       });
+      SmsRoom.addScope('agent', function(id) {
+        return {
+          include: [{
+            model: models.User,
+            where: {
+              id: id
+            }
+          }]
+        }
+      });
     }