diff --git a/docker-compose.yml b/docker-compose.yml index b9e4c33..d65381e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: "3" - services: meldingen-core: image: amsterdam/meldingen-core diff --git a/meldingen_core/actions/melding.py b/meldingen_core/actions/melding.py index 0646487..d0dccd3 100644 --- a/meldingen_core/actions/melding.py +++ b/meldingen_core/actions/melding.py @@ -66,6 +66,8 @@ class MeldingRetrieveAction(BaseRetrieveAction[T, T_co]): class MeldingUpdateAction(BaseCRUDAction[T, T_co]): + """Action that updates the melding and reclassifies it""" + _verify_token: TokenVerifier[T, T_co] _classify: Classifier _state_machine: BaseMeldingStateMachine[T] @@ -97,6 +99,30 @@ async def __call__(self, pk: int, values: dict[str, Any], token: str) -> T: return melding +class MeldingAddContactAction(BaseCRUDAction[T, T_co]): + """Action that adds contact information to a melding.""" + + _verify_token: TokenVerifier[T, T_co] + + def __init__( + self, + repository: BaseMeldingRepository[T, T_co], + token_verifier: TokenVerifier[T, T_co], + ) -> None: + super().__init__(repository) + self._verify_token = token_verifier + + async def __call__(self, pk: int, phone: str | None, email: str | None, token: str) -> T: + melding = await self._verify_token(pk, token) + + melding.phone = phone + melding.email = email + + await self._repository.save(melding) + + return melding + + class BaseStateTransitionAction(Generic[T, T_co], metaclass=ABCMeta): """ This action covers transitions that do not require the melding's token to be verified. diff --git a/meldingen_core/models.py b/meldingen_core/models.py index 677a5cc..8613ce6 100644 --- a/meldingen_core/models.py +++ b/meldingen_core/models.py @@ -15,9 +15,11 @@ class Melding: text: str classification: Classification | None = None + attachments: list["Attachment"] = field(default_factory=list) token: str | None = None token_expires: datetime | None = None - attachments: list["Attachment"] = field(default_factory=list) + email: str | None = None + phone: str | None = None @dataclass diff --git a/tests/test_actions/test_melding_actions.py b/tests/test_actions/test_melding_actions.py index 6e166a1..f008d37 100644 --- a/tests/test_actions/test_melding_actions.py +++ b/tests/test_actions/test_melding_actions.py @@ -6,6 +6,7 @@ from meldingen_core.actions.melding import ( MeldingAddAttachmentsAction, + MeldingAddContactAction, MeldingAnswerQuestionsAction, MeldingCompleteAction, MeldingCreateAction, @@ -90,6 +91,35 @@ async def test_melding_update_action() -> None: assert melding.classification == classification +@pytest.mark.anyio +async def test_melding_add_contact_action() -> None: + token = "123456" + repository = Mock(BaseMeldingRepository) + repository.retrieve.return_value = Melding("text", token=token, token_expires=datetime.now() + timedelta(days=1)) + token_verifier = AsyncMock(TokenVerifier) + + action: MeldingAddContactAction[Melding, Melding] = MeldingAddContactAction(repository, token_verifier) + + phone = "1234567" + email = "user@test.com" + melding = await action(123, phone, email, token) + + assert melding.phone == phone + assert melding.email == email + + +@pytest.mark.anyio +async def test_melding_add_contact_action_not_found() -> None: + repository = Mock(BaseMeldingRepository) + repository.retrieve.return_value = None + token_verifier: TokenVerifier[Melding, Melding] = TokenVerifier(repository) + + action: MeldingAddContactAction[Melding, Melding] = MeldingAddContactAction(repository, token_verifier) + + with pytest.raises(NotFoundException): + await action(123, "1234567", "user@test.com", "token") + + @pytest.mark.anyio async def test_melding_answer_questions_action() -> None: state_machine = Mock(BaseMeldingStateMachine)