[CORD-1927] Fixing generate url for service models
Change-Id: I08d0d853ba2ab041626e133d079ab4562d73171e
(cherry picked from commit 6349847ecd1040111de5e7def26304e9e093cb92)
diff --git a/src/app/datasources/stores/model.store.spec.ts b/src/app/datasources/stores/model.store.spec.ts
index 4e94495..09dc3f9 100644
--- a/src/app/datasources/stores/model.store.spec.ts
+++ b/src/app/datasources/stores/model.store.spec.ts
@@ -27,11 +27,13 @@
import {ConfigHelpers} from '../../core/services/helpers/config.helpers';
import {AuthService} from '../rest/auth.rest';
import {XosDebouncer} from '../../core/services/helpers/debounce.helper';
+import {IXosModeldefsCache} from '../helpers/modeldefs.service';
let service: IXosModelStoreService;
let httpBackend: ng.IHttpBackendService;
let $scope;
let WebSocket;
+let XosModeldefsCache;
class MockWs {
private _list;
@@ -69,6 +71,10 @@
.service('ConfigHelpers', ConfigHelpers) // TODO mock
.service('AuthService', AuthService)
.constant('AppConfig', MockAppCfg)
+ .value('XosModeldefsCache', {
+ get: jasmine.createSpy('XosModeldefsCache.get').and.returnValue({}),
+ getApiUrlFromModel: jasmine.createSpy('XosModeldefsCache.getApiUrlFromModel')
+ })
.service('XosDebouncer', XosDebouncer);
angular.mock.module('ModelStore');
@@ -78,12 +84,14 @@
XosModelStore: IXosModelStoreService,
$httpBackend: ng.IHttpBackendService,
_$rootScope_: ng.IRootScopeService,
- _WebSocket_: any
+ _WebSocket_: any,
+ _XosModeldefsCache_: IXosModeldefsCache
) => {
service = XosModelStore;
httpBackend = $httpBackend;
$scope = _$rootScope_;
WebSocket = _WebSocket_;
+ XosModeldefsCache = _XosModeldefsCache_;
// ModelRest will call the backend
httpBackend.whenGET(`${MockAppCfg.apiEndpoint}/core/samples`)
@@ -96,6 +104,7 @@
});
it('the first event should be the resource response', (done) => {
+ XosModeldefsCache.getApiUrlFromModel.and.returnValue(`/core/samples`);
let event = 0;
service.query('sample')
.subscribe(collection => {
@@ -114,6 +123,7 @@
describe('the GET method', () => {
it('should return an observable containing a single model', (done) => {
let event = 0;
+ XosModeldefsCache.getApiUrlFromModel.and.returnValue(`/core/samples`);
service.get('sample', queryData[1].id)
.subscribe((model) => {
event++;
@@ -177,6 +187,8 @@
it('should create different Subject', (done) => {
let fevent = 0;
let sevent = 0;
+ XosModeldefsCache.get.and.callFake(v => v);
+ XosModeldefsCache.getApiUrlFromModel.and.callFake(v => `/core/${v}s`);
service.query('first')
.subscribe(first => {
fevent++;
diff --git a/src/app/datasources/stores/model.store.ts b/src/app/datasources/stores/model.store.ts
index 7735ff0..10c4d2e 100644
--- a/src/app/datasources/stores/model.store.ts
+++ b/src/app/datasources/stores/model.store.ts
@@ -23,6 +23,7 @@
import {IXosResourceService} from '../rest/model.rest';
import {IStoreHelpersService} from '../helpers/store.helpers';
import {IXosDebouncer} from '../../core/services/helpers/debounce.helper';
+import {IXosModeldefsCache} from '../helpers/modeldefs.service';
export interface IXosModelStoreService {
query(model: string, apiUrl?: string): Observable<any>;
@@ -31,7 +32,14 @@
}
export class XosModelStore implements IXosModelStoreService {
- static $inject = ['$log', 'WebSocket', 'StoreHelpers', 'ModelRest', 'XosDebouncer'];
+ static $inject = [
+ '$log',
+ 'WebSocket',
+ 'StoreHelpers',
+ 'ModelRest',
+ 'XosDebouncer',
+ 'XosModeldefsCache'
+ ];
private _collections: any; // NOTE contains a map of {model: BehaviourSubject}
private efficientNext: any; // NOTE debounce next
constructor(
@@ -39,7 +47,8 @@
private webSocket: IWSEventService,
private storeHelpers: IStoreHelpersService,
private ModelRest: IXosResourceService,
- private XosDebouncer: IXosDebouncer
+ private XosDebouncer: IXosDebouncer,
+ private XosModeldefsCache: IXosModeldefsCache
) {
this._collections = {};
this.efficientNext = this.XosDebouncer.debounce(this.next, 500, this, false);
@@ -59,6 +68,7 @@
this.efficientNext(this._collections[modelName]);
}
+ // NOTE do we need to subscriber every time we query?
this.webSocket.list()
.filter((e: IWSEvent) => e.model === modelName)
.subscribe(
@@ -128,8 +138,7 @@
private loadInitialData(model: string, apiUrl?: string) {
// TODO provide always the apiUrl together with the query() params
if (!angular.isDefined(apiUrl)) {
- // NOTE check what is the correct pattern to pluralize this
- apiUrl = this.storeHelpers.urlFromCoreModel(model);
+ apiUrl = this.XosModeldefsCache.getApiUrlFromModel(this.XosModeldefsCache.get(model));
}
this.ModelRest.getResource(apiUrl).query().$promise
.then(