From 14bb45cb95c05b15b06a5964363a350b443dbc95 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Fri, 23 Dec 2022 14:40:24 -0600 Subject: [PATCH] Front End for WooCommerce config --- src/app/settings/settings.component.css | 11 ++++ src/app/settings/settings.component.html | 47 ++++++++++++++++- src/app/settings/settings.component.ts | 47 ++++++++++++++++- src/app/woocommerce.service.spec.ts | 16 ++++++ src/app/woocommerce.service.ts | 65 ++++++++++++++++++++++++ 5 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 src/app/woocommerce.service.spec.ts create mode 100644 src/app/woocommerce.service.ts diff --git a/src/app/settings/settings.component.css b/src/app/settings/settings.component.css index 777aca3..2779c59 100644 --- a/src/app/settings/settings.component.css +++ b/src/app/settings/settings.component.css @@ -45,4 +45,15 @@ color: dodgerblue; } +.small { + font-size: 12px; + background: #dddddd; +} +.heading { + padding-top: 10px; +} + +.toolbar { + padding: 12px; +} diff --git a/src/app/settings/settings.component.html b/src/app/settings/settings.component.html index 1b6e121..cbb18c2 100644 --- a/src/app/settings/settings.component.html +++ b/src/app/settings/settings.component.html @@ -170,7 +170,52 @@ -

Woo va aqui

+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
Owner:
{{wooOwner}} + + + + +
Token:
{{wooToken}} + + + + +
URL:
{{wooUrl}}
+
+ +
+
diff --git a/src/app/settings/settings.component.ts b/src/app/settings/settings.component.ts index bdbe35f..98d1f5a 100644 --- a/src/app/settings/settings.component.ts +++ b/src/app/settings/settings.component.ts @@ -5,6 +5,7 @@ import { UntypedFormBuilder, Validators, UntypedFormGroup, FormControl } from '@ import { Observable } from 'rxjs'; import { Owner } from '../owner.model'; import { XeroService } from '../xero.service'; +import { WoocommerceService } from '../woocommerce.service'; import { NotifierService } from '../notifier.service'; import { faCopy } from '@fortawesome/free-solid-svg-icons'; @@ -56,6 +57,13 @@ export class SettingsComponent implements OnInit { xeroLink: string = ''; localToken: string = ''; clientId: string = ''; + wooOwner: string = ''; + wooToken: string = ''; + wooUrl: string = ''; + wooOwnerUpdate: Observable; + wooTokenUpdate: Observable; + wooUrlUpdate: Observable; + clientIdUpdate: Observable; accCodeUpdate: Observable; linked2Xero : boolean = false; @@ -65,6 +73,7 @@ export class SettingsComponent implements OnInit { private notifierService : NotifierService, private fb: UntypedFormBuilder, public xeroService: XeroService, + public wooService: WoocommerceService, private dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: Owner) { this.useZats = data.zats; @@ -102,6 +111,19 @@ export class SettingsComponent implements OnInit { console.log("xeroAccCod -> [" + this.xeroAccCod + "]"); this.accCodForm.get('xAcc')!.setValue(this.xeroAccCod); }); + this.wooOwnerUpdate = wooService.ownerUpdate; + this.wooTokenUpdate = wooService.tokenUpdate; + this.wooUrlUpdate = wooService.siteurlUpdate; + wooService.getWooToken(this.owner._id!); + this.wooOwnerUpdate.subscribe(owData => { + this.wooOwner = owData; + }); + this.wooTokenUpdate.subscribe(tkData => { + this.wooToken = tkData; + }); + this.wooUrlUpdate.subscribe(uData => { + this.wooUrl = uData; + }); } ngOnInit() { @@ -128,7 +150,7 @@ export class SettingsComponent implements OnInit { closeIntegration() { if ( (this.xeroAccCod == '') && (this.linked2Xero) ) this.notifierService - .showNotification("Payment confirmation disabled!!","Close",'warning'); + .showNotification("Xero Payment confirmation disabled!!","Close",'warning'); this.dialogRef.close(); } @@ -179,6 +201,29 @@ export class SettingsComponent implements OnInit { } } + copyWooOwner(){ + try { + navigator.clipboard.writeText(this.wooOwner); + this.notifierService.showNotification("Owner ID copied to clipboard", "Close", "success"); + } catch (err) { + this.notifierService.showNotification("Copying not available in your browser", "Close", "error"); + } + } + + copyWooToken(){ + try { + navigator.clipboard.writeText(this.wooToken); + this.notifierService.showNotification("WooCommerce Token copied to clipboard", "Close", "success"); + } catch (err) { + this.notifierService.showNotification("Copying not available in your browser", "Close", "error"); + } + } + + generateWooToken(){ + this.wooService.createWooToken(this.owner._id!); + this.wooService.getWooToken(this.owner._id!); + } + saveAccCod() { this.xeroAccCod = this.accCodForm.value.xAcc; diff --git a/src/app/woocommerce.service.spec.ts b/src/app/woocommerce.service.spec.ts new file mode 100644 index 0000000..500c1f1 --- /dev/null +++ b/src/app/woocommerce.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { WoocommerceService } from './woocommerce.service'; + +describe('WoocommerceService', () => { + let service: WoocommerceService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(WoocommerceService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/woocommerce.service.ts b/src/app/woocommerce.service.ts new file mode 100644 index 0000000..b519c17 --- /dev/null +++ b/src/app/woocommerce.service.ts @@ -0,0 +1,65 @@ +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 WoocommerceService { + beUrl = ConfigData.Be_URL; + private reqHeaders: HttpHeaders; + private ownerId: string = ''; + private token: string = ''; + private siteurl: string = ''; + private _ownerIdUpdated: BehaviorSubject = new BehaviorSubject(this.ownerId); + private _tokenUpdated: BehaviorSubject = new BehaviorSubject(this.token); + private _siteurlUpdated: BehaviorSubject = new BehaviorSubject(this.siteurl); + public readonly ownerUpdate: Observable = this._ownerIdUpdated.asObservable(); + public readonly tokenUpdate: Observable = this._tokenUpdated.asObservable(); + public readonly siteurlUpdate: Observable = this._siteurlUpdated.asObservable(); + + constructor( + private http: HttpClient + ) { + var auth = 'Basic ' + Buffer.from(ConfigData.UsrPwd).toString('base64'); + this.reqHeaders = new HttpHeaders().set('Authorization', auth); + this._ownerIdUpdated.next(Object.assign({}, this).ownerId); + this._tokenUpdated.next(Object.assign({}, this).token); + this._siteurlUpdated.next(Object.assign({}, this).siteurl); + } + + getWooToken(ownerId: string) { + const params = new HttpParams().append('ownerid', ownerId); + let obs = this.http.get<{ownerid: string, token: string, siteurl: string}>(this.beUrl + 'api/wootoken', {headers: this.reqHeaders, params: params, observe: 'response'}); + obs.subscribe(tokenResponse => { + if (tokenResponse.status == 200) { + this.ownerId = tokenResponse.body!.ownerid; + this.token = tokenResponse.body!.token; + this.siteurl = tokenResponse.body!.siteurl; + this._ownerIdUpdated.next(Object.assign({}, this).ownerId); + this._tokenUpdated.next(Object.assign({}, this).token); + this._siteurlUpdated.next(Object.assign({}, this).siteurl); + } else { + console.log('No WooCommerce token found'); + } + }); + return obs; + } + + createWooToken(ownerId: string) { + const params = new HttpParams().append('ownerid', ownerId); + let obs = this.http.post(this.beUrl+'api/wootoken', {}, {headers: this.reqHeaders, params: params, observe: 'response'}); + obs.subscribe(responseData => { + if (responseData.status == 202) { + console.log('WooToken created.'); + } else { + console.log('WooToken creation failed.'); + } + }); + + } +}