Description
As a System, I want to orchestrate a multi-step cleanup process that uploads pending data, deletes centre-specific records, and resets the remapping state, So that the device is fully transitioned to the new centre without data loss or inconsistency.
Purpose
To ensure complete, consistent, and irreversible transition from old centre to new centre with all data preserved on server before local deletion.
Pre-requisites
- Centre remapping flag is set (issue-1045)
- No pending approval packets remain
- Device is online
- All previous cleanup steps successful (if resuming)
Basic Flow
Step 1: Verify Preconditions
- System checks if device is online (throws error if offline)
- System checks if any active registration operations in progress (blocks if true)
- System checks GLOBAL_PARAM for remapping flag (must be true)
- System acquires cleanup lock to prevent concurrent cleanups
- If all checks pass, proceeds to Step 2
Step 2: Upload Pending Packets
- System queries all registration packets with status PROCESSING or RE-SEND
- System displays progress dialog: "Uploading packets. Please don't close the application"
- System uploads each packet to server
- On success: Marks packet as uploaded; proceeds to next packet
- On failure: Retries up to 5 times with exponential backoff (1s, 2s, 4s, 8s, 16s)
- If all retries fail: Throws UploadFailedException; stops sync; allows retry
- When all packets uploaded: Updates AUDIT with success; proceeds to Step 3
Step 3: Upload Operator Onboarding Data
- System queries all operator records for current centre
- System uploads operator data (credentials, biometrics, roles) to server
- Retry logic same as Step 2
- On success: Proceeds to Step 4
- On failure: Throws exception; allows retry
Step 4: Delete Pre-registration Data
- System queries all PRE_REGISTRATION records
- System deletes all pre-registration packet files from disk
- System deletes all PRE_REGISTRATION database records (within transaction)
- System deletes associated metadata
- Logs count of deleted records in AUDIT
- Proceeds to Step 5
Step 5: Delete Registration Packets (Specific Statuses Only)
- System queries registration packets with status: PROCESSING, PROCESSED, RE-REGISTER
- Note: DOES NOT delete packets with status RECEIVED or RE-SEND
- System deletes matching packet files
- System deletes matching database records (within transaction)
- Logs detailed deletion counts in AUDIT
- Proceeds to Step 6
Step 6: Delete Centre-Specific Master Data
- System deletes centre-specific document types
- System deletes centre-specific device registrations
- System deletes centre-specific templates/configurations
- Note: DOES NOT delete GLOBAL_PARAM (global) or AUDIT (immutable)
- Uses transaction to ensure atomicity
- Logs deletion summary in AUDIT
- Proceeds to Step 7
Step 7: Remove All Operators for Current Centre
- System queries all OPERATOR records for current centre
- System deletes OPERATOR records
- System deletes OPERATOR_BIOMETRIC records
- System deletes OPERATOR_ROLE records
- System deletes operator preferences
- If current operator is logged in: Forces logout
- Logs operator IDs deleted in AUDIT
- Proceeds to Step 8
Step 8: Reset Remapping Flag and Re-enable Auto-sync
- System updates GLOBAL_PARAM: sets
mosip.registration.centermappedchanged to 'false'
- System clears SessionContext.centreRemapped flag
- System re-enables automatic sync scheduling
- System releases cleanup lock
- Logs completion with full statistics in AUDIT
- Displays success message on screen
Alternate Flows
Alternate Flow A1: Offline During Sync
- Sync is running (e.g., at Step 3 uploading operator data)
- Device loses connectivity
- System detects offline condition
- Current step is rolled back (transaction)
- System pauses cleanup and saves state (last completed step = 2)
- System displays dialog: "System went offline. Sync paused. Will resume when online."
- When connectivity restored:
- System displays: "Connectivity restored. Resuming cleanup..."
- System resumes from Step 3 (continues from next step after last completed)
- Remaining steps execute normally
- Cleanup completes successfully
Alternate Flow A2: Cleanup Failure at Any Step
- Step fails (e.g., database deletion throws error)
- System catches exception
- System rolls back transaction (no partial deletion)
- System saves failure state in CLEANUP_STATE table
- System displays error dialog with user-friendly message
- Dialog includes:
- What failed (e.g., "Failed to delete pre-registration data")
- Why it failed (e.g., "Disk space low")
- "RETRY" button to restart from failed step
- "CONTACT SUPPORT" button to show error details
- User clicks RETRY:
- System resumes from failed step
- If successful: Continues normally
- If fails again: Shows error again with retry option
Alternate Flow A3: App Crashes During Cleanup
- Cleanup is running (e.g., at Step 4)
- App crashes or is terminated
- Cleanup state is saved to CLEANUP_STATE table (before each step)
- On app restart:
- System detects incomplete cleanup (flag still true)
- System shows dialog: "Previous cleanup interrupted. Resume?"
- Options: "RESUME" (continues from last completed step) or "START OVER"
- If user selects RESUME:
- Loads cleanup state from database
- Continues from Step 5 (last completed was Step 4)
- Completes normally
Alternate Flow A4: Multiple Cleanup Attempts
- First cleanup attempt partially succeeds (completes Steps 1-4, fails on Step 5)
- User retries cleanup
- System resumes from Step 5
- Completes remaining steps
- Success
Alternate Flow A5: Cleanup Completes Successfully
- All 8 steps complete
- System displays success dialog with statistics:
- Pre-registration packets deleted: 23
- Registration packets deleted: 156
- Operators removed: 5
- Cleanup completed at: 2:45 PM
- Dialog has "CLOSE" button
- User clicks CLOSE
- System navigates to home screen
- All blocked operations now available
- Centre Remapping menu option disappears
Scenarios
Scenarios
| Scenario |
Flow |
Expected Result |
| S1 |
Normal cleanup: online, no interruptions |
All steps complete; success message |
| S2 |
Offline at Step 3 |
Pause and resume when online; complete |
| S3 |
Failure at Step 5 (database error) |
Error shown; retry available |
| S4 |
App crash at Step 4 |
State saved; resume option on restart |
| S5 |
User retries from failed attempt |
Resume from failure point; complete |
| S6 |
Network timeout during upload (Step 2) |
Auto-retry with backoff; eventually succeed |
Error Messages
| Step |
Error Condition |
Message |
Recovery |
| Step 1-2 |
Upload fails after retries |
"Failed to upload packets. Please check your connection and retry." |
Retry button |
| Step 4 |
Disk space insufficient |
"Insufficient storage for sync. Please free up space." |
Manual cleanup of files; retry |
| Step 6 |
Database locked |
"System busy. Please wait and retry." |
Automatic retry |
| General |
Network offline during cleanup |
"Device went offline. Sync will resume when online." |
Auto-resume |
| General |
Permission denied |
"System permission required for sync. Please contact administrator." |
Contact support |
Data Fields
| Field |
Purpose |
Updated At |
| Last completed step |
Tracks progress for resume |
After each step |
| Error details |
Logging and recovery |
When error occurs |
| Deleted packet count |
Audit statistics |
After deletion step |
| Deleted operator count |
Audit statistics |
After operator deletion |
| Cleanup start timestamp |
Audit trail |
Step 1 |
| Cleanup end timestamp |
Audit trail |
Step 8 |
Business Rules (With Audit)
BR-001: Cleanup MUST execute steps in exact order; no skipping or reordering
BR-002: Before cleanup starts:
- Device MUST be online
- No active registration operations
- Remapping flag MUST be true
- No pending approval packets
BR-003: Each deletion MUST be atomic (transaction); no partial deletions
BR-004: Upload MUST complete before any deletion begins (data safety first)
BR-005: Operators cannot login after deletion (Step 7)
BR-006: GLOBAL_PARAM and AUDIT tables MUST NEVER be deleted
BR-007: Cleanup state MUST be persisted after each step (for resume capability)
BR-008: All cleanup steps MUST be logged in AUDIT table
BR-009: On success, remapping flag MUST be reset (Step 8)
BR-010: Auto-sync MUST be re-enabled after success (Step 8)
Acceptance Criteria
| Criteria |
Verification |
| All steps execute in correct order |
Audit logs show sequence |
| No data loss; all uploaded packets before deletion |
Verify server has data; local deleted |
| Offline detected and cleanup paused |
Disconnect network; observe pause |
| Cleanup resumed correctly after offline |
Restore connection; verify continuation |
| Failure at any step shows error; allows retry |
Simulate failure; verify error dialog |
| App crash saves state; resume option works |
Force crash; restart app; verify resume |
| Success message shows accurate statistics |
Calculate expected counts; verify displayed |
| GLOBAL_PARAM and AUDIT untouched |
Query database post-cleanup |
| Operators cannot login post-cleanup |
Attempt login with old credentials; verify blocked |
| Auto-sync re-enabled after success |
Verify sync scheduler running |
Exceptions
| Exception |
Handling |
Recovery |
| Offline detected |
Roll back current transaction; pause |
Resume when online |
| Upload failure |
Retry up to 5x with backoff |
Allow manual retry if all fail |
| Database error |
Roll back transaction; log error |
Manual retry after fixing issue |
| Permission denied |
Log error; show message |
Contact support; manual intervention |
| App crash |
Save state to database |
Auto-resume on restart |
Documentation
No response
Reference UX
No response
Compatibility
No response
Implementation Details
Files to Create:
lib/services/registration/centre_remapping_service.dart (main orchestration)
lib/models/remapping_result.dart
lib/models/remapping_stats.dart
lib/models/cleanup_progress.dart
lib/models/cleanup_state.dart
Files to Modify:
lib/repositories/registration_packet_repository.dart - Add queries and delete methods
lib/repositories/pre_registration_repository.dart - Add delete methods
lib/repositories/operator_repository.dart - Add delete methods
lib/repositories/cleanup_state_repository.dart - Create for state persistence
lib/services/sync_service.dart - Use for packet upload
lib/utils/audit_logger.dart - Add cleanup event types
lib/utils/session_context.dart - Track cleanup progress
lib/db/database_helper.dart - Transaction management
Key Implementation Points:
- Use database transactions for atomicity
- Implement step-by-step state saving for resume capability
- Network connectivity monitoring for pause/resume
- Comprehensive audit logging at each step
- User-friendly error messages with recovery options
Definition of Done
Non-Functional Requirements
| Requirement |
Specification |
| Sync Duration |
<15 minutes for 500 packets |
| Data Consistency |
100% accuracy; no corruption |
| Reliability |
No data loss even on crash |
| Auditability |
Complete trail of all operations |
| Error Recovery |
Auto-resume capability |
| Security |
Only authenticated cleanup; no bypass |
Description
As a System, I want to orchestrate a multi-step cleanup process that uploads pending data, deletes centre-specific records, and resets the remapping state, So that the device is fully transitioned to the new centre without data loss or inconsistency.
Purpose
To ensure complete, consistent, and irreversible transition from old centre to new centre with all data preserved on server before local deletion.
Pre-requisites
Basic Flow
Step 1: Verify Preconditions
Step 2: Upload Pending Packets
Step 3: Upload Operator Onboarding Data
Step 4: Delete Pre-registration Data
Step 5: Delete Registration Packets (Specific Statuses Only)
Step 6: Delete Centre-Specific Master Data
Step 7: Remove All Operators for Current Centre
Step 8: Reset Remapping Flag and Re-enable Auto-sync
mosip.registration.centermappedchangedto 'false'Alternate Flows
Alternate Flow A1: Offline During Sync
Alternate Flow A2: Cleanup Failure at Any Step
Alternate Flow A3: App Crashes During Cleanup
Alternate Flow A4: Multiple Cleanup Attempts
Alternate Flow A5: Cleanup Completes Successfully
Scenarios
Scenarios
Error Messages
Data Fields
Business Rules (With Audit)
BR-001: Cleanup MUST execute steps in exact order; no skipping or reordering
BR-002: Before cleanup starts:
BR-003: Each deletion MUST be atomic (transaction); no partial deletions
BR-004: Upload MUST complete before any deletion begins (data safety first)
BR-005: Operators cannot login after deletion (Step 7)
BR-006: GLOBAL_PARAM and AUDIT tables MUST NEVER be deleted
BR-007: Cleanup state MUST be persisted after each step (for resume capability)
BR-008: All cleanup steps MUST be logged in AUDIT table
BR-009: On success, remapping flag MUST be reset (Step 8)
BR-010: Auto-sync MUST be re-enabled after success (Step 8)
Acceptance Criteria
Exceptions
Documentation
No response
Reference UX
No response
Compatibility
No response
Implementation Details
Files to Create:
lib/services/registration/centre_remapping_service.dart(main orchestration)lib/models/remapping_result.dartlib/models/remapping_stats.dartlib/models/cleanup_progress.dartlib/models/cleanup_state.dartFiles to Modify:
lib/repositories/registration_packet_repository.dart- Add queries and delete methodslib/repositories/pre_registration_repository.dart- Add delete methodslib/repositories/operator_repository.dart- Add delete methodslib/repositories/cleanup_state_repository.dart- Create for state persistencelib/services/sync_service.dart- Use for packet uploadlib/utils/audit_logger.dart- Add cleanup event typeslib/utils/session_context.dart- Track cleanup progresslib/db/database_helper.dart- Transaction managementKey Implementation Points:
Definition of Done
Non-Functional Requirements