Harden clean architecture and domain workflows - #42
Merged
Conversation
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.
What this PR fixes
This PR fixes several cases where the system could save conflicting or invalid data. Authentication is intentionally not part of this change.
Borrowing a book
Previously, a client could create a loan directly and provide its own patron email, book title, and loan length. This allowed loans for missing patrons or books and could bypass borrowing limits.
Now:
When this PR says Lending is authoritative, it means Lending is the single source of truth for loan decisions. Catalog tracks whether a physical book is available, but it does not independently decide whether a loan exists or has ended.
Protecting a newer reservation from old messages
Borrowing happens through background messages, so an old delayed message could previously confirm or cancel a newer patron's reservation.
Every reservation attempt now has:
Every later step must match all of those values before changing the book or loan.
This safety check is sometimes called fencing. A simple way to think about it is a numbered ticket: a message holding ticket 1 cannot change work that now belongs to ticket 2.
Returning a book
Previously, the book endpoint and loan endpoint could each record only half of a return. That could leave an available book with an active loan, or a returned loan with a book still marked as borrowed.
Now the loan return is the only public return command:
A delayed return message for an older loan cannot return a book that has since been borrowed again.
Reliable background processing
Background messages now have stable names and version numbers. This lets newer code understand older saved messages and prevents a class rename from silently breaking message delivery.
For every message handler, the database records whether that exact handler has already completed the message. This means:
Database and deployment safety
API retries and read performance
Why this matters
After these changes, Catalog and Lending cannot be changed independently through public commands. Old background messages cannot overwrite newer work, request retries are safe, invalid data is rejected at both the domain and database levels, and correctness-critical workers are part of the normal deployment.
Validation
009Authentication remains unchanged and is outside the scope of this PR.