blob: 779259048dac746b902cf4538a8892d01943a690 [file] [log] [blame]
Matteo Scandolo035c5932016-12-14 09:55:15 -08001import {BehaviorSubject} from 'rxjs';
2import * as _ from 'lodash';
Matteo Scandolo04964232017-01-07 12:53:46 -08003import * as pluralize from 'pluralize';
Matteo Scandolo035c5932016-12-14 09:55:15 -08004import {IWSEvent} from '../websocket/global';
Matteo Scandoloaa024ff2017-01-04 15:04:46 -08005import {IXosResourceService} from '../rest/model.rest';
Matteo Scandolo035c5932016-12-14 09:55:15 -08006
7export interface IStoreHelpersService {
Matteo Scandolo04964232017-01-07 12:53:46 -08008 urlFromCoreModel(name: string): string;
Matteo Scandolo035c5932016-12-14 09:55:15 -08009 updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any>;
10}
11
12export class StoreHelpers {
Matteo Scandolo04964232017-01-07 12:53:46 -080013 static $inject = ['ModelRest'];
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080014
15 constructor (
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080016 private modelRest: IXosResourceService
17 ) {
18 }
19
Matteo Scandolo04964232017-01-07 12:53:46 -080020 public urlFromCoreModel(name: string): string {
Matteo Scandolo07e2f622017-01-09 10:54:13 -080021 switch (name) {
22 // FIXME handling exceptions, understand why these 3 endpoints are autogenerated with an _f
23 case 'SiteRole':
24 case 'SliceRole':
25 case 'SlicePrivilege':
26 return `/core/${pluralize(name.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join('_'))}`;
27 default:
28 return `/core/${pluralize(name.toLowerCase())}`;
29 }
Matteo Scandolo04964232017-01-07 12:53:46 -080030 }
31
Matteo Scandolo035c5932016-12-14 09:55:15 -080032 public updateCollection(event: IWSEvent, subject: BehaviorSubject<any>): BehaviorSubject<any> {
33 const collection: any[] = subject.value;
34 const index: number = _.findIndex(collection, (i) => {
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080035 // NOTE evaluate to use event.msg.pk
Matteo Scandolo035c5932016-12-14 09:55:15 -080036 return i.id === event.msg.object.id;
37 });
38 const exist: boolean = index > -1;
39 const isDeleted: boolean = _.includes(event.msg.changed_fields, 'deleted');
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080040
41 // generate a resource for the model
Matteo Scandolo04964232017-01-07 12:53:46 -080042 const endpoint = this.urlFromCoreModel(event.model);
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080043 const resource = this.modelRest.getResource(endpoint);
44 const model = new resource(event.msg.object);
45
46 // remove
Matteo Scandolo035c5932016-12-14 09:55:15 -080047 if (exist && isDeleted) {
48 _.remove(collection, {id: event.msg.object.id});
49 }
50 // Replace item at index using native splice
51 else if (exist && !isDeleted) {
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080052 collection.splice(index, 1, model);
Matteo Scandolo035c5932016-12-14 09:55:15 -080053 }
54 // if the element is not deleted add it
55 else if (!exist && !isDeleted) {
Matteo Scandoloaa024ff2017-01-04 15:04:46 -080056 collection.push(model);
Matteo Scandolo035c5932016-12-14 09:55:15 -080057 }
58
59 subject.next(collection);
60
61 return subject;
62 }
63}