Built motion from commit 1038d87.|0.0.141
[motion.git] / server / models / sms_room.js
index 4a7a8d3..e3e6783 100644 (file)
@@ -1,34 +1,91 @@
-'use strict';
+const moment = require('moment');
+
 
 module.exports = function(sequelize, DataTypes) {
   var SmsRoom = sequelize.define('SmsRoom', {
-    from: {
+    contact: {
       type: DataTypes.STRING,
       validate: {
-        is: /^[0-9]+$/
+        is: /^[\+]?[0-9]+$/
       }
     },
+    smsIn: {
+      type: DataTypes.INTEGER,
+      defaultValue: 0
+    },
+    smsOut: {
+      type: DataTypes.INTEGER,
+      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('SENDING', 'SENT', 'RECEIVED', 'FAILED', 'NOTE')
+    },
+    lastEventAt: {
+      type: DataTypes.DATE
+    },
+    waiting: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
     },
     tags: {
       type: DataTypes.TEXT,
       get: function() {
-        return this.getDataValue('tags') ? this.getDataValue('tags').split(';') : [];
+        var tags;
+        if (this.getDataValue('tags')) {
+          tags = this.getDataValue('tags').split(';');
+          tags.pop();
+        } else {
+          tags = [];
+        }
+        return tags;
       },
       set: function(val) {
-        this.setDataValue('tags', val.join(';'));
+        this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
       }
     },
     disposition: {
       type: DataTypes.STRING
     },
+    openReason: DataTypes.STRING,
+    arrivedAt: {
+      type: DataTypes.DATE
+    },
+    closedAt: {
+      type: DataTypes.DATE
+    },
+    ParentId: {
+      type: DataTypes.INTEGER
+    },
+    voiceSource: {
+      type: DataTypes.BOOLEAN,
+      defaultValue: false
+    }
   }, {
     tableName: 'sms_rooms',
     associate: function(models) {
       // BINDING
       SmsRoom.hasMany(models.SmsMessage);
+      SmsRoom.hasMany(models.SmsRoomStatus, {
+        plural: 'SmsRoomStatuses'
+      });
       SmsRoom.belongsToMany(models.User, {
         through: 'user_has_sms_rooms'
       });