blob: 0df37c7883029925743e19ba154dd2cba2c84c82 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo035c5932016-12-14 09:55:15 -080019import {BehaviorSubject} from 'rxjs';
20import * as _ from 'lodash';
21import {IWSEvent} from '../websocket/global';
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080022import {IXosResourceService} from '../rest/model.rest';
Matteo Scandolo63498472017-09-26 17:21:41 -070023import {IXosModeldefsCache} from './modeldefs.service';
Matteo Scandolo035c5932016-12-14 09:55:15 -080024
25export interface IStoreHelpersService {
26 updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>;
27}
28
Matteo Scandolo1aee1982017-02-17 08:33:23 -080029export class StoreHelpers implements IStoreHelpersService {
Matteo Scandolo63498472017-09-26 17:21:41 -070030 static $inject = [
31 'ModelRest',
32 'XosModeldefsCache'
33 ];
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080034
35 constructor (
Matteo Scandolo63498472017-09-26 17:21:41 -070036 private modelRest: IXosResourceService,
37 private XosModeldefsCache: IXosModeldefsCache
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080038 ) {
39 }
40
Matteo Scandolo035c5932016-12-14 09:55:15 -080041 public updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> {
42 const collection: any[] = subject.value;
43 const index: number = _.findIndex(collection, (i) => {
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080044 // NOTE evaluate to use event.msg.pk
Matteo Scandolo035c5932016-12-14 09:55:15 -080045 return i.id === event.msg.object.id;
46 });
47 const exist: boolean = index > -1;
48 const isDeleted: boolean = _.includes(event.msg.changed_fields, 'deleted');
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080049
50 // generate a resource for the model
Matteo Scandolo63498472017-09-26 17:21:41 -070051 const modelDef = this.XosModeldefsCache.get(event.model); // get the model definition
52 const endpoint = this.XosModeldefsCache.getApiUrlFromModel(modelDef);
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080053 const resource = this.modelRest.getResource(endpoint);
54 const model = new resource(event.msg.object);
55
56 // remove
Matteo Scandolo035c5932016-12-14 09:55:15 -080057 if (exist && isDeleted) {
58 _.remove(collection, {id: event.msg.object.id});
59 }
60 // Replace item at index using native splice
61 else if (exist && !isDeleted) {
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080062 collection.splice(index, 1, model);
Matteo Scandolo035c5932016-12-14 09:55:15 -080063 }
64 // if the element is not deleted add it
65 else if (!exist && !isDeleted) {
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080066 collection.push(model);
Matteo Scandolo035c5932016-12-14 09:55:15 -080067 }
68
69 subject.next(collection);
70
71 return subject;
72 }
73}