Repository navigation
feat(booking): let lenders accept booking requests - #378
Conversation
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
left a comment
There was a problem hiding this comment.
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 !
| // 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'] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
thanna23
left a comment
There was a problem hiding this comment.
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!



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 inFirebaseBookingRepositoryas one batched write:CONFIRMED;availableDates(arrayRemove), so the existing create rule refuses new requests for them and the "Choose dates" calendar no longer offers them;REQUESTEDbooking on the listing that shares a day becomesCANCELLED. 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 onlystatusmay 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.FirebaseBookingRepository: the four existing methods use a sharedsignedInUidhelper andBOOKINGS/STATUSconstants. Behaviour is unchanged.FakeBookingRepository.acceptBookingis 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
availableDatesreuses 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.connectedChecknot run (no emulator).rules-tests,npm test): 202 pass, 0 fail. New tests:REQUESTED.acceptBooking: batch contents and order, refused commit, signed-out / unstored / non-requested refusals, andoverlaps.Follow-ups
ReceivedRequestsViewModel) and the "Booking requests" screen on the listing: next two PRs (part of As a lender, I want to accept or decline booking requests, so that I control who borrows from me. #28).cancelBookingexists, but there is no button yet. As a lender, I want to accept or decline booking requests, so that I control who borrows from me. #28 says "declined"; a cancelled request currently reads "cancelled" (no issue yet).REQUESTED, but can never be accepted since its days are gone (no issue yet).firestore.rulesmust be deployed (firebase deploy --only firestore:rules), otherwise accepting is refused. feat(booking): request separate days in one booking #360's and feat(booking): allow renters and lenders to cancel bookings #363's rules were also only deployed on 18 Nov; a CI step that deploys rules on merge tomainwould avoid this (no issue yet).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.