Skip to content

Commit

Permalink
[#3283] Removed-refactored appointments according to the new flow
Browse files Browse the repository at this point in the history
  • Loading branch information
vaszig committed Nov 29, 2024
1 parent 9c1dbbb commit ab241e8
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 1,115 deletions.
19 changes: 2 additions & 17 deletions src/openforms/appointments/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@ def __str__(self):
return self.identifier


@dataclass()
class Customer:
"""
Deprecated in favour of the :class:`CustomerDetails`.
"""

last_name: str
birthdate: date
initials: str | None = None
phonenumber: str | None = None

def __str__(self):
return self.last_name


F = TypeVar("F", bound=TextChoices)
# generic type for the plugin-specific enum of field names

Expand Down Expand Up @@ -210,7 +195,7 @@ def create_appointment(
products: list[Product],
location: Location,
start_at: datetime,
client: CustomerDetails[F] | Customer,
client: CustomerDetails[F],
remarks: str = "",
) -> str:
"""
Expand All @@ -219,7 +204,7 @@ def create_appointment(
:param products: List of :class:`Product`, as obtained from :meth:`get_available_products`.
:param location: An :class:`Location`, as obtained from :meth:`get_locations`.
:param start_at: A `datetime` to start the appointment, as obtained from :meth:`get_dates`.
:param client: A :class:`Customer` that holds client details.
:param client: A :class:`CustomerDetails` that holds client details.
:param remarks: A ``str`` for additional remarks, added to the appointment.
:returns: An appointment identifier as ``str``.
:raises AppointmentCreateFailed: If the appointment could not be created.
Expand Down
2 changes: 1 addition & 1 deletion src/openforms/appointments/contrib/demo/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_dates(self, products, location, start_at=None, end_at=None):

def get_times(self, products, location, day):
today = timezone.localdate()
times = (time(12, 0), time(15, 15), time(15, 45))
times = (time(12, 0), time(15, 15), time(15, 45), time(17, 45))
return [timezone.make_aware(datetime.combine(today, _time)) for _time in times]

def get_required_customer_fields(
Expand Down
30 changes: 2 additions & 28 deletions src/openforms/appointments/contrib/jcc/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import warnings
from collections import Counter
from contextlib import contextmanager
from datetime import date, datetime
Expand All @@ -20,14 +19,7 @@
from openforms.plugins.exceptions import InvalidPluginConfiguration
from openforms.utils.date import TIMEZONE_AMS, datetime_in_amsterdam

from ...base import (
AppointmentDetails,
BasePlugin,
Customer,
CustomerDetails,
Location,
Product,
)
from ...base import AppointmentDetails, BasePlugin, CustomerDetails, Location, Product
from ...exceptions import (
AppointmentCreateFailed,
AppointmentDeleteFailed,
Expand Down Expand Up @@ -251,28 +243,10 @@ def create_appointment(
products: list[Product],
location: Location,
start_at: datetime,
client: CustomerDetails[CustomerFields] | Customer,
client: CustomerDetails[CustomerFields],
remarks: str = "",
) -> str:
product_ids = squash_ids(products)

# Phasing out Customer in favour of CustomerDetails, so convert to the new type
if isinstance(client, Customer):
warnings.warn(
"Fixed customer fields via the Customer class are deprecated, use "
"dynamic CustomerDetails with 'get_required_customer_fields' instead.",
DeprecationWarning,
)
client = CustomerDetails(
details={
CustomerFields.last_name: client.last_name,
CustomerFields.birthday: client.birthdate.isoformat(),
# Phone number is often required for appointment,
# use fake phone number if no client phone number
CustomerFields.main_tel: client.phonenumber or "0123456789",
}
)

customer_details = {
FIELD_TO_XML_NAME[key]: value for key, value in client.details.items()
}
Expand Down
40 changes: 10 additions & 30 deletions src/openforms/appointments/contrib/qmatic/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import logging
import warnings
from collections import Counter
from contextlib import contextmanager
Expand All @@ -19,14 +18,7 @@
from openforms.formio.typing import Component
from openforms.plugins.exceptions import InvalidPluginConfiguration

from ...base import (
AppointmentDetails,
BasePlugin,
Customer,
CustomerDetails,
Location,
Product,
)
from ...base import AppointmentDetails, BasePlugin, CustomerDetails, Location, Product
from ...exceptions import (
AppointmentCreateFailed,
AppointmentDeleteFailed,
Expand Down Expand Up @@ -73,25 +65,6 @@ def wrapper(*args, **kwargs) -> T:
return decorator


def normalize_customer_details(client: _CustomerDetails | Customer) -> _CustomerDetails:
# Phasing out Customer in favour of CustomerDetails, so convert to the new type
if isinstance(client, Customer):
warnings.warn(
"Fixed customer fields via the Customer class are deprecated, use "
"dynamic CustomerDetails with 'get_required_customer_fields' instead.",
DeprecationWarning,
)
client = _CustomerDetails(
details={
CustomerFields.last_name: client.last_name,
CustomerFields.birthday: client.birthdate.isoformat(),
CustomerFields.first_name: client.initials or "",
CustomerFields.phone_number: client.phonenumber or "",
}
)
return client


@register("qmatic")
class QmaticAppointment(BasePlugin[CustomerFields]):
"""
Expand Down Expand Up @@ -314,11 +287,18 @@ def create_appointment(
products: list[Product],
location: Location,
start_at: datetime,
client: _CustomerDetails | Customer,
client: _CustomerDetails,
remarks: str = "",
) -> str:
assert products, "Can't book for empty products"
customer = normalize_customer_details(client)
customer = _CustomerDetails(
details={
CustomerFields.last_name: client.last_name,
CustomerFields.birthday: client.birthdate.isoformat(),
CustomerFields.first_name: client.initials or "",
CustomerFields.phone_number: client.phonenumber or "",
}
)

product_names = ", ".join(sorted({product.name for product in products}))
unique_product_ids, num_customers = self._count_products(products)
Expand Down
38 changes: 3 additions & 35 deletions src/openforms/appointments/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import warnings

from celery_once import QueueOnce

Expand All @@ -8,10 +7,8 @@

from .core import book_for_submission
from .exceptions import AppointmentRegistrationFailed, NoAppointmentForm
from .models import AppointmentInfo
from .utils import book_appointment_for_submission

__all__ = ["maybe_register_appointment"]
__all__ = ["register_appointment"]

logger = logging.getLogger(__name__)

Expand All @@ -21,7 +18,7 @@
ignore_result=False,
once={"graceful": True}, # do not spam error monitoring
)
def maybe_register_appointment(submission_id: int) -> None | str:
def register_appointment(submission_id: int) -> None | str:
"""
Register an appointment for the submission IF relevant.
Expand All @@ -32,26 +29,9 @@ def maybe_register_appointment(submission_id: int) -> None | str:
be stored in the database. If appointment registration fails, this feedback
should find its way back to the end-user.
"""
warnings.warn(
"This task is deprecated because of the new appointment flow.",
PendingDeprecationWarning,
)
logger.info("Registering appointment for submission %d (if needed!)", submission_id)
logger.info("Registering appointment for submission %d", submission_id)
submission = Submission.objects.select_related("form").get(id=submission_id)

try:
appointment_id = submission.appointment_info.appointment_id
except AppointmentInfo.DoesNotExist:
pass
else:
# idempotency - do not register a new appointment if there already is one.
if appointment_id:
logger.info(
"Submission %s already has an appointment ID, aborting.", submission.pk
)
return

# Try the new appointments implementation first
try:
return book_for_submission(submission=submission)
except NoAppointmentForm:
Expand All @@ -63,15 +43,3 @@ def maybe_register_appointment(submission_id: int) -> None | str:
extra={"submission": submission_id},
)
raise

# otherwise, fall back to the old form
logger.info("Attempting old appointment booking for submission %r", submission_id)
try:
book_appointment_for_submission(submission)
except AppointmentRegistrationFailed as exc:
logger.info(
"Appointment registration failed, aborting workflow.",
exc_info=exc,
extra={"submission": submission_id},
)
raise
Loading

0 comments on commit ab241e8

Please sign in to comment.