Skip to content

feat(booking): let lenders accept booking requests - #378

Merged
lucaschristiansson merged 3 commits into
mainfrom
feature/accept-booking-backend
Nov 19, 2026
Merged

lucaschristiansson merged 3 commits into
mainfrom
feature/accept-booking-backend

Conversation

@lucaschristiansson

Copy link
Copy Markdown
Contributor

Summary

Part of #28. Adds the backend for a lender accepting a booking request: the request is confirmed, its days stop being bookable, and the other requests for those days are cancelled. No screen calls it yet; the ViewModel and the screen follow in two stacked PRs.

What

  • BookingRepository.acceptBooking(booking), implemented in FirebaseBookingRepository as one batched write:
    • the request becomes CONFIRMED;
    • its days are removed from the listing's availableDates (arrayRemove), so the existing create rule refuses new requests for them and the "Choose dates" calendar no longer offers them;
    • every other REQUESTED booking on the listing that shares a day becomes CANCELLED. The bookings are read again at accept time, so requests sent since the screen loaded are cancelled too.
  • Booking.overlaps(other): whether two bookings share a day.
  • firestore.rules: the booking update rule now has two branches, and only status may change:
    • isCancel(): feat(booking): allow renters and lenders to cancel bookings #363's rule, unchanged (renter or lender cancels a requested or confirmed booking);
    • isAccept(): the lender confirms a request only if all its days are offered before the write (get) and none remain after it (getAfter). The days can only be removed once, so two requests sharing a day can never both be accepted, even if accepted at the same time.
  • Small tidy-up in FirebaseBookingRepository: the four existing methods use a shared signedInUid helper and BOOKINGS/STATUS constants. Behaviour is unchanged.
  • FakeBookingRepository.acceptBooking is a no-op for now; the ViewModel PR adds the behaviour its tests need.

Why

Issue #28: a lender must be able to accept requests, and "given a confirmed booking, overlapping requests are rejected". Removing the accepted days from availableDates reuses the existing create rule and calendar instead of adding a separate "booked days" field, and the rules make double booking impossible even from a modified client.

Validation

  • ./gradlew ktfmtCheck check: passes.
  • ./gradlew compileDebugAndroidTestKotlin: passes. connectedCheck not run (no emulator).
  • Rules tests (rules-tests, npm test): 202 pass, 0 fail. New tests:
    • accepting confirms the request, cancels the overlapping one, leaves the others, and removes exactly its days (accepted days can no longer be requested, others still can);
    • a confirm is refused unless the same write removes all its days;
    • two requests sharing a day cannot both be accepted;
    • only the verified lender accepts, and only a request still REQUESTED.
  • Unit tests for acceptBooking: batch contents and order, refused commit, signed-out / unstored / non-requested refusals, and overlaps.

Follow-ups

AI use

Claude Code (Claude Opus 5.5) wrote the repository method, the Firestore rule and all the tests in this PR, combined them with #363's cancel rule, 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.
thanna23
thanna23 previously approved these changes Nov 19, 2026

@thanna23 thanna23 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.

Really clean work. Having the rules check get before the write and getAfter after it is a neat way to make double booking impossible even from a modified client, and the rules tests cover it well. The signedInUid tidy-up is nice too. I left a question about the accept batch being refused if a renter cancels an overlapping request at the same moment; it's rare, but worth handling here or in the ViewModel PR. Approving !

Comment thread firestore.rules Outdated
// the other requests for its days, through this rule.
function isCancel() {
return (resource.data.lenderUid == request.auth.uid || resource.data.renterUid == request.auth.uid)
&& resource.data.status in ['REQUESTED', 'CONFIRMED']

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.

Question: Could the whole accept fail if a renter cancels their overlapping request at the same moment? acceptBooking reads the requests first, then writes them all as one batch. If a renter cancels an overlapping request between the read and the write, the batch still tries to set that request to CANCELLED. isCancel() only allows that from REQUESTED or CONFIRMED, so the rules refuse the update, and with it the whole batch. The lender then sees a generic permission error, even though their accept was valid. Trying again would work, because the read runs again. It's rare, but the lender could read it as "you're not allowed to accept".

Suggested fix: Either let a booking that's already CANCELLED be set to CANCELLED again, or note the case so the next PR's ViewModel shows a "try again" message for a refused accept. A rules test where one overlapping request is cancelled before the accept batch would cover it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed here: the rules now treat cancelling an already-cancelled booking as a no-op, with a rules test for this race. I also leave finished overlapping requests alone, which could fail the accept the same way.

@sonarqubecloud

Copy link
Copy Markdown

@thanna23 thanna23 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.

Looks good to me. The accept can no longer fail because a renter cancels at the same moment, and the finished-request filter covers the other way it could fail. Don't forget to deploy the rules after merging. Approving!

@lucaschristiansson
lucaschristiansson merged commit 22bfe4a into main Nov 19, 2026
4 of 5 checks passed
@lucaschristiansson
lucaschristiansson deleted the feature/accept-booking-backend branch November 19, 2026 14:44
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