diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index bbff5eac8..1df2a033a 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -112,6 +112,10 @@ router.include_router(notes_router) router.include_router(patch_router) +# RomUser fields the statuses filter branches on. +STATUS_MEMBERSHIP_FIELDS = frozenset({"status", "now_playing", "backlogged", "hidden"}) + + def safe_int_or_none(value: Any) -> int | None: if value is None or value == "": return None @@ -2119,7 +2123,9 @@ async def update_rom_user( if "hidden" in cleaned_data: db_rom_handler.invalidate_filter_values_cache() - if "status" in cleaned_data: + # The statuses filter reads all four of these, and `hidden` also drops the + # ROM from every user-scoped query, so any of them can move membership. + if STATUS_MEMBERSHIP_FIELDS & cleaned_data.keys(): refresh_affected_smart_collections([id], membership_only=True) return RomUserSchema.model_validate(rom_user) diff --git a/backend/tests/endpoints/test_collection.py b/backend/tests/endpoints/test_collection.py index 73ded92b1..c57ae8157 100644 --- a/backend/tests/endpoints/test_collection.py +++ b/backend/tests/endpoints/test_collection.py @@ -780,3 +780,28 @@ class TestSmartCollectionEndpoints: refreshed = db_collection_handler.get_smart_collection(smart_collection.id) assert refreshed is not None assert refreshed.rom_ids == [rom.id] + + @pytest.mark.parametrize("field", ("now_playing", "backlogged", "hidden")) + def test_setting_a_rom_flag_refreshes_membership( + self, client, access_token: str, admin_user: User, rom: Rom, field: str + ): + smart_collection = db_collection_handler.add_smart_collection( + SmartCollection( + name=f"Flagged {field}", + description="", + user_id=admin_user.id, + filter_criteria={"statuses": [field]}, + ) + ) + db_collection_handler.refresh_smart_collection(smart_collection.id) + + response = client.put( + f"/api/roms/{rom.id}/props", + json={field: True}, + headers={"Authorization": f"Bearer {access_token}"}, + ) + assert response.status_code == status.HTTP_200_OK + + refreshed = db_collection_handler.get_smart_collection(smart_collection.id) + assert refreshed is not None + assert refreshed.rom_ids == [rom.id]