import { Injectable } from '@angular/core'; import {BehaviorSubject, Observable, of} from 'rxjs'; import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import {environment} from '../../environments/environment'; import {first, map} from 'rxjs/operators'; import { Envelope, Syncable } from '../../assets/js/cic-meta/sync.js'; @Injectable({ providedIn: 'root' }) export class UserService { 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) { } resetPin(phone: string): Observable { const params = new HttpParams().set('phoneNumber', phone); return this.http.get(`${environment.cicUssdUrl}/pin`, { params }); } createAccount(accountType: string, idNumber: string, phoneNumber: string, givenName: string, surname: string, directoryEntry: string, location: string, gender: string, referrer: string, businessCategory: string): Observable { console.log(accountType); return this.http.post( `${environment.cicUssdUrl}/user`, { accountType: accountType, idNumber: idNumber, phone: phoneNumber, givenName: givenName, surname: surname, directoryEntry: directoryEntry, location: location, gender: gender, referrer: referrer, businessCategory: businessCategory } ); } changeAccountInfo(address: string, status: string, name: string, phoneNumber: string, type: string, token: string, failedPinAttempts: string, bio: string, gender: string, businessCategory: string, userLocation: string, location: string, referrer: string): Observable { const accountDetails = { status, name, phoneNumber, type, token, failedPinAttempts, bio, gender, businessCategory, userLocation, location, referrer }; const addressStub = address.slice(2); const headers = new HttpHeaders().append('x-cic-automerge', 'client'); this.http.get(`${environment.cicMetaUrl}/${addressStub}`, { headers }).pipe(first()).subscribe(res => { console.log(res); const response = Envelope.fromJSON(res); response.unwrap(); console.log(response); }, error => { console.error('There is an error!', error); }); this.http.put(`${environment.cicMetaUrl}/${addressStub}`, { data: accountDetails }, { headers }).pipe(first()).subscribe(res => { console.log(res); }); return of(addressStub); } 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 }) .pipe(map(response => { return response; })); } }