Cleanup services to use mock backend.

This commit is contained in:
Spencer Ofwiti 2020-12-05 09:29:18 +03:00
parent bff58f7a43
commit 156fa4067f
6 changed files with 134 additions and 83 deletions

View File

@ -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';

View File

@ -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();
});
});

View File

@ -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<any>(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));
}
}

View File

@ -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<any>(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}`);
}
}

View File

@ -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<Transaction[]>(this.transactions);
transactions: any[] = [];
private transactionList = new BehaviorSubject<any[]>(this.transactions);
transactionsSubject = this.transactionList.asObservable();
private conversionList = new BehaviorSubject<Conversion[]>(this.conversions);
conversionsSubject = this.conversionList.asObservable();
userInfo: any;
constructor(
@ -42,27 +38,33 @@ 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.addTransaction(transaction, cacheSize);
});
});
}
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.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);
});
});
}
setConversion(conversion): void {
const cachedConversion = this.conversions.find(cachedTx => cachedTx.tx.txHash === conversion.tx.txHash);
if (cachedConversion) { return; }
this.getUser(conversion.trader).then(() => {
conversion.user = this.userInfo;
this.conversions.push(conversion);
this.conversionList.next(this.conversions);
});
}
async getUser(address: string): Promise<void> {

View File

@ -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<any>(this.accounts);
accountsSubject = this.accountsList.asObservable();
actions: any = '';
private actionsList = new BehaviorSubject<any>(this.actions);
actionsSubject = this.actionsList.asObservable();
staff: any = '';
private staffList = new BehaviorSubject<any>(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<any> {
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<any> {
return this.http.post(`${environment.cicCacheUrl}/actions/${id}`, { approval: true });
}
revokeAction(id: string): Observable<any> {
return this.http.post(`${environment.cicCacheUrl}/actions/${id}`, { approval: false });
}
getHistoryByUser(id: string): Observable<any> {
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<any> {
return this.http.get(`${environment.cicCacheUrl}/staff/${id}`);
}
activateStaff(id: string): Observable<any> {
return this.http.post(`${environment.cicCacheUrl}/staff/${id}`, {status: 'activated'});
}
deactivateStaff(id: string): Observable<any> {
return this.http.post(`${environment.cicCacheUrl}/staff/${id}`, {status: 'deactivated'});
}
changeStaffType(id: string, type: string): Observable<any> {
return this.http.post(`${environment.cicCacheUrl}/staff/${id}`, {accountType: type});
}
getUser(userKey: string): Observable<any> {
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;
}
}