{
 "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": [
    "AwaitingFulfilment = 20\n",
    "jobQueryResult = BigApi.adminGet(f\"/admin/fabricator/jobs?jobState={AwaitingFulfilment}\")\n",
    "print(jobQueryResult[\"count\"])\n",
    "len(jobQueryResult[\"items\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "problemString = \"\\\"QAInProgress\\\" (18) to \\\"AwaitingFulfilment\\\" (20)\"\n",
    "for job in jobQueryResult[\"items\"]:\n",
    "    # get last item in job.history\n",
    "    lastHistory = job[\"history\"][-1]\n",
    "    lastHistoryMessage = lastHistory[\"message\"]\n",
    "\n",
    "    # check if the lastHistoryMessage ends with prolbmeString\n",
    "    if lastHistoryMessage.endswith(problemString):\n",
    "        print(job[\"id\"])\n",
    "        # print(job[\"history\"])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "status = \"InStock\"\n",
    "type = \"BeyondCushionV1\"\n",
    "inventoryQueryResponse = BigApi.adminGet(f\"/admin/inventory/items?type={type}&status={status}\")\n",
    "inventoryItems = inventoryQueryResponse[\"items\"]\n",
    "print(len(inventoryItems))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(inventoryItems[0][\"jobId\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for job in jobQueryResult[\"items\"]:\n",
    "    found = False\n",
    "    for inventoryItem in inventoryItems:\n",
    "        if job[\"id\"] == inventoryItem[\"jobId\"]:\n",
    "            break\n",
    "    if not found:\n",
    "        print(f\"Job {job['id']} not found in inventory\")\n",
    "        break"
   ]
  }
 ],
 "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
}
