{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from api import BigApi\n",
    "BigApi.init(\".admin.env\")\n",
    "BigApi.adminLogin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "orders = BigApi.adminGet(f\"/admin/shop/orders?origin=Shopify&state=WaitingForManufacturing&shopifyProductIds=8346621739225\")\n",
    "print(len(orders[\"items\"]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url_dict = {}\n",
    "for order in orders[\"items\"]:\n",
    "    bigOrderId = order[\"id\"]\n",
    "    url_dict[order[\"id\"]] = f\"/admin/fabricator/jobs?bigOrderId={bigOrderId}\"\n",
    "\n",
    "results = BigApi.adminGetAll(url_dict)\n",
    "\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time \n",
    "print(len(results))\n",
    "\n",
    "jobIdsToDestroy = []\n",
    "\n",
    "skippedNewerJobCount = 0\n",
    "\n",
    "for bigOrderId, jobQueryResults in results.items():\n",
    "    #print(orderId)\n",
    "    # Count undestroyed jobs\n",
    "    undestroyedJobs = [job for job in jobQueryResults[\"items\"] if job[\"jobState\"] != 666]\n",
    "\n",
    "    if len(undestroyedJobs) != 2:\n",
    "        #print(\"skipping\", orderId)\n",
    "        continue\n",
    "\n",
    "    # sort jobs by createdAt\n",
    "    undestroyedJobs.sort(key=lambda job: job[\"createdAt\"])\n",
    "\n",
    "    olderJob = undestroyedJobs[0]\n",
    "    newerJob = undestroyedJobs[1]\n",
    "\n",
    "    if newerJob[\"jobState\"] != 5:\n",
    "        print(\"Expected new job state to be 5\", bigOrderId)\n",
    "        continue\n",
    "\n",
    "    # milliseconds since epoch, 4 hours ago\n",
    "    sixHoursAgo = int((time.time() - 5*60*60) * 1000)\n",
    "\n",
    "    if newerJob[\"createdAt\"] < sixHoursAgo:\n",
    "        print(\"Job is too old, skipping\", newerJob[\"id\"])\n",
    "        skippedNewerJobCount += 1\n",
    "        continue\n",
    "\n",
    "    # If both jobs are 5, delete the older one\n",
    "    if olderJob[\"jobState\"] == 5 and newerJob[\"jobState\"] == 5:\n",
    "        jobIdsToDestroy.append(olderJob[\"id\"])\n",
    "        #print(\"Job has two\", orderId)\n",
    "        continue\n",
    "\n",
    "    # We want to delete the older job and override the order with the newer job id.\n",
    "    jobIdsToDestroy.append(newerJob[\"id\"])\n",
    "\n",
    "    print(\"Restoring older job: \", olderJob[\"id\"])\n",
    "\n",
    "    #data = BigApi.adminPut(f\"/admin/shop/order/{bigOrderId}\", {\n",
    "     #   \"action\": \"AdminOverrideJobId\",\n",
    "     #   \"overrideJobId\": olderJob[\"id\"]\n",
    "    #})\n",
    "\n",
    "    #data = BigApi.adminGet(f\"/admin/shop/order/{bigOrderId}/checklist\")\n",
    "\n",
    "print(skippedNewerJobCount)\n",
    "print(len(jobIdsToDestroy))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for jobId in jobIdsToDestroy:\n",
    "    BigApi.adminPut(f\"/admin/fabricator/job/{jobId}\", {\"action\" : 2004 })\n",
    "print(results)"
   ]
  }
 ],
 "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
}
