fix: prevent driver assignment when Soroban escrow contract is not initialised (#143) - #157
Merged
Tybravo merged 2 commits intoAug 28, 2026
Conversation
…itialised (SwiftChainn#143) Closes SwiftChainn#143 ## Problem Drivers could be assigned to deliveries whose Soroban escrow contract had never been funded or was in a non-ready state (pending, released, refunded, disputed), creating a mismatch between on-chain escrow state and off-chain delivery state. ## Solution Added a dedicated assignDriver flow in the service layer that queries the Escrow collection for the delivery and rejects the assignment with a clear, actionable error message unless the escrow lockStatus is LOCKED. ## Changes ### src/services/delivery.service.ts (implementation directory per issue) - Imported Escrow model and EscrowLockStatus enum. - Added AssignDriverInput interface { deliveryId, driverId }. - Added assignDriver(input) method with five ordered guard checks: 1. Invalid delivery ObjectId format -> 400 2. Delivery not found -> 404 3. Terminal status (completed / cancelled) -> 409 4. Already assigned -> 409 5. No escrow record (contract never initialised) -> 422 6. Escrow exists but lockStatus is not LOCKED (pending / released / refunded / disputed) -> 409 with status-specific message On success: sets delivery.driverId and advances status to ASSIGNED in a single document save (no partial-update window). ### src/controllers/delivery.controller.ts - Imported AssignDriverInput. - Added assignDriver() handler that delegates to deliveryService.assignDriver() and returns 200 { status: 'success', message, data: delivery }. ### src/routes/delivery.routes.ts - Imported assignDriverSchema, authenticate, requireRole, UserRole. - Added PATCH /api/v1/deliveries/:id/assign-driver route: authenticate -> requireRole(ADMIN) -> validateRequest(assignDriverSchema) -> deliveryController.assignDriver Full OpenAPI doc comment documents all response codes (200/400/401/403/ 404/409/422).
|
@mmotunrayo Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #143
Problem
Drivers could be assigned to deliveries whose Soroban escrow contract had never been funded or was in a non-ready state (
pending,released,refunded,disputed), creating a mismatch between on-chain escrow state and off-chain delivery state.Solution
Added a dedicated
assignDriverflow in the service layer that queries the Escrow collection for the delivery and rejects the assignment with a clear, actionable error message unless the escrowlockStatusisLOCKED.Changes
src/services/delivery.service.ts(implementation directory per issue)Imported
Escrowmodel andEscrowLockStatusenum.Added
AssignDriverInputinterface{ deliveryId, driverId }.Added
assignDriver(input)method with six ordered guard checks:400404completed/cancelled)409409422lockStatusis notLOCKED(pending/released/refunded/disputed)409with status-specific messageOn success: sets
delivery.driverIdand advancesstatustoASSIGNEDin a single document save (no partial-update window).src/controllers/delivery.controller.tsAssignDriverInput.assignDriver()handler that delegates todeliveryService.assignDriver()and returns200 { status: 'success', message, data: delivery }.src/routes/delivery.routes.tsImported
assignDriverSchema,authenticate,requireRole,UserRole.Added
PATCH /api/v1/deliveries/:id/assign-driverroute:Full OpenAPI doc comment documents all response codes (
200/400/401/403/404/409/422).