Skip to content

Commit

Permalink
feat(WIP): Introduce shipping_date for datetime format for carrier ex…
Browse files Browse the repository at this point in the history
…pecting ship date with time
  • Loading branch information
danh91 committed Sep 30, 2024
1 parent a3bc8fb commit 388405d
Show file tree
Hide file tree
Showing 14 changed files with 394 additions and 273 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
import karrio.schemas.eshipper.shipping_request as eshipper
import karrio.schemas.eshipper.shipping_response as shipping
import typing
Expand Down Expand Up @@ -133,15 +132,14 @@ def shipment_request(
else None
),
)
now = datetime.datetime.now() + datetime.timedelta(minutes=5)
shipping_time = lib.ftime(options.shipping_time.state or now, "%H:%M")
shipping_date = lib.fdate(options.shipping_date.state or now)

request = eshipper.ShippingRequestType(
scheduledShipDate=lib.fdatetime(
f"{shipping_date} {shipping_time}",
current_format="%Y-%m-%d %H:%M",
output_format="%Y-%m-%d %H:%M",
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
),
shippingrequestfrom=eshipper.FromType(
attention=shipper.contact,
Expand Down
12 changes: 6 additions & 6 deletions modules/connectors/eshipper/karrio/providers/eshipper/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def to_service_code(service: typing.Dict[str, str]) -> str:
parts = list(
dict.fromkeys(
[
to_carrier_code(service.get("carrierDTO")),
to_carrier_code(service.get("carrierDTO")), # type: ignore
*[
_.lower()
for _ in (
Expand All @@ -132,7 +132,7 @@ def to_service_code(service: typing.Dict[str, str]) -> str:
return output


def get_service(search: str, test_mode: bool = False, service_id: str = None) -> str:
def get_service(search: str, test_mode: bool = False, service_id: str = None):
prod_metadata = METADATA_JSON["PROD_SERVICES"]
test_metadata = METADATA_JSON["DEV_SERVICES"]
metadata = lib.identity(
Expand All @@ -152,7 +152,7 @@ def get_service(search: str, test_mode: bool = False, service_id: str = None) ->
)


def get_service_id(search: str, test_mode: bool = False, service_id: str = None) -> str:
def get_service_id(search: str, test_mode: bool = False, service_id: str = None):
return (
get_service(search, test_mode=test_mode, service_id=service_id).get("id")
or service_id
Expand All @@ -177,7 +177,7 @@ def get_carrier(
test_mode: bool = False,
service_search: str = None,
service_id: str = None,
) -> str:
):
id_key = "test_id" if test_mode else "prod_id"
alternate_key = "prod_id" if not test_mode else "test_id"
service = get_service(service_search, test_mode=test_mode, service_id=service_id)
Expand All @@ -204,7 +204,7 @@ def get_carrier_id(
test_mode: bool = False,
service_search: str = None,
service_id: str = None,
) -> str:
):
return get_carrier(
search,
test_mode=test_mode,
Expand All @@ -218,7 +218,7 @@ def find_rate_provider(
test_mode: bool = False,
service_search: str = None,
service_id: str = None,
) -> str:
):

if RateProvider.map(lib.to_snake_case(search)).name:
return RateProvider.map(lib.to_snake_case(search))
Expand Down
5 changes: 2 additions & 3 deletions modules/connectors/eshipper/tests/eshipper/test_shipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ def test_parse_cancel_shipment_response(self):
},
],
"options": {
"shipping_date": "2024-07-16",
"shipping_time": "10:30",
"shipping_date": "2024-07-16T10:30",
},
}

Expand Down Expand Up @@ -179,7 +178,7 @@ def test_parse_cancel_shipment_response(self):
"type": "Package",
},
"packagingUnit": "Metric",
"scheduledShipDate": "2024-07-16 10:30",
"scheduledShipDate": "2024-07-16T10:30:00.000000Z",
"serviceId": 4500,
"from": {
"address1": "9, Van Der Graaf Court",
Expand Down
12 changes: 6 additions & 6 deletions modules/core/karrio/server/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,8 @@ class RateRequest(validators.OptionDefaultSerializer):
"hold_at_location": true,
"paperless_trade": true,
"preferred_service": "fedex_express_saver",
"shipment_date": "2020-01-01",
"shipping_date": "2020-01-01T00:00:00",
"shipment_date": "2020-01-01", # TODO: deprecate
"shipping_date": "2020-01-01T00:00",
"shipment_note": "This is a shipment note",
"signature_confirmation": true,
"saturday_delivery": true,
Expand Down Expand Up @@ -1253,8 +1253,8 @@ class ShippingData(validators.OptionDefaultSerializer):
"hold_at_location": true,
"paperless_trade": true,
"preferred_service": "fedex_express_saver",
"shipment_date": "2020-01-01",
"shipping_date": "2020-01-01T00:00:00",
"shipment_date": "2020-01-01", # TODO: deprecate
"shipping_date": "2020-01-01T00:00",
"shipment_note": "This is a shipment note",
"signature_confirmation": true,
"saturday_delivery": true,
Expand Down Expand Up @@ -1476,8 +1476,8 @@ class ShipmentContent(serializers.Serializer):
"hold_at_location": true,
"paperless_trade": true,
"preferred_service": "fedex_express_saver",
"shipment_date": "2020-01-01",
"shipping_date": "2020-01-01T00:00:00",
"shipment_date": "2020-01-01", # TODO: deprecate
"shipping_date": "2020-01-01T00:00",
"shipment_note": "This is a shipment note",
"signature_confirmation": true,
"saturday_delivery": true,
Expand Down
15 changes: 10 additions & 5 deletions modules/core/karrio/server/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,28 @@ def __init__(self, instance=None, **kwargs):
data = kwargs.get("data", {})
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")
or (getattr(instance, "options", None) or {}).get("shipping_date"),
current_format="%Y-%m-%dT%H:%M",
)

if shipment_date is None or shipment_date.date() < datetime.now().date():
# 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:%S")
)
options.update(shipping_date=datetime.now().strftime("%Y-%m-%dT%H:%M"))
kwargs["data"].update(dict(options=options))

super().__init__(instance, **kwargs)
Expand Down
12 changes: 6 additions & 6 deletions modules/manager/karrio/server/manager/serializers/shipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ class ShipmentUpdateData(validators.OptionDefaultSerializer):
"hold_at_location": true,
"paperless_trade": true,
"preferred_service": "fedex_express_saver",
"shipment_date": "2020-01-01",
"shipping_date": "2020-01-01T00:00:00",
"shipment_date": "2020-01-01", # TODO: deprecate
"shipping_date": "2020-01-01T00:00",
"shipment_note": "This is a shipment note",
"signature_confirmation": true,
"saturday_delivery": true,
Expand Down Expand Up @@ -404,8 +404,8 @@ class ShipmentRateData(validators.OptionDefaultSerializer):
"hold_at_location": true,
"paperless_trade": true,
"preferred_service": "fedex_express_saver",
"shipment_date": "2020-01-01",
"shipping_date": "2020-01-01T00:00:00",
"shipment_date": "2020-01-01", # TODO: deprecate
"shipping_date": "2020-01-01T00:00",
"shipment_note": "This is a shipment note",
"signature_confirmation": true,
"saturday_delivery": true,
Expand Down Expand Up @@ -461,8 +461,8 @@ class ShipmentPurchaseSerializer(Shipment):
"hold_at_location": true,
"paperless_trade": true,
"preferred_service": "fedex_express_saver",
"shipment_date": "2020-01-01",
"shipping_date": "2020-01-01T00:00:00",
"shipment_date": "2020-01-01", # TODO: deprecate
"shipping_date": "2020-01-01T00:00",
"shipment_note": "This is a shipment note",
"signature_confirmation": true,
"saturday_delivery": true,
Expand Down
10 changes: 7 additions & 3 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": {"shipment_date": ANY, "shipping_date": ANY},
"options": {"shipping_date": ANY},
"customs": None,
"reference": None,
"carrier_ids": ["canadapost"],
Expand All @@ -372,7 +372,11 @@ def test_cancel_purchased_shipment(self):
}

SHIPMENT_OPTIONS = {
"options": {"insurance": 54, "currency": "CAD", "shipment_date": "2050-01-01"},
"options": {
"insurance": 54,
"currency": "CAD",
"shipping_date": "2050-01-01T10:30",
},
}

RETURNED_RATES_VALUE = [
Expand Down Expand Up @@ -509,7 +513,7 @@ def test_cancel_purchased_shipment(self):
}
],
"services": [],
"options": {"shipment_date": ANY},
"options": {"shipping_date": ANY},
"payment": {"paid_by": "sender", "currency": "CAD", "account_number": None},
"return_address": None,
"billing_address": None,
Expand Down
2 changes: 1 addition & 1 deletion modules/proxy/karrio/server/proxy/tests/test_shipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def test_shipping_failed_cancel(self):
}
],
"services": [],
"options": {"shipment_date": ANY},
"options": {"shipment_date": ANY, "shipping_date": ANY},
"payment": {"paid_by": "sender", "currency": "CAD", "account_number": None},
"billing_address": None,
"customs": None,
Expand Down
9 changes: 2 additions & 7 deletions modules/sdk/karrio/core/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,8 +1123,7 @@ class ShippingOption(utils.Enum):
shipment_date = utils.OptionEnum("shipment_date")

"""TODO: standardize to these"""
shipping_date = utils.OptionEnum("shipping_date") # TODO: change format to datetime
shipping_time = utils.OptionEnum("shipping_time")
shipping_date = utils.OptionEnum("shipping_date") # format: %Y-%m-%dT%H:%M


class ShippingOptions(Options):
Expand Down Expand Up @@ -1174,11 +1173,7 @@ def shipment_date(self) -> utils.OptionEnum:

@property
def shipping_date(self) -> utils.OptionEnum:
return self[ShippingOption.shipping_date.name] or self.shipment_date

@property
def shipping_time(self) -> utils.OptionEnum:
return self[ShippingOption.shipping_time.name]
return self[ShippingOption.shipping_date.name]

@property
def signature_confirmation(self) -> utils.OptionEnum:
Expand Down
13 changes: 7 additions & 6 deletions packages/core/modules/Labels/create_labels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { useAppMode } from "@karrio/hooks/app-mode";
import { useSearchParams } from "next/navigation";
import { useOrders } from "@karrio/hooks/order";
import Image from "next/legacy/image";
import moment from "moment";
import React from "react";

export const generateMetadata = dynamicMetadata("Create labels");
Expand Down Expand Up @@ -1404,21 +1405,21 @@ export default function Page(pageProps: any) {
/>

<div className="p-3 pb-0">
{/* shipment date */}
{/* shipping date */}
<InputField
name="shipment_date"
label="shipment date"
type="date"
name="shipping_date"
label="shipping date"
type="datetime-local"
className="is-small"
fieldClass="column mb-0 is-8 p-0 mb-2"
defaultValue={
shipment.options?.shipment_date
shipment.options?.shipping_date
}
onChange={(e) =>
onChange(shipment_index, shipment, {
options: {
...shipment.options,
shipment_date: e.target.value,
shipping_date: e.target.value,
},
})
}
Expand Down
12 changes: 6 additions & 6 deletions packages/core/modules/Orders/create_label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -776,20 +776,20 @@ export default function Page(pageProps: any) {
<hr className="my-1" style={{ height: "1px" }} />

<div className="p-3 pb-0">
{/* shipment date */}
{/* shipping date */}
<InputField
name="shipment_date"
label="shipment date"
type="date"
name="shipping_date"
label="shipping date"
type="datetime-local"
className="is-small"
wrapperClass="px-1 py-2"
fieldClass="column mb-0 is-4 p-0"
defaultValue={shipment.options?.shipment_date}
defaultValue={shipment.options?.shipping_date}
onChange={(e) =>
onChange({
options: {
...shipment.options,
shipment_date: e.target.value,
shipping_date: e.target.value,
},
})
}
Expand Down
15 changes: 8 additions & 7 deletions packages/core/modules/Shipments/create_label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ import { bundleContexts } from "@karrio/hooks/utils";
import { useLocation } from "@karrio/hooks/location";
import { useAppMode } from "@karrio/hooks/app-mode";
import React, { useEffect, useState } from "react";
import { useSearchParams } from "next/navigation";
import { useOrders } from "@karrio/hooks/order";
import { Disclosure } from "@headlessui/react";
import { useSearchParams } from "next/navigation";
import moment from "moment";

export const generateMetadata = dynamicMetadata("Create Label");
const ContextProviders = bundleContexts([
Expand Down Expand Up @@ -870,20 +871,20 @@ export default function CreateLabelPage(pageProps: any) {
<hr className="my-1" style={{ height: "1px" }} />

<div className="p-3 pb-0">
{/* shipment date */}
{/* shipping date */}
<InputField
name="shipment_date"
label="shipment date"
type="date"
name="shipping_date"
label="shipping date"
type="datetime-local"
className="is-small"
wrapperClass="px-1 py-2"
fieldClass="column mb-0 is-4 p-0"
defaultValue={shipment.options?.shipment_date}
defaultValue={shipment.options?.shipping_date}
onChange={(e) =>
onChange({
options: {
...shipment.options,
shipment_date: e.target.value,
shipping_date: e.target.value,
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/label-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const DEFAULT_SHIPMENT_DATA = {
shipper: {} as AddressType,
recipient: {} as AddressType,
parcels: [] as ParcelType[],
options: { shipment_date: moment().format("YYYY-MM-DD") },
options: { shipping_date: moment().format("YYYY-MM-DDTHH:mm") },
payment: { paid_by: PaidByEnum.sender },
label_type: LabelTypeEnum.PDF,
} as ShipmentType;
Expand Down
Loading

0 comments on commit 388405d

Please sign in to comment.