Skip to content

Commit

Permalink
add contact to melding model and add action
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonavic committed Jan 2, 2025
1 parent 179f454 commit 676244c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 28 additions & 1 deletion meldingen_core/actions/melding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABCMeta, abstractmethod
from datetime import datetime, timedelta
from typing import Any, Generic, TypeVar, override
from typing import Any, Generic, TypeVar, override, Literal

import structlog

Expand Down Expand Up @@ -66,6 +66,7 @@ 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 +98,32 @@ async def __call__(self, pk: int, values: dict[str, Any], token: str) -> T:
return melding


CONTACT_OPTIONS = dict[Literal["phone", "email"], str | None]


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, values: CONTACT_OPTIONS, token: str) -> T:
melding = await self._verify_token(pk, token)

for key, value in values.items():
setattr(melding, key, value)

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
2 changes: 2 additions & 0 deletions meldingen_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Melding:
token: str | None = None
token_expires: datetime | None = None
attachments: list["Attachment"] = field(default_factory=list)
email: str | None
phone: str | None


@dataclass
Expand Down

0 comments on commit 676244c

Please sign in to comment.