diff --git a/CHANGELOG.md b/CHANGELOG.md index 78e322a..bc15eaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Added +- Added new connection for Xero account code - Added new service for Xero integration ### Changed diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index d70110c..86675e4 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -20,7 +20,7 @@ const routes: Routes = [ { path: 'receipt/:orderId', component: ReceiptComponent}, { path: 'invoice/:orderId', component: InvoiceComponent}, { path: 'pmtservice', component: PmtserviceComponent}, - { path: 'test', component: XeroRegComponent}, + { path: 'xeroauth', component: XeroRegComponent}, { path: 'login', component: LoginComponent, resolve: { response: NodeResolverService}} ]; diff --git a/src/app/settings/settings.component.ts b/src/app/settings/settings.component.ts index 1509f30..6b951ce 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 = [ @@ -47,12 +47,16 @@ export class SettingsComponent implements OnInit { },{ label: 'Australian Dollar', symbol: 'aud' + },{ + label: 'New Zealand Dollar', + symbol: 'nzd' } ]; xeroLink: string = ''; localToken: string = ''; clientId: string = ''; clientIdUpdate: Observable; + accCodeUpdate: Observable; linked2Xero : boolean = false; pmtServiceURL : string = ''; @@ -86,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; } }