From 156fa4067f84b5d0f8ae51921c17929a65e6f376 Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Sat, 5 Dec 2020 09:29:18 +0300 Subject: [PATCH] Cleanup services to use mock backend. --- src/app/_services/index.ts | 1 + src/app/_services/location.service.spec.ts | 16 +++ src/app/_services/location.service.ts | 22 ++++ src/app/_services/token.service.ts | 33 +++--- src/app/_services/transaction.service.ts | 34 ++++--- src/app/_services/user.service.ts | 111 +++++++++++---------- 6 files changed, 134 insertions(+), 83 deletions(-) create mode 100644 src/app/_services/location.service.spec.ts create mode 100644 src/app/_services/location.service.ts diff --git a/src/app/_services/index.ts b/src/app/_services/index.ts index 802b6c6..061008e 100644 --- a/src/app/_services/index.ts +++ b/src/app/_services/index.ts @@ -2,3 +2,4 @@ export * from './transaction.service'; export * from './user.service'; export * from './token.service'; export * from './block-sync.service'; +export * from './location.service'; diff --git a/src/app/_services/location.service.spec.ts b/src/app/_services/location.service.spec.ts new file mode 100644 index 0000000..49dca00 --- /dev/null +++ b/src/app/_services/location.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { LocationService } from './location.service'; + +describe('LocationService', () => { + let service: LocationService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(LocationService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/_services/location.service.ts b/src/app/_services/location.service.ts new file mode 100644 index 0000000..ca2567f --- /dev/null +++ b/src/app/_services/location.service.ts @@ -0,0 +1,22 @@ +import { Injectable } from '@angular/core'; +import {BehaviorSubject} from 'rxjs'; +import {HttpClient} from '@angular/common/http'; +import {environment} from '../../environments/environment'; +import {first} from 'rxjs/operators'; + +@Injectable({ + providedIn: 'root' +}) +export class LocationService { + locations: any = ''; + private locationsList = new BehaviorSubject(this.locations); + locationsSubject = this.locationsList.asObservable(); + + constructor( + private http: HttpClient + ) { } + + getLocations(): void { + this.http.get(`${environment.cicCacheUrl}/locations`).pipe(first()).subscribe(locations => this.locationsList.next(locations)); + } +} diff --git a/src/app/_services/token.service.ts b/src/app/_services/token.service.ts index 50c66de..657eac8 100644 --- a/src/app/_services/token.service.ts +++ b/src/app/_services/token.service.ts @@ -1,27 +1,26 @@ import { Injectable } from '@angular/core'; +import {HttpClient} from '@angular/common/http'; +import {environment} from '../../environments/environment'; +import {first} from 'rxjs/operators'; +import {BehaviorSubject} from 'rxjs'; @Injectable({ providedIn: 'root' }) export class TokenService { - data = [ - { name: 'Reserve', symbol: 'RSV' }, - { name: 'Bert', symbol: 'BRT' }, - { name: 'Ernie', symbol: 'ERN' }, - { name: 'Reserve', symbol: 'RSV' }, - { name: 'Bert', symbol: 'BRT' }, - { name: 'Ernie', symbol: 'ERN' }, - { name: 'Reserve', symbol: 'RSV' }, - { name: 'Bert', symbol: 'BRT' }, - { name: 'Ernie', symbol: 'ERN' }, - { name: 'Reserve', symbol: 'RSV' }, - { name: 'Bert', symbol: 'BRT' }, - { name: 'Ernie', symbol: 'ERN' }, - ]; + tokens: any = ''; + private tokensList = new BehaviorSubject(this.tokens); + tokensSubject = this.tokensList.asObservable(); - constructor() { } + constructor( + private http: HttpClient + ) { } - getBySymbol(symbol: string): any { - return this.data.find(token => token.symbol === symbol); + getTokens(): any { + this.http.get(`${environment.cicCacheUrl}/tokens`).pipe(first()).subscribe(tokens => this.tokensList.next(tokens)); + } + + getTokenBySymbol(symbol: string): any { + return this.http.get(`${environment.cicCacheUrl}/tokens/${symbol}`); } } diff --git a/src/app/_services/transaction.service.ts b/src/app/_services/transaction.service.ts index bf027ca..9abc86b 100644 --- a/src/app/_services/transaction.service.ts +++ b/src/app/_services/transaction.service.ts @@ -3,7 +3,6 @@ import {HttpClient} from '@angular/common/http'; import {first, map} from 'rxjs/operators'; import {BehaviorSubject, Observable} from 'rxjs'; import {environment} from '../../environments/environment'; -import {Conversion, Transaction} from '../_models'; import {User} from 'cic-client-meta'; import {UserService} from './user.service'; import {parse} from '../../assets/js/parse-vcard'; @@ -12,12 +11,9 @@ import {parse} from '../../assets/js/parse-vcard'; providedIn: 'root' }) export class TransactionService { - transactions: Transaction[] = []; - conversions: Conversion[] = []; - private transactionList = new BehaviorSubject(this.transactions); + transactions: any[] = []; + private transactionList = new BehaviorSubject(this.transactions); transactionsSubject = this.transactionList.asObservable(); - private conversionList = new BehaviorSubject(this.conversions); - conversionsSubject = this.conversionList.asObservable(); userInfo: any; constructor( @@ -42,29 +38,35 @@ export class TransactionService { setTransaction(transaction, cacheSize: number): void { const cachedTransaction = this.transactions.find(cachedTx => cachedTx.tx.txHash === transaction.tx.txHash); if (cachedTransaction) { return; } + transaction.type = 'transaction'; this.getUser(transaction.from).then(() => { transaction.sender = this.userInfo; this.getUser(transaction.to).then(() => { transaction.recipient = this.userInfo; - this.transactions.unshift(transaction); - if (this.transactions.length > cacheSize) { - this.transactions.length = cacheSize; - } - this.transactionList.next(this.transactions); + this.addTransaction(transaction, cacheSize); }); }); } - setConversion(conversion): void { - const cachedConversion = this.conversions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash); + setConversion(conversion, cacheSize): void { + const cachedConversion = this.transactions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash); if (cachedConversion) { return; } + conversion.type = 'conversion'; this.getUser(conversion.trader).then(() => { - conversion.user = this.userInfo; - this.conversions.push(conversion); - this.conversionList.next(this.conversions); + conversion.sender = this.userInfo; + conversion.recipient = this.userInfo; + this.addTransaction(conversion, cacheSize); }); } + addTransaction(transaction, cacheSize: number): void { + this.transactions.unshift(transaction); + if (this.transactions.length > cacheSize) { + this.transactions.length = cacheSize; + } + this.transactionList.next(this.transactions); + } + async getUser(address: string): Promise { this.userService.getUser(await User.toKey(address)).pipe(first()).subscribe(res => { const vcard = parse(atob(res.vcard)); diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index c55b924..f73594b 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -1,46 +1,75 @@ import { Injectable } from '@angular/core'; -import {Observable} from 'rxjs'; +import {BehaviorSubject, Observable} from 'rxjs'; import {HttpClient, HttpParams} from '@angular/common/http'; import {environment} from '../../environments/environment'; -import {map} from 'rxjs/operators'; +import {first, map} from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class UserService { - users = [ - {id: 1, name: 'John Doe', phone: '+25412345678', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'user', created: '08/16/2020', balance: '12987', failedPinAttempts: 1, status: 'approved', bio: 'Bodaboda', gender: 'male'}, - {id: 2, name: 'Jane Buck', phone: '+25412341234', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'vendor', created: '04/02/2020', balance: '56281', failedPinAttempts: 0, status: 'approved', bio: 'Groceries', gender: 'female'}, - {id: 3, name: 'Mc Donald', phone: '+25498765432', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'group', created: '11/16/2020', balance: '450', failedPinAttempts: 2, status: 'unapproved', bio: 'Food', gender: 'male'}, - {id: 4, name: 'Hera Cles', phone: '+25498769876', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'user', created: '05/28/2020', balance: '5621', failedPinAttempts: 3, status: 'approved', bio: 'Shop', gender: 'female'}, - {id: 5, name: 'Silver Fia', phone: '+25462518374', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'token agent', created: '10/10/2020', balance: '817', failedPinAttempts: 0, status: 'unapproved', bio: 'Electronics', gender: 'male'}, - {id: 1, name: 'John Doe', phone: '+25412345678', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'user', created: '08/16/2020', balance: '12987', failedPinAttempts: 1, status: 'approved', bio: 'Bodaboda', gender: 'male'}, - {id: 2, name: 'Jane Buck', phone: '+25412341234', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'vendor', created: '04/02/2020', balance: '56281', failedPinAttempts: 0, status: 'approved', bio: 'Groceries', gender: 'female'}, - {id: 3, name: 'Mc Donald', phone: '+25498765432', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'group', created: '11/16/2020', balance: '450', failedPinAttempts: 2, status: 'unapproved', bio: 'Food', gender: 'male'}, - {id: 4, name: 'Hera Cles', phone: '+25498769876', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'user', created: '05/28/2020', balance: '5621', failedPinAttempts: 3, status: 'approved', bio: 'Shop', gender: 'female'}, - {id: 5, name: 'Silver Fia', phone: '+25462518374', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'token agent', created: '10/10/2020', balance: '817', failedPinAttempts: 0, status: 'unapproved', bio: 'Electronics', gender: 'male'}, - {id: 1, name: 'John Doe', phone: '+25412345678', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'user', created: '08/16/2020', balance: '12987', failedPinAttempts: 1, status: 'approved', bio: 'Bodaboda', gender: 'male'}, - {id: 2, name: 'Jane Buck', phone: '+25412341234', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'vendor', created: '04/02/2020', balance: '56281', failedPinAttempts: 0, status: 'approved', bio: 'Groceries', gender: 'female'}, - {id: 3, name: 'Mc Donald', phone: '+25498765432', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'group', created: '11/16/2020', balance: '450', failedPinAttempts: 2, status: 'unapproved', bio: 'Food', gender: 'male'}, - {id: 4, name: 'Hera Cles', phone: '+25498769876', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'user', created: '05/28/2020', balance: '5621', failedPinAttempts: 3, status: 'approved', bio: 'Shop', gender: 'female'}, - {id: 5, name: 'Silver Fia', phone: '+25462518374', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'token agent', created: '10/10/2020', balance: '817', failedPinAttempts: 0, status: 'unapproved', bio: 'Electronics', gender: 'male'}, - {id: 1, name: 'John Doe', phone: '+25412345678', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'user', created: '08/16/2020', balance: '12987', failedPinAttempts: 1, status: 'approved', bio: 'Bodaboda', gender: 'male'}, - {id: 2, name: 'Jane Buck', phone: '+25412341234', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'vendor', created: '04/02/2020', balance: '56281', failedPinAttempts: 0, status: 'approved', bio: 'Groceries', gender: 'female'}, - {id: 3, name: 'Mc Donald', phone: '+25498765432', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'group', created: '11/16/2020', balance: '450', failedPinAttempts: 2, status: 'unapproved', bio: 'Food', gender: 'male'}, - {id: 4, name: 'Hera Cles', phone: '+25498769876', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'user', created: '05/28/2020', balance: '5621', failedPinAttempts: 3, status: 'approved', bio: 'Shop', gender: 'female'}, - {id: 5, name: 'Silver Fia', phone: '+25462518374', address: '0xc86ff893ac40d3950b4d5f94a9b837258b0a9865', type: 'token agent', created: '10/10/2020', balance: '817', failedPinAttempts: 0, status: 'unapproved', bio: 'Electronics', gender: 'male'}, - ]; - actions = [ - { id: 1, user: 'Tom', role: 'enroller', action: 'Disburse RSV 100', approval: false }, - { id: 2, user: 'Christine', role: 'admin', action: 'Change user phone number', approval: true }, - { id: 3, user: 'Will', role: 'superadmin', action: 'Reclaim RSV 1000', approval: true }, - { id: 4, user: 'Vivian', role: 'enroller', action: 'Complete user profile', approval: true }, - { id: 5, user: 'Jack', role: 'enroller', action: 'Reclaim RSV 200', approval: false }, - { id: 6, user: 'Patience', role: 'enroller', action: 'Change user information', approval: false } - ]; + accounts: any = ''; + private accountsList = new BehaviorSubject(this.accounts); + accountsSubject = this.accountsList.asObservable(); + + actions: any = ''; + private actionsList = new BehaviorSubject(this.actions); + actionsSubject = this.actionsList.asObservable(); + + staff: any = ''; + private staffList = new BehaviorSubject(this.staff); + staffSubject = this.staffList.asObservable(); constructor(private http: HttpClient) { } + getAccounts(): void { + this.http.get(`${environment.cicCacheUrl}/accounts`).pipe(first()).subscribe(accounts => this.accountsList.next(accounts)); + } + + getAccountById(id: number): Observable { + return this.http.get(`${environment.cicCacheUrl}/accounts/${id}`); + } + + getActions(): void { + this.http.get(`${environment.cicCacheUrl}/actions`).pipe(first()).subscribe(actions => this.actionsList.next(actions)); + } + + getActionById(id: string): any { + return this.http.get(`${environment.cicCacheUrl}/actions/${id}`); + } + + approveAction(id: string): Observable { + return this.http.post(`${environment.cicCacheUrl}/actions/${id}`, { approval: true }); + } + + revokeAction(id: string): Observable { + return this.http.post(`${environment.cicCacheUrl}/actions/${id}`, { approval: false }); + } + + getHistoryByUser(id: string): Observable { + return this.http.get(`${environment.cicCacheUrl}/history/${id}`); + } + + getStaff(): void { + this.http.get(`${environment.cicCacheUrl}/staff`).pipe(first()).subscribe(staff => this.staffList.next(staff)); + } + + getStaffById(id: string): Observable { + return this.http.get(`${environment.cicCacheUrl}/staff/${id}`); + } + + activateStaff(id: string): Observable { + return this.http.post(`${environment.cicCacheUrl}/staff/${id}`, {status: 'activated'}); + } + + deactivateStaff(id: string): Observable { + return this.http.post(`${environment.cicCacheUrl}/staff/${id}`, {status: 'deactivated'}); + } + + changeStaffType(id: string, type: string): Observable { + return this.http.post(`${environment.cicCacheUrl}/staff/${id}`, {accountType: type}); + } + getUser(userKey: string): Observable { const params = new HttpParams().set('userKey', '0970c6e9cdad650ba9006e5a1caf090a13da312792389a147263c98ac78cd037'); return this.http.get(`${environment.cicScriptsUrl}`, { params }) @@ -48,22 +77,4 @@ export class UserService { return response; })); } - - getUserById(id: string): any { - return this.users.find(user => user.id === parseInt(id, 10)); - } - - getActionById(id: string): any { - return this.actions.find(action => action.id === parseInt(id, 10)); - } - - approveAction(id: string): void { - const action = this.actions.find(queriedAction => queriedAction.id === parseInt(id, 10)); - action.approval = true; - } - - revokeAction(id: string): void { - const action = this.actions.find(queriedAction => queriedAction.id === parseInt(id, 10)); - action.approval = false; - } }