Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: release #192

Merged
merged 8 commits into from
Jun 27, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.query_builder import Interval
from frappe.query_builder.functions import Now
from frappe.utils import strip_html
from frappe.utils.data import cstr

Expand All @@ -27,6 +29,13 @@ def _set_title(self):
title = strip_html(title)
self.title = title if len(title) < 100 else title[:100] + "..."

@staticmethod
def clear_old_logs(days=90):
table = frappe.qb.DocType("Ecommerce Integration Log")
frappe.db.delete(
table, filters=((table.modified < (Now() - Interval(days=days)))) & (table.status == "Success")
)


def create_log(
module_def=None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ frappe.ui.form.on("Shopify Setting", {
frm.add_custom_button(__('Import Products'), function () {
frappe.set_route('shopify-import-products');
});
frm.add_custom_button(__("View Logs"), () => {
frappe.set_route("List", "Ecommerce Integration Log", {"integration": "Shopify"});
});
}
});

Expand Down
8 changes: 5 additions & 3 deletions ecommerce_integrations/unicommerce/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def request(
body: Optional[JsonDict] = None,
params: Optional[JsonDict] = None,
files: Optional[JsonDict] = None,
log_error=True,
) -> Tuple[JsonDict, bool]:

if headers is None:
Expand All @@ -60,7 +61,8 @@ def request(
response.reason = cstr(response.reason) + cstr(response.text)
response.raise_for_status()
except Exception:
create_unicommerce_log(status="Error", make_new=True)
if log_error:
create_unicommerce_log(status="Error", make_new=True)
return None, False

if method == "GET" and "application/json" not in response.headers.get("content-type"):
Expand All @@ -81,13 +83,13 @@ def request(

return data, status

def get_unicommerce_item(self, sku: str) -> Optional[JsonDict]:
def get_unicommerce_item(self, sku: str, log_error=True) -> Optional[JsonDict]:
"""Get Unicommerce item data for specified SKU code.

ref: https://documentation.unicommerce.com/docs/itemtype-get.html
"""
item, status = self.request(
endpoint="/services/rest/v1/catalog/itemType/get", body={"skuCode": sku}
endpoint="/services/rest/v1/catalog/itemType/get", body={"skuCode": sku}, log_error=log_error
)
if status:
return item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ frappe.ui.form.on("Unicommerce Settings", {
}

frm.add_custom_button(__("View Logs"), () => {
frappe.set_route("List", "Ecommerce Integration Log", "List");
frappe.set_route("List", "Ecommerce Integration Log", {"integration": "Unicommerce"});
});

let sync_buttons = ["Items", "Orders", "Inventory"];
Expand Down
9 changes: 4 additions & 5 deletions ecommerce_integrations/unicommerce/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def sync_new_orders(client: UnicommerceAPIClient = None, force=False):

status = "COMPLETE" if settings.only_sync_completed_orders else None

new_orders = _get_new_orders(
client, from_date=add_to_date(settings.last_order_sync, days=-1), status=status
)
new_orders = _get_new_orders(client, status=status)

if new_orders is None:
return
Expand All @@ -63,12 +61,13 @@ def sync_new_orders(client: UnicommerceAPIClient = None, force=False):


def _get_new_orders(
client: UnicommerceAPIClient, from_date: str, status: Optional[str]
client: UnicommerceAPIClient, status: Optional[str]
) -> Optional[Iterator[UnicommerceOrder]]:

"""Search new sales order from unicommerce."""

uni_orders = client.search_sales_order(from_date=from_date, status=status)
updated_since = 24 * 60 # minutes
uni_orders = client.search_sales_order(updated_since=updated_since, status=status)
configured_channels = {
c.channel_id
for c in frappe.get_all("Unicommerce Channel", filters={"enabled": 1}, fields="channel_id")
Expand Down
2 changes: 1 addition & 1 deletion ecommerce_integrations/unicommerce/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def upload_items_to_unicommerce(
item_data = _build_unicommerce_item(item_code)
sku = item_data.get("skuCode")

item_exists = bool(client.get_unicommerce_item(sku))
item_exists = bool(client.get_unicommerce_item(sku, log_error=False))
_, status = client.create_update_item(item_data, update=item_exists)

if status:
Expand Down