[PR #4485] [CLOSED] fix: preserve request order when importing and exporting collections #4829

Closed
opened 2026-03-17 02:19:46 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hoppscotch/hoppscotch/pull/4485
Author: @sz-2302
Created: 10/27/2024
Status: Closed

Base: nextHead: fix/import-order


📝 Commits (6)

  • 8d11c04 Ensure correct order and prevent duplicate requests during import of user collections
  • 4063711 Implement consistent request ordering and remove duplicates during import
  • a9b9e66 Adjust schema to improve collection and request indexing
  • b07829d Ensure request order consistency during collection export
  • 11c34dc Correct request ordering and indexing during team collection export
  • 1cf3b9c Fix inconsistencies

📊 Changes

4 files changed (+517 additions, -247 deletions)

View changed files

📝 packages/hoppscotch-backend/prisma/schema.prisma (+2 -1)
📝 packages/hoppscotch-backend/src/user-collection/user-collection.service.ts (+137 -3)
📝 packages/hoppscotch-selfhost-web/src/platform/collections/collections.platform.ts (+249 -221)
📝 packages/hoppscotch-selfhost-web/src/platform/collections/collections.sync.ts (+129 -22)

📄 Description

This pull request addresses the issue (potentially 4407 and 4375) where importing collections while logged in to the self-hosted web results in duplicate and out-of-order requests within collections. The problem was due to asynchronous operations not being properly awaited and orderIndex values not being preserved or utilised correctly during the import and synchronisation process.

What's changed

  • Fixed asynchronous handling in collections.sync.ts

    • Modified the recursivelySyncCollections function to properly handle asynchronous operations using async/await.
      • Replaced forEach loops with for...of loops to ensure that each asynchronous operation completes before proceeding.
      • This change ensures that IDs are set in the store before subscription handlers run, preventing duplicates.
  • Updated IDs in the store after backend creation

    • After creating collections and requests in the backend, the corresponding IDs are now updated in the frontend store immediately.
      • This allows subscription handlers to recognise existing entries and avoid adding duplicates.
  • Preserved and utilised orderIndex in exportedCollectionToHoppCollection

    • Included the orderIndex from the imported data when parsing requests.
    • Sorted the requests array based on orderIndex after mapping.
      • This ensures that requests appear in the correct order within collections.
  • Adjusted subscription handlers in collections.platform.ts

    • Modified setupUserRequestCreatedSubscription to check for existing requests using updated IDs.
      • Prevents duplicates from being added when subscription events are received.
    • Inserted new requests into the store at the correct position based on orderIndex.
      • Ensures that requests maintain the correct order after synchronisation.
  • Backend adjustments in user-collection.service.ts

    • Ensured the backend includes orderIndex in the data sent through subscription events.
      • Allows the frontend to correctly position requests within collections.

File to import:
request_import_test.json

Before fix:
image

After fix:
image

Notes to reviewers

  • Testing Instructions:

    • Import collections while logged in and verify that no duplicates are created and that requests are in the correct order.
    • Refresh the page to ensure that collections and requests persist correctly without duplication or order issues.
    • Test the import functionality while logged out to ensure consistent behaviour.
  • Potential Impact:

    • These changes focus on the import and synchronisation process. Please verify that other functionalities related to collections and requests are unaffected.
  • Code Review Suggestions:

    • Pay special attention to the asynchronous handling in recursivelySyncCollections to ensure that there are no unintended side effects.
    • Review the handling of orderIndex in both the frontend and backend to confirm that it is consistently utilised.
  • Future Improvements:

    • Consider enhancing error handling during the import process to manage any failures gracefully.
    • Explore performance optimisations for handling large collections in future iterations.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hoppscotch/hoppscotch/pull/4485 **Author:** [@sz-2302](https://github.com/sz-2302) **Created:** 10/27/2024 **Status:** ❌ Closed **Base:** `next` ← **Head:** `fix/import-order` --- ### 📝 Commits (6) - [`8d11c04`](https://github.com/hoppscotch/hoppscotch/commit/8d11c04aa41e9c7871f57646b14189929101b7fc) Ensure correct order and prevent duplicate requests during import of user collections - [`4063711`](https://github.com/hoppscotch/hoppscotch/commit/406371191591c1c17f39cf625254df39d279da0a) Implement consistent request ordering and remove duplicates during import - [`a9b9e66`](https://github.com/hoppscotch/hoppscotch/commit/a9b9e664b71c1acbbe6a2d50161ed0fc6b19d8db) Adjust schema to improve collection and request indexing - [`b07829d`](https://github.com/hoppscotch/hoppscotch/commit/b07829d284f8b82a6585f8ab019d447e2156fd28) Ensure request order consistency during collection export - [`11c34dc`](https://github.com/hoppscotch/hoppscotch/commit/11c34dc4e5290ee1ef509e90b3efd92a151211f9) Correct request ordering and indexing during team collection export - [`1cf3b9c`](https://github.com/hoppscotch/hoppscotch/commit/1cf3b9c827d3c14c36bed69b48ec9d44ec3608b7) Fix inconsistencies ### 📊 Changes **4 files changed** (+517 additions, -247 deletions) <details> <summary>View changed files</summary> 📝 `packages/hoppscotch-backend/prisma/schema.prisma` (+2 -1) 📝 `packages/hoppscotch-backend/src/user-collection/user-collection.service.ts` (+137 -3) 📝 `packages/hoppscotch-selfhost-web/src/platform/collections/collections.platform.ts` (+249 -221) 📝 `packages/hoppscotch-selfhost-web/src/platform/collections/collections.sync.ts` (+129 -22) </details> ### 📄 Description This pull request addresses the issue (potentially 4407 and 4375) where importing collections while logged in to the self-hosted web results in duplicate and out-of-order requests within collections. The problem was due to asynchronous operations not being properly awaited and `orderIndex` values not being preserved or utilised correctly during the import and synchronisation process. ### What's changed - **Fixed asynchronous handling in `collections.sync.ts`** - Modified the `recursivelySyncCollections` function to properly handle asynchronous operations using `async/await`. - Replaced `forEach` loops with `for...of` loops to ensure that each asynchronous operation completes before proceeding. - This change ensures that IDs are set in the store before subscription handlers run, preventing duplicates. - **Updated IDs in the store after backend creation** - After creating collections and requests in the backend, the corresponding IDs are now updated in the frontend store immediately. - This allows subscription handlers to recognise existing entries and avoid adding duplicates. - **Preserved and utilised `orderIndex` in `exportedCollectionToHoppCollection`** - Included the `orderIndex` from the imported data when parsing requests. - Sorted the `requests` array based on `orderIndex` after mapping. - This ensures that requests appear in the correct order within collections. - **Adjusted subscription handlers in `collections.platform.ts`** - Modified `setupUserRequestCreatedSubscription` to check for existing requests using updated IDs. - Prevents duplicates from being added when subscription events are received. - Inserted new requests into the store at the correct position based on `orderIndex`. - Ensures that requests maintain the correct order after synchronisation. - **Backend adjustments in `user-collection.service.ts`** - Ensured the backend includes `orderIndex` in the data sent through subscription events. - Allows the frontend to correctly position requests within collections. File to import: [request_import_test.json](https://github.com/user-attachments/files/17535830/request_import_test.json) Before fix: <img width="236" alt="image" src="https://github.com/user-attachments/assets/b840788d-5ab2-4888-8f60-0c03114e4a4e"> After fix: <img width="229" alt="image" src="https://github.com/user-attachments/assets/79c5c478-5291-4bc0-b09e-624e401b397b"> ### Notes to reviewers - **Testing Instructions:** - Import collections while logged in and verify that no duplicates are created and that requests are in the correct order. - Refresh the page to ensure that collections and requests persist correctly without duplication or order issues. - Test the import functionality while logged out to ensure consistent behaviour. - **Potential Impact:** - These changes focus on the import and synchronisation process. Please verify that other functionalities related to collections and requests are unaffected. - **Code Review Suggestions:** - Pay special attention to the asynchronous handling in `recursivelySyncCollections` to ensure that there are no unintended side effects. - Review the handling of `orderIndex` in both the frontend and backend to confirm that it is consistently utilised. - **Future Improvements:** - Consider enhancing error handling during the import process to manage any failures gracefully. - Explore performance optimisations for handling large collections in future iterations. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-17 02:19:46 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hoppscotch#4829
No description provided.