Skip to content

Commit

Permalink
feat: add fallback to shipping_date from shipment_date when shipment_…
Browse files Browse the repository at this point in the history
…date is not defined for backward compatibility before deprecation
  • Loading branch information
danh91 committed Sep 30, 2024
1 parent 1341b59 commit d70733d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
9 changes: 7 additions & 2 deletions modules/connectors/eshipper/karrio/providers/eshipper/rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ def rate_request(
payload.options,
package_options=packages.options,
)
shipping_date = lib.to_date(options.shipment_date.state or datetime.datetime.now())

request = eshipper.RateRequestType(
scheduledShipDate=lib.fdatetime(shipping_date, output_format="%Y-%m-%d %H:%M"),
scheduledShipDate=lib.fdatetime(
lib.to_next_business_datetime(
options.shipping_date.state or datetime.datetime.now(),
current_format="%Y-%m-%dT%H:%M",
),
output_format="%Y-%m-%dT%H:%M:%S.%fZ", # 2024-09-30T09:10:29.195Z
),
raterequestfrom=eshipper.FromType(
attention=shipper.contact,
company=shipper.company_name,
Expand Down
4 changes: 2 additions & 2 deletions modules/connectors/eshipper/tests/eshipper/test_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_parse_rate_response(self):
},
],
"options": {
"shipment_date": "2024-07-16",
"shipping_date": "2024-07-16T10:00",
},
}

Expand Down Expand Up @@ -326,7 +326,7 @@ def test_parse_rate_response(self):
"zip": "L4T3T1",
"country": "CA",
},
"scheduledShipDate": "2024-07-16 00:00",
"scheduledShipDate": "2024-07-16T10:00:00.000000Z",
"packagingUnit": "Metric",
"packages": {
"type": "Package",
Expand Down
22 changes: 9 additions & 13 deletions modules/core/karrio/server/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,25 @@ def __init__(self, instance=None, **kwargs):
if data:
options = (data or {}).get("options") or {}

# TODO: remove this when we have a standard shipping date field
shipment_date = lib.to_date(
options.get("shipment_date")
or (getattr(instance, "options", None) or {}).get("shipment_date")
)
shipping_date = lib.to_date(
options.get("shipping_date")
or (getattr(instance, "options", None) or {}).get("shipping_date"),
current_format="%Y-%m-%dT%H:%M",
)

# TODO: remove this when we have a standard shipping date field
if (
shipment_date is not None
and shipment_date.date() < datetime.now().date()
):
options.update(shipment_date=datetime.now().strftime("%Y-%m-%d"))
kwargs["data"].update(dict(options=options))

if shipping_date is None or shipping_date.date() < datetime.now().date():
options.update(shipping_date=datetime.now().strftime("%Y-%m-%dT%H:%M"))
kwargs["data"].update(dict(options=options))

if shipping_date or options.get("shipping_date"):
options.update(
shipment_date=lib.fdate(
shipping_date or options.get("shipping_date"),
current_format="%Y-%m-%dT%H:%M",
)
)
kwargs["data"].update(dict(options=options))

super().__init__(instance, **kwargs)


Expand Down
5 changes: 3 additions & 2 deletions modules/manager/karrio/server/manager/tests/test_shipments.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def test_cancel_purchased_shipment(self):
"return_address": None,
"billing_address": None,
"services": [],
"options": {"shipping_date": ANY},
"options": {"shipping_date": ANY, "shipment_date": ANY},
"customs": None,
"reference": None,
"carrier_ids": ["canadapost"],
Expand All @@ -375,6 +375,7 @@ def test_cancel_purchased_shipment(self):
"options": {
"insurance": 54,
"currency": "CAD",
"shipment_date": "2050-01-01",
"shipping_date": "2050-01-01T10:30",
},
}
Expand Down Expand Up @@ -513,7 +514,7 @@ def test_cancel_purchased_shipment(self):
}
],
"services": [],
"options": {"shipping_date": ANY},
"options": {"shipping_date": ANY, "shipment_date": ANY},
"payment": {"paid_by": "sender", "currency": "CAD", "account_number": None},
"return_address": None,
"billing_address": None,
Expand Down
6 changes: 3 additions & 3 deletions modules/orders/karrio/server/orders/tests/test_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def test_fulfilled_order_when_all_items_are_fulfilled(self):
}
],
"services": [],
"options": {"currency": "CAD", "shipment_date": ANY},
"options": {"currency": "CAD", "shipment_date": ANY, "shipping_date": ANY},
"payment": {"paid_by": "sender", "currency": "CAD", "account_number": None},
"billing_address": None,
"customs": None,
Expand Down Expand Up @@ -748,7 +748,7 @@ def test_fulfilled_order_when_all_items_are_fulfilled(self):
}
],
"services": [],
"options": {"currency": "CAD", "shipment_date": ANY},
"options": {"currency": "CAD", "shipment_date": ANY, "shipping_date": ANY},
"payment": {"paid_by": "sender", "currency": "CAD", "account_number": None},
"billing_address": None,
"customs": None,
Expand Down Expand Up @@ -962,7 +962,7 @@ def test_fulfilled_order_when_all_items_are_fulfilled(self):
}
],
"services": [],
"options": {"currency": "CAD", "shipment_date": ANY},
"options": {"currency": "CAD", "shipment_date": ANY, "shipping_date": ANY},
"payment": {"paid_by": "sender", "currency": "CAD", "account_number": None},
"billing_address": None,
"customs": None,
Expand Down
12 changes: 12 additions & 0 deletions modules/sdk/karrio/core/units.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Karrio universal data types and units definitions"""

from ctypes import util
import attr
import typing
import numbers
Expand Down Expand Up @@ -1057,6 +1058,7 @@ def __init__(
_key = key
option_values[key] = _val

self._raw_options = options
self._options = option_values
self._option_list = self._filter(
option_values, (items_filter or utils.identity)
Expand Down Expand Up @@ -1169,6 +1171,16 @@ def email_notification_to(self) -> utils.OptionEnum:

@property
def shipment_date(self) -> utils.OptionEnum:
# Check if shipment_date is not defined and fallback to shipping_date
if not self[ShippingOption.shipment_date.name].state:
return utils.OptionEnum(
"shipment_date",
str,
utils.DF.fdate(
self._raw_options.get("shipping_date"), "%Y-%m-%dT%H:%M"
),
)

return self[ShippingOption.shipment_date.name]

@property
Expand Down

0 comments on commit d70733d

Please sign in to comment.