import { Injectable } from '@angular/core'; import { ApiProvider } from '../../services/api/api.provider'; import { Touchpoint } from './touchpoint'; import 'rxjs/add/operator/map'; import { Subject } from 'rxjs/Subject'; import * as _ from 'lodash'; // touchpoint holder // current touchpoint // observable? // Generated class for the Touchpoint provider. // See https://angular.io/docs/ts/latest/guide/dependency-injection.html // for more info on providers and Angular 2 DI. @Injectable() export class TouchpointProvider { touchpoints: Touchpoint[]; currentTouchpoint: Touchpoint; touchpointObservable: Subject; touchpointsObservable: Subject; constructor( public apiProvider: ApiProvider) { this.touchpoints = []; this.touchpointObservable = > new Subject(); this.touchpointsObservable = > new Subject(); } create(touchpointParameters: any) { // TODO REMOVE let payload = touchpointParameters; let promise = new Promise((resolve, reject) => { this.apiProvider.api('POST', '/v1/touchpoint' , payload) .then((response: any) => { if (response.id) { // insert or replace in Set this.currentTouchpoint = Touchpoint.fromJson(response); this.touchpoints.push(this.currentTouchpoint); this.touchpointState(this.currentTouchpoint); resolve(this.currentTouchpoint); // ?? } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } retrieveDetails( tenantId: string, touchpointId: string) { let payload: any = {}; payload.tenantId = tenantId; let promise = new Promise((resolve, reject) => { this.apiProvider.api('GET', `/v1/touchpoint/${touchpointId}`, payload) .then((response: any) => { if (response.id) { // this.touchpointState(Touchpoint.fromJson(response[0])); const touchpoint = Touchpoint.fromJson(response); const position = this.touchpoints.findIndex((item) => { return item.id === touchpoint.id; }); this.touchpoints.splice(position, 1, touchpoint); this.touchpointState(touchpoint); // resolve(this.currentTouchpoint); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } // http://dev.omnixell.com:8000/v2/v1/touchpoints?tenantId=56ea50cb8f535eecb185917f&storeId=57a9594774b4839936128133 // tenantId=57b54045d4c62844f361a890&storeId=57b6706bc2d8c14c7274826d retrieveTouchpoints( tenantId: string, storeId: string) { // loading the parameters let payload: any = {}; if(tenantId) payload.tenantId = tenantId; if(storeId) payload.storeId = storeId; let promise = new Promise((resolve, reject) => { this.apiProvider.api('GET', '/v1/touchpoints' , payload) .then( (response: any) => { // this.touchpoints = Touchpoint.fromJson(response); // this.touchpoints = Touchpoint.fromJson(response); // CHECK LODASH turn to "next-stage style" this.touchpoints = _.map((_.values(response)), Touchpoint.fromJson); this.touchpointsObservable.next(this.touchpoints); resolve(this.touchpoints); // ?? }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } /* create an object including only the params you want to update * e.g. obj.name, obj. description */ update(touchpointId: string, obj: any){ let payload = obj; let promise = new Promise((resolve, reject) => { this.apiProvider.api('PUT', '/v1/touchpoint/' + touchpointId, obj) .then((response: any) => { // if (response._id) { if (response.id) { // this.carts[response._id] = response; // this.cartState(response); const touchpoint = Touchpoint.fromJson(response); const position = this.touchpoints.findIndex((item) => { return item.id === touchpoint.id; }); this.touchpoints.splice(position, 1, touchpoint); this.touchpointState(touchpoint); resolve(this.currentTouchpoint); // this.cartState(cart); // console.log('deleting'); // resolve(cart); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } delete (touchpointId: string){ let payload: any = { }; let promise = new Promise((resolve, reject) => { this.apiProvider.api('DELETE', '/v1/touchpoint/' + touchpointId, payload) .then((response: any) => { // if (response._id) { if (response.id) { // this.carts[response._id] = response; // this.cartState(response); const touchpoint = Touchpoint.fromJson(response); const position = this.touchpoints.findIndex((item) => { return item.id === touchpoint.id; }); this.touchpoints.splice(position, 1); this.touchpointState(touchpoint) // this.touchpointObservable.next(touchpoint); // this.touchpointsObservable.next(this.touchpoints); resolve(touchpoint); // this.cartState(cart); // console.log('deleting'); // resolve(cart); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } addUser(touchpointId: string, userId: string) { let payload: any = {}; let promise = new Promise((resolve, reject) => { this.apiProvider.api('PUT', `/v1/touchpoint/${touchpointId}/user/${userId}`, payload) .then((response: any) => { // if (response._id) { if (response.id) { // this.carts[response._id] = response; // this.cartState(response); const touchpoint = Touchpoint.fromJson(response); const position = this.touchpoints.findIndex((item) => { return item.id === touchpoint.id; }); this.touchpoints.splice(position, 1, touchpoint); this.touchpointObservable.next(touchpoint); this.touchpointsObservable.next(this.touchpoints); resolve(touchpoint); // this.cartState(cart); // console.log('deleting'); // resolve(cart); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } removeUser(touchpointId: string, userId: string){ let payload: any = {}; let promise = new Promise((resolve, reject) => { this.apiProvider.api('DELETE', `/v1/touchpoint/${touchpointId}/user/${userId}`, payload) .then((response: any) => { // if (response._id) { if (response.id) { // this.carts[response._id] = response; // this.cartState(response); const touchpoint = Touchpoint.fromJson(response); const position = this.touchpoints.findIndex((item) => { return item.id === touchpoint.id; }); this.touchpoints.splice(position, 1, touchpoint); this.touchpointObservable.next(touchpoint); this.touchpointsObservable.next(this.touchpoints); resolve(touchpoint); // this.cartState(cart); // console.log('deleting'); // resolve(cart); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } // pending insert or replace touchpointState(result: Touchpoint) { this.currentTouchpoint = result; // this.touchpoints.push(this.currentTouchpoint); this.touchpointObservable.next(this.currentTouchpoint); this.touchpointsObservable.next(this.touchpoints); } }