cic-staff-client/src/app/_services/user.service.ts

286 lines
9.9 KiB
TypeScript
Raw Normal View History

2021-05-10 18:15:25 +02:00
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { environment } from '@src/environments/environment';
import { first } from 'rxjs/operators';
import { ArgPair, Envelope, Phone, Syncable, User } from 'cic-client-meta';
import { AccountDetails } from '@app/_models';
import { LoggingService } from '@app/_services/logging.service';
import { TokenService } from '@app/_services/token.service';
import { AccountIndex } from '@app/_eth';
import { MutableKeyStore, PGPSigner, Signer } from '@app/_pgp';
import { RegistryService } from '@app/_services/registry.service';
2021-06-10 17:40:45 +02:00
import { CICRegistry } from '@cicnet/cic-client';
2021-05-10 18:15:25 +02:00
import { AuthService } from '@app/_services/auth.service';
import { personValidation, vcardValidation } from '@app/_helpers';
import { add0x } from '@src/assets/js/ethtx/dist/hex';
import { KeystoreService } from '@app/_services/keystore.service';
const vCard = require('vcard-parser');
@Injectable({
2021-05-10 18:15:25 +02:00
providedIn: 'root',
})
export class UserService {
2021-05-10 18:15:25 +02:00
headers: HttpHeaders = new HttpHeaders({ 'x-cic-automerge': 'client' });
keystore: MutableKeyStore;
signer: Signer;
2021-04-20 10:28:40 +02:00
registry: CICRegistry;
2021-02-17 11:00:38 +01:00
accounts: Array<AccountDetails> = [];
2021-05-10 18:15:25 +02:00
private accountsList: BehaviorSubject<Array<AccountDetails>> = new BehaviorSubject<
Array<AccountDetails>
>(this.accounts);
accountsSubject: Observable<Array<AccountDetails>> = this.accountsList.asObservable();
2020-12-05 07:29:18 +01:00
actions: Array<any> = [];
private actionsList: BehaviorSubject<any> = new BehaviorSubject<any>(this.actions);
actionsSubject: Observable<Array<any>> = this.actionsList.asObservable();
constructor(
2021-03-21 12:02:18 +01:00
private httpClient: HttpClient,
private loggingService: LoggingService,
2021-04-20 10:28:40 +02:00
private tokenService: TokenService,
2021-05-10 18:15:25 +02:00
private authService: AuthService
2021-05-19 18:57:10 +02:00
) {}
async init(): Promise<void> {
await this.authService.init();
await this.tokenService.init();
this.keystore = await KeystoreService.getKeystore();
2021-05-19 18:57:10 +02:00
this.signer = new PGPSigner(this.keystore);
this.registry = await RegistryService.getRegistry();
}
2021-02-08 12:47:07 +01:00
resetPin(phone: string): Observable<any> {
const params: HttpParams = new HttpParams().set('phoneNumber', phone);
2021-05-26 09:26:11 +02:00
return this.httpClient.put(`${environment.cicUssdUrl}/pin`, { params });
}
getAccountStatus(phone: string): Observable<any> {
const params: HttpParams = new HttpParams().set('phoneNumber', phone);
2021-05-10 18:15:25 +02:00
return this.httpClient.get(`${environment.cicUssdUrl}/pin`, { params });
}
getLockedAccounts(offset: number, limit: number): Observable<any> {
2021-03-21 12:02:18 +01:00
return this.httpClient.get(`${environment.cicUssdUrl}/accounts/locked/${offset}/${limit}`);
}
2021-05-10 18:15:25 +02:00
async changeAccountInfo(
address: string,
name: string,
phoneNumber: string,
age: string,
type: string,
bio: string,
gender: string,
businessCategory: string,
userLocation: string,
location: string,
locationType: string
): Promise<any> {
const accountInfo: any = {
vcard: {
fn: [{}],
n: [{}],
2021-05-10 18:15:25 +02:00
tel: [{}],
},
2021-05-10 18:15:25 +02:00
location: {},
};
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-27 16:10:50 +02:00
await vcardValidation(accountInfo.vcard);
accountInfo.vcard = btoa(vCard.generate(accountInfo.vcard));
const accountKey: string = await User.toKey(address);
2021-05-10 18:15:25 +02:00
this.getAccountDetailsFromMeta(accountKey)
.pipe(first())
.subscribe(
async (res) => {
const syncableAccount: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap();
const update: Array<ArgPair> = [];
for (const prop of Object.keys(accountInfo)) {
update.push(new ArgPair(prop, accountInfo[prop]));
}
syncableAccount.update(update, 'client-branch');
await personValidation(syncableAccount.m.data);
await this.updateMeta(syncableAccount, accountKey, this.headers);
},
async (error) => {
this.loggingService.sendErrorLevelMessage(
'Cannot find account info in meta service',
this,
{ error }
);
const syncableAccount: Syncable = new Syncable(accountKey, accountInfo);
await this.updateMeta(syncableAccount, accountKey, this.headers);
}
);
return accountKey;
2021-02-17 11:00:38 +01:00
}
2021-05-10 18:15:25 +02:00
async updateMeta(
syncableAccount: Syncable,
accountKey: string,
headers: HttpHeaders
): Promise<any> {
const envelope: Envelope = await this.wrap(syncableAccount, this.signer);
const reqBody: string = envelope.toJSON();
2021-05-10 18:15:25 +02:00
this.httpClient
.put(`${environment.cicMetaUrl}/${accountKey}`, reqBody, { headers })
.pipe(first())
.subscribe((res) => {
this.loggingService.sendInfoLevelMessage(`Response: ${res}`);
});
2021-02-08 12:47:07 +01:00
}
2020-12-05 07:29:18 +01:00
getActions(): void {
2021-05-10 18:15:25 +02:00
this.httpClient
.get(`${environment.cicCacheUrl}/actions`)
.pipe(first())
.subscribe((res) => this.actionsList.next(res));
}
getActionById(id: string): Observable<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
}
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-17 11:00:38 +01:00
wrap(syncable: Syncable, signer: Signer): Promise<Envelope> {
return new Promise<Envelope>(async (resolve, reject) => {
2021-02-17 11:00:38 +01:00
syncable.setSigner(signer);
syncable.onwrap = async (env) => {
if (env === undefined) {
reject();
2021-02-17 11:00:38 +01:00
return;
}
resolve(env);
2021-02-17 11:00:38 +01:00
};
await syncable.sign();
2021-02-17 11:00:38 +01:00
});
}
async loadAccounts(limit: number = 100, offset: number = 0): Promise<void> {
this.resetAccountsList();
const accountIndexAddress: string = await this.registry.getContractAddressByName(
'AccountRegistry'
);
const accountIndexQuery = new AccountIndex(accountIndexAddress);
const accountAddresses: Array<string> = await accountIndexQuery.last(limit);
this.loggingService.sendInfoLevelMessage(accountAddresses);
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
await this.getAccountByAddress(accountAddress, limit);
}
}
2021-05-10 18:15:25 +02:00
async getAccountByAddress(
accountAddress: string,
limit: number = 100
): Promise<Observable<AccountDetails>> {
const accountSubject: Subject<any> = new Subject<any>();
this.getAccountDetailsFromMeta(await User.toKey(add0x(accountAddress)))
.pipe(first())
.subscribe(async (res) => {
const account: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap();
const accountInfo = account.m.data;
2021-05-10 18:15:25 +02:00
await personValidation(accountInfo);
this.tokenService.load.subscribe(async (status: boolean) => {
if (status) {
accountInfo.balance = await this.tokenService.getTokenBalance(
accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0]
);
}
});
2021-05-10 18:15:25 +02:00
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
await vcardValidation(accountInfo.vcard);
2021-05-19 09:56:39 +02:00
this.addAccount(accountInfo, limit);
2021-05-10 18:15:25 +02:00
accountSubject.next(accountInfo);
});
return accountSubject.asObservable();
}
2021-05-10 18:15:25 +02:00
async getAccountByPhone(
phoneNumber: string,
limit: number = 100
): Promise<Observable<AccountDetails>> {
const accountSubject: Subject<any> = new Subject<any>();
this.getAccountDetailsFromMeta(await Phone.toKey(phoneNumber))
.pipe(first())
.subscribe(async (res) => {
const response: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap();
const address: string = response.m.data;
const account: Observable<AccountDetails> = await this.getAccountByAddress(address, limit);
account.subscribe((result) => {
accountSubject.next(result);
});
});
return accountSubject.asObservable();
}
resetAccountsList(): void {
this.accounts = [];
this.accountsList.next(this.accounts);
}
2021-05-10 18:15:25 +02:00
searchAccountByName(name: string): any {
return;
}
getCategories(): Observable<any> {
2021-04-28 16:31:36 +02:00
return this.httpClient.get(`${environment.cicMetaUrl}/categories`);
}
getCategoryByProduct(product: string): Observable<any> {
2021-04-28 16:31:36 +02:00
return this.httpClient.get(`${environment.cicMetaUrl}/categories/${product.toLowerCase()}`);
}
getAccountTypes(): Observable<any> {
return this.httpClient.get(`${environment.cicMetaUrl}/accounttypes`);
}
getTransactionTypes(): Observable<any> {
return this.httpClient.get(`${environment.cicMetaUrl}/transactiontypes`);
}
getGenders(): Observable<any> {
return this.httpClient.get(`${environment.cicMetaUrl}/genders`);
}
2021-05-19 09:56:39 +02:00
addAccount(account: AccountDetails, cacheSize: number): void {
const savedIndex = this.accounts.findIndex(
(acc) =>
acc.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] ===
account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0]
);
if (savedIndex === 0) {
return;
}
if (savedIndex > 0) {
this.accounts.splice(savedIndex, 1);
}
this.accounts.unshift(account);
if (this.accounts.length > cacheSize) {
this.accounts.length = Math.min(this.accounts.length, cacheSize);
}
this.accountsList.next(this.accounts);
}
}