{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import websockets\n",
    "import json\n",
    "import base64\n",
    "\n",
    "from api import BigApi\n",
    "BigApi.init(\".admin.env\")\n",
    "BigApi.adminLogin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def containsAudioStrap(shopifyOrder):\n",
    "    for lineItem in shopifyOrder[\"line_items\"]:\n",
    "        if lineItem[\"product_id\"] == 8100195008729:\n",
    "            if lineItem[\"fulfillment_status\"] == None and lineItem[\"fulfillable_quantity\"] == 1:\n",
    "                return True\n",
    "    return False\n",
    "\n",
    "def isValidOrder(shopifyOrder):\n",
    "    if shopifyOrder[\"cancelled_at\"] != None:\n",
    "        return False\n",
    "    if shopifyOrder[\"financial_status\"] == \"refunded\" or shopifyOrder[\"financial_status\"] == \"partially_refunded\":\n",
    "        return False\n",
    "    return True\n",
    "\n",
    "def containsOneUnfulfilledItem(shopifyOrder):\n",
    "    unfulfilledLineItems = 0\n",
    "    for lineItem in shopifyOrder[\"line_items\"]:\n",
    "        if lineItem[\"fulfillment_status\"] == None:\n",
    "            unfulfilledLineItems += 1\n",
    "    return unfulfilledLineItems >= 1\n",
    "\n",
    "def getShopifyOrders(status, fulfillment_status = None):\n",
    "    nextCursor = None\n",
    "    orders = []\n",
    "    while(True):\n",
    "        url = f\"/admin/shop/shopify_orders?limit=200\"\n",
    "        if status != None:\n",
    "            url += \"&status=\" + status\n",
    "        if nextCursor != None:\n",
    "            url += \"&cursor=\" + nextCursor\n",
    "        if fulfillment_status != None:\n",
    "            url += \"&fulfillment_status=\" + fulfillment_status\n",
    "        data = BigApi.adminGet(url)\n",
    "\n",
    "        for order in data[\"orders\"]:\n",
    "            if order.get(\"bigOrder\"):\n",
    "                # Skip if there's already a big order with exactly one line item and it's the audio strap\n",
    "                if order[\"bigOrder\"].get(\"currentLineItems\") and len(order[\"bigOrder\"][\"currentLineItems\"]) == 1:\n",
    "                    if order[\"bigOrder\"][\"currentLineItems\"][0][\"type\"] == \"AudioStrapV1\":\n",
    "                        #print(f\"Skipping order {order['shopifyOrder']['id']} because it has a big order with exactly one line item\")\n",
    "                        continue\n",
    "\n",
    "                if isValidOrder(order[\"shopifyOrder\"]) == False:\n",
    "                    #print(f\"Ignoring order {order['shopifyOrder']['id']} because it is not valid\")\n",
    "                    pass\n",
    "                else:\n",
    "                    if containsAudioStrap(order['shopifyOrder']):\n",
    "                        if containsOneUnfulfilledItem(order['shopifyOrder']):\n",
    "                            #print(f\"Adding order {order['shopifyOrder']['id']} because it looks good to go\")\n",
    "                            #orders.append(order['shopifyOrder'])\n",
    "                            pass\n",
    "                        else:\n",
    "                            #print(f\"Ignoring order {order['shopifyOrder']['id']} because it has more than one unfulfilled item\")\n",
    "                            pass\n",
    "            else:\n",
    "                if isValidOrder(order[\"shopifyOrder\"]) == False:\n",
    "                    #print(f\"Ignoring order {order['shopifyOrder']['id']} because it is not valid\")\n",
    "                    pass\n",
    "                else:\n",
    "                    if containsAudioStrap(order['shopifyOrder']):\n",
    "                        if containsOneUnfulfilledItem(order['shopifyOrder']):\n",
    "                            #print(f\"Adding order {order['shopifyOrder']['id']} because it looks good to go\")\n",
    "                            print(f\"https://admin.shopify.com/store/bigscreenvr/orders/{order['shopifyOrder']['id']}\")\n",
    "                            #orders.append(order['shopifyOrder'])\n",
    "\n",
    "        if \"nextCursor\" in data:\n",
    "            nextCursor = data[\"nextCursor\"]\n",
    "\n",
    "        if (len(data[\"orders\"]) < 200):\n",
    "            break\n",
    "        if (len(orders) > 10000):\n",
    "            print(\"Breaking because we have more than 2000 orders\")\n",
    "            break   \n",
    "    return orders\n",
    "\n",
    "audioStrapOrders = getShopifyOrders(\"open\", None)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for order in audioStrapOrders:\n",
    "    print(order[\"id\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start = 0\n",
    "end = 100\n",
    "\n",
    "print(len(audioStrapOrders[start:end]))\n",
    "print([order[\"id\"] for order in audioStrapOrders[start:end]])\n",
    "\n",
    "for order in audioStrapOrders[start:end]:\n",
    "    print(order[\"id\"])\n",
    "    try:\n",
    "        data = BigApi.adminPost(\"/admin/shop/order?audiostrap=1\", {\"shopifyOrderId\": order[\"id\"]})\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        print(order)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shopifyId = \"\"\n",
    "\n",
    "try:\n",
    "    data = BigApi.adminPost(\"/admin/shop/order?audiostrap=1\", {\"shopifyOrderId\": shopifyId})\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "    print(order)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
