import { Injectable } from '@angular/core'; import { ApiProvider } from '../../services/api/api.provider'; import { Subject } from 'rxjs/Subject'; import { Account } from './account'; import { Transaction } from './transaction'; // import { Product } from '../product/product'; import 'rxjs/add/operator/map'; import * as _ from 'lodash'; @Injectable() export class AccountProvider { accounts: Account[]; currentAccount: Account; accountObservable: Subject; accountsObservable: Subject; constructor( public apiProvider: ApiProvider) { this.accounts = []; this.accountObservable = > new Subject(); this.accountsObservable = > new Subject(); } // accountState(result: Account) { this.currentAccount = result; this.accountObservable.next(this.currentAccount); // TODO REMOVE need to check if a account actually exists this.accountsObservable.next(this.accounts); } create(userId: string) { let payload: any = { userId: userId }; let promise = new Promise( (resolve, reject) => { this.apiProvider.api('POST', '/v1/account', payload) .then((response: any) => { if (response.id) { // DO NOT DELETE, we are utilizing update to override the default userId on create // will need them in the future // this.update(tenantId, storeId, userId, response[0]._id).then( (account: Account) => resolve(account)); // comment all below if you use the line above const account = Account.fromJson(response); this.accounts.push(account); this.accountState(account); console.log('resolving account..'); resolve(account); } else { reject('No id in account create response!'); } }, (err) => { // @todo: We should check for errors ?!? reject(err); }); }); return promise; } retrieveAllAccounts() { let payload = {}; let promise = new Promise((resolve, reject) => { if(this.accounts.length === 0){ this.apiProvider.api('GET', '/v2/accounts', payload) .then((response: any) => { if (response) { this.accounts = this.deserializeAccounts(response); this.accountsObservable.next(this.accounts); console.log('resolving'); resolve(this.accounts); // ?? } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); } else resolve(this.accounts); }); return promise; } retrieveAccounts(filter: any) { // TODO: Check if filter is valid let promise = new Promise((resolve, reject) => { if(this.accounts.length === 0){ this.apiProvider.api('GET', '/v2/accounts', filter) .then((response: any) => { if (response) { this.accounts = this.deserializeAccounts(response); this.accountsObservable.next(this.accounts); resolve(this.accounts); // ?? } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); } else resolve(this.accounts); }); return promise; } retrieve(accountId: string) { let payload = {}; let promise = new Promise((resolve, reject) => { this.apiProvider.api('GET', '/v1/account/' + accountId, payload) .then((response: any) => { if (response.id) { const account = Account.fromJson(response); const position = this.accounts.findIndex((item) => { return item.id === account.id; }); this.accounts.splice(position, 1, account); this.accountState(account); resolve(account); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); // } }); return promise; } update(userId: string, accountId: string) { let payload = { 'userId': userId }; let promise = new Promise((resolve, reject) => { this.apiProvider.api('PUT', '/v1/account/' + accountId, payload) .then((response: any) => { if (response.id) { const account = Account.fromJson(response); const position = this.accounts.findIndex((item) => { return item.id === account.id; }); this.accounts.splice(position, 1, account); this.accountState(account); console.log('updateing account...'); resolve(account); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } delete(accountId: string) { let payload = {}; let promise = new Promise((resolve, reject) => { this.apiProvider.api('DELETE', '/v1/account/' + accountId, payload) .then((response: any) => { // if (response._id) { if (response.id) { // this.accounts[response._id] = response; // this.accountState(response); const account = Account.fromJson(response); const position = this.accounts.findIndex((item) => { return item.id === account.id; }); // No third argument in splice => deleting this.accounts.splice(position, 1); this.accountsObservable.next(this.accounts); console.log('deleting'); resolve(this.accounts); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } changeStatus(accountId: string, newStatus: string) { // need to retreive status of account? let payload = {}; let promise = new Promise((resolve, reject) => { this.retrieve(accountId).then( (result: any) => { if (result.id) { // let newStatus: string = (this.currentAccount.status === 'open' ? 'closed' : 'open'); // this.apiProvider.api('PATCH','v1/account/'+accountId+'/status/'+newStatus, payload); this.apiProvider.api('PATCH', `/v1/account/${accountId}/status/${newStatus}`, payload).then((response: any) => { if (response.id) { // this.accounts[response._id] = response; // this.accountState(response); const account = Account.fromJson(response); const position = this.accounts.findIndex((item) => { return item.id === account.id; }); this.accounts.splice(position, 1, account); this.accountState(account); console.log('resoving'); resolve(account); // ?? } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); // then } else { // TODO add error reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } addTransaction(accountId: string, transaction: Transaction) { // let payload = product.toJson(); let payload = transaction.toJson(); let promise = new Promise((resolve, reject) => { this.apiProvider.api('POST', '/v1/account/' + accountId, payload) .then((response: any) => { if (response.id) { const account = Account.fromJson(response); const position = this.accounts.findIndex((item) => { return item.id === account.id; }); this.accounts.splice(position, 1, account); this.accountState(account); resolve(account); } else { reject(); } }, (err) => { // catch error // @todo: We should check for errors ?!? reject(err); }); }); return promise; } retrieveTransaction(accountId: string, transactionId: string) { // http://{{url}}/v1/account/57ad9b365179a63b00b717dc/ksjldhflkdsjfhlds // GET let payload: any = {}; let promise = new Promise( (resolve, reject) => { this.apiProvider.api('GET', `/v1/account/${accountId}/${transactionId}`, payload) .then((response: any) => { // getting 800 error will handle errors when handler is ready if (response.id) { // turn into Product resolve(Transaction.fromJson(response)); } else { reject('Error retrieving accounts'); } }, (err) => { // @todo: We should check for errors ?!? reject(err); }); }); return promise; } deserializeAccounts(incoming: any) { let accounts: Account[] = _.map((_.values(incoming)), Account.fromJson); return accounts; } // End of }