2022-07-22 04:06:23 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Subject, BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
|
|
|
|
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
|
2022-07-22 20:42:58 +00:00
|
|
|
|
|
|
|
import { NotifierService } from './notifier.service';
|
2022-07-22 04:06:23 +00:00
|
|
|
|
|
|
|
import { User } from './user.model';
|
|
|
|
import { Owner } from './owner.model';
|
2022-01-19 20:50:00 +00:00
|
|
|
import { Country } from './country.model';
|
2022-07-22 04:06:23 +00:00
|
|
|
import { Tx } from './tx.model';
|
2021-10-15 19:14:49 +00:00
|
|
|
|
2022-07-12 18:22:17 +00:00
|
|
|
import { ConfigData } from './configdata';
|
|
|
|
|
2022-05-18 20:51:39 +00:00
|
|
|
var Buffer = require('buffer/').Buffer;
|
|
|
|
|
2021-10-15 19:14:49 +00:00
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
|
|
|
|
export class UserService{
|
2022-07-12 18:22:17 +00:00
|
|
|
beUrl = ConfigData.Be_URL;
|
2022-01-19 20:50:00 +00:00
|
|
|
private dataStore: { user: User, owner: Owner, txs: Tx[], countries: Country[]} = {
|
2021-10-21 16:22:48 +00:00
|
|
|
user: {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
2021-11-11 15:18:38 +00:00
|
|
|
blocktime: 0,
|
|
|
|
pin: '',
|
|
|
|
validated: false
|
2021-10-21 16:22:48 +00:00
|
|
|
},
|
|
|
|
owner: {
|
|
|
|
address: '',
|
2021-11-22 20:37:45 +00:00
|
|
|
name: '',
|
|
|
|
currency: 'usd',
|
|
|
|
tax: false,
|
|
|
|
taxValue: 0,
|
|
|
|
vat: false,
|
2022-01-17 20:49:08 +00:00
|
|
|
vatValue: 0,
|
2022-01-28 20:03:35 +00:00
|
|
|
first: '',
|
|
|
|
last: '',
|
2022-01-17 20:49:08 +00:00
|
|
|
email: '',
|
|
|
|
street: '',
|
|
|
|
city: '',
|
|
|
|
state: '',
|
|
|
|
postal: '',
|
|
|
|
phone: '',
|
2022-01-18 22:40:20 +00:00
|
|
|
paid: false,
|
2022-01-19 16:26:25 +00:00
|
|
|
website: '',
|
2022-03-07 17:14:29 +00:00
|
|
|
country: '',
|
2022-05-18 20:51:39 +00:00
|
|
|
zats: false,
|
|
|
|
invoices: false,
|
2022-07-14 16:11:04 +00:00
|
|
|
expiration: new Date(Date.now()).toISOString(),
|
2022-07-18 20:29:27 +00:00
|
|
|
payconf: false,
|
2022-08-31 13:56:06 +00:00
|
|
|
viewkey: '',
|
|
|
|
crmToken: ''
|
2021-11-09 21:00:01 +00:00
|
|
|
},
|
2022-01-19 20:50:00 +00:00
|
|
|
txs : [],
|
|
|
|
countries: []
|
2021-10-21 16:22:48 +00:00
|
|
|
};
|
2021-10-15 19:14:49 +00:00
|
|
|
private uZaddr = '';
|
2021-10-20 20:51:14 +00:00
|
|
|
private oZaddr = '';
|
|
|
|
private uName = '';
|
|
|
|
private session: string | null = '';
|
2021-10-21 15:23:58 +00:00
|
|
|
private _uZaddrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.uZaddr);
|
2021-10-25 20:01:06 +00:00
|
|
|
private _userUpdated: BehaviorSubject<User> = new BehaviorSubject(this.dataStore.user);
|
2021-10-20 20:51:14 +00:00
|
|
|
private uNameUpdated = new Subject<string>();
|
2021-10-21 16:22:48 +00:00
|
|
|
private _ownerUpdated: BehaviorSubject<Owner> = new BehaviorSubject(this.dataStore.owner);
|
2021-11-09 21:00:01 +00:00
|
|
|
private _txsUpdated: BehaviorSubject<Tx[]> = new BehaviorSubject(this.dataStore.txs);
|
2022-01-17 20:49:08 +00:00
|
|
|
private _paidUpdated: BehaviorSubject<boolean> = new BehaviorSubject(this.dataStore.owner.paid);
|
2022-01-19 20:50:00 +00:00
|
|
|
private _countriesUpdated: BehaviorSubject<Country[]> = new BehaviorSubject(this.dataStore.countries);
|
2021-10-21 15:23:58 +00:00
|
|
|
public readonly uZaddrUpdate: Observable<string> = this._uZaddrUpdated.asObservable();
|
2021-10-21 16:22:48 +00:00
|
|
|
public readonly ownerUpdate: Observable<Owner> = this._ownerUpdated.asObservable();
|
2021-10-25 20:01:06 +00:00
|
|
|
public readonly userUpdate: Observable<User> = this._userUpdated.asObservable();
|
2021-11-09 21:00:01 +00:00
|
|
|
public readonly txUpdate: Observable<Tx[]> = this._txsUpdated.asObservable();
|
2022-01-17 20:49:08 +00:00
|
|
|
public readonly paidUpdate: Observable<boolean> = this._paidUpdated.asObservable();
|
2022-01-19 20:50:00 +00:00
|
|
|
public readonly countriesUpdate: Observable<Country[]> = this._countriesUpdated.asObservable();
|
2021-11-09 18:39:16 +00:00
|
|
|
private reqHeaders: HttpHeaders;
|
2021-10-15 19:14:49 +00:00
|
|
|
|
2022-07-22 04:06:23 +00:00
|
|
|
constructor(private http: HttpClient,
|
2022-07-22 20:42:58 +00:00
|
|
|
private notifierService : NotifierService ){
|
2022-07-12 18:22:17 +00:00
|
|
|
var auth = 'Basic ' + Buffer.from(ConfigData.UsrPwd).toString('base64');
|
2022-05-18 20:51:39 +00:00
|
|
|
this.reqHeaders = new HttpHeaders().set('Authorization', auth);
|
2021-11-09 21:00:01 +00:00
|
|
|
//console.log('US:', this.reqHeaders);
|
2021-10-20 20:51:14 +00:00
|
|
|
this.session = localStorage.getItem('s4z_token');
|
2021-10-21 15:23:58 +00:00
|
|
|
if (this.session != null) {
|
2021-11-02 21:13:24 +00:00
|
|
|
this.findUser();
|
2021-10-21 15:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 20:50:00 +00:00
|
|
|
getCountries() {
|
|
|
|
let obs = this.http.get<{message: string, countries: any}>(this.beUrl+'api/countries', { headers: this.reqHeaders, observe: 'response'});
|
|
|
|
|
|
|
|
obs.subscribe((CountryResponse) => {
|
|
|
|
if (CountryResponse.status == 200) {
|
|
|
|
this.dataStore.countries = CountryResponse.body!.countries;
|
|
|
|
this._countriesUpdated.next(Object.assign({}, this.dataStore).countries);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-02 21:13:24 +00:00
|
|
|
findUser() {
|
2021-11-04 12:49:09 +00:00
|
|
|
this.session = localStorage.getItem('s4z_token');
|
|
|
|
if (this.session != null) {
|
|
|
|
const params = new HttpParams().append('session', this.session!);
|
2022-05-18 20:51:39 +00:00
|
|
|
let obs = this.http.get<{message: string, user: any}>(this.beUrl+'api/user', { headers: this.reqHeaders, params: params, observe: 'response'});
|
2021-10-21 16:22:48 +00:00
|
|
|
|
2021-11-04 12:49:09 +00:00
|
|
|
obs.subscribe((UserDataResponse) => {
|
|
|
|
console.log(UserDataResponse.status);
|
|
|
|
if (UserDataResponse.status == 200){
|
2022-05-18 20:51:39 +00:00
|
|
|
this.dataStore.user = UserDataResponse.body!.user;
|
2021-11-04 12:49:09 +00:00
|
|
|
console.log(`US: Found user, returning it`);
|
|
|
|
this._uZaddrUpdated.next(Object.assign({},this.dataStore).user.address);
|
|
|
|
this._userUpdated.next(Object.assign({}, this.dataStore).user);
|
|
|
|
this.getOwner(Object.assign({},this.dataStore.user).address);
|
|
|
|
} else {
|
2022-02-01 18:04:16 +00:00
|
|
|
this.dataStore.user = {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
blocktime: 0,
|
|
|
|
pin: '',
|
|
|
|
validated: false
|
|
|
|
};
|
|
|
|
this._uZaddrUpdated.next(Object.assign({},this.dataStore).user.address);
|
|
|
|
this._userUpdated.next(Object.assign({}, this.dataStore).user);
|
2021-11-04 12:49:09 +00:00
|
|
|
console.log('US: Did not find user');
|
|
|
|
}
|
2021-10-21 16:22:48 +00:00
|
|
|
});
|
|
|
|
|
2021-11-04 12:49:09 +00:00
|
|
|
return obs;
|
|
|
|
} else {
|
|
|
|
console.log('No session loaded');
|
|
|
|
return null;
|
|
|
|
}
|
2021-10-20 20:51:14 +00:00
|
|
|
}
|
2021-11-04 12:49:09 +00:00
|
|
|
|
2022-05-20 14:47:17 +00:00
|
|
|
validateUser(pinString:string){
|
2022-05-20 15:40:49 +00:00
|
|
|
const params = new HttpParams().append('session', this.dataStore.user.session).append('pin', pinString);
|
2022-05-20 18:41:48 +00:00
|
|
|
let obs = this.http.post(this.beUrl+'api/validateuser', {}, {headers: this.reqHeaders, params: params, observe: 'response'});
|
|
|
|
obs.subscribe((responseData) => {
|
2022-05-20 14:47:17 +00:00
|
|
|
if (responseData.status == 202) {
|
|
|
|
console.log('Pin validated!');
|
2022-05-20 18:41:48 +00:00
|
|
|
return true;
|
2022-05-20 14:47:17 +00:00
|
|
|
} else {
|
|
|
|
console.log('Wrong pin!');
|
2022-05-20 18:41:48 +00:00
|
|
|
return false;
|
2022-05-20 14:47:17 +00:00
|
|
|
}
|
2021-11-11 15:18:38 +00:00
|
|
|
});
|
2022-05-20 18:41:48 +00:00
|
|
|
|
|
|
|
return obs;
|
2021-11-11 15:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:49:08 +00:00
|
|
|
addOwner(owner: Owner) {
|
2022-07-22 04:06:23 +00:00
|
|
|
|
2022-01-22 13:49:22 +00:00
|
|
|
owner.address = this.dataStore.user.address;
|
2022-05-18 20:51:39 +00:00
|
|
|
let obs = this.http.post(this.beUrl+'api/owner', {payload: owner}, {headers: this.reqHeaders});
|
2021-10-21 16:22:48 +00:00
|
|
|
|
|
|
|
obs.subscribe((responseData) => {
|
2022-07-26 15:00:17 +00:00
|
|
|
//console.log("Entra a console log");
|
2022-01-22 13:49:22 +00:00
|
|
|
this.getOwner(this.dataStore.user.address);
|
2022-07-22 04:06:23 +00:00
|
|
|
}, (error) => {
|
2022-07-26 15:00:17 +00:00
|
|
|
//console.log("Status is : [" + error.status + "]");
|
2022-07-22 04:06:23 +00:00
|
|
|
if ( error.status = 500 ) {
|
2022-07-22 20:42:58 +00:00
|
|
|
this.notifierService
|
|
|
|
.showNotification("Invalid Viewing Key, changes not saved!!","Close",'error');
|
2022-07-22 04:06:23 +00:00
|
|
|
};
|
2021-10-20 20:51:14 +00:00
|
|
|
});
|
2021-10-21 16:22:48 +00:00
|
|
|
|
|
|
|
return obs;
|
2021-10-20 20:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getOwner(address: string) {
|
2021-10-21 19:29:04 +00:00
|
|
|
console.log('getOwner', address);
|
2021-10-20 20:51:14 +00:00
|
|
|
const ownParams = new HttpParams().append('address', address);
|
2022-05-18 20:51:39 +00:00
|
|
|
let obs = this.http.get<{message:string, owner: any}>(this.beUrl+'api/owner', { headers: this.reqHeaders, params: ownParams, observe: 'response'});
|
2021-10-21 16:22:48 +00:00
|
|
|
|
|
|
|
obs.subscribe((OwnerDataResponse) => {
|
2022-07-26 15:00:17 +00:00
|
|
|
//console.log('api/getowner', OwnerDataResponse.status);
|
2021-10-20 20:51:14 +00:00
|
|
|
if (OwnerDataResponse.status == 200) {
|
2022-05-18 20:51:39 +00:00
|
|
|
this.dataStore.owner = OwnerDataResponse.body!.owner;
|
2022-07-26 15:00:17 +00:00
|
|
|
//console.log('getOwner object', this.dataStore.owner);
|
|
|
|
//console.log('Payment Conf.?: [' + ( this.dataStore.owner.payconf ? "On]" : "Off]") );
|
|
|
|
//console.log('Viewkey : [' + this.dataStore.owner.viewkey + "]");
|
2021-10-21 19:29:04 +00:00
|
|
|
this._ownerUpdated.next(Object.assign({},this.dataStore).owner);
|
2022-01-17 20:49:08 +00:00
|
|
|
this._paidUpdated.next(Object.assign({}, this.dataStore).owner.paid);
|
2021-10-20 20:51:14 +00:00
|
|
|
}
|
2022-07-20 15:16:06 +00:00
|
|
|
});
|
2021-10-21 16:22:48 +00:00
|
|
|
|
|
|
|
return obs;
|
2021-10-20 20:51:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 18:04:16 +00:00
|
|
|
deleteUser() {
|
|
|
|
let obs = this.http.delete<{message: string}>(this.beUrl+'api/user/'+this.dataStore.user._id, {headers: this.reqHeaders });
|
2021-10-20 20:51:14 +00:00
|
|
|
|
2022-02-01 18:04:16 +00:00
|
|
|
obs.subscribe(UserResponse => {
|
2022-07-26 15:00:17 +00:00
|
|
|
//console.log('User delete request sent.');
|
2022-02-01 18:04:16 +00:00
|
|
|
this.findUser();
|
|
|
|
});
|
2021-10-15 19:14:49 +00:00
|
|
|
|
2022-02-01 18:04:16 +00:00
|
|
|
return obs;
|
|
|
|
}
|
2022-07-22 04:06:23 +00:00
|
|
|
|
2022-08-23 21:22:34 +00:00
|
|
|
currentOwner() : Owner {
|
|
|
|
return this.dataStore.owner;
|
|
|
|
}
|
|
|
|
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|