Skip to content
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
5 changes: 4 additions & 1 deletion ffc/flows/fulfillment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ffc.flows.error import strip_trace_id
from ffc.flows.order import (
PURCHASE_TEMPLATE_NAME,
OrderContext,
is_purchase_order,
)
Expand All @@ -20,6 +21,7 @@
ResetOrderErrors,
SetupAgreementExternalId,
SetupDueDate,
StartOrderProcessing,
)
from ffc.notifications import notify_unhandled_exception_in_teams

Expand All @@ -32,12 +34,13 @@
CheckDueDate(),
CheckOrderParameters(),
QueryIfInvalid(),
StartOrderProcessing(PURCHASE_TEMPLATE_NAME),
CreateEmployee(),
CreateOrganization(),
SetupAgreementExternalId(),
CreateSubscription(),
ResetDueDate(),
CompleteOrder("purchase_order"),
CompleteOrder(PURCHASE_TEMPLATE_NAME),
)


Expand Down
9 changes: 9 additions & 0 deletions ffc/flows/order.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from dataclasses import dataclass, field

from mpt_extension_sdk.flows.context import Context as BaseContext
Expand All @@ -10,6 +11,8 @@

ORDER_TYPE_PURCHASE = "Purchase"

PURCHASE_TEMPLATE_NAME = "Purchase"


def is_purchase_order(order):
"""
Expand Down Expand Up @@ -66,3 +69,9 @@ def get_subscription_by_line_and_item_id(subscriptions, item_id, line_id):

if item:
return subscription


def set_template(order, template):
updated_order = copy.deepcopy(order)
updated_order["template"] = template
return updated_order
2 changes: 2 additions & 0 deletions ffc/flows/steps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
QueryIfInvalid,
ResetOrderErrors,
SetupAgreementExternalId,
StartOrderProcessing,
)
from ffc.flows.steps.subscription import CreateSubscription

Expand All @@ -21,4 +22,5 @@
"CreateOrganization",
"CheckOrderParameters",
"SetupAgreementExternalId",
"StartOrderProcessing",
]
42 changes: 41 additions & 1 deletion ffc/flows/steps/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@
get_product_template_or_default,
query_order,
update_agreement,
update_order,
)

from ffc.flows.error import (
ERR_ADMIN_CONTACT,
ERR_CURRENCY,
ERR_ORGANIZATION_NAME,
)
from ffc.flows.order import MPT_ORDER_STATUS_COMPLETED, MPT_ORDER_STATUS_QUERYING
from ffc.flows.order import (
MPT_ORDER_STATUS_COMPLETED,
MPT_ORDER_STATUS_PROCESSING,
MPT_ORDER_STATUS_QUERYING,
set_template,
)
from ffc.flows.steps.utils import reset_order_error
from ffc.notifications import send_email_notification
from ffc.parameters import (
PARAM_ADMIN_CONTACT,
PARAM_CURRENCY,
PARAM_ORGANIZATION_NAME,
get_due_date,
get_ordering_parameter,
reset_ordering_parameters_error,
set_ordering_parameter_error,
Expand Down Expand Up @@ -140,3 +147,36 @@ def __call__(self, client, context, next_step):
context.order = reset_ordering_parameters_error(context.order)

next_step(client, context)


class StartOrderProcessing(Step):
"""
Set the template for the processing status
"""

def __init__(self, template_name):
self.template_name = template_name

def __call__(self, client, context, next_step):
template = get_product_template_or_default(
client,
context.order["agreement"]["product"]["id"],
MPT_ORDER_STATUS_PROCESSING,
self.template_name,
)
current_template_id = context.order.get("template", {}).get("id")
if template["id"] != current_template_id:
context.order = set_template(context.order, template)
update_order(
client, context.order["id"], template=context.order["template"]
)
logger.info(
f"{context}: processing template set to {self.template_name} "
f"({template['id']})"
)
logger.info(f"{context}: processing template is ok, continue")

if not get_due_date(context.order):
send_email_notification(client, context.order)

next_step(client, context)
85 changes: 85 additions & 0 deletions tests/ffc/flows/steps/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
QueryIfInvalid,
ResetOrderErrors,
SetupAgreementExternalId,
StartOrderProcessing,
)
from ffc.parameters import PARAM_PHASE_ORDERING

Expand Down Expand Up @@ -193,3 +194,87 @@ def test_reset_order_error(
for param in ctx.order["parameters"][PARAM_PHASE_ORDERING]
),
)


def test_start_order_processing_same_template(
mocker,
mocked_next_step,
mpt_client,
processing_purchase_order,
template,
):
processing_purchase_order["template"] = template
mocker.patch(
"ffc.flows.steps.order.get_product_template_or_default",
return_value=template,
)
mocked_update_order = mocker.patch("ffc.flows.steps.order.update_order")
mocked_send_email_notification = mocker.patch(
"ffc.flows.steps.order.send_email_notification"
)
ctx = OrderContext(order=processing_purchase_order)
step = StartOrderProcessing("Purchase")

step(mpt_client, ctx, mocked_next_step)

mocked_next_step.assert_called_once()
mocked_update_order.assert_not_called()
mocked_send_email_notification.assert_not_called()


def test_start_order_processing(
mocker,
mocked_next_step,
mpt_client,
processing_purchase_order,
template,
):
mocker.patch(
"ffc.flows.steps.order.get_product_template_or_default",
return_value=template,
)
mocked_update_order = mocker.patch("ffc.flows.steps.order.update_order")
mocked_send_email_notification = mocker.patch(
"ffc.flows.steps.order.send_email_notification"
)
ctx = OrderContext(order=processing_purchase_order)
step = StartOrderProcessing("Purchase")

step(mpt_client, ctx, mocked_next_step)

mocked_next_step.assert_called_once()
mocked_update_order.assert_called_once_with(
mpt_client,
processing_purchase_order["id"],
template=template,
)
mocked_send_email_notification.assert_not_called()


def test_start_order_processing_send_notification(
mocker,
mocked_next_step,
mpt_client,
first_attempt_processing_purchase_order,
template,
):
first_attempt_processing_purchase_order["template"] = template
mocker.patch(
"ffc.flows.steps.order.get_product_template_or_default",
return_value=template,
)
mocked_update_order = mocker.patch("ffc.flows.steps.order.update_order")
mocked_send_email_notification = mocker.patch(
"ffc.flows.steps.order.send_email_notification"
)
ctx = OrderContext(order=first_attempt_processing_purchase_order)
step = StartOrderProcessing("Purchase")

step(mpt_client, ctx, mocked_next_step)

mocked_next_step.assert_called_once()
mocked_update_order.assert_not_called()
mocked_send_email_notification.assert_called_once_with(
mpt_client,
first_attempt_processing_purchase_order,
)
6 changes: 6 additions & 0 deletions tests/ffc/flows/test_fulfillment.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_purchase_order(
mocked_send_email_notification_complete_order = mocker.patch(
"ffc.flows.steps.order.send_email_notification",
)
mocked_update_order = mocker.patch("ffc.flows.steps.order.update_order")
mocked_update_agreement = mocker.patch("ffc.flows.steps.order.update_agreement")

mocked_ffc_client = mocker.MagicMock()
Expand All @@ -40,6 +41,11 @@ def test_purchase_order(

fulfill_order(mpt_client, processing_purchase_order)

mocked_update_order.assert_called_once_with(
mpt_client,
processing_purchase_order["id"],
template=template,
)
mocked_update_agreement.assert_called_once_with(
mpt_client,
processing_purchase_order["agreement"]["id"],
Expand Down
Loading