Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/6333 add contact #113

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3"

services:
meldingen-core:
image: amsterdam/meldingen-core
Expand Down
26 changes: 26 additions & 0 deletions meldingen_core/actions/melding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion meldingen_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/test_actions/test_melding_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from meldingen_core.actions.melding import (
MeldingAddAttachmentsAction,
MeldingAddContactAction,
MeldingAnswerQuestionsAction,
MeldingCompleteAction,
MeldingCreateAction,
Expand Down Expand Up @@ -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 = "[email protected]"
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", "[email protected]", "token")


@pytest.mark.anyio
async def test_melding_answer_questions_action() -> None:
state_machine = Mock(BaseMeldingStateMachine)
Expand Down
Loading