Skip to content

Commit 9070f62

Browse files
authored
Merge pull request #8 from softwareone-platform/feature/MPT-9498/use-non-default-templates
MPT-9498 Setup Purchase template for Purchase order processing
2 parents e77098b + b189242 commit 9070f62

File tree

6 files changed

+147
-2
lines changed

6 files changed

+147
-2
lines changed

ffc/flows/fulfillment.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from ffc.flows.error import strip_trace_id
77
from ffc.flows.order import (
8+
PURCHASE_TEMPLATE_NAME,
89
OrderContext,
910
is_purchase_order,
1011
)
@@ -20,6 +21,7 @@
2021
ResetOrderErrors,
2122
SetupAgreementExternalId,
2223
SetupDueDate,
24+
StartOrderProcessing,
2325
)
2426
from ffc.notifications import notify_unhandled_exception_in_teams
2527

@@ -32,12 +34,13 @@
3234
CheckDueDate(),
3335
CheckOrderParameters(),
3436
QueryIfInvalid(),
37+
StartOrderProcessing(PURCHASE_TEMPLATE_NAME),
3538
CreateEmployee(),
3639
CreateOrganization(),
3740
SetupAgreementExternalId(),
3841
CreateSubscription(),
3942
ResetDueDate(),
40-
CompleteOrder("purchase_order"),
43+
CompleteOrder(PURCHASE_TEMPLATE_NAME),
4144
)
4245

4346

ffc/flows/order.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
from dataclasses import dataclass, field
23

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

1112
ORDER_TYPE_PURCHASE = "Purchase"
1213

14+
PURCHASE_TEMPLATE_NAME = "Purchase"
15+
1316

1417
def is_purchase_order(order):
1518
"""
@@ -66,3 +69,9 @@ def get_subscription_by_line_and_item_id(subscriptions, item_id, line_id):
6669

6770
if item:
6871
return subscription
72+
73+
74+
def set_template(order, template):
75+
updated_order = copy.deepcopy(order)
76+
updated_order["template"] = template
77+
return updated_order

ffc/flows/steps/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
QueryIfInvalid,
77
ResetOrderErrors,
88
SetupAgreementExternalId,
9+
StartOrderProcessing,
910
)
1011
from ffc.flows.steps.subscription import CreateSubscription
1112

@@ -21,4 +22,5 @@
2122
"CreateOrganization",
2223
"CheckOrderParameters",
2324
"SetupAgreementExternalId",
25+
"StartOrderProcessing",
2426
]

ffc/flows/steps/order.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@
66
get_product_template_or_default,
77
query_order,
88
update_agreement,
9+
update_order,
910
)
1011

1112
from ffc.flows.error import (
1213
ERR_ADMIN_CONTACT,
1314
ERR_CURRENCY,
1415
ERR_ORGANIZATION_NAME,
1516
)
16-
from ffc.flows.order import MPT_ORDER_STATUS_COMPLETED, MPT_ORDER_STATUS_QUERYING
17+
from ffc.flows.order import (
18+
MPT_ORDER_STATUS_COMPLETED,
19+
MPT_ORDER_STATUS_PROCESSING,
20+
MPT_ORDER_STATUS_QUERYING,
21+
set_template,
22+
)
1723
from ffc.flows.steps.utils import reset_order_error
1824
from ffc.notifications import send_email_notification
1925
from ffc.parameters import (
2026
PARAM_ADMIN_CONTACT,
2127
PARAM_CURRENCY,
2228
PARAM_ORGANIZATION_NAME,
29+
get_due_date,
2330
get_ordering_parameter,
2431
reset_ordering_parameters_error,
2532
set_ordering_parameter_error,
@@ -140,3 +147,36 @@ def __call__(self, client, context, next_step):
140147
context.order = reset_ordering_parameters_error(context.order)
141148

142149
next_step(client, context)
150+
151+
152+
class StartOrderProcessing(Step):
153+
"""
154+
Set the template for the processing status
155+
"""
156+
157+
def __init__(self, template_name):
158+
self.template_name = template_name
159+
160+
def __call__(self, client, context, next_step):
161+
template = get_product_template_or_default(
162+
client,
163+
context.order["agreement"]["product"]["id"],
164+
MPT_ORDER_STATUS_PROCESSING,
165+
self.template_name,
166+
)
167+
current_template_id = context.order.get("template", {}).get("id")
168+
if template["id"] != current_template_id:
169+
context.order = set_template(context.order, template)
170+
update_order(
171+
client, context.order["id"], template=context.order["template"]
172+
)
173+
logger.info(
174+
f"{context}: processing template set to {self.template_name} "
175+
f"({template['id']})"
176+
)
177+
logger.info(f"{context}: processing template is ok, continue")
178+
179+
if not get_due_date(context.order):
180+
send_email_notification(client, context.order)
181+
182+
next_step(client, context)

tests/ffc/flows/steps/test_order.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
QueryIfInvalid,
66
ResetOrderErrors,
77
SetupAgreementExternalId,
8+
StartOrderProcessing,
89
)
910
from ffc.parameters import PARAM_PHASE_ORDERING
1011

@@ -193,3 +194,87 @@ def test_reset_order_error(
193194
for param in ctx.order["parameters"][PARAM_PHASE_ORDERING]
194195
),
195196
)
197+
198+
199+
def test_start_order_processing_same_template(
200+
mocker,
201+
mocked_next_step,
202+
mpt_client,
203+
processing_purchase_order,
204+
template,
205+
):
206+
processing_purchase_order["template"] = template
207+
mocker.patch(
208+
"ffc.flows.steps.order.get_product_template_or_default",
209+
return_value=template,
210+
)
211+
mocked_update_order = mocker.patch("ffc.flows.steps.order.update_order")
212+
mocked_send_email_notification = mocker.patch(
213+
"ffc.flows.steps.order.send_email_notification"
214+
)
215+
ctx = OrderContext(order=processing_purchase_order)
216+
step = StartOrderProcessing("Purchase")
217+
218+
step(mpt_client, ctx, mocked_next_step)
219+
220+
mocked_next_step.assert_called_once()
221+
mocked_update_order.assert_not_called()
222+
mocked_send_email_notification.assert_not_called()
223+
224+
225+
def test_start_order_processing(
226+
mocker,
227+
mocked_next_step,
228+
mpt_client,
229+
processing_purchase_order,
230+
template,
231+
):
232+
mocker.patch(
233+
"ffc.flows.steps.order.get_product_template_or_default",
234+
return_value=template,
235+
)
236+
mocked_update_order = mocker.patch("ffc.flows.steps.order.update_order")
237+
mocked_send_email_notification = mocker.patch(
238+
"ffc.flows.steps.order.send_email_notification"
239+
)
240+
ctx = OrderContext(order=processing_purchase_order)
241+
step = StartOrderProcessing("Purchase")
242+
243+
step(mpt_client, ctx, mocked_next_step)
244+
245+
mocked_next_step.assert_called_once()
246+
mocked_update_order.assert_called_once_with(
247+
mpt_client,
248+
processing_purchase_order["id"],
249+
template=template,
250+
)
251+
mocked_send_email_notification.assert_not_called()
252+
253+
254+
def test_start_order_processing_send_notification(
255+
mocker,
256+
mocked_next_step,
257+
mpt_client,
258+
first_attempt_processing_purchase_order,
259+
template,
260+
):
261+
first_attempt_processing_purchase_order["template"] = template
262+
mocker.patch(
263+
"ffc.flows.steps.order.get_product_template_or_default",
264+
return_value=template,
265+
)
266+
mocked_update_order = mocker.patch("ffc.flows.steps.order.update_order")
267+
mocked_send_email_notification = mocker.patch(
268+
"ffc.flows.steps.order.send_email_notification"
269+
)
270+
ctx = OrderContext(order=first_attempt_processing_purchase_order)
271+
step = StartOrderProcessing("Purchase")
272+
273+
step(mpt_client, ctx, mocked_next_step)
274+
275+
mocked_next_step.assert_called_once()
276+
mocked_update_order.assert_not_called()
277+
mocked_send_email_notification.assert_called_once_with(
278+
mpt_client,
279+
first_attempt_processing_purchase_order,
280+
)

tests/ffc/flows/test_fulfillment.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def test_purchase_order(
2929
mocked_send_email_notification_complete_order = mocker.patch(
3030
"ffc.flows.steps.order.send_email_notification",
3131
)
32+
mocked_update_order = mocker.patch("ffc.flows.steps.order.update_order")
3233
mocked_update_agreement = mocker.patch("ffc.flows.steps.order.update_agreement")
3334

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

4142
fulfill_order(mpt_client, processing_purchase_order)
4243

44+
mocked_update_order.assert_called_once_with(
45+
mpt_client,
46+
processing_purchase_order["id"],
47+
template=template,
48+
)
4349
mocked_update_agreement.assert_called_once_with(
4450
mpt_client,
4551
processing_purchase_order["agreement"]["id"],

0 commit comments

Comments
 (0)