From ce0f85f8ddff8933d8d105b7ad4910d8c010628a Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 6 Sep 2022 15:44:01 -0500 Subject: [PATCH] Implement Xero account service --- src/app/settings/settings.component.ts | 10 +++++++-- src/app/xero.service.ts | 31 +++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/app/settings/settings.component.ts b/src/app/settings/settings.component.ts index a733c35..f8afc53 100644 --- a/src/app/settings/settings.component.ts +++ b/src/app/settings/settings.component.ts @@ -27,8 +27,8 @@ export class SettingsComponent implements OnInit { useZats: boolean; proVersion: boolean = false; useVKey: boolean = false; - linkMsg: String = 'Link to Xero'; - xeroAccCod: String = ''; + linkMsg: string = 'Link to Xero'; + xeroAccCod: string = ''; saveAccOk: boolean = false; coins = [ @@ -56,6 +56,7 @@ export class SettingsComponent implements OnInit { localToken: string = ''; clientId: string = ''; clientIdUpdate: Observable; + accCodeUpdate: Observable; linked2Xero : boolean = false; pmtServiceURL : string = ''; @@ -89,6 +90,11 @@ export class SettingsComponent implements OnInit { this.clientId = clientId; this.xeroLink = `https://login.xero.com/identity/connect/authorize?response_type=code&client_id=${this.clientId}&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Ftest&scope=accounting.transactions offline_access&state=${this.owner.address.substring(0, 6)}` }); + this.accCodeUpdate = xeroService.accCodeUpdate; + xeroService.getXeroAccountCode(this.owner.address); + this.accCodeUpdate.subscribe(accData => { + this.xeroAccCod = accData; + }); } ngOnInit() { diff --git a/src/app/xero.service.ts b/src/app/xero.service.ts index d5ed579..5258d90 100644 --- a/src/app/xero.service.ts +++ b/src/app/xero.service.ts @@ -20,12 +20,15 @@ export class XeroService { scope: '', tokenType: '' }; + xeroAcc: string = ''; private _clientIdUpdated: BehaviorSubject = new BehaviorSubject(this.clientId); //private _clientSecretUpdated: BehaviorSubject = new BehaviorSubject(this.clientSecret); private _tokenUpdated: BehaviorSubject = new BehaviorSubject(this.xeroToken); + private _accCodeUpdated: BehaviorSubject = new BehaviorSubject(this.xeroAcc); public readonly clientIdUpdate: Observable = this._clientIdUpdated.asObservable(); //public readonly clientSecretUpdate: Observable = this._clientSecretUpdated.asObservable(); public readonly tokenUpdate: Observable = this._tokenUpdated.asObservable(); + public readonly accCodeUpdate: Observable = this._accCodeUpdated.asObservable(); private reqHeaders: HttpHeaders; constructor( @@ -55,7 +58,33 @@ export class XeroService { getXeroAccessToken(code: string, address: string){ const params = new HttpParams().append('code', code).append('address', address); - let obs = this.http.get(this.beUrl + 'api/xerotoken' , {headers: this.reqHeaders, params: params, observe: 'response'}) + let obs = this.http.get(this.beUrl + 'api/xerotoken' , {headers: this.reqHeaders, params: params, observe: 'response'}); + return obs; + } + + getXeroAccountCode(address: string){ + const params = new HttpParams().append('address', address); + let obs = this.http.get<{message: string, code: string}>(this.beUrl + 'api/xeroaccount', {headers: this.reqHeaders, params: params, observe: 'response'}); + obs.subscribe(accountResponse => { + if (accountResponse.status == 200) { + this.xeroAcc = accountResponse.body!.code; + this._accCodeUpdated.next(Object.assign({}, this).xeroAcc); + } else { + console.log('No account in DB'); + } + }); + return obs; + + } + + setXeroAccountCode(address: string, code: string){ + const params = new HttpParams().append('address', address).append('code', code); + let obs = this.http.post(this.beUrl + 'api/xerotoken', {}, {headers: this.reqHeaders, params: params, observe: 'response'}); + obs.subscribe(responseData => { + if (responseData.status == 202) { + console.log('Account saved'); + } + }); return obs; } }