import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; import { ApiProvider } from '../api/api.provider'; import { Subject } from 'rxjs/Subject'; import * as _ from 'lodash'; import { Customer } from './client'; @Injectable() export class ClientProvider { clients: Customer[]; allClients: Customer[]; clientsObservable: Subject; clientObservable: Subject; instore: Customer[]; instoreObservable: Subject; isManager: boolean = false; constructor(public http: Http , public api: ApiProvider) { this.clients = []; this.clientsObservable = > new Subject(); this.instore = []; this.instoreObservable = > new Subject(); this.clientObservable = > new Subject(); } setManagerStatus(rank: boolean) { this.isManager = rank; } getManagerStatus() { return this.isManager; } // todo handle when none is in store getCustomersInStore (tenantId: string, storeId: string) { let payload: any = {}; // if (storeId) { // payload = { // store_id: storeId // }; // } let promise: any = new Promise( (resolve, reject) => { if (tenantId) { // payload.tenant_id = tenantId; this.api.api('GET', `/v1/customers/instore/${storeId}`, payload) .then((response) => { resolve(this.deserializeCustomers(response)); }, (response) => { if(response === 400){ resolve ([]); } else reject(response); }); } else { reject(); } }); return promise; } getCustomers(tenantId: string, storeId: string) { let payload: any; if (storeId) { payload = { storeId: storeId }; } let promise: any = new Promise( (resolve, reject) => { if (tenantId) { payload.tenantId = tenantId; this.api.api('GET', '/v1/customers/', payload) .then((response) => { resolve(this.deserializeCustomers(response)); }, (response) => { reject(response); }); } else { reject(); } }); return promise; } loadClients(tenantId: string, storeId: string) { return new Promise( (resolve) => { // if(_.isUndefined(this.clients)){ if (this.clients.length === 0){ this.getCustomers(tenantId, storeId).then( (result: any) => { this.clients = result; this.clientsObservable.next(this.clients); resolve(this.clients); }); } else { this.clientsObservable.next(this.clients); resolve(this.clients); } }); } loadClientsInStore(tenantId: string, storeId: string) { return new Promise( (resolve) => { this.getCustomersInStore(tenantId, storeId).then( (result: any) => { this.instore = result; this.instoreObservable.next(this.instore); resolve(this.instore); }); }); } getCustomersDetails(tenantId: string, storeId: string, userId: string, customerId?: string) { let payload: any ={}; if (storeId) { payload = { storeId: storeId, userId: userId }; } if (_.isUndefined(customerId)) { customerId = ''; } let promise: any = new Promise( (resolve, reject) => { if (tenantId) { payload.tenantId = tenantId; this.api.api('GET', `/v1/customer/${customerId}`, payload) .then((response: any[]) => { // resolve(Customer.fromJson(response[0])); const customer = Customer.fromJson(response); const position = this.clients.findIndex((item) => { return item.id === customer.id; }); this.clients.splice(position, 1, customer); this.clientObservable.next(customer); this.clientsObservable.next(this.clients); // resolve(Customer.fromJson(response)); }, (response) => { reject(response); }); } else { reject(); } }); return promise; } // createClient(tenantId: string, storeId: string, fname: string, lname: string) { // this.createCustomer(tenantId, storeId, fname, lname).then( this.loadClients(tenantId, storeId)); // } createCustomer(tenantId: string, storeId: string, fname: string, lname: string, social: any, addresses: any, telephones:any, badges:any) { let payload: any; if (storeId) { payload = { storeId: storeId }; } payload.firstName = fname; payload.lastName = lname; payload.social = social; payload.addresses = addresses; payload.telephones = telephones; payload.badges = badges; let promise: any = new Promise( (resolve, reject) => { if (tenantId) { payload.tenantId = tenantId; this.api.api('POST', '/v1/customer', payload) .then((response) => { const customer = Customer.fromJson(response); // const position = this.clients.findIndex((item) => { return item.id === customer.id; }); // this.clients.splice(position, 1, customer); // this.cartState(cart); this.clients.push(customer); this.clientsObservable.next(this.clients); this.clientObservable.next(customer); resolve(customer); }, (response) => { reject(response); }); } else { reject(); } }); return promise; } updateCustomer(customerUpdate: Customer){ let payload = customerUpdate.toJson(); return new Promise( (resolve,reject) =>{ this.api.api('PUT', `/v1/customer/${customerUpdate.getId()}`, payload) .then((response) =>{ const customer = Customer.fromJson(response); const position = this.clients.findIndex((item) => { return item.id === customer.id; }); this.clients.splice(position, 1, customer); // this.cartState(cart); this.clientsObservable.next(this.clients); this.clientObservable.next(customer); resolve(customer); },(response) => { reject(response); }); }); } deserializeCustomers(incoming: any) { let customers: Customer[] = []; // same parern as carts, customers = {} // swap with lodash _.map((_.values(obj.items)), function); for (let att in incoming) { let customer = incoming[att]; customers.push(new Customer( customer.tenantId, customer.firstName, customer.lastName, customer.avatar, customer.gender, customer.id, customer.social, customer.badges, customer.addresses, customer.telephones, customer.storeId, customer.userId )); } return customers; // return customers; } testCustomer() { console.log(this.deserializeCustomers(this.clients)); } // DON'T KEEP // post('/v1/customer/checkin/?userId=') checkIn(tenantId: string, storeId: string, userId: string , inStore: string, customerId?: string) { let payload: any; if (storeId) { payload = { storeId: storeId, inStore: inStore, userId: userId, // customerId: customerId }; } if (_.isUndefined(customerId)) { customerId = ''; } let promise: any = new Promise( (resolve, reject) => { if (tenantId) { payload.tenantId = tenantId; this.api.api('POST', `/v1/customer/checkin/${customerId}`, payload) .then((response: Customer[]) => { // trying observable const customer = Customer.fromJson(response); const position = this.clients.findIndex((item) => { return item.id === customer.id; }); this.clients.splice(position, 1, customer); this.clientObservable.next(customer); this.clientsObservable.next(this.clients); // resolve(Customer.fromJson(response)); }, (response) => { reject(response); }); } else { reject(); } }); return promise; } engaged(tenantId: string, storeId: string, userId: string, touchpointId: string, inStore?: string, customerId?: string) { let payload: any; if (storeId) { payload = { storeId: storeId, userId: userId, engaged: touchpointId, inStore: inStore // customerId: customerId }; } if (_.isUndefined(customerId)) { customerId = ''; } if(_.isUndefined(customerId)){ inStore = ''; } let promise: any = new Promise( (resolve, reject) => { if (tenantId) { payload.tenantId = tenantId; this.api.api('POST', `/v1/customer/engaged/${customerId}`, payload) .then((response) => { // trying observable const customer = Customer.fromJson(response); const position = this.clients.findIndex((item) => { return item.id === customer.id; }); this.clients.splice(position, 1, customer); this.clientObservable.next(customer); this.clientsObservable.next(this.clients); // resolve(customer); }, (response) => { reject(response); }); } else { reject(); } }); return promise; } }