Ordering backend project · Chapter 6 of 12
Admin: delivery slots
By the end of this chapter you can let Bhavna plan delivery slots safely: check a slot's times and capacity and name everything wrong with it at once, find a clash with the one condition that catches every overlap, see why a unique constraint cannot, and close a slot with several changes in one transaction. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Running the kitchens
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Ordering backend project are free. This one comes with the all labs plan.
Why you need this
A delivery slot is what DabbaDrop sells: half an hour in which the riders of one zone carry at most so many dabbas. Bhavna plans them, and in the sheet she plans them by hand. Two things went wrong in October. A Goregaon slot from 12:45 to 1:15 pm was typed on top of the 12:30 and 1:00 pm slots, so the same two riders were promised to three slots at once. And a row for Thane said 300 dabbas instead of 30, so the sheet kept taking orders for a slot that one scooter serves.
The seed has 90 slots for launch week, made by chapter 2's setup. From now on Bhavna adds and closes slots through the API, and the rules the sheet never had are yours to write: a slot is a sensible half hour or so, on one Mumbai day, for a sensible number of dabbas; it never overlaps another open slot of its zone; and a slot that members have paid for is not closed with one click.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
rules.py: everything wrong with a slot
Write slot_problems(starts_at, ends_at, capacity) in app/modules/slots/rules.py. It returns a list of these messages, exactly, in this order; an empty list means the slot is fine.
If either time has no tzinfo, return only ["starts_at and ends_at need their offset, like +05:30"]. Otherwise: "ends_at must be after starts_at" when ends_at is not after starts_at, or else "a slot lasts at most 60 minutes" when it is longer than LONGEST_MINUTES (60, exactly 60 is fine); "starts_at must be on a quarter hour (:00, :15, :30 or :45)" when its minute is not a multiple of 15 or it has seconds, and the same message for ends_at with "ends_at"; "a slot starts and ends on the same day in Mumbai" when mumbai_day (given) of the two differs; "capacity must be from 1 to 200" when capacity is outside 1 to MAX_CAPACITY (200).
The Check calls it on good slots, on each problem alone, on four problems at once, on 23:45 to 00:15 Mumbai time, on 05:15 to 05:45 Mumbai time given in UTC (fine), and on times with no offset.
- Task 2 · interview question
Interview: service.py, a slot in a free half hour
Write create_slot(db, data) in app/modules/slots/service.py. data is a SlotIn (zone_id, starts_at, ends_at, capacity). In this order: no such zone is not_found(...) (404); when slot_problems finds any, raise unprocessable("bad_slot", problems[0], problems) (422, the first problem as the message, all of them as details); an open slot of the same zone that overlaps is conflict("slot_overlap", ...) (409): two slots overlap when each starts before the other ends, so one that ends at 13:00 and one that starts at 13:00 do not, and closed slots do not count. Then add the Slot, commit, and return slot_out(slot) (given). A closed slot at the very same start still breaks the unique slots_zone_starts at the commit: roll back and raise conflict("slot_overlap", ...).
The Check makes Goregaon's 13:30 to 14:00 (slot 91, open), tries zone 999, a 90-minute slot for 0 dabbas (422 with both problems), 12:45 to 13:15 and 11:45 to 12:15 (409 each), makes 14:00 to 14:30 right after it and a Malad slot at 12:30, closes slot 18 and makes 13:15 to 13:30 over it, and tries 13:00, slot 18's own start (409).
Kunal will ask: why is "each starts before the other ends" enough to find every overlap, and why can't a unique constraint catch an overlap the way it catches a twin?
- Task 3
service.py: close a slot
Write close_slot(db, slot_id) in app/modules/slots/service.py. No such slot is a 404. A slot with a confirmed order cannot close: raise conflict("slot_has_orders", ...) (409) and change nothing. Otherwise set its status to "closed", delete its slot_holds, set its pending orders to "cancelled", commit, and return slot_out(slot). Closing a closed slot again answers the same.
The Check gives slot 20 a hold and a pending order, closes it twice, tries slot 999 and slot 31 (Advait has paid: 409, still open), then sends POST /admin/slots as Bhavna (201, then an overlap 409, then times with no offset 422), POST /admin/slots/31/close (409) and /admin/slots/21/close (200), and /admin/slots/22/close as Tara (403).
What you will learn
- Moments from a request: aware and naive datetimes, why Python refuses to compare them, and why .date() of a UTC moment can be yesterday in Mumbai.
- Every problem at once: a list of problems, and a 422 whose details carries all of them.
- Overlap as a query: chapter 2's rule, "each starts before the other ends", as a where.
- What a unique constraint can and cannot see: equal keys, not overlapping ranges.
- Statements for many rows: SQLAlchemy's update() and delete() run through the session, in the same transaction as the object changes.
More in Running the kitchens
Build "Admin: delivery slots" yourself
DabbaDrop delivers tiffins across Mumbai. You build its ordering API, one module a chapter, and take the whole codebase home. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Ordering backend project are free. This one comes with the all labs plan.