@@ -17,6 +17,33 @@ const TOS_RESTRICTED_ATTRIBUTES = [
1717 "vehicleWaypoints" ,
1818] ;
1919
20+ /**
21+ * Recursively sorts the keys of an object or objects within an array,
22+ * ensuring a consistent order for display and comparison.
23+ * @param {* } data The object or array to sort.
24+ * @returns {* } The sorted object or array.
25+ */
26+ export function sortObjectKeysRecursively ( data ) {
27+ const _sort = ( obj ) => {
28+ if ( obj === null || typeof obj !== "object" ) {
29+ return obj ;
30+ }
31+
32+ if ( Array . isArray ( obj ) ) {
33+ return obj . map ( _sort ) ;
34+ }
35+
36+ return Object . keys ( obj )
37+ . sort ( )
38+ . reduce ( ( sorted , key ) => {
39+ sorted [ key ] = _sort ( obj [ key ] ) ;
40+ return sorted ;
41+ } , { } ) ;
42+ } ;
43+
44+ return _sort ( data ) ;
45+ }
46+
2047async function openDB ( ) {
2148 return new Promise ( ( resolve , reject ) => {
2249 const request = indexedDB . open ( DB_NAME , 1 ) ;
@@ -32,8 +59,10 @@ export async function uploadFile(file, index) {
3259 console . log ( `Importing file: ${ file . name } ` ) ;
3360 let parsedData ;
3461 if ( file . name . endsWith ( ".zip" ) ) {
62+ log ( "uploadFile: Processing ZIP file." ) ;
3563 parsedData = await processZipFile ( file ) ;
3664 } else if ( file . name . endsWith ( ".json" ) ) {
65+ log ( "uploadFile: Processing JSON file." ) ;
3766 parsedData = await processJsonFile ( file ) ;
3867 } else {
3968 throw new Error ( "Unsupported file format. Please upload a ZIP or JSON file." ) ;
@@ -128,19 +157,6 @@ async function processJsonFile(file) {
128157export function parseJsonContent ( content ) {
129158 log ( "Parsing JSON content" ) ;
130159
131- const sortObjectKeys = ( obj ) => {
132- if ( obj === null || typeof obj !== "object" || Array . isArray ( obj ) ) {
133- return obj ;
134- }
135-
136- return Object . keys ( obj )
137- . sort ( )
138- . reduce ( ( sorted , key ) => {
139- sorted [ key ] = sortObjectKeys ( obj [ key ] ) ;
140- return sorted ;
141- } , { } ) ;
142- } ;
143-
144160 const processJsonObject = ( obj ) => {
145161 if ( obj === null || typeof obj !== "object" ) return obj ;
146162 if ( Array . isArray ( obj ) ) return obj . map ( processJsonObject ) ;
@@ -186,14 +202,14 @@ export function parseJsonContent(content) {
186202 const parsed = JSON . parse ( content ) ;
187203 const processedData = processJsonObject ( parsed ) ;
188204 log ( "Processed JSON data: removed underscores, flattened value objects, and pruned null/undefined fields" ) ;
189- return sortObjectKeys ( processedData ) ;
205+ return sortObjectKeysRecursively ( processedData ) ;
190206 } catch ( error ) {
191207 log ( "Initial JSON parsing failed, attempting to wrap in array" ) ;
192208 try {
193209 const parsed = JSON . parse ( `[${ content } ]` ) ;
194210 const processedData = processJsonObject ( parsed ) ;
195211 log ( "Processed JSON data in array format" ) ;
196- return sortObjectKeys ( processedData ) ;
212+ return sortObjectKeysRecursively ( processedData ) ;
197213 } catch ( innerError ) {
198214 console . error ( "JSON parsing error:" , innerError ) ;
199215 throw new Error ( `Invalid JSON content: ${ innerError . message } ` ) ;
@@ -271,6 +287,12 @@ export function ensureCorrectFormat(data) {
271287 return true ;
272288 } ) ;
273289
290+ mergedLogs . forEach ( ( row ) => {
291+ if ( row . jsonPayload ) {
292+ row . jsonPayload = sortObjectKeysRecursively ( row . jsonPayload ) ;
293+ }
294+ } ) ;
295+
274296 // Determine the solution type based on the presence of _delivery_vehicle logs
275297 const isLMFS = mergedLogs . some ( ( row ) => row . logName ?. includes ( "_delivery_vehicle" ) ) ;
276298 const solutionType = isLMFS ? "LMFS" : "ODRD" ;
0 commit comments