[CORD-2192] Converting dates back to timestamp
Change-Id: I6dfa85dea5de78a090f720bba5a84437c6ef65ff
(cherry picked from commit a1654576041a2bfa2b1e4a2d85ece8dfa03ac4c3)
diff --git a/src/app/core/debug/debug-model.spec.ts b/src/app/core/debug/debug-model.spec.ts
index 46b6344..b081de0 100644
--- a/src/app/core/debug/debug-model.spec.ts
+++ b/src/app/core/debug/debug-model.spec.ts
@@ -91,7 +91,7 @@
dateFields.forEach(f => {
const date = isolatedScope.parseField(f, model[f]);
- expect(date).toEqual('Thu Aug 17 2017 15:45:20 GMT-0700 (PDT)');
+ expect(date).toEqual(new Date(model[f] * 1000).toString());
});
});
diff --git a/src/app/datasources/helpers/store.helpers.spec.ts b/src/app/datasources/helpers/store.helpers.spec.ts
index f3b2321..2f617bc 100644
--- a/src/app/datasources/helpers/store.helpers.spec.ts
+++ b/src/app/datasources/helpers/store.helpers.spec.ts
@@ -26,6 +26,7 @@
import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
import {AuthService} from '../rest/auth.rest';
import {IXosModeldefsCache} from './modeldefs.service';
+import {XosFormHelpers} from '../../core/form/form-helpers';
let service: IStoreHelpersService;
let subject: BehaviorSubject<any>;
@@ -42,6 +43,7 @@
.service('ModelRest', ModelRest) // NOTE evaluate mock
.service('StoreHelpers', StoreHelpers)
.service('AuthService', AuthService)
+ .service('XosFormHelpers', XosFormHelpers)
.value('XosModeldefsCache', {
get: jasmine.createSpy('XosModeldefsCache.get'),
getApiUrlFromModel: jasmine.createSpy('XosModeldefsCache.getApiUrlFromModel')
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 {
diff --git a/src/app/datasources/stores/model.store.spec.ts b/src/app/datasources/stores/model.store.spec.ts
index 09dc3f9..ce3e2cd 100644
--- a/src/app/datasources/stores/model.store.spec.ts
+++ b/src/app/datasources/stores/model.store.spec.ts
@@ -28,6 +28,7 @@
import {AuthService} from '../rest/auth.rest';
import {XosDebouncer} from '../../core/services/helpers/debounce.helper';
import {IXosModeldefsCache} from '../helpers/modeldefs.service';
+import {XosFormHelpers} from '../../core/form/form-helpers';
let service: IXosModelStoreService;
let httpBackend: ng.IHttpBackendService;
@@ -70,6 +71,7 @@
.service('XosModelStore', XosModelStore)
.service('ConfigHelpers', ConfigHelpers) // TODO mock
.service('AuthService', AuthService)
+ .service('XosFormHelpers', XosFormHelpers)
.constant('AppConfig', MockAppCfg)
.value('XosModeldefsCache', {
get: jasmine.createSpy('XosModeldefsCache.get').and.returnValue({}),