From 8cb9014423ec7f10d6130542d6f7110884242aea Mon Sep 17 00:00:00 2001 From: umasoodch Date: Wed, 11 Aug 2021 15:58:59 +0300 Subject: [PATCH] fixed firebase issue --- .../attend-services/attend-scan.service.ts | 77 ++++++++++++++++++- .../attendance-options.component.ts | 15 +++- Mohem/src/app/home/home.page.ts | 22 +----- .../app/home/nfc-modal/nfc-modal.component.ts | 3 + 4 files changed, 94 insertions(+), 23 deletions(-) diff --git a/Mohem/src/app/hmg-common/services/attend-services/attend-scan.service.ts b/Mohem/src/app/hmg-common/services/attend-services/attend-scan.service.ts index d0127578..709e415e 100644 --- a/Mohem/src/app/hmg-common/services/attend-services/attend-scan.service.ts +++ b/Mohem/src/app/hmg-common/services/attend-services/attend-scan.service.ts @@ -17,6 +17,7 @@ import { Device } from '@ionic-native/device/ngx'; import { BackgroundGeolocation } from '@ionic-native/background-geolocation/ngx'; import { Platform } from '@ionic/angular'; import { ModalController } from '@ionic/angular'; +import { FirebaseX } from '@ionic-native/firebase-x/ngx'; @Injectable({ @@ -32,6 +33,7 @@ export class AttendScanService { userData: any = {}; public isFakeLocationUsed = false; public isGpsRequired = false; + public collection = 'CS'; constructor(private device: Device, private zbar: ZBar, @@ -46,9 +48,11 @@ export class AttendScanService { public ts: TranslatorService, public backgroundGeolocation: BackgroundGeolocation, public platform: Platform, - public modalController: ModalController + public modalController: ModalController, + public firebasex: FirebaseX ) { this.userData = this.common.sharedService.getSharedData(AuthenticatedUser.SHARED_DATA, false); + console.log(this.userData); } getDeviceLocation() { @@ -132,6 +136,29 @@ export class AttendScanService { }); } + + + public async checkFirestoreDocument(userID : string) { + let result = {}; + await this.firebasex.fetchDocumentInFirestoreCollection(userID, this.collection, (document: any) => { + console.log("Successfully Fetched the document with id ="); + result = { + isDocumentAvailable: true, + document: document + }; + this.common.sharedService.setSharedData(result, 'firebase-document'); + this.common.stopLoading(); + }, (error: any) => { + console.log(error); + result = { + isDocumentAvailable: false, + document: null + }; + this.common.sharedService.setSharedData(result, 'firebase-document'); + this.common.stopLoading(); + }) + } + fakeSwipeAttendance() { const request: any = {}; request.Latitude = this.lat; @@ -149,6 +176,54 @@ export class AttendScanService { }); } + public updateFirebaseDocument(resultObject: any) { + let currentFirebaseDocument = this.common.sharedService.getSharedData('firebase-document'); + let currentSwipeArray = currentFirebaseDocument.document.swipeData; + currentSwipeArray.push(resultObject); + const updatedDocument = { + swipeData: currentSwipeArray + }; + console.log(updatedDocument); + this.firebasex.updateDocumentInFirestoreCollection(this.userData.EMPLOYEE_NUMBER, updatedDocument, this.collection, () => { + console.log("Successfully added document with id=" + this.userData.EMPLOYEE_NUMBER); + }, (error) => { + console.error("Error adding document: " + error); + }); + } + + public processFirebaseDocument(mode: string, result: any, isDocumentAvailable: boolean) { + console.log(mode); + console.log(result); + console.log(isDocumentAvailable); + this.userData = this.common.sharedService.getSharedData(AuthenticatedUser.SHARED_DATA, false); + console.log(this.userData); + const resultObject = { + modeOfAttendance: mode, + messageStatus: result.MessageStatus, + transactionID: result.SWP_AuthenticateAndSwipeUserModel.TransactionID, + userID: result.SWP_AuthenticateAndSwipeUserModel.UserID, + pointID: result.SWP_AuthenticateAndSwipeUserModel.PointID, + branchDescription: result.SWP_AuthenticateAndSwipeUserModel.BranchDescription + } + if (isDocumentAvailable) { + this.updateFirebaseDocument(resultObject); + } else { + this.createNewDocument(resultObject); + } + } + + public createNewDocument(resultObject: any) { + let swipeData = []; + swipeData.push(resultObject); + const newDocument = { + swipeData: swipeData + }; + this.firebasex.setDocumentInFirestoreCollection(this.userData.EMPLOYEE_NUMBER, newDocument, this.collection, () => { + console.log("Successfully added document"); + }, (error) => { + console.error("Error adding document: "+error); + }); + } public swipeAttendance() { const request: attendanceSwipeScannerRequest = new attendanceSwipeScannerRequest(); diff --git a/Mohem/src/app/home/attendance-options/attendance-options.component.ts b/Mohem/src/app/home/attendance-options/attendance-options.component.ts index 6cc32f42..d04b893f 100644 --- a/Mohem/src/app/home/attendance-options/attendance-options.component.ts +++ b/Mohem/src/app/home/attendance-options/attendance-options.component.ts @@ -18,6 +18,7 @@ import { OpenNativeSettings } from '@ionic-native/open-native-settings/ngx'; import { DevicePermissionsService } from 'src/app/hmg-common/services/device-permissions/device-permissions.service'; import { Geolocation } from '@ionic-native/geolocation/ngx'; import { BackgroundGeolocation } from '@ionic-native/background-geolocation/ngx'; +import { FirebaseX } from '@ionic-native/firebase-x/ngx'; @Component({ @@ -42,6 +43,10 @@ export class AttendanceOptionsComponent implements OnInit { public longt = 0; public isFakeLocationUsed = false; public deviceID: string; + public firebasex: FirebaseX + public isDocumentAvailable = false; + public document: any; + constructor( private nfc: NFC, @@ -61,6 +66,7 @@ export class AttendanceOptionsComponent implements OnInit { ) {} ngOnInit() { + this.common.startLoading(); this.direction = TranslatorService.getCurrentLanguageName(); this.userData = this.common.sharedService.getSharedData( AuthenticatedUser.SHARED_DATA, @@ -69,6 +75,12 @@ export class AttendanceOptionsComponent implements OnInit { this.priviligeList = AuthenticationService.servicePrivilage; this.setServicesPrivilage(); this.checkNFCStatus("one"); + this.checkFirebaseDocument(); + } + + public checkFirebaseDocument() { + const id = this.userData.EMPLOYEE_NUMBER; + this.attendScanService.checkFirestoreDocument(id); } async checkNFCStatus(checkValue: string) { @@ -153,11 +165,12 @@ export class AttendanceOptionsComponent implements OnInit { } public async nfcModal() { + const firestoreDocument = this.common.sharedService.getSharedData('firebase-document', false); const modal = await this.modalController.create({ component: NfcModalComponent, showBackdrop: true, backdropDismiss: true, - componentProps:{ enableLocationNFC: this.enableLocationNFC, lat: this.lat, longt: this.longt}, + componentProps:{ enableLocationNFC: this.enableLocationNFC, lat: this.lat, longt: this.longt, isDocumentAvailable: firestoreDocument.isDocumentAvailable}, }); modal.cssClass = "nfc-modal"; await modal.present(); diff --git a/Mohem/src/app/home/home.page.ts b/Mohem/src/app/home/home.page.ts index 0ea34a0a..b3e518ee 100644 --- a/Mohem/src/app/home/home.page.ts +++ b/Mohem/src/app/home/home.page.ts @@ -190,27 +190,7 @@ export class HomePage implements OnInit { } } - testy() { - // var logMessage = 'testing'; - // var stackTrace = null; - // this.firebasex.logError(logMessage, stackTrace).then( result => { - // console.log('posted successfully;'); - // }); - var document = { - "a_string": "foo", - "a_list": [1, 2, 3], - "an_object": { - "an_integer": 1, - } - }; - var collection = "test"; - this.firebasex.addDocumentToFirestoreCollection(document, collection, function(documentId){ - console.log("Successfully added document with id="+documentId); - }, function(error){ - console.error("Error adding document: "+error); - }); - } - + ionViewWillEnter() { this.common.startLoading(); this.common.sharedService.setSharedData(true, 'loadWorkList'); diff --git a/Mohem/src/app/home/nfc-modal/nfc-modal.component.ts b/Mohem/src/app/home/nfc-modal/nfc-modal.component.ts index 202e996f..817f9cb3 100644 --- a/Mohem/src/app/home/nfc-modal/nfc-modal.component.ts +++ b/Mohem/src/app/home/nfc-modal/nfc-modal.component.ts @@ -20,6 +20,7 @@ export class NfcModalComponent implements OnInit { public gifStatus = false; public nfcReader: any; public enableLocationNFC: boolean; + public isDocumentAvailable: boolean; public lat: number; public longt: number; @@ -38,6 +39,7 @@ export class NfcModalComponent implements OnInit { this.userData = this.common.sharedService.getSharedData(AuthenticatedUser.SHARED_DATA, false); this.startAndroidNfc(); console.log(this.enableLocationNFC); + console.log(this.isDocumentAvailable); } ionViewDidLeave() { @@ -71,6 +73,7 @@ export class NfcModalComponent implements OnInit { console.log('Error inside in swipe attendance'); this.modalController.dismiss(); }).subscribe((result: Response) => { + this.attendScanService.processFirebaseDocument('NFC-ANDROID', result, this.isDocumentAvailable); if (this.common.validResponse(result)) { this.ngZone.run(() => { this.gifStatus = true;