import {Injectable} from '@angular/core'; import {Subject, BehaviorSubject, Observable} from 'rxjs'; import {HttpClient, HttpParams, HttpHeaders} from '@angular/common/http'; import {User} from './user.model'; import {Owner} from './owner.model'; import { Country } from './country.model'; import {Tx} from './tx.model'; @Injectable({providedIn: 'root'}) export class UserService{ beUrl = 'http://localhost:3000/'; private dataStore: { user: User, owner: Owner, txs: Tx[], countries: Country[]} = { user: { address: '', session: '', blocktime: 0, expiration: 0, pin: '', validated: false }, owner: { address: '', name: '', currency: 'usd', tax: false, taxValue: 0, vat: false, vatValue: 0, first: '', last: '', email: '', street: '', city: '', state: '', postal: '', phone: '', paid: false, website: '', country: '' }, txs : [], countries: [] }; private uZaddr = ''; private oZaddr = ''; private uName = ''; private session: string | null = ''; private _uZaddrUpdated: BehaviorSubject = new BehaviorSubject(this.uZaddr); private _userUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.user); private uNameUpdated = new Subject(); private _ownerUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.owner); private _txsUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.txs); private _paidUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.owner.paid); private _countriesUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.countries); public readonly uZaddrUpdate: Observable = this._uZaddrUpdated.asObservable(); public readonly ownerUpdate: Observable = this._ownerUpdated.asObservable(); public readonly userUpdate: Observable = this._userUpdated.asObservable(); public readonly txUpdate: Observable = this._txsUpdated.asObservable(); public readonly paidUpdate: Observable = this._paidUpdated.asObservable(); public readonly countriesUpdate: Observable = this._countriesUpdated.asObservable(); private reqHeaders: HttpHeaders; private apiKey = 'Le2adeic8Thah4Aeng4daem6i'; constructor(private http: HttpClient){ this.reqHeaders = new HttpHeaders().set('Authorization', this.apiKey); //console.log('US:', this.reqHeaders); this.session = localStorage.getItem('s4z_token'); if (this.session != null) { this.findUser(); this.findPending(); } } 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); } }); } findUser() { this.session = localStorage.getItem('s4z_token'); if (this.session != null) { const params = new HttpParams().append('session', this.session!); let obs = this.http.get<{message: string, user: any}>(this.beUrl+'api/getuser', { headers: this.reqHeaders, params: params, observe: 'response'}); obs.subscribe((UserDataResponse) => { console.log(UserDataResponse.status); if (UserDataResponse.status == 200){ this.dataStore.user = UserDataResponse.body!.user[0]; 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 { console.log('US: Did not find user'); } }); return obs; } else { console.log('No session loaded'); return null; } } findPending() { this.session = localStorage.getItem('s4z_token'); if (this.session != null) { const params = new HttpParams().append('session', this.session!); let obs = this.http.get<{message: string, txs: any}>(this.beUrl+'api/pending', { headers: this.reqHeaders, params: params, observe: 'response'}); obs.subscribe((TxDataResponse) => { //console.log('US Tx', TxDataResponse); if (TxDataResponse.status == 200){ this.dataStore.txs = TxDataResponse.body!.txs; console.log(`US: Pending logins found`); this._txsUpdated.next(Object.assign({},this.dataStore).txs); } else { console.log('US: Did not find pending txs'); this.dataStore.txs = []; this._txsUpdated.next(Object.assign({},this.dataStore).txs); } }); return obs; } else { console.log('No session loaded'); return null; } } validateUser(){ var validatedUser: User = this.dataStore.user; validatedUser.validated = true; this.http.post<{message: string, user: User}>(this.beUrl+'api/validateuser', {user: validatedUser}, {headers: this.reqHeaders}). subscribe((responseData) => { console.log(responseData.message); }); } addOwner(owner: Owner) { owner.address = this.dataStore.user.address; let obs = this.http.post<{message: string}>(this.beUrl+'api/addowner', {owner: owner}, {headers: this.reqHeaders}); obs.subscribe((responseData) => { console.log(responseData.message); this.getOwner(this.dataStore.user.address); }); return obs; } updateOwner(owner: Owner) { this.http.post<{message: string, owner: Owner}>(this.beUrl+'api/updateowner', {owner: owner}, {headers: this.reqHeaders}). subscribe((responseData) => { console.log(responseData.message); //this.dataStore.owner = responseData.owner; //this._ownerUpdated.next(Object.assign({},this.dataStore).owner); }); } getOwner(address: string) { console.log('getOwner', address); const ownParams = new HttpParams().append('address', address); let obs = this.http.get<{message:string, owner: any}>(this.beUrl+'api/getowner', { headers: this.reqHeaders, params: ownParams, observe: 'response'}); obs.subscribe((OwnerDataResponse) => { console.log('api/getowner', OwnerDataResponse.status); if (OwnerDataResponse.status == 200) { this.dataStore.owner = OwnerDataResponse.body!.owner[0]; //console.log('getOwner object', this.dataStore.owner); this._ownerUpdated.next(Object.assign({},this.dataStore).owner); this._paidUpdated.next(Object.assign({}, this.dataStore).owner.paid); } }); return obs; } hasOwner() { return this.uZaddr === this.oZaddr; } getNameUpdateListener() { return this.uNameUpdated; } }