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 Dec 11, 2024
1 parent 4ab580c commit 5b5cc0e
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 1,278 deletions.
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
34 changes: 3 additions & 31 deletions src/openforms/appointments/contrib/qmatic/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
import warnings
from collections import Counter
from contextlib import contextmanager
from datetime import date, datetime
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,10 @@ 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)

product_names = ", ".join(sorted({product.name for product in products}))
unique_product_ids, num_customers = self._count_products(products)
Expand All @@ -328,7 +300,7 @@ def create_appointment(
# we repeat the same customer information for every customer, as we currently
# don't support getting the contact details for each individual customer
"customers": [
{choice: value for choice, value in customer.details.items() if value}
{choice: value for choice, value in client.details.items() if value}
]
* num_customers,
"services": [{"publicId": product_id} for product_id in unique_product_ids],
Expand Down
2 changes: 0 additions & 2 deletions src/openforms/appointments/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
)
from .models import Appointment, AppointmentInfo
from .registry import register
from .utils import cancel_previous_submission_appointment

__all__ = ["book_for_submission"]

Expand Down Expand Up @@ -116,5 +115,4 @@ def book_for_submission(submission: Submission) -> str:
logevent.appointment_register_failure(appointment_info, plugin, e)
raise AppointmentRegistrationFailed("Unable to create appointment") from e

cancel_previous_submission_appointment(submission)
return appointment_id
6 changes: 4 additions & 2 deletions src/openforms/appointments/management/commands/appointment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from openforms.submissions.models import Submission

from ...base import Customer
from ...base import CustomerDetails
from ...core import book_for_submission
from ...registry import register
from ...utils import get_plugin
Expand Down Expand Up @@ -122,7 +122,9 @@ def create_booking(self):

# Customer

customer = Customer(last_name="Doe", birthdate=date(1970, 1, 1))
customer = CustomerDetails(
details={"lastName": "Doe", "birthdate": date(1970, 1, 1)}
)

# Book

Expand Down
36 changes: 2 additions & 34 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,8 +7,6 @@

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"]

Expand All @@ -28,30 +25,13 @@ def maybe_register_appointment(submission_id: int) -> None | str:
If the submission is for a form which is configured to create appointments,
ensure that the appointment is registered in the configured backend.
This can either not be needed, be successful or fail. Either way, the result should
This can either be successful or fail. Either way, the result should
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 5b5cc0e

Please sign in to comment.