var router = module.exports = new (require('koa-router'))();

router.param('docUUID', function *(id, next) {
  this.prevDoc = yield Doc.find({
    where: { UUID: id, current: true },
    include: [{
      model: Collection,
      attributes: ['id'],
      required: true,
      include: [{
        model: Project,
        attributes: ['id'],
        required: true
      }]
    }]
  });
  this.assert(this.prevDoc, new HTTP_ERROR.NotFound('Doc %s', id));

  this.permission = yield this.me.getPermission(this.prevDoc.Collection.Project);

  this.checkPermission = function(permission) {
    return ProjectTeam.higherPermission(permission, this.permission) === this.permission;
  };
  yield next;
});

router.patch('/:docUUID', function *() {
  this.assert(this.checkPermission('write'), new HTTP_ERROR.NoPermission());

  var properties = ['title', 'content'];
  var body = this.request.body;
  var changed = _.intersection(properties, Object.keys(body));

  changed = changed.filter(function(key) {
    return body[key] !== this.prevDoc[key];
  }, this);

  var doc = this.prevDoc;
  if (changed.length) {
    doc = yield Doc.createWithTransaction({
      CollectionId: this.prevDoc.CollectionId,
      UUID: this.prevDoc.UUID,
      title: typeof body.title === 'string' ? body.title : this.prevDoc.title,
      content: typeof body.content === 'string' ? body.content : this.prevDoc.content
    });
  }

  this.body = {
    versions: {
      previous: this.prevDoc.version,
      current: doc.version
    },
    distance: this.prevDoc === doc ? 0 : doc.distance,
    changedProperties: changed || []
  };
});

@@ -98,24 +98,34 @@
 id, next) %7B%0A
+  try %7B%0A  
   this.prevD
@@ -146,16 +146,18 @@
 .find(%7B%0A
+  
     wher
@@ -184,24 +184,26 @@
 nt: true %7D,%0A
+  
     include:
@@ -208,24 +208,26 @@
 e: %5B%7B%0A      
+  
 model: Colle
@@ -229,24 +229,26 @@
 Collection,%0A
+  
       attrib
@@ -263,24 +263,26 @@
 id'%5D,%0A      
+  
 required: tr
@@ -283,32 +283,34 @@
 ed: true,%0A      
+ 
+ 
 include: %5B%7B%0A    
@@ -297,32 +297,34 @@
     include: %5B%7B%0A
+  
         model: P
@@ -331,32 +331,34 @@
 roject,%0A        
+  
 attributes: %5B'id
@@ -361,32 +361,34 @@
 %5B'id'%5D,%0A        
+ 
+ 
 required: true%0A 
@@ -396,24 +396,49 @@
     
+  
 %7D%5D%0A    
+ 
+ 
 %7D%5D%0A
+  
   %7D);%0A
+  %7D catch (err) %7B%7D%0A
   th

Fix error on postgres