Some advances.

This commit is contained in:
counterweight 2023-08-03 09:42:22 +02:00
parent 2fb57b5beb
commit ad4bc8c0da
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C

View file

@ -1,6 +1,7 @@
from pathlib import Path from pathlib import Path
import datetime import datetime
from typing import List, Dict from typing import List, Dict
import logging
from dotenv import dotenv_values from dotenv import dotenv_values
from woocommerce import API from woocommerce import API
@ -15,9 +16,14 @@ WC_API = API(
version=API_CONFIG["VERSION"], version=API_CONFIG["VERSION"],
) )
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
def check_health(): def check_health():
print(f"Connecting to the configured woocomerce at {API_CONFIG['URL']}") logger.info(
f"Connecting to the configured woocomerce at {API_CONFIG['URL']}"
)
try: try:
api_reported_version = WC_API.get("").json()["namespace"] api_reported_version = WC_API.get("").json()["namespace"]
@ -26,24 +32,65 @@ def check_health():
"There was an issue connecting to the woocomerce API." "There was an issue connecting to the woocomerce API."
) )
print(f"Informed version of the API: {API_CONFIG['VERSION']}") logger.info(f"Informed version of the API: {API_CONFIG['VERSION']}")
print(f"Version reported by the API itself: {api_reported_version}") logger.info(f"Version reported by the API itself: {api_reported_version}")
print("Connection successful. The API is reachable.") logger.info("Connection successful. The API is reachable.")
def generate_um_report(start_date: str, end_date: str): def generate_um_report(start_date: str, end_date: str):
start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d").isoformat()
end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d").isoformat()
logger.info(f"Fetching orders between {start_date} and {end_date}.")
orders_in_date_range = WC_API.get( orders_in_date_range = WC_API.get(
endpoint="orders", endpoint="orders",
params={ params={
"after": datetime.datetime.strptime( "after": start_date,
start_date, "%Y-%m-%d" "before": end_date,
).isoformat(),
"before": datetime.datetime.strptime(
end_date, "%Y-%m-%d"
).isoformat(),
"per_page": 100, "per_page": 100,
"status": "processing,completed", "status": "processing,completed",
}, },
).json()
logger.info(f"Received {len(orders_in_date_range)} orders.")
skus_to_keep = ["TEE-05-BBO-BLACK"]
logger.info(f"Filtering by SKUs: {skus_to_keep}")
relevant_orders = filter_orders_by_sku(
orders_in_date_range, skus=skus_to_keep
) )
logger.info(f"Kept {len(relevant_orders)} orders.")
# Fetch orders:
# - Between specific dates
# - That contain the hardcoded products
# - That have been paid, hence status is either processing or completed
# - That have not been settled yet (is_settled_with_um: 0)
# Print to screen:
# - Orders that do not have the `sats_received` metadata informed
# - The unit count for each product
# - The sales sum for each product
# - The sats sum for each product
# - The corresponding payment owed to UM
# - The list of order ids that have been taken into account
# Update orders:
# - Add metadata entry: is_settled_with_um: 1
def filter_orders_by_sku(orders: List[Dict], skus: List[str]):
filtered_orders = []
for order in orders:
for item in order["line_items"]:
if item["sku"] in skus:
filtered_orders.append(order)
return filtered_orders