zgo/src/app/xero.service.ts

105 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-08-10 19:12:28 +00:00
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { ConfigData } from './configdata';
var Buffer = require('buffer/').Buffer;
@Injectable({
providedIn: 'root'
})
export class XeroService {
beUrl = ConfigData.Be_URL;
clientId: string = '';
//clientSecret: string = '';
2022-08-10 19:12:28 +00:00
xeroToken: any = {
accessToken: '',
refreshToken: '',
expiresIn: 0,
scope: '',
tokenType: ''
};
2022-09-06 20:44:01 +00:00
xeroAcc: string = '';
savedAcc : boolean = false;
public savedAccObs = new Observable((observer) => {
2023-06-19 15:30:41 +00:00
//console.log("starting savedAccObs");
setTimeout(() => {observer.next(this.savedAcc)},1000);
})
2022-09-07 22:04:07 +00:00
2022-08-10 19:12:28 +00:00
private _clientIdUpdated: BehaviorSubject<string> = new BehaviorSubject(this.clientId);
//private _clientSecretUpdated: BehaviorSubject<string> = new BehaviorSubject(this.clientSecret);
2022-08-10 19:12:28 +00:00
private _tokenUpdated: BehaviorSubject<any> = new BehaviorSubject(this.xeroToken);
2022-09-06 20:44:01 +00:00
private _accCodeUpdated: BehaviorSubject<string> = new BehaviorSubject(this.xeroAcc);
2022-08-10 19:12:28 +00:00
public readonly clientIdUpdate: Observable<string> = this._clientIdUpdated.asObservable();
//public readonly clientSecretUpdate: Observable<string> = this._clientSecretUpdated.asObservable();
2022-08-10 19:12:28 +00:00
public readonly tokenUpdate: Observable<any> = this._tokenUpdated.asObservable();
2022-09-06 20:44:01 +00:00
public readonly accCodeUpdate: Observable<string> = this._accCodeUpdated.asObservable();
2022-08-10 19:12:28 +00:00
private reqHeaders: HttpHeaders;
2023-05-10 16:05:15 +00:00
private reqParams: HttpParams;
private session: null | string;
2022-08-10 19:12:28 +00:00
constructor(
private http: HttpClient
) {
var auth = 'Basic ' + Buffer.from(ConfigData.UsrPwd).toString('base64');
this.reqHeaders = new HttpHeaders().set('Authorization', auth);
2023-05-10 16:05:15 +00:00
this.session = localStorage.getItem('s4z_token');
this.reqParams = new HttpParams().append('session', this.session!);
2022-08-10 19:12:28 +00:00
this.getXeroConfig();
}
getXeroConfig(){
2023-05-10 16:05:15 +00:00
let obs = this.http.get<{message: string, xeroConfig: any}>(this.beUrl+'api/xero', { headers:this.reqHeaders, observe: 'response', params: this.reqParams});
2022-08-10 19:12:28 +00:00
obs.subscribe(xeroDataResponse => {
if (xeroDataResponse.status == 200) {
this.clientId = xeroDataResponse.body!.xeroConfig.clientId;
this._clientIdUpdated.next(Object.assign({}, this).clientId);
} else {
console.log('No config in DB!');
}
});
return obs;
}
2023-06-20 19:11:40 +00:00
getXeroAccessToken(code: string){
const params = this.reqParams.append('code', code);
2022-09-06 20:44:01 +00:00
let obs = this.http.get(this.beUrl + 'api/xerotoken' , {headers: this.reqHeaders, params: params, observe: 'response'});
return obs;
}
2023-06-19 15:30:41 +00:00
getXeroAccountCode(){
let obs = this.http.get<{message: string, code: string}>(this.beUrl + 'api/xeroaccount', {headers: this.reqHeaders, params: this.reqParams, observe: 'response'});
2022-09-06 20:44:01 +00:00
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;
}
2022-09-07 22:04:07 +00:00
setXeroAccountCode(address: string, code: string) {
2023-05-10 16:05:15 +00:00
const params = this.reqParams.append('address', address).append('code', code);
2022-09-07 22:04:07 +00:00
let obs = this.http.post(this.beUrl + 'api/xeroaccount', {}, {headers: this.reqHeaders, params: params, observe: 'response'});
/*
2022-09-06 20:44:01 +00:00
obs.subscribe(responseData => {
if (responseData.status == 202) {
console.log('Account saved');
this.savedAcc = true;
2022-09-06 20:44:01 +00:00
}
}, error => {
this.savedAcc = false;
console.log("error : " + error.msg)
2022-09-06 20:44:01 +00:00
});
*/
return obs;
2022-08-10 19:12:28 +00:00
}
}