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
1 change: 1 addition & 0 deletions ffc/flows/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ def to_dict(self, **kwargs):
ERR_ORDER_TYPE_NOT_SUPPORTED = ValidationError(
"FFC0004", "Order type `{order_type}` is not supported by FinOps"
)
ERR_DUE_DATE_IS_REACHED = ValidationError("EXT1000", "Due date is reached {due_date}")
5 changes: 2 additions & 3 deletions ffc/flows/fulfillment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

from mpt_extension_sdk.flows.pipeline import Pipeline

from ffc.flows.error import strip_trace_id
from ffc.flows.error import ERR_ORDER_TYPE_NOT_SUPPORTED, strip_trace_id
from ffc.flows.order import (
FAILURE_REASON,
PURCHASE_TEMPLATE_NAME,
OrderContext,
is_purchase_order,
Expand Down Expand Up @@ -46,7 +45,7 @@
)

fail = Pipeline(
FailOrder(FAILURE_REASON),
FailOrder(ERR_ORDER_TYPE_NOT_SUPPORTED),
)


Expand Down
5 changes: 0 additions & 5 deletions ffc/flows/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

PURCHASE_TEMPLATE_NAME = "Purchase"

FAILURE_REASON = (
"Change, Termination and Configuration Orders are not supported "
"by FinOps product."
)


def is_purchase_order(order):
"""
Expand Down
8 changes: 5 additions & 3 deletions ffc/flows/steps/due_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from mpt_extension_sdk.flows.pipeline import Step
from mpt_extension_sdk.mpt_http.mpt import update_order

from ffc.flows.error import ERR_DUE_DATE_IS_REACHED
from ffc.flows.steps.utils import switch_order_to_failed
from ffc.notifications import send_email_notification
from ffc.parameters import get_due_date, set_due_date
Expand Down Expand Up @@ -56,14 +57,15 @@ class CheckDueDate(Step):
def __call__(self, client, context, next_step):
due_date = get_due_date(context.order)
if date.today() > due_date:
reason = f"Due date is reached {due_date.strftime('%Y-%m-%d')}"
due_date_str = due_date.strftime("%Y-%m-%d")
logging.info(
f"Swith order {context.order['id']} to failed status. Reason: {reason}"
f"Swith order {context.order['id']} to failed status. "
f"Reason: due date is reached {due_date_str}"
)
switch_order_to_failed(
client,
context.order,
reason,
ERR_DUE_DATE_IS_REACHED.to_dict(due_date=due_date.strftime("%Y-%m-%d")),
)
return

Expand Down
8 changes: 4 additions & 4 deletions ffc/flows/steps/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ def __call__(self, client, context, next_step):

class FailOrder(Step):
"""
Fail the order with a reason
Fail the order with an error
"""

def __init__(self, reason):
self.reason = reason
def __init__(self, error):
self.error = error

def __call__(self, client, context, next_step):
switch_order_to_failed(
client,
context.order,
self.reason,
self.error.to_dict(order_type=context.order["type"]),
)
9 changes: 3 additions & 6 deletions ffc/flows/steps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@
from ffc.parameters import set_due_date


def switch_order_to_failed(client, order, reason):
def switch_order_to_failed(client, order, error):
"""
Marks an MPT order as failed by resetting due date and updating its status.

Args:
client (MPTClient): An instance of the Marketplace platform client.
order (dict): The MPT order to be marked as failed.
reason, reason (str): Additional notes or context related to the failure.
error (dict): Additional notes or context related to the failure. {id, message}

Returns:
dict: The updated order with the appropriate status and notes.
"""
order = set_due_date(order, None)
agreement = order["agreement"]
# TODO: incorrect number of parameters in SDK :-( fix it
order = fail_order(
client, order["id"], reason, reason, parameters=order["parameters"]
)
order = fail_order(client, order["id"], error, parameters=order["parameters"])
order["agreement"] = agreement
send_email_notification(client, order)
return order
Expand Down
380 changes: 191 additions & 189 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ boto3 = "1.34.*"
django = "4.2.*" # should it be a dependency of the extension ? it is already a dependency of the sdk.
jinja2 = "3.1.*"
markdown-it-py = "3.0.*"
mpt-extension-sdk = "^4.0.5"
mpt-extension-sdk = "4.0.*"
openpyxl = "3.1.*"
phonenumbers = "8.13.*"
pyairtable = "2.3.*"
Expand Down
3 changes: 2 additions & 1 deletion tests/ffc/flows/steps/test_due_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from freezegun import freeze_time

from ffc.flows.error import ERR_DUE_DATE_IS_REACHED
from ffc.flows.order import OrderContext
from ffc.flows.steps.due_date import CheckDueDate, ResetDueDate, SetupDueDate
from ffc.parameters import get_due_date, set_due_date
Expand Down Expand Up @@ -119,5 +120,5 @@ def test_check_due_date_fail_order(
mocked_switch_order_to_failed.assert_called_once_with(
mpt_client,
processing_purchase_order,
"Due date is reached 2025-01-01",
ERR_DUE_DATE_IS_REACHED.to_dict(due_date="2025-01-01"),
)
5 changes: 3 additions & 2 deletions tests/ffc/flows/steps/test_order.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ffc.flows.error import ERR_ORDER_TYPE_NOT_SUPPORTED
from ffc.flows.order import OrderContext
from ffc.flows.steps.order import (
CheckOrderParameters,
Expand Down Expand Up @@ -291,13 +292,13 @@ def test_fail_order(
"ffc.flows.steps.order.switch_order_to_failed"
)
ctx = OrderContext(order=processing_purchase_order)
step = FailOrder("reason")
step = FailOrder(ERR_ORDER_TYPE_NOT_SUPPORTED)

step(mpt_client, ctx, mocked_next_step)

mocked_next_step.assert_not_called()
mocked_switch_order_to_failed.assert_called_once_with(
mpt_client,
processing_purchase_order,
"reason",
ERR_ORDER_TYPE_NOT_SUPPORTED.to_dict(order_type=processing_purchase_order["type"]),
)
7 changes: 4 additions & 3 deletions tests/ffc/flows/steps/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ def test_switch_order_to_failed(
)

failed_order = switch_order_to_failed(
mpt_client, processing_purchase_order, "error"
mpt_client,
processing_purchase_order,
ERR_ORDER_TYPE_NOT_SUPPORTED.to_dict(order_type="Purchase")
)

no_due_date_order = set_due_date(processing_purchase_order, None)
assert failed_order == failed_purchase_order
mock_fail_order.assert_called_once_with(
mpt_client,
processing_purchase_order["id"],
"error",
"error",
ERR_ORDER_TYPE_NOT_SUPPORTED.to_dict(order_type="Purchase"),
parameters=no_due_date_order["parameters"],
)
mock_send_email_notifications.assert_called_once_with(
Expand Down
4 changes: 2 additions & 2 deletions tests/ffc/flows/test_fulfillment.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from freezegun import freeze_time

from ffc.flows.error import ERR_ORDER_TYPE_NOT_SUPPORTED
from ffc.flows.fulfillment import fulfill_order
from ffc.flows.order import FAILURE_REASON


@freeze_time("2025-01-01")
Expand Down Expand Up @@ -149,5 +149,5 @@ def test_other_order_types(
mocked_switch_order_to_failed.assert_called_once_with(
mpt_client,
order_to_fail,
FAILURE_REASON,
ERR_ORDER_TYPE_NOT_SUPPORTED.to_dict(order_type=order_to_fail["type"])
)
Loading