import { Injectable } from '@angular/core'; import {BehaviorSubject, Observable} from 'rxjs'; import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import {environment} from '@src/environments/environment'; import {first, map} from 'rxjs/operators'; import { ArgPair, Envelope, Syncable } from '@src/assets/js/cic-meta/sync.js'; import {MutableKeyStore, MutablePgpKeyStore, PGPSigner, Signer} from '@app/_helpers'; @Injectable({ providedIn: 'root' }) export class UserService { keystore: MutableKeyStore = new MutablePgpKeyStore(); syncableAccount: Syncable; signer: Signer = new PGPSigner(this.keystore); 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 } ); } async 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): Promise { const accountDetails = { status, name, phoneNumber, type, token, failedPinAttempts, bio, gender, businessCategory, userLocation, location, referrer }; let addressStub = address.slice(2); addressStub = '0000000000000000000000000000000000000000000000000000000000000000'; const headers = new HttpHeaders({'x-cic-automerge': 'client'}); this.http.get(`${environment.cicMetaUrl}/${addressStub}`, { headers }).pipe(first()).subscribe(async res => { this.syncableAccount = Envelope.fromJSON(res).unwrap(); let update = []; for (const prop in accountDetails) { update.push(new ArgPair(prop, accountDetails[prop])); } this.syncableAccount.update(update, 'client-branch'); await this.updateMeta(addressStub, headers); }, async error => { console.error('There is an error!', error); const refName = addressStub + ':cic-person'; this.syncableAccount = new Syncable(refName, accountDetails); await this.updateMeta(addressStub, headers); }); return addressStub; } async updateMeta(addressStub: string, headers: HttpHeaders): Promise { const envelope = await this.wrap(this.syncableAccount , this.signer); const reqBody = envelope.toJSON(); this.http.put(`${environment.cicMetaUrl}/${addressStub}`, { data: reqBody }, { headers }).pipe(first()).subscribe(res => { console.log(res); }); } 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; })); } wrap(syncable: Syncable, signer: Signer): Promise { return new Promise((whohoo, doh) => { syncable.setSigner(signer); syncable.onwrap = async (env) => { if (env === undefined) { doh(); return; } whohoo(env); }; syncable.sign(); }); } }