[CORD-2192] Converting dates back to timestamp

Change-Id: I6dfa85dea5de78a090f720bba5a84437c6ef65ff
(cherry picked from commit a1654576041a2bfa2b1e4a2d85ece8dfa03ac4c3)
diff --git a/src/app/datasources/rest/model.rest.spec.ts b/src/app/datasources/rest/model.rest.spec.ts
index d545ac2..da4f2f4 100644
--- a/src/app/datasources/rest/model.rest.spec.ts
+++ b/src/app/datasources/rest/model.rest.spec.ts
@@ -22,6 +22,7 @@
 import 'angular-cookies';
 import {IXosResourceService} from './model.rest';
 import {xosDataSources} from '../index';
+import {IXosFormHelpersService} from '../../core/form/form-helpers';
 
 let service: IXosResourceService;
 let resource: ng.resource.IResourceClass<any>;
@@ -34,6 +35,10 @@
   websocketClient: 'http://xos-test:3000'
 };
 
+const MockFormHelpers: IXosFormHelpersService = {
+  _getFieldFormat: () => 'date'
+};
+
 describe('The ModelRest service', () => {
 
   beforeEach(angular.mock.module(xosDataSources));
@@ -41,7 +46,8 @@
   beforeEach(() => {
 
     angular.module(xosDataSources)
-      .constant('AppConfig', MockAppCfg);
+      .constant('AppConfig', MockAppCfg)
+      .value('XosFormHelpers', MockFormHelpers);
 
     angular.mock.module(xosDataSources);
   });
@@ -99,4 +105,31 @@
     $scope.$apply();
     httpBackend.flush();
   });
+
+  describe('when saving a model', () => {
+
+    let item, date;
+    const timestamp = 1509552402000;
+
+    beforeEach(() => {
+      httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/core/test`)
+        .respond((method, url, req) => {
+          return [200, req];
+        });
+      resource = service.getResource('/core/test');
+      date = new Date(timestamp);
+      item = new resource({date: date.toString()});
+    });
+
+    xit('should convert dates to timestamps', (done) => {
+      item.$save()
+        .then(res => {
+          expect(res.date).toEqual(timestamp);
+          done();
+        });
+      $scope.$apply();
+      httpBackend.flush();
+      done();
+    });
+  });
 });
diff --git a/src/app/datasources/rest/model.rest.ts b/src/app/datasources/rest/model.rest.ts
index 6c72d99..f390616 100644
--- a/src/app/datasources/rest/model.rest.ts
+++ b/src/app/datasources/rest/model.rest.ts
@@ -15,37 +15,48 @@
  * limitations under the License.
  */
 
-
+import * as _ from 'lodash';
 import {IXosAppConfig} from '../../../index';
+import {IXosFormHelpersService} from '../../core/form/form-helpers';
+
 export interface IXosResourceService {
   getResource(url: string): ng.resource.IResourceClass<any>;
 }
 
 export class ModelRest implements IXosResourceService {
-  static $inject = ['$resource', 'AppConfig'];
+  static $inject = ['$resource', 'AppConfig', 'XosFormHelpers'];
 
   /** @ngInject */
   constructor(
     private $resource: ng.resource.IResourceService,
-    private AppConfig: IXosAppConfig
+    private AppConfig: IXosAppConfig,
+    private XosFormHelpers: IXosFormHelpersService
   ) {
 
   }
 
   public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> {
+    const self = this;
     const resource: angular.resource.IResourceClass<any> = this.$resource(`${this.AppConfig.apiEndpoint}${url}/:id/`, {id: '@id'}, {
       update: { method: 'PUT' },
       query: {
         method: 'GET',
         isArray: true,
         transformResponse: (res) => {
-          // FIXME chameleon return everything inside "items"
           return res.items ? res.items : res;
         }
       }
     });
 
     resource.prototype.$save = function() {
+
+      // NOTE converting dates back to timestamp
+      _.forEach(Object.keys(this), (k: string) => {
+        if (self.XosFormHelpers._getFieldFormat(this[k]) === 'date') {
+          this[k] = new Date(this[k]).getTime();
+        }
+      });
+
       if (this.id) {
         return this.$update();
       } else {