Skip to content

feat(booking): add the view model for answering booking requests - #379

Merged
lucaschristiansson merged 5 commits into
mainfrom
feature/accept-booking-viewmodel
Nov 19, 2026
Merged

lucaschristiansson merged 5 commits into
mainfrom
feature/accept-booking-viewmodel

Conversation

@lucaschristiansson

@lucaschristiansson lucaschristiansson commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part of #28. Adds ReceivedRequestsViewModel, which loads the booking requests on one of the lender's listings and accepts the one they choose. It builds on the backend from #378, now merged. The screen follows in the next PR.

What

  • ReceivedRequestsViewModel(listingId, bookingRepository, listingRepository, profileRepository, today):
    • loads the listing's bookings and the days it still offers, hides those whose days have all passed, sorts them by first day and splits them into waiting (requests) and accepted;
    • reads each renter's username once, in the background; on failure the request is shown without a name and the name is read again on the next load;
    • accept(request): one at a time (acceptingId), only for a request still waiting whose days are all still offered. On success the request moves to accepted and the requests sharing its days leave the list, then the bookings are read again to catch requests sent since the screen loaded. On failure nothing changes and acceptError says why;
    • loadRequests() for Retry; ignored while a load or an accept runs; keeps the list shown on failure.
  • ReceivedRequestsUiState.canAccept(request): true only when every day of the request is still offered on the listing, as the rules require. A request the owner's edit cut short, or one sharing a day with an accepted booking, stays listed without Accept.
  • ReceivedRequestsUiState.conflictsOf(request): the waiting requests that accepting request cancels, for the confirm dialog in the next PR.
  • FakeBookingRepository: getBookingsForListing returns that listing's listingBookings, acceptBooking confirms and cancels in them like the real repository, and tests can hold back (pendingLoad, acceptGate) or fail (loadFailure, acceptFailure) a read or an accept.

Why

The screen needs a tested place for the loading, ordering, name lookups and accept states, independent of Compose. It depends only on the repository interfaces (no Firebase), per AGENTS.md.

Validation

  • ./gradlew ktfmtCheck check compileDebugAndroidTestKotlin: passes. connectedCheck not run (no emulator).
  • ReceivedRequestsViewModelTest: 16 tests, all pass. They cover loading, ordering and filtering (other listings, cancelled, past, ongoing), conflicts, a failed load and Retry, a failed name read again, a successful accept and its reload, a second accept while one runs, an unknown request, a failed accept that keeps the list, a failed reload after an accept, a load ignored while another runs, a request with a day no longer offered or taken by an accepted booking (listed, not acceptable), the accepted days leaving the offered ones, and a failed listing read with Retry.

Follow-ups

AI use

Claude Code (Claude Opus 5.5) wrote the view model, the fake repository changes and the tests, and reviewed the diff against the team checklist. I reviewed and tested the result.

Accepting confirms the request, takes its days off the listing and
cancels the other requests for those days, in one batched write. The
rules only confirm a request whose days the same write removes, so two
requests sharing a day can never both be accepted.

Part of #28.
Loads the bookings of one listing for its owner, waiting and accepted,
with the renters' names, and accepts the request the owner chooses.

Part of #28.
Base automatically changed from feature/accept-booking-backend to main November 19, 2026 14:44
@lucaschristiansson
lucaschristiansson marked this pull request as ready for review November 19, 2026 14:55
@lucaschristiansson lucaschristiansson self-assigned this Nov 19, 2026

@peng-0110 peng-0110 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, the accept flow and its tests are solid. One thing to fix before the screen PR:

Should fix: show() lists every REQUESTED booking with a day from today on, but isAccept() also requires all its days to still be in the listing's availableDates. Two cases are listed with an Accept the rules will always refuse: (1) an ongoing request after the owner edits the listing, because saving drops past days through upcomingDays; (2) a request left REQUESTED by the accept race noted in #378, which overlaps a CONFIRMED booking. I reproduced both: the ViewModel lists them, and the emulator refuses both accepts. The lender would get a permission error with no way out. Suggest reading the listing's availableDates and only offering Accept when all the request's days are still offered.

@lucaschristiansson

Copy link
Copy Markdown
Contributor Author

@peng-0110
Good catch, thanks for reproducing both. Fixed: the ViewModel now reads the listing's availableDates with the bookings, and canAccept(request) is true only when all of the request's days are still offered, the same check the rules make. Requests that fail it stay listed without Accept, and accept() ignores them. Tests cover both of your cases.

@sonarqubecloud

Copy link
Copy Markdown

@peng-0110 peng-0110 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, verified. canAccept mirrors isAccept(), and both cases are covered by tests. Approving.

@lucaschristiansson
lucaschristiansson merged commit 1e341f5 into main Nov 19, 2026
3 checks passed
@lucaschristiansson
lucaschristiansson deleted the feature/accept-booking-viewmodel branch November 19, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants