2021-02-23 13:32:02 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2021-04-25 12:32:23 +02:00
|
|
|
import {BehaviorSubject, Observable, Subject} from 'rxjs';
|
2021-02-16 09:04:22 +01:00
|
|
|
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
|
2021-02-17 13:49:04 +01:00
|
|
|
import {environment} from '@src/environments/environment';
|
2021-02-17 19:22:06 +01:00
|
|
|
import {first} from 'rxjs/operators';
|
2021-04-08 14:16:00 +02:00
|
|
|
import {ArgPair, Envelope, Phone, Syncable, User} from 'cic-client-meta';
|
2021-04-25 12:32:23 +02:00
|
|
|
import {AccountDetails} from '@app/_models';
|
2021-03-14 09:19:25 +01:00
|
|
|
import {LoggingService} from '@app/_services/logging.service';
|
2021-03-16 11:08:18 +01:00
|
|
|
import {TokenService} from '@app/_services/token.service';
|
2021-04-20 10:28:40 +02:00
|
|
|
import {AccountIndex} from '@app/_eth';
|
2021-04-25 12:32:23 +02:00
|
|
|
import {MutableKeyStore, PGPSigner, Signer} from '@app/_pgp';
|
2021-04-20 10:28:40 +02:00
|
|
|
import {RegistryService} from '@app/_services/registry.service';
|
|
|
|
import {CICRegistry} from 'cic-client';
|
2021-04-25 12:32:23 +02:00
|
|
|
import {AuthService} from './auth.service';
|
2021-03-02 08:29:14 +01:00
|
|
|
const vCard = require('vcard-parser');
|
2020-11-25 08:51:15 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class UserService {
|
2021-02-23 13:32:02 +01:00
|
|
|
headers: HttpHeaders = new HttpHeaders({'x-cic-automerge': 'client'});
|
2021-04-25 12:32:23 +02:00
|
|
|
keystore: MutableKeyStore;
|
|
|
|
signer: Signer;
|
2021-04-20 10:28:40 +02:00
|
|
|
registry: CICRegistry;
|
2021-02-17 11:00:38 +01:00
|
|
|
|
2021-03-02 08:29:14 +01:00
|
|
|
accountsMeta = [];
|
2021-03-01 09:12:49 +01:00
|
|
|
accounts: any = [];
|
2020-12-05 07:29:18 +01:00
|
|
|
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();
|
2020-11-25 08:51:15 +01:00
|
|
|
|
2021-03-14 09:19:25 +01:00
|
|
|
constructor(
|
2021-03-21 12:02:18 +01:00
|
|
|
private httpClient: HttpClient,
|
2021-03-16 11:08:18 +01:00
|
|
|
private loggingService: LoggingService,
|
2021-04-20 10:28:40 +02:00
|
|
|
private tokenService: TokenService,
|
|
|
|
private registryService: RegistryService,
|
2021-04-25 12:32:23 +02:00
|
|
|
private authService: AuthService,
|
2021-03-14 09:19:25 +01:00
|
|
|
) {
|
2021-04-25 12:32:23 +02:00
|
|
|
this.authService.init().then(() => {
|
|
|
|
this.keystore = authService.mutableKeyStore;
|
|
|
|
this.signer = new PGPSigner(this.keystore);
|
|
|
|
});
|
2021-04-20 10:28:40 +02:00
|
|
|
this.registry = registryService.getRegistry();
|
|
|
|
this.registry.load();
|
2021-03-06 07:28:29 +01:00
|
|
|
}
|
2020-11-25 08:51:15 +01:00
|
|
|
|
2021-02-08 12:47:07 +01:00
|
|
|
resetPin(phone: string): Observable<any> {
|
|
|
|
const params = new HttpParams().set('phoneNumber', phone);
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.get(`${environment.cicUssdUrl}/pin`, {params});
|
2021-03-14 09:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getAccountStatus(phone: string): any {
|
|
|
|
const params = new HttpParams().set('phoneNumber', phone);
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.get(`${environment.cicUssdUrl}/pin`, {params});
|
2021-03-14 09:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getLockedAccounts(offset: number, limit: number): any {
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.get(`${environment.cicUssdUrl}/accounts/locked/${offset}/${limit}`);
|
2021-03-14 09:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async changeAccountInfo(address: string, name: string, phoneNumber: string, age: string, type: string, bio: string, gender: string,
|
2021-04-25 12:32:23 +02:00
|
|
|
businessCategory: string, userLocation: string, location: string, locationType: string,
|
2021-03-14 09:19:25 +01:00
|
|
|
): Promise<any> {
|
2021-04-25 12:32:23 +02:00
|
|
|
let accountInfo: any = {
|
|
|
|
vcard: {
|
|
|
|
fn: [{}],
|
|
|
|
n: [{}],
|
|
|
|
tel: [{}]
|
|
|
|
},
|
|
|
|
location: {}
|
|
|
|
};
|
2021-03-14 09:19:25 +01:00
|
|
|
accountInfo.vcard.fn[0].value = name;
|
|
|
|
accountInfo.vcard.n[0].value = name.split(' ');
|
|
|
|
accountInfo.vcard.tel[0].value = phoneNumber;
|
|
|
|
accountInfo.products = [bio];
|
|
|
|
accountInfo.gender = gender;
|
|
|
|
accountInfo.age = age;
|
|
|
|
accountInfo.type = type;
|
|
|
|
accountInfo.category = businessCategory;
|
|
|
|
accountInfo.location.area = location;
|
|
|
|
accountInfo.location.area_name = userLocation;
|
|
|
|
accountInfo.location.area_type = locationType;
|
2021-04-25 12:32:23 +02:00
|
|
|
accountInfo.vcard = btoa(vCard.generate(accountInfo.vcard));
|
2021-02-17 19:22:06 +01:00
|
|
|
const accountKey = await User.toKey(address);
|
2021-04-25 12:32:23 +02:00
|
|
|
this.getAccountDetailsFromMeta(accountKey).pipe(first()).subscribe(async res => {
|
2021-03-21 12:02:18 +01:00
|
|
|
const syncableAccount: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap();
|
2021-02-17 11:00:38 +01:00
|
|
|
let update = [];
|
2021-04-25 12:32:23 +02:00
|
|
|
for (const prop in accountInfo) {
|
|
|
|
update.push(new ArgPair(prop, accountInfo[prop]));
|
2021-02-17 11:00:38 +01:00
|
|
|
}
|
2021-02-23 13:32:02 +01:00
|
|
|
syncableAccount.update(update, 'client-branch');
|
|
|
|
await this.updateMeta(syncableAccount, accountKey, this.headers);
|
2021-02-17 11:00:38 +01:00
|
|
|
}, async error => {
|
2021-03-14 09:19:25 +01:00
|
|
|
this.loggingService.sendErrorLevelMessage('Can\'t find account info in meta service', this, {error});
|
|
|
|
const syncableAccount: Syncable = new Syncable(accountKey, accountInfo);
|
2021-02-23 13:32:02 +01:00
|
|
|
await this.updateMeta(syncableAccount, accountKey, this.headers);
|
2021-02-16 09:04:22 +01:00
|
|
|
});
|
2021-02-17 19:22:06 +01:00
|
|
|
return accountKey;
|
2021-02-17 11:00:38 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:32:02 +01:00
|
|
|
async updateMeta(syncableAccount: Syncable, accountKey: string, headers: HttpHeaders): Promise<any> {
|
|
|
|
const envelope = await this.wrap(syncableAccount , this.signer);
|
2021-02-17 11:00:38 +01:00
|
|
|
const reqBody = envelope.toJSON();
|
2021-03-21 12:02:18 +01:00
|
|
|
this.httpClient.put(`${environment.cicMetaUrl}/${accountKey}`, reqBody , { headers }).pipe(first()).subscribe(res => {
|
|
|
|
this.loggingService.sendInfoLevelMessage(`Response: ${res}`);
|
2021-02-16 09:04:22 +01:00
|
|
|
});
|
2021-02-08 12:47:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 07:29:18 +01:00
|
|
|
getAccounts(): void {
|
2021-03-21 12:02:18 +01:00
|
|
|
this.httpClient.get(`${environment.cicCacheUrl}/accounts`).pipe(first()).subscribe(res => this.accountsList.next(res));
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 07:29:18 +01:00
|
|
|
getAccountById(id: number): Observable<any> {
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.get(`${environment.cicCacheUrl}/accounts/${id}`);
|
2020-12-05 07:29:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getActions(): void {
|
2021-03-21 12:02:18 +01:00
|
|
|
this.httpClient.get(`${environment.cicCacheUrl}/actions`).pipe(first()).subscribe(res => this.actionsList.next(res));
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getActionById(id: string): any {
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.get(`${environment.cicCacheUrl}/actions/${id}`);
|
2020-12-05 07:29:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
approveAction(id: string): Observable<any> {
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.post(`${environment.cicCacheUrl}/actions/${id}`, { approval: true });
|
2020-12-05 07:29:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
revokeAction(id: string): Observable<any> {
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.post(`${environment.cicCacheUrl}/actions/${id}`, { approval: false });
|
2020-12-05 07:29:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getHistoryByUser(id: string): Observable<any> {
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.get(`${environment.cicCacheUrl}/history/${id}`);
|
2020-12-05 07:29:18 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:32:02 +01:00
|
|
|
getAccountDetailsFromMeta(userKey: string): Observable<any> {
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.get(`${environment.cicMetaUrl}/${userKey}`, { headers: this.headers });
|
2021-02-23 13:32:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 19:22:06 +01:00
|
|
|
getUser(userKey: string): any {
|
2021-03-21 12:02:18 +01:00
|
|
|
return this.httpClient.get(`${environment.cicMetaUrl}/${userKey}`, { headers: this.headers })
|
2021-03-14 09:19:25 +01:00
|
|
|
.pipe(first()).subscribe(async res => {
|
2021-03-21 12:02:18 +01:00
|
|
|
return Envelope.fromJSON(JSON.stringify(res)).unwrap();
|
2021-02-17 19:22:06 +01:00
|
|
|
});
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
2021-02-17 11:00:38 +01:00
|
|
|
|
|
|
|
wrap(syncable: Syncable, signer: Signer): Promise<Envelope> {
|
2021-02-17 19:22:06 +01:00
|
|
|
return new Promise<Envelope>(async (whohoo, doh) => {
|
2021-02-17 11:00:38 +01:00
|
|
|
syncable.setSigner(signer);
|
|
|
|
syncable.onwrap = async (env) => {
|
|
|
|
if (env === undefined) {
|
|
|
|
doh();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
whohoo(env);
|
|
|
|
};
|
2021-02-17 19:22:06 +01:00
|
|
|
await syncable.sign();
|
2021-02-17 11:00:38 +01:00
|
|
|
});
|
|
|
|
}
|
2021-03-01 09:12:49 +01:00
|
|
|
|
2021-03-16 11:08:18 +01:00
|
|
|
async loadAccounts(limit: number = 100, offset: number = 0): Promise<void> {
|
2021-03-10 10:47:01 +01:00
|
|
|
this.resetAccountsList();
|
2021-04-20 10:28:40 +02:00
|
|
|
const accountIndexAddress = await this.registry.getContractAddressByName('AccountRegistry');
|
2021-03-06 07:28:29 +01:00
|
|
|
const accountIndexQuery = new AccountIndex(accountIndexAddress);
|
|
|
|
const accountAddresses = await accountIndexQuery.last(await accountIndexQuery.totalAccounts());
|
2021-03-16 11:08:18 +01:00
|
|
|
this.loggingService.sendInfoLevelMessage(accountAddresses);
|
2021-03-01 09:12:49 +01:00
|
|
|
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
|
2021-04-08 14:16:00 +02:00
|
|
|
await this.getAccountByAddress(accountAddress, limit);
|
2021-03-01 09:12:49 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-10 10:47:01 +01:00
|
|
|
|
2021-04-08 14:16:00 +02:00
|
|
|
async getAccountByAddress(accountAddress: string, limit: number = 100): Promise<Observable<AccountDetails>> {
|
|
|
|
let accountSubject = new Subject<any>();
|
|
|
|
this.getAccountDetailsFromMeta(await User.toKey(accountAddress)).pipe(first()).subscribe(async res => {
|
|
|
|
const account = Envelope.fromJSON(JSON.stringify(res)).unwrap();
|
|
|
|
this.accountsMeta.push(account);
|
|
|
|
const accountInfo = account.m.data;
|
|
|
|
accountInfo.balance = await this.tokenService.getTokenBalance(accountInfo.identities.evm['bloxberg:8996'][0]);
|
|
|
|
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
|
|
|
|
this.accounts.unshift(accountInfo);
|
|
|
|
if (this.accounts.length > limit) {
|
|
|
|
this.accounts.length = limit;
|
|
|
|
}
|
|
|
|
this.accountsList.next(this.accounts);
|
|
|
|
accountSubject.next(accountInfo);
|
|
|
|
});
|
|
|
|
return accountSubject.asObservable();
|
|
|
|
}
|
|
|
|
|
2021-04-25 12:32:23 +02:00
|
|
|
async getAccountByPhone(phoneNumber: string, limit: number = 100): Promise<Observable<AccountDetails>> {
|
2021-04-24 21:21:15 +02:00
|
|
|
let accountSubject = new Subject<any>();
|
|
|
|
this.getAccountDetailsFromMeta(await Phone.toKey(phoneNumber)).pipe(first()).subscribe(async res => {
|
|
|
|
const response = Envelope.fromJSON(JSON.stringify(res)).unwrap();
|
|
|
|
const address = '0x' + response.m.data;
|
|
|
|
const account = await this.getAccountByAddress(address, limit);
|
|
|
|
account.subscribe(result => {
|
|
|
|
accountSubject.next(result);
|
|
|
|
});
|
2021-04-08 14:16:00 +02:00
|
|
|
});
|
2021-04-24 21:21:15 +02:00
|
|
|
return accountSubject.asObservable();
|
2021-04-08 14:16:00 +02:00
|
|
|
}
|
|
|
|
|
2021-03-10 10:47:01 +01:00
|
|
|
resetAccountsList(): void {
|
|
|
|
this.accounts = [];
|
|
|
|
this.accountsList.next(this.accounts);
|
|
|
|
}
|
2021-04-07 11:24:32 +02:00
|
|
|
|
|
|
|
searchAccountByName(name: string): any { return; }
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|