Shopping requests parity
This commit is contained in:
parent
c4e1293557
commit
76eeaba51c
5 changed files with 95 additions and 32 deletions
|
|
@ -13,7 +13,6 @@ from api.shopping_models import (
|
||||||
ShoppingListOut,
|
ShoppingListOut,
|
||||||
PurchasedShoppingList,
|
PurchasedShoppingList,
|
||||||
_to_ingredient_item,
|
_to_ingredient_item,
|
||||||
ListIngredientItem,
|
|
||||||
_to_meal_item,
|
_to_meal_item,
|
||||||
_to_shopping_list_out,
|
_to_shopping_list_out,
|
||||||
RequestedMealItem,
|
RequestedMealItem,
|
||||||
|
|
@ -113,8 +112,6 @@ async def purchase_ingredients_scoped(
|
||||||
user=Depends(get_current_user),
|
user=Depends(get_current_user),
|
||||||
conn: aiosqlite.Connection = Depends(get_db),
|
conn: aiosqlite.Connection = Depends(get_db),
|
||||||
) -> PurchasedShoppingList | Response:
|
) -> PurchasedShoppingList | Response:
|
||||||
hid = household["id"]
|
|
||||||
|
|
||||||
# Map outward input DTO to domain model
|
# Map outward input DTO to domain model
|
||||||
domain_items: List[shopping.ShoppingListItem] = []
|
domain_items: List[shopping.ShoppingListItem] = []
|
||||||
for it in shopping_list.items:
|
for it in shopping_list.items:
|
||||||
|
|
@ -205,33 +202,6 @@ class IngredientIdWrapper(_ApiModel):
|
||||||
ingredient_id: int
|
ingredient_id: int
|
||||||
|
|
||||||
|
|
||||||
@router.post(
|
|
||||||
"/current/ingredients",
|
|
||||||
response_model=ListIngredientItem,
|
|
||||||
operation_id="requestIngredientV2",
|
|
||||||
summary="Request an ingredient for shopping (scoped)",
|
|
||||||
)
|
|
||||||
async def request_ingredient_scoped(
|
|
||||||
r: IngredientIdWrapper,
|
|
||||||
request: Request,
|
|
||||||
household=Depends(get_household_from_slug),
|
|
||||||
user=Depends(get_current_user),
|
|
||||||
conn: aiosqlite.Connection = Depends(get_db),
|
|
||||||
):
|
|
||||||
hid = household["id"]
|
|
||||||
from ingredients.repository import find_ingredient_by_id
|
|
||||||
|
|
||||||
ingredient = await find_ingredient_by_id(conn, r.ingredient_id)
|
|
||||||
if not ingredient:
|
|
||||||
return error_response(request, 404, "Ingredient not found")
|
|
||||||
|
|
||||||
try:
|
|
||||||
item = await shopping.request_ingredient_scoped(conn, ingredient, hid, user.id)
|
|
||||||
except ValueError as e:
|
|
||||||
return error_response(request, 400, str(e))
|
|
||||||
return _to_ingredient_item(item)
|
|
||||||
|
|
||||||
|
|
||||||
# Re-export shared DTOs for importers
|
# Re-export shared DTOs for importers
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"router",
|
"router",
|
||||||
|
|
|
||||||
|
|
@ -164,6 +164,10 @@ Household-scoped routes (implemented):
|
||||||
- `api/meals.py`: `/api/v1/households/{householdSlug}/meals` upcoming/get/create/update/consumed/delete (inlined from v2).
|
- `api/meals.py`: `/api/v1/households/{householdSlug}/meals` upcoming/get/create/update/consumed/delete (inlined from v2).
|
||||||
- `api/shopping.py`: `/api/v1/households/{householdSlug}/shopping` current/list-by-id/purchase/request/unrequest (inlined from v2, with shared DTOs in `api/shopping_models.py`).
|
- `api/shopping.py`: `/api/v1/households/{householdSlug}/shopping` current/list-by-id/purchase/request/unrequest (inlined from v2, with shared DTOs in `api/shopping_models.py`).
|
||||||
|
|
||||||
|
Shopping requests parity (preserved in v2):
|
||||||
|
- Request meal: `POST /api/v1/households/{householdSlug}/shopping/current/meals/me` (scoped) and unrequest `DELETE /current/meals/{mealId}`.
|
||||||
|
- Request individual ingredient: `POST /api/v1/households/{householdSlug}/shopping/current/ingredients` (scoped). Response is `ListIngredientItem`; item appears in `GET /current` under `outstandingItems`. Household isolation enforced.
|
||||||
|
|
||||||
Repositories accept `household_id` and filter by it across `meals`, `recipes`, and `shopping` domains.
|
Repositories accept `household_id` and filter by it across `meals`, `recipes`, and `shopping` domains.
|
||||||
|
|
||||||
### 3.3. Invitation API
|
### 3.3. Invitation API
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ from shopping.repository import (
|
||||||
get_purchased_ingredients_scoped as _get_purchased_ingredients_scoped,
|
get_purchased_ingredients_scoped as _get_purchased_ingredients_scoped,
|
||||||
is_requested as is_requested,
|
is_requested as is_requested,
|
||||||
request_meal_scoped as request_meal_scoped,
|
request_meal_scoped as request_meal_scoped,
|
||||||
request_ingredient_scoped as request_ingredient_scoped,
|
|
||||||
remove_meal_request_scoped as remove_meal_request_scoped,
|
remove_meal_request_scoped as remove_meal_request_scoped,
|
||||||
load_shopping_list as load_shopping_list,
|
load_shopping_list as load_shopping_list,
|
||||||
load_shopping_list_scoped as load_shopping_list_scoped,
|
load_shopping_list_scoped as load_shopping_list_scoped,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import Any, AsyncIterator, Dict, List, Optional
|
from typing import Any, AsyncIterator, List, Optional
|
||||||
|
|
||||||
from shopping.models import ShoppingList, ShoppingListItem
|
from shopping.models import ShoppingList, ShoppingListItem
|
||||||
|
|
||||||
|
|
|
||||||
90
tests/test_shopping_request_ingredient_v2.py
Normal file
90
tests/test_shopping_request_ingredient_v2.py
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
import unittest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
import main
|
||||||
|
from db import connect, create
|
||||||
|
from scripts.migration_to_households import run_migration
|
||||||
|
|
||||||
|
|
||||||
|
class TestShoppingRequestIngredientV2(unittest.IsolatedAsyncioTestCase):
|
||||||
|
async def asyncSetUp(self):
|
||||||
|
self.conn = await connect(":memory:")
|
||||||
|
await create(self.conn)
|
||||||
|
await run_migration(self.conn)
|
||||||
|
|
||||||
|
async def override_get_db():
|
||||||
|
try:
|
||||||
|
yield self.conn
|
||||||
|
finally:
|
||||||
|
pass
|
||||||
|
|
||||||
|
main.app.dependency_overrides[main.get_db] = override_get_db
|
||||||
|
self.client = TestClient(main.app)
|
||||||
|
|
||||||
|
# Register user and create two households
|
||||||
|
r = self.client.post(
|
||||||
|
"/api/v1/auth/register",
|
||||||
|
json={"email": "ing@test.com", "password": "pw", "displayName": "Ing"},
|
||||||
|
)
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
token = r.json()["accessToken"]
|
||||||
|
self.headers = {"Authorization": f"Bearer {token}"}
|
||||||
|
|
||||||
|
r = self.client.post("/api/v1/households", headers=self.headers, json={"name": "H1"})
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
self.h1 = r.json()["slug"]
|
||||||
|
r = self.client.post("/api/v1/households", headers=self.headers, json={"name": "H2"})
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
self.h2 = r.json()["slug"]
|
||||||
|
|
||||||
|
# Resolve household ids
|
||||||
|
async with self.conn.execute("SELECT id FROM Household WHERE slug = ?", (self.h1,)) as c:
|
||||||
|
row = await c.fetchone()
|
||||||
|
assert row is not None
|
||||||
|
self.h1_id = int(row[0])
|
||||||
|
async with self.conn.execute("SELECT id FROM Household WHERE slug = ?", (self.h2,)) as c:
|
||||||
|
row = await c.fetchone()
|
||||||
|
assert row is not None
|
||||||
|
self.h2_id = int(row[0])
|
||||||
|
|
||||||
|
# Seed one ingredient in H1
|
||||||
|
await self.conn.execute(
|
||||||
|
"INSERT INTO Ingredient (name, line, unit, quantity, recipe_id, meal_id, household_id) VALUES ('Milk', '1L milk', 'L', 1, NULL, NULL, ?)",
|
||||||
|
(self.h1_id,),
|
||||||
|
)
|
||||||
|
async with self.conn.execute("SELECT last_insert_rowid()") as c:
|
||||||
|
row = await c.fetchone()
|
||||||
|
assert row is not None
|
||||||
|
self.milk_id = int(row[0])
|
||||||
|
await self.conn.commit()
|
||||||
|
|
||||||
|
async def asyncTearDown(self):
|
||||||
|
await self.conn.close()
|
||||||
|
main.app.dependency_overrides.clear()
|
||||||
|
|
||||||
|
def test_request_ingredient_scoped(self):
|
||||||
|
# Request ingredient in H1
|
||||||
|
r = self.client.post(
|
||||||
|
f"/api/v1/households/{self.h1}/shopping/current/ingredients",
|
||||||
|
headers=self.headers,
|
||||||
|
json={"ingredientId": self.milk_id},
|
||||||
|
)
|
||||||
|
assert r.status_code == 200, r.text
|
||||||
|
item = r.json()
|
||||||
|
assert item["ingredientId"] == self.milk_id
|
||||||
|
assert item["mealId"] is None
|
||||||
|
|
||||||
|
# Visible in H1 current, not in H2
|
||||||
|
r1 = self.client.get(
|
||||||
|
f"/api/v1/households/{self.h1}/shopping/current", headers=self.headers
|
||||||
|
)
|
||||||
|
assert r1.status_code == 200, r1.text
|
||||||
|
cur1 = r1.json()
|
||||||
|
assert any(i.get("ingredientId") == self.milk_id for i in cur1["outstandingItems"])
|
||||||
|
|
||||||
|
r2 = self.client.get(
|
||||||
|
f"/api/v1/households/{self.h2}/shopping/current", headers=self.headers
|
||||||
|
)
|
||||||
|
assert r2.status_code == 200, r2.text
|
||||||
|
cur2 = r2.json()
|
||||||
|
assert not any(i.get("ingredientId") == self.milk_id for i in cur2["outstandingItems"])
|
||||||
Loading…
Reference in a new issue