2021-02-23 13:32:02 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2021-02-17 13:49:04 +01:00
|
|
|
import {BehaviorSubject, Observable} 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-02-17 13:49:04 +01:00
|
|
|
import {MutableKeyStore, MutablePgpKeyStore, PGPSigner, Signer} from '@app/_helpers';
|
2021-02-25 11:46:24 +01:00
|
|
|
import {ArgPair, Envelope, Syncable, User} from 'cic-client-meta';
|
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-02-17 13:49:04 +01:00
|
|
|
keystore: MutableKeyStore = new MutablePgpKeyStore();
|
2021-02-17 11:00:38 +01:00
|
|
|
signer: Signer = new PGPSigner(this.keystore);
|
|
|
|
|
2020-12-05 07:29:18 +01:00
|
|
|
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();
|
2020-11-25 08:51:15 +01:00
|
|
|
|
|
|
|
constructor(private http: HttpClient) { }
|
|
|
|
|
2021-02-08 12:47:07 +01:00
|
|
|
resetPin(phone: string): Observable<any> {
|
|
|
|
const params = new HttpParams().set('phoneNumber', phone);
|
|
|
|
return this.http.get(`${environment.cicUssdUrl}/pin`, { params });
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:32:02 +01:00
|
|
|
async changeAccountInfo(address: string, status: string, name: string, phoneNumber: string, age: string, type: string, token: string,
|
2021-02-17 11:00:38 +01:00
|
|
|
failedPinAttempts: string, bio: string, gender: string, businessCategory: string, userLocation: string,
|
2021-02-23 13:32:02 +01:00
|
|
|
location: string, locationType: string, referrer: string): Promise<any> {
|
2021-02-16 09:04:22 +01:00
|
|
|
const accountDetails = {
|
|
|
|
status,
|
|
|
|
name,
|
|
|
|
phoneNumber,
|
2021-02-23 13:32:02 +01:00
|
|
|
age,
|
2021-02-16 09:04:22 +01:00
|
|
|
type,
|
|
|
|
token,
|
|
|
|
failedPinAttempts,
|
|
|
|
bio,
|
|
|
|
gender,
|
|
|
|
businessCategory,
|
|
|
|
userLocation,
|
|
|
|
location,
|
2021-02-23 13:32:02 +01:00
|
|
|
locationType,
|
2021-02-16 09:04:22 +01:00
|
|
|
referrer
|
|
|
|
};
|
2021-02-17 19:22:06 +01:00
|
|
|
const accountKey = await User.toKey(address);
|
2021-02-23 13:32:02 +01:00
|
|
|
this.http.get<JSON>(`${environment.cicMetaUrl}/${accountKey}`, { headers: this.headers }).pipe(first()).subscribe(async res => {
|
|
|
|
const syncableAccount: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap();
|
2021-02-17 11:00:38 +01:00
|
|
|
let update = [];
|
|
|
|
for (const prop in accountDetails) {
|
|
|
|
update.push(new ArgPair(prop, accountDetails[prop]));
|
|
|
|
}
|
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-02-16 09:04:22 +01:00
|
|
|
console.error('There is an error!', error);
|
2021-02-25 11:46:24 +01:00
|
|
|
const syncableAccount: Syncable = new Syncable(accountKey, accountDetails);
|
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-02-23 13:32:02 +01:00
|
|
|
this.http.put(`${environment.cicMetaUrl}/${accountKey}`, reqBody , { headers }).pipe(first()).subscribe(res => {
|
2021-02-16 09:04:22 +01:00
|
|
|
console.log(res);
|
|
|
|
});
|
2021-02-08 12:47:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 07:29:18 +01:00
|
|
|
getAccounts(): void {
|
|
|
|
this.http.get(`${environment.cicCacheUrl}/accounts`).pipe(first()).subscribe(accounts => this.accountsList.next(accounts));
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 07:29:18 +01:00
|
|
|
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));
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getActionById(id: string): any {
|
2020-12-05 07:29:18 +01:00
|
|
|
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'});
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 07:29:18 +01:00
|
|
|
deactivateStaff(id: string): Observable<any> {
|
|
|
|
return this.http.post(`${environment.cicCacheUrl}/staff/${id}`, {status: 'deactivated'});
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 07:29:18 +01:00
|
|
|
changeStaffType(id: string, type: string): Observable<any> {
|
|
|
|
return this.http.post(`${environment.cicCacheUrl}/staff/${id}`, {accountType: type});
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:32:02 +01:00
|
|
|
getAccountDetailsFromMeta(userKey: string): Observable<any> {
|
|
|
|
return this.http.get(`${environment.cicMetaUrl}/${userKey}`, { headers: this.headers });
|
|
|
|
}
|
|
|
|
|
2021-02-17 19:22:06 +01:00
|
|
|
getUser(userKey: string): any {
|
2021-02-23 13:32:02 +01:00
|
|
|
this.http.get(`${environment.cicMetaUrl}/${userKey}`, { headers: this.headers }).pipe(first()).subscribe(async res => {
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
2020-11-25 08:51:15 +01:00
|
|
|
}
|