Built motion from commit 57474b6.|0.0.99
[motion.git] / server / models / sms_room.js
index 9600743..b8e6f18 100644 (file)
@@ -2,18 +2,33 @@
 
 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', 'CSQUARE'),
       defaultValue: 'NEW'
+    },
+    tags: {
+      type: DataTypes.TEXT,
+      get: function() {
+        return this.getDataValue('tags') ? this.getDataValue('tags').split(';') : [];
+      },
+      set: function(val) {
+        this.setDataValue('tags', val.join(';'));
+      }
     }
   }, {
     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 +47,16 @@ module.exports = function(sequelize, DataTypes) {
           attributes: ['id', 'name', 'fullname', 'email']
         }]
       });
+      SmsRoom.addScope('agent', function(id) {
+        return {
+          include: [{
+            model: models.User,
+            where: {
+              id: id
+            }
+          }]
+        }
+      });
     }