'use strict';

module.exports = function (newDoc, oldDoc) {
    if (newDoc.$type === 'entry') {
        if (typeof newDoc.$creationDate !== 'number' || typeof newDoc.$modificationDate !== 'number') {
            throw({forbidden: 'Creation and modification dates are mandatory'});
        }
        if (oldDoc && newDoc.$creationDate !== oldDoc.$creationDate) {
            throw({forbidden: 'Cannot change creation date'});
        }
        if (newDoc.$modificationDate < oldDoc.$modificationDate) {
            throw({forbidden: 'Modification date cannot change to the past'});
        }
        if (newDoc.$modificationDate < newDoc.$creationDate) {
            throw({forbidden: 'Modification date cannot be before creation date'});
        }
    }
};

@@ -85,24 +85,57 @@
  'entry') %7B%0A
+        // todo $id is mandatory%0A
         if (
@@ -333,26 +333,16 @@
 if (
-oldDoc && 
 newDoc.$
 crea
@@ -329,35 +329,39 @@
     if (newDoc.$
-cre
+modific
 ationDate !== ol
@@ -358,15 +358,13 @@
 ate 
-!== old
+%3C new
 Doc.
@@ -415,20 +415,41 @@
 n: '
-Cannot
+Modification date
  c
-h
 an
-g
+not be befor
 e cr
@@ -466,32 +466,58 @@
 e'%7D);%0A        %7D%0A
+        if (oldDoc) %7B%0A    
         if (newD
@@ -512,39 +512,35 @@
     if (newDoc.$
-modific
+cre
 ationDate %3C oldD
@@ -533,17 +533,19 @@
 ionDate 
-%3C
+!==
  oldDoc.
@@ -541,31 +541,27 @@
 !== oldDoc.$
-modific
+cre
 ationDate) %7B
@@ -553,32 +553,36 @@
 creationDate) %7B%0A
+    
             thro
@@ -600,51 +600,35 @@
 n: '
-Modification date cannot change to the past
+Cannot change creation date
 '%7D);
@@ -628,32 +628,36 @@
 ate'%7D);%0A        
+    
 %7D%0A        if (ne
@@ -642,32 +642,36 @@
       %7D%0A        
+ 
+   
 if (newDoc.$modi
@@ -681,35 +681,39 @@
 ationDate %3C 
-new
+old
 Doc.$
-cre
+modific
 ationDate) %7B
@@ -705,32 +705,36 @@
 ficationDate) %7B%0A
+    
             thro
@@ -777,35 +777,44 @@
 not 
-be before creation date'%7D);
+change to the past'%7D);%0A            %7D
 %0A   

update validateDocUpdate