Implement Xero account service

This commit is contained in:
Rene Vergara 2022-09-06 15:44:01 -05:00
parent 072da66c10
commit ce0f85f8dd
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
2 changed files with 38 additions and 3 deletions

View file

@ -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<string>;
accCodeUpdate: Observable<string>;
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() {

View file

@ -20,12 +20,15 @@ export class XeroService {
scope: '',
tokenType: ''
};
xeroAcc: string = '';
private _clientIdUpdated: BehaviorSubject<string> = new BehaviorSubject(this.clientId);
//private _clientSecretUpdated: BehaviorSubject<string> = new BehaviorSubject(this.clientSecret);
private _tokenUpdated: BehaviorSubject<any> = new BehaviorSubject(this.xeroToken);
private _accCodeUpdated: BehaviorSubject<string> = new BehaviorSubject(this.xeroAcc);
public readonly clientIdUpdate: Observable<string> = this._clientIdUpdated.asObservable();
//public readonly clientSecretUpdate: Observable<string> = this._clientSecretUpdated.asObservable();
public readonly tokenUpdate: Observable<any> = this._tokenUpdated.asObservable();
public readonly accCodeUpdate: Observable<string> = 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;
}
}