From 0d8d5b522c70a015b8ddf99874894d282fc2fe12 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Sun, 12 Jun 2022 22:03:34 +0530 Subject: [PATCH 1/5] feat: log clearing support --- .../ecommerce_integration_log.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ecommerce_integrations/ecommerce_integrations/doctype/ecommerce_integration_log/ecommerce_integration_log.py b/ecommerce_integrations/ecommerce_integrations/doctype/ecommerce_integration_log/ecommerce_integration_log.py index 54517501..94ab25e4 100644 --- a/ecommerce_integrations/ecommerce_integrations/doctype/ecommerce_integration_log/ecommerce_integration_log.py +++ b/ecommerce_integrations/ecommerce_integrations/doctype/ecommerce_integration_log/ecommerce_integration_log.py @@ -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 @@ -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, From 9d131f58469529145a4bd1a4ca70b24281798c54 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 27 Jun 2022 11:23:40 +0530 Subject: [PATCH 2/5] fix(UX): show logs button on shopify setting --- .../shopify/doctype/shopify_setting/shopify_setting.js | 3 +++ .../doctype/unicommerce_settings/unicommerce_settings.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ecommerce_integrations/shopify/doctype/shopify_setting/shopify_setting.js b/ecommerce_integrations/shopify/doctype/shopify_setting/shopify_setting.js index 68127c63..e80cf302 100644 --- a/ecommerce_integrations/shopify/doctype/shopify_setting/shopify_setting.js +++ b/ecommerce_integrations/shopify/doctype/shopify_setting/shopify_setting.js @@ -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"}); + }); } }); diff --git a/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.js b/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.js index ec9d2d83..4f4344c3 100644 --- a/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.js +++ b/ecommerce_integrations/unicommerce/doctype/unicommerce_settings/unicommerce_settings.js @@ -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"]; From 91b2ee9a1530ad67f67d151370edf6f885f79e58 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 27 Jun 2022 11:31:48 +0530 Subject: [PATCH 3/5] fix(unicommerce): use updated since instead of from_date Use case: 1. If order is created long time ago but completed in last 1 day then it should still show up in the searched list. This is only applicable to users using "only sync completed orders" --- ecommerce_integrations/unicommerce/order.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ecommerce_integrations/unicommerce/order.py b/ecommerce_integrations/unicommerce/order.py index 76b7776a..1c7e4824 100644 --- a/ecommerce_integrations/unicommerce/order.py +++ b/ecommerce_integrations/unicommerce/order.py @@ -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 @@ -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") From 2b3fa0ca4dcb29219ff3394a09b00e40b9e433a2 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 27 Jun 2022 11:38:15 +0530 Subject: [PATCH 4/5] fix: dont log error for item query For all new item, this is only done to check if they don't already exist. Logging them as error not required. --- ecommerce_integrations/unicommerce/api_client.py | 8 +++++--- ecommerce_integrations/unicommerce/product.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ecommerce_integrations/unicommerce/api_client.py b/ecommerce_integrations/unicommerce/api_client.py index dcedac38..1b3581eb 100644 --- a/ecommerce_integrations/unicommerce/api_client.py +++ b/ecommerce_integrations/unicommerce/api_client.py @@ -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: @@ -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"): @@ -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 diff --git a/ecommerce_integrations/unicommerce/product.py b/ecommerce_integrations/unicommerce/product.py index ea61d98a..d6179206 100644 --- a/ecommerce_integrations/unicommerce/product.py +++ b/ecommerce_integrations/unicommerce/product.py @@ -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: From 7e72611750205ea2def1a86680c8806714e45f17 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 27 Jun 2022 12:11:25 +0530 Subject: [PATCH 5/5] ci: run CI everyday --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 02134f52..48b11e21 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,6 +8,9 @@ on: branches: - develop - main + schedule: + # Run everday at midnight UTC / 5:30 IST + - cron: "0 0 * * *" concurrency: group: develop-${{ github.event.number }}