Jelajahi Sumber

reorganize code

Enzo 1 tahun lalu
induk
melakukan
b21ef6abc9
1 mengubah file dengan 33 tambahan dan 36 penghapusan
  1. 33 36
      services/message-auditor.service.ts

+ 33 - 36
services/message-auditor.service.ts

@@ -33,7 +33,7 @@ export class MessageAuditorService implements MessageAuditorServiceInterface {
                 if (!this.settings.filters) {
                     console.log(`No filters applies`)
                 } else {
-                    console.log(`Synchronizating with filters: ${Object.keys(this.settings.filters)}: ${Object.values(this.settings.filters)}`)
+                    console.log(`Synchronizating with filters: '${Object.keys(this.settings.filters)}': '${Object.values(this.settings.filters)}'`)
                 }
                 let missingMsg: Observable<MessageLog> = this.synchronize()
                 missingMsg.subscribe({
@@ -54,33 +54,6 @@ export class MessageAuditorService implements MessageAuditorServiceInterface {
         let response: boolean = true //Just using this like a statemanagement
         let payload: BaseMessage = JSON.parse(message.appData.msgPayload as string) // Extract the payload from the messageLog first
         // Making a separate function to cater to different multi filters conditions are coded below
-        function checkValues(filters): boolean { //FYI, all parameters are string
-            let key = Object.keys(filters)
-            // console.log(Object.values(filters))
-            let value = Object.values(filters)[0]
-            let res = _.get(payload, key[0])
-            // Check first if the payload has the filtering properties/path
-            if (_.has(payload, key[0])) {
-                // check if value is equal to fitler's
-                let strarray: string[]
-                // check array 
-                if (Array.isArray(value)) {
-                    strarray = value as string[]
-                }
-                else {
-                    strarray = [value as string]
-                }
-                // compare array with that string 
-                if (strarray.includes(res)) {
-                    return true
-                } else {
-                    return false
-                }
-            } else {
-                console.log(`${key} does not exists in payload`)
-                return false
-            }
-        }
         if (filters) { // if filters is not null
             if (Object.keys(filters).length > 1) {
                 let totalCount = Object.keys(filters).length
@@ -88,21 +61,21 @@ export class MessageAuditorService implements MessageAuditorServiceInterface {
                 Object.entries(filters).forEach(([key, value]) => {
                     let filters = { [key]: value }
                     // console.log(filters)
-                    if (checkValues(filters) == true) matchedCount++
+                    if (this.checkValues(payload, filters) == true) matchedCount++
                 })
-                if (totalCount == matchedCount) {
+                if (totalCount == matchedCount) { // check if all the criterias are met
                     response = true
                 } else {
                     response = false
                 }
             } else {
-                if (checkValues(filters) == true) {
+                if (this.checkValues(payload, filters) == true) {
                     response = true
                 } else {
                     response = false
                 }
             }
-        } else {
+        } else { // if not filters is provided. Then the just let response be true so that the data can be further processed
             response = true
         }
         return response
@@ -131,10 +104,7 @@ export class MessageAuditorService implements MessageAuditorServiceInterface {
     private async acquireData(): Promise<any> {
         const promiseQuery: Promise<any> = new Promise((resolve, reject) => {
             // declare what to expect.
-            let allSets: {
-                arr1: MessageLog[],
-                arr2: MessageLog[]
-            } = {
+            let allSets: { arr1: MessageLog[], arr2: MessageLog[] } = {
                 arr1: [],
                 arr2: []
             }
@@ -186,5 +156,32 @@ export class MessageAuditorService implements MessageAuditorServiceInterface {
         })
     }
 
+    // To be used by the filterData function to check between payload values and filter conditions
+    private checkValues(payload, filters): boolean { //FYI, all parameters are string
+        let key = Object.keys(filters)
+        // console.log(Object.values(filters))
+        let value = Object.values(filters)[0]
+        let res = _.get(payload, key[0])
+        // Check first if the payload has the filtering properties/path
+        if (_.has(payload, key[0])) {
+            let strarray: string[]
+            // check array 
+            if (Array.isArray(value)) {
+                strarray = value as string[]
+            }
+            else {
+                strarray = [value as string]
+            }
+            // compare array with that string 
+            if (strarray.includes(res)) {
+                return true
+            } else {
+                return false
+            }
+        } else {
+            console.log(`${key} does not exists in payload`)
+            return false
+        }
+    }
 
 }