diff --git a/jobs/correction-ben-statement/.env.example b/jobs/correction-ben-statement/.env.example new file mode 100644 index 0000000000..2d2cbecb8b --- /dev/null +++ b/jobs/correction-ben-statement/.env.example @@ -0,0 +1,9 @@ +ACCOUNT_SVC_AUTH_URL= +ACCOUNT_SVC_CLIENT_ID= +ACCOUNT_SVC_CLIENT_SECRET= +LEGAL_API_BASE_URL= +ENTITY_DATABASE_USERNAME= +ENTITY_DATABASE_PASSWORD= +ENTITY_DATABASE_HOST= +ENTITY_DATABASE_NAME= +ENTITY_DATABASE_PORT= diff --git a/jobs/correction-ben-statement/add_corrections_alterations.ipynb b/jobs/correction-ben-statement/add_corrections_alterations.ipynb index 129683660c..a23bdc1ea1 100644 --- a/jobs/correction-ben-statement/add_corrections_alterations.ipynb +++ b/jobs/correction-ben-statement/add_corrections_alterations.ipynb @@ -26,8 +26,7 @@ "source": [ "import os\n", "from dotenv import load_dotenv, find_dotenv\n", - "import psycopg2\n", - "import pandas as pd\n", + "from sqlalchemy import create_engine, text\n", "\n", "# this will load all the envars from a .env file located in the project root (api)\n", "load_dotenv(find_dotenv())\n", @@ -45,19 +44,11 @@ " os.getenv('ENTITY_DATABASE_USERNAME', '') + \":\" + os.getenv('ENTITY_DATABASE_PASSWORD', '') +'@' + \\\n", " os.getenv('ENTITY_DATABASE_HOST', '') + ':' + os.getenv('ENTITY_DATABASE_PORT', '5432') + '/' + \\\n", " os.getenv('ENTITY_DATABASE_NAME', '');\n", - "connect_to_db\n", - " \n", - "%sql $connect_to_db" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%%sql \n", - "select now() AT TIME ZONE 'PST' as current_date" + "engine = create_engine(connect_to_db)\n", + "\n", + "# Test connection\n", + "with engine.connect() as conn:\n", + " print(\"Connected successfully!\")" ] }, { @@ -129,82 +120,91 @@ "skipped_identifiers = []\n", "\n", "# loop through list of businesses to create filing\n", - "for identifier in businesses:\n", - " filing_details = %sql \\\n", - " SELECT f.id, f.filing_date \\\n", - " FROM businesses b \\\n", - " JOIN filings f ON b.id = f.business_id \\\n", - " WHERE f.filing_type = 'alteration' \\\n", - " AND f.meta_data->'alteration'->>'fromLegalType' IN ('BC', 'ULC', 'CC', 'C', 'CUL', 'CCC') \\\n", - " AND f.meta_data->'alteration'->>'toLegalType' IN ('BEN', 'CBEN') \\\n", - " AND b.identifier = :identifier\n", - " \n", - " if filing_details:\n", - " filing_id = filing_details[0]['id']\n", - " filing_date = filing_details[0]['filing_date']\n", - "\n", - " formatted_filing_date = filing_date.strftime(\"%B %d, %Y\")\n", - " \n", - " draft_details = %sql \\\n", - " SELECT b.state, \\\n", - " (SELECT COUNT(1) \\\n", - " FROM filings f \\\n", - " WHERE f.business_id = b.id \\\n", - " AND f.status in ('DRAFT', 'PENDING')) <> 0 AS has_draft \\\n", - " FROM businesses b \\\n", - " WHERE b.identifier = :identifier\n", - " state = None\n", - " has_draft = None\n", - " if draft_details:\n", - " state = draft_details[0]['state']\n", - " has_draft = draft_details[0]['has_draft']\n", - " \n", - " if state != 'ACTIVE' or has_draft:\n", - " skipped_identifiers.append(identifier)\n", - " continue\n", - " \n", - " correction_filing_data = {\n", - " \"filing\": {\n", - " \"header\": {\n", - " \"name\": \"correction\",\n", - " \"date\": current_date,\n", - " \"certifiedBy\": \"system\",\n", - " \"correctionBenStatement\": True,\n", - " \"waiveFees\": True\n", - " },\n", - " \"business\": {\n", - " \"identifier\": identifier,\n", - " \"legalType\": \"BEN\"\n", - " },\n", - " \"correction\": {\n", - " \"details\": \"BEN Correction statement\",\n", - " \"correctedFilingId\": filing_id,\n", - " \"correctedFilingType\": \"alteration\",\n", - " \"commentOnly\": True,\n", - " \"comment\": f\"\"\"Correction for Alteration filed on {formatted_filing_date} \\n{correction_statement}\"\"\"\n", + "with engine.connect() as conn:\n", + " for identifier in businesses:\n", + " filing_details_query = text(\"\"\"\n", + " SELECT f.id, f.filing_date \n", + " FROM businesses b \n", + " JOIN filings f ON b.id = f.business_id \n", + " WHERE f.filing_type = 'alteration' \n", + " AND f.meta_data->'alteration'->>'fromLegalType' IN ('BC', 'ULC', 'CC', 'C', 'CUL', 'CCC') \n", + " AND f.meta_data->'alteration'->>'toLegalType' IN ('BEN', 'CBEN') \n", + " AND b.identifier = :identifier\n", + " \"\"\")\n", + " filing_details_query_result = conn.execute(filing_details_query, {\"identifier\": identifier})\n", + " filing_details = filing_details_query_result.mappings().fetchone()\n", + " \n", + " if filing_details:\n", + " filing_id = filing_details['id']\n", + " filing_date = filing_details['filing_date']\n", + "\n", + " formatted_filing_date = filing_date.strftime(\"%B %d, %Y\")\n", + " \n", + " draft_details_query = text(\"\"\"\n", + " SELECT b.state,\n", + " (SELECT COUNT(1)\n", + " FROM filings f\n", + " WHERE f.business_id = b.id\n", + " AND f.status in ('DRAFT', 'PENDING')) <> 0 AS has_draft\n", + " FROM businesses b\n", + " WHERE b.identifier = :identifier\n", + " \"\"\")\n", + " \n", + " draft_details_query_result = conn.execute(draft_details_query, {\"identifier\": identifier})\n", + " draft_details = draft_details_query_result.mappings().fetchone()\n", + " \n", + " state = None\n", + " has_draft = None\n", + " if draft_details:\n", + " state = draft_details['state']\n", + " has_draft = draft_details['has_draft']\n", + " \n", + " if state != 'ACTIVE' or has_draft:\n", + " skipped_identifiers.append(identifier)\n", + " continue\n", + " \n", + " correction_filing_data = {\n", + " \"filing\": {\n", + " \"header\": {\n", + " \"name\": \"correction\",\n", + " \"date\": current_date,\n", + " \"certifiedBy\": \"system\",\n", + " \"correctionBenStatement\": True,\n", + " \"waiveFees\": True\n", + " },\n", + " \"business\": {\n", + " \"identifier\": identifier,\n", + " \"legalType\": \"BEN\"\n", + " },\n", + " \"correction\": {\n", + " \"details\": \"BEN Correction statement\",\n", + " \"correctedFilingId\": filing_id,\n", + " \"correctedFilingType\": \"alteration\",\n", + " \"commentOnly\": True,\n", + " \"comment\": f\"\"\"Correction for Alteration filed on {formatted_filing_date} \\n{correction_statement}\"\"\"\n", + " }\n", " }\n", " }\n", - " }\n", - "\n", - " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", - " rv = requests.post(filing_url, headers=headers, json=correction_filing_data)\n", - "\n", - " # Check the status code of the response\n", - " if rv.status_code == 201:\n", - " correction_filing_id = rv.json()[\"filing\"][\"header\"][\"filingId\"]\n", - " successful_identifiers.append(identifier)\n", - " else:\n", - " failed_identifiers.append(identifier)\n", - " print(f\"Failed to make POST request. Status code: {rv.status_code}: {rv.text} for {identifier}\")\n", - "print('Successfully filed Corrections for:', successful_identifiers) # Print the error message if the request fails \n", - "print('Failed to file Corrections for:', failed_identifiers) # Print the error message if the request fails\n", - "print('Skipped to file Corrections for:', skipped_identifiers) # Print the skipped identifiers\n" + "\n", + " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", + " rv = requests.post(filing_url, headers=headers, json=correction_filing_data)\n", + "\n", + " # Check the status code of the response\n", + " if rv.status_code == 201:\n", + " correction_filing_id = rv.json()[\"filing\"][\"header\"][\"filingId\"]\n", + " successful_identifiers.append(identifier)\n", + " else:\n", + " failed_identifiers.append(identifier)\n", + " print(f\"Failed to make POST request. Status code: {rv.status_code}: {rv.text} for {identifier}\")\n", + " print('Successfully filed Corrections for:', successful_identifiers) # Print the error message if the request fails \n", + " print('Failed to file Corrections for:', failed_identifiers) # Print the error message if the request fails\n", + " print('Skipped to file Corrections for:', skipped_identifiers) # Print the skipped identifiers" ] } ], "metadata": { "kernelspec": { - "display_name": "3.8.17", + "display_name": "3.11.7", "language": "python", "name": "python3" }, @@ -218,7 +218,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.17" + "version": "3.11.7" } }, "nbformat": 4, diff --git a/jobs/correction-ben-statement/add_corrections_ia.ipynb b/jobs/correction-ben-statement/add_corrections_ia.ipynb index dc330cc548..e8b5966705 100644 --- a/jobs/correction-ben-statement/add_corrections_ia.ipynb +++ b/jobs/correction-ben-statement/add_corrections_ia.ipynb @@ -26,8 +26,7 @@ "source": [ "import os\n", "from dotenv import load_dotenv, find_dotenv\n", - "import psycopg2\n", - "import pandas as pd\n", + "from sqlalchemy import create_engine, text\n", "\n", "# this will load all the envars from a .env file located in the project root (api)\n", "load_dotenv(find_dotenv())\n", @@ -45,19 +44,11 @@ " os.getenv('ENTITY_DATABASE_USERNAME', '') + \":\" + os.getenv('ENTITY_DATABASE_PASSWORD', '') +'@' + \\\n", " os.getenv('ENTITY_DATABASE_HOST', '') + ':' + os.getenv('ENTITY_DATABASE_PORT', '5432') + '/' + \\\n", " os.getenv('ENTITY_DATABASE_NAME', '');\n", - "connect_to_db\n", - " \n", - "%sql $connect_to_db" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%%sql \n", - "select now() AT TIME ZONE 'PST' as current_date" + "engine = create_engine(connect_to_db)\n", + "\n", + "# Test connection\n", + "with engine.connect() as conn:\n", + " print(\"Connected successfully!\")" ] }, { @@ -129,80 +120,87 @@ "skipped_identifiers = []\n", "\n", "# loop through list of businesses to create filing\n", - "for identifier in businesses:\n", - " filing_details = %sql \\\n", - " SELECT f.id, f.filing_date \\\n", - " FROM businesses b \\\n", - " JOIN filings f ON b.id = f.business_id \\\n", - " WHERE f.filing_type = 'incorporationApplication' \\\n", - " AND b.identifier = :identifier\n", - " \n", - " if filing_details:\n", - " filing_id = filing_details[0]['id']\n", - " filing_date = filing_details[0]['filing_date']\n", - "\n", - " formatted_filing_date = filing_date.strftime(\"%B %d, %Y\")\n", - " \n", - " draft_details = %sql \\\n", - " SELECT b.state, \\\n", - " (SELECT COUNT(1) \\\n", - " FROM filings f \\\n", - " WHERE f.business_id = b.id \\\n", - " AND f.status in ('DRAFT', 'PENDING')) <> 0 AS has_draft \\\n", - " FROM businesses b \\\n", - " WHERE b.identifier = :identifier\n", - " \n", - " state = None\n", - " has_draft = None\n", - " if draft_details:\n", - " state = draft_details[0]['state']\n", - " has_draft = draft_details[0]['has_draft']\n", - " \n", - " if state == 'HISTORICAL' or has_draft:\n", - " skipped_identifiers.append(identifier)\n", - " continue\n", - " \n", - " correction_filing_data = {\n", - " \"filing\": {\n", - " \"header\": {\n", - " \"name\": \"correction\",\n", - " \"date\": current_date,\n", - " \"certifiedBy\": \"system\",\n", - " \"correctionBenStatement\": True,\n", - " \"waiveFees\": True\n", - " },\n", - " \"business\": {\n", - " \"identifier\": identifier,\n", - " \"legalType\": \"BEN\"\n", - " },\n", - " \"correction\": {\n", - " \"details\": \"BEN Correction statement\",\n", - " \"correctedFilingId\": filing_id,\n", - " \"correctedFilingType\": \"incorporationApplication\",\n", - " \"commentOnly\": True,\n", - " \"comment\": f\"\"\"Correction for Incorporation Application filed on {formatted_filing_date} \\n{correction_statement}\"\"\"\n", + "with engine.connect() as conn:\n", + " for identifier in businesses:\n", + " filing_details_query = text(\"\"\"\n", + " SELECT f.id, f.filing_date \n", + " FROM businesses b \n", + " JOIN filings f ON b.id = f.business_id \n", + " WHERE f.filing_type = 'incorporationApplication' \n", + " AND b.identifier = :identifier\n", + " \"\"\")\n", + " filing_details_query_result = conn.execute(filing_details_query, {\"identifier\": identifier})\n", + " filing_details = filing_details_query_result.mappings().fetchone()\n", + " \n", + " if filing_details:\n", + " filing_id = filing_details['id']\n", + " filing_date = filing_details['filing_date']\n", + "\n", + " formatted_filing_date = filing_date.strftime(\"%B %d, %Y\")\n", + " \n", + " draft_details_query = text(\"\"\"\n", + " SELECT b.state, \n", + " (SELECT COUNT(1) \n", + " FROM filings f \n", + " WHERE f.business_id = b.id \n", + " AND f.status in ('DRAFT', 'PENDING')) <> 0 AS has_draft \n", + " FROM businesses b \n", + " WHERE b.identifier = :identifier\n", + " \"\"\")\n", + " draft_details_query_result = conn.execute(draft_details_query, {\"identifier\": identifier})\n", + " draft_details = draft_details_query_result.mappings().fetchone()\n", + " \n", + " state = None\n", + " has_draft = None\n", + " if draft_details:\n", + " state = draft_details['state']\n", + " has_draft = draft_details['has_draft']\n", + " \n", + " if state == 'HISTORICAL' or has_draft:\n", + " skipped_identifiers.append(identifier)\n", + " continue\n", + " \n", + " correction_filing_data = {\n", + " \"filing\": {\n", + " \"header\": {\n", + " \"name\": \"correction\",\n", + " \"date\": current_date,\n", + " \"certifiedBy\": \"system\",\n", + " \"correctionBenStatement\": True,\n", + " \"waiveFees\": True\n", + " },\n", + " \"business\": {\n", + " \"identifier\": identifier,\n", + " \"legalType\": \"BEN\"\n", + " },\n", + " \"correction\": {\n", + " \"details\": \"BEN Correction statement\",\n", + " \"correctedFilingId\": filing_id,\n", + " \"correctedFilingType\": \"incorporationApplication\",\n", + " \"commentOnly\": True,\n", + " \"comment\": f\"\"\"Correction for Incorporation Application filed on {formatted_filing_date} \\n{correction_statement}\"\"\"\n", + " }\n", " }\n", " }\n", - " }\n", - "\n", - " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", - " rv = requests.post(filing_url, headers=headers, json=correction_filing_data)\n", - "\n", - " # Check the status code of the response\n", - " if rv.status_code == 201:\n", - " successful_identifiers.append(identifier)\n", - " else:\n", - " failed_identifiers.append(identifier)\n", - " print(f\"Failed to make POST request. Status code: {rv.status_code}: {rv.text} for {identifier}\")\n", - "print('Successfully filed Corrections for:', successful_identifiers) # Print the successful identifiers\n", - "print('Failed to file Corrections for:', failed_identifiers) # Print the failed identifiers\n", - "print('Skipped to file Corrections for:', skipped_identifiers) # Print the skipped identifiers\n" + "\n", + " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", + " rv = requests.post(filing_url, headers=headers, json=correction_filing_data)\n", + "\n", + " # Check the status code of the response\n", + " if rv.status_code == 201:\n", + " successful_identifiers.append(identifier)\n", + " else:\n", + " failed_identifiers.append(identifier)\n", + " print(f\"Failed to make POST request. Status code: {rv.status_code}: {rv.text} for {identifier}\")\n", + " print('Successfully filed Corrections for:', successful_identifiers) # Print the successful identifiers\n", + " print('Failed to file Corrections for:', failed_identifiers) # Print the failed identifiers\n", + " print('Skipped to file Corrections for:', skipped_identifiers) # Print the skipped identifiers" ] } ], "metadata": { "kernelspec": { - "display_name": "3.8.17", + "display_name": "3.11.7", "language": "python", "name": "python3" }, @@ -216,7 +214,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.17" + "version": "3.11.7" } }, "nbformat": 4, diff --git a/jobs/correction-ben-statement/add_registrars_notation_alteration.ipynb b/jobs/correction-ben-statement/add_registrars_notation_alteration.ipynb index 24ded64ccf..d6e98bc715 100644 --- a/jobs/correction-ben-statement/add_registrars_notation_alteration.ipynb +++ b/jobs/correction-ben-statement/add_registrars_notation_alteration.ipynb @@ -26,8 +26,7 @@ "source": [ "import os\n", "from dotenv import load_dotenv, find_dotenv\n", - "import psycopg2\n", - "import pandas as pd\n", + "from sqlalchemy import create_engine, text\n", "\n", "# this will load all the envars from a .env file located in the project root (api)\n", "load_dotenv(find_dotenv())\n", @@ -45,19 +44,11 @@ " os.getenv('ENTITY_DATABASE_USERNAME', '') + \":\" + os.getenv('ENTITY_DATABASE_PASSWORD', '') +'@' + \\\n", " os.getenv('ENTITY_DATABASE_HOST', '') + ':' + os.getenv('ENTITY_DATABASE_PORT', '5432') + '/' + \\\n", " os.getenv('ENTITY_DATABASE_NAME', '');\n", - "connect_to_db\n", - " \n", - "%sql $connect_to_db" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%%sql \n", - "select now() AT TIME ZONE 'PST' as current_date" + "engine = create_engine(connect_to_db)\n", + "\n", + "# Test connection\n", + "with engine.connect() as conn:\n", + " print(\"Connected successfully!\")" ] }, { @@ -121,79 +112,87 @@ "skipped_identifiers = []\n", "\n", "# loop through list of businesses to create filing\n", - "for identifier in businesses:\n", - " filing_details = %sql \\\n", - " SELECT f.id, f.filing_date \\\n", - " FROM businesses b \\\n", - " JOIN filings f ON b.id = f.business_id \\\n", - " WHERE f.filing_type = 'alteration' \\\n", - " AND f.meta_data->'alteration'->>'fromLegalType' IN ('BC', 'ULC', 'CC', 'C', 'CUL', 'CCC') \\\n", - " AND f.meta_data->'alteration'->>'toLegalType' IN ('BEN', 'CBEN') \\\n", - " AND b.identifier = :identifier\n", - " \n", - " if filing_details:\n", - " filing_id = filing_details[0]['id']\n", - " filing_date = filing_details[0]['filing_date']\n", + "with engine.connect() as conn:\n", + " for identifier in businesses:\n", + " filing_details_query = text(\"\"\"\n", + " SELECT f.id, f.filing_date\n", + " FROM businesses b\n", + " JOIN filings f ON b.id = f.business_id\n", + " WHERE f.filing_type = 'alteration'\n", + " AND f.meta_data->'alteration'->>'fromLegalType' IN ('BC', 'ULC', 'CC', 'C', 'CUL', 'CCC')\n", + " AND f.meta_data->'alteration'->>'toLegalType' IN ('BEN', 'CBEN')\n", + " AND b.identifier = :identifier\n", + " \"\"\")\n", + " filing_details_query_result = conn.execute(filing_details_query, {\"identifier\": identifier})\n", + " filing_details = filing_details_query_result.mappings().fetchone()\n", + " \n", + " if filing_details:\n", + " filing_id = filing_details['id']\n", + " filing_date = filing_details['filing_date']\n", "\n", - " formatted_filing_date = filing_date.strftime(\"%B %d, %Y\")\n", - " \n", - " draft_details = %sql \\\n", - " SELECT b.state, \\\n", - " (SELECT COUNT(1) \\\n", - " FROM filings f \\\n", - " WHERE f.business_id = b.id \\\n", - " AND f.status in ('DRAFT', 'PENDING')) <> 0 AS has_draft \\\n", - " FROM businesses b \\\n", - " WHERE b.identifier = :identifier\n", - " \n", - " if draft_details:\n", - " state = draft_details[0]['state']\n", - " has_draft = draft_details[0]['has_draft']\n", - " \n", - " if state == 'HISTORICAL' or has_draft:\n", - " skipped_identifiers.append(identifier)\n", - " continue\n", - " \n", - " filing_data = {\n", - " \"filing\": {\n", - " \"header\": {\n", - " \"name\": \"registrarsNotation\",\n", - " \"date\": current_date,\n", - " \"certifiedBy\": \"system\"\n", - " },\n", - " \"business\": {\n", - " \"identifier\": identifier,\n", - " \"legalType\": \"BEN\"\n", - " },\n", - " \"registrarsNotation\": {\n", - " \"orderDetails\": \"BC benefit company statement contained in notice of articles as required under \" + \n", - " \"section 51.992 of the Business Corporations Act corrected from \" +\n", - " \"\\\"This company is a benefit company and, as such, has purposes that include conducting its business \" +\n", - " \" in a responsible and sustainable manner and promoting one or more public benefits\\\" to \" + \n", - " \"\\\"This company is a benefit company and, as such, is committed to conducting its business in a \" + \n", - " \"responsible and sustainable manner and promoting one or more public benefits\\\".\"\n", + " formatted_filing_date = filing_date.strftime(\"%B %d, %Y\")\n", + " \n", + " draft_details_query = text(\"\"\"\n", + " SELECT b.state,\n", + " (SELECT COUNT(1)\n", + " FROM filings f\n", + " WHERE f.business_id = b.id\n", + " AND f.status in ('DRAFT', 'PENDING')) <> 0 AS has_draft\n", + " FROM businesses b\n", + " WHERE b.identifier = :identifier\n", + " \"\"\")\n", + " \n", + " draft_details_query_result = conn.execute(draft_details_query, {\"identifier\": identifier})\n", + " draft_details = draft_details_query_result.mappings().fetchone()\n", + " \n", + " if draft_details:\n", + " state = draft_details['state']\n", + " has_draft = draft_details['has_draft']\n", + " \n", + " if state == 'HISTORICAL' or has_draft:\n", + " skipped_identifiers.append(identifier)\n", + " continue\n", + " \n", + " filing_data = {\n", + " \"filing\": {\n", + " \"header\": {\n", + " \"name\": \"registrarsNotation\",\n", + " \"date\": current_date,\n", + " \"certifiedBy\": \"system\"\n", + " },\n", + " \"business\": {\n", + " \"identifier\": identifier,\n", + " \"legalType\": \"BEN\"\n", + " },\n", + " \"registrarsNotation\": {\n", + " \"orderDetails\": \"BC benefit company statement contained in notice of articles as required under \" + \n", + " \"section 51.992 of the Business Corporations Act corrected from \" +\n", + " \"\\\"This company is a benefit company and, as such, has purposes that include conducting its business \" +\n", + " \" in a responsible and sustainable manner and promoting one or more public benefits\\\" to \" + \n", + " \"\\\"This company is a benefit company and, as such, is committed to conducting its business in a \" + \n", + " \"responsible and sustainable manner and promoting one or more public benefits\\\".\"\n", + " }\n", " }\n", " }\n", - " }\n", "\n", - " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", - " response = requests.post(filing_url, headers=headers, json=filing_data)\n", + " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", + " response = requests.post(filing_url, headers=headers, json=filing_data)\n", "\n", - " # Check the status code of the response\n", - " if response.status_code == 201:\n", - " successful_identifiers.append(identifier)\n", - " else:\n", - " failed_identifiers.append(identifier)\n", - " print(f\"Failed to make POST request. Status code: {response.status_code} for {identifier}\")\n", - "print('Successfully filed Registrar Notation for:', successful_identifiers) # Print the successful identifiers\n", - "print('Failed to file Registrar Notation for:', failed_identifiers) # Print the failed identifiers\n", - "print('Skipped to file Registrar Notation for:', skipped_identifiers) # Print the skipped identifiers\n" + " # Check the status code of the response\n", + " if response.status_code == 201:\n", + " successful_identifiers.append(identifier)\n", + " else:\n", + " failed_identifiers.append(identifier)\n", + " print(f\"Failed to make POST request. Status code: {response.status_code} for {identifier}\")\n", + " print('Successfully filed Registrar Notation for:', successful_identifiers) # Print the successful identifiers\n", + " print('Failed to file Registrar Notation for:', failed_identifiers) # Print the failed identifiers\n", + " print('Skipped to file Registrar Notation for:', skipped_identifiers) # Print the skipped identifiers" ] } ], "metadata": { "kernelspec": { - "display_name": "3.8.17", + "display_name": "3.11.7", "language": "python", "name": "python3" }, @@ -207,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.17" + "version": "3.11.7" } }, "nbformat": 4, diff --git a/jobs/correction-ben-statement/add_registrars_notation_historical.ipynb b/jobs/correction-ben-statement/add_registrars_notation_historical.ipynb index fb5ad05dd3..af2548bde8 100644 --- a/jobs/correction-ben-statement/add_registrars_notation_historical.ipynb +++ b/jobs/correction-ben-statement/add_registrars_notation_historical.ipynb @@ -26,8 +26,7 @@ "source": [ "import os\n", "from dotenv import load_dotenv, find_dotenv\n", - "import psycopg2\n", - "import pandas as pd\n", + "from sqlalchemy import create_engine, text\n", "\n", "# this will load all the envars from a .env file located in the project root (api)\n", "load_dotenv(find_dotenv())\n", @@ -45,9 +44,11 @@ " os.getenv('ENTITY_DATABASE_USERNAME', '') + \":\" + os.getenv('ENTITY_DATABASE_PASSWORD', '') +'@' + \\\n", " os.getenv('ENTITY_DATABASE_HOST', '') + ':' + os.getenv('ENTITY_DATABASE_PORT', '5434') + '/' + \\\n", " os.getenv('ENTITY_DATABASE_NAME', '');\n", - "connect_to_db\n", - " \n", - "%sql $connect_to_db" + "engine = create_engine(connect_to_db)\n", + "\n", + "# Test connection\n", + "with engine.connect() as conn:\n", + " print(\"Connected successfully!\")" ] }, { @@ -121,41 +122,42 @@ "skipped_identifiers = []\n", "\n", "# loop through list of businesses to create filing\n", - "for identifier in businesses: \n", - " filing_data = {\n", - " \"filing\": {\n", - " \"header\": {\n", - " \"name\": \"registrarsNotation\",\n", - " \"date\": current_date,\n", - " \"certifiedBy\": \"system\"\n", - " },\n", - " \"business\": {\n", - " \"identifier\": identifier,\n", - " \"legalType\": \"BEN\"\n", - " },\n", - " \"registrarsNotation\": {\n", - " \"orderDetails\": \"BC benefit company statement contained in notice of articles as required under \" + \n", - " \"section 51.992 of the Business Corporations Act corrected from \" +\n", - " \"\\\"This company is a benefit company and, as such, has purposes that include conducting its business \" +\n", - " \" in a responsible and sustainable manner and promoting one or more public benefits\\\" to \" + \n", - " \"\\\"This company is a benefit company and, as such, is committed to conducting its business in a \" + \n", - " \"responsible and sustainable manner and promoting one or more public benefits\\\".\"\n", + "with engine.connect() as conn:\n", + " for identifier in businesses: \n", + " filing_data = {\n", + " \"filing\": {\n", + " \"header\": {\n", + " \"name\": \"registrarsNotation\",\n", + " \"date\": current_date,\n", + " \"certifiedBy\": \"system\"\n", + " },\n", + " \"business\": {\n", + " \"identifier\": identifier,\n", + " \"legalType\": \"BEN\"\n", + " },\n", + " \"registrarsNotation\": {\n", + " \"orderDetails\": \"BC benefit company statement contained in notice of articles as required under \" + \n", + " \"section 51.992 of the Business Corporations Act corrected from \" +\n", + " \"\\\"This company is a benefit company and, as such, has purposes that include conducting its business \" +\n", + " \" in a responsible and sustainable manner and promoting one or more public benefits\\\" to \" + \n", + " \"\\\"This company is a benefit company and, as such, is committed to conducting its business in a \" + \n", + " \"responsible and sustainable manner and promoting one or more public benefits\\\".\"\n", + " }\n", " }\n", " }\n", - " }\n", "\n", - " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", - " response = requests.post(filing_url, headers=headers, json=filing_data)\n", + " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", + " response = requests.post(filing_url, headers=headers, json=filing_data)\n", "\n", - " # Check the status code of the response\n", - " if response.status_code == 201:\n", - " successful_identifiers.append(identifier)\n", - " else:\n", - " failed_identifiers.append(identifier)\n", - " print(f\"Failed to make POST request. Status code: {response.status_code} for {identifier}\")\n", - "print('Successfully filed Registrar Notation for:', successful_identifiers) # Print the successful identifiers\n", - "print('Failed to file Registrar Notation for:', failed_identifiers) # Print the failed identifiers\n", - "print('Skipped to file Registrar Notation for:', skipped_identifiers) # Print the skipped identifiers\n" + " # Check the status code of the response\n", + " if response.status_code == 201:\n", + " successful_identifiers.append(identifier)\n", + " else:\n", + " failed_identifiers.append(identifier)\n", + " print(f\"Failed to make POST request. Status code: {response.status_code} for {identifier}\")\n", + " print('Successfully filed Registrar Notation for:', successful_identifiers) # Print the successful identifiers\n", + " print('Failed to file Registrar Notation for:', failed_identifiers) # Print the failed identifiers\n", + " print('Skipped to file Registrar Notation for:', skipped_identifiers) # Print the skipped identifiers\n" ] } ], diff --git a/jobs/correction-ben-statement/add_registrars_notation_ia.ipynb b/jobs/correction-ben-statement/add_registrars_notation_ia.ipynb index 292656df85..9c13f189bf 100644 --- a/jobs/correction-ben-statement/add_registrars_notation_ia.ipynb +++ b/jobs/correction-ben-statement/add_registrars_notation_ia.ipynb @@ -26,8 +26,7 @@ "source": [ "import os\n", "from dotenv import load_dotenv, find_dotenv\n", - "import psycopg2\n", - "import pandas as pd\n", + "from sqlalchemy import create_engine, text\n", "\n", "# this will load all the envars from a .env file located in the project root (api)\n", "load_dotenv(find_dotenv())\n", @@ -45,19 +44,11 @@ " os.getenv('ENTITY_DATABASE_USERNAME', '') + \":\" + os.getenv('ENTITY_DATABASE_PASSWORD', '') +'@' + \\\n", " os.getenv('ENTITY_DATABASE_HOST', '') + ':' + os.getenv('ENTITY_DATABASE_PORT', '5432') + '/' + \\\n", " os.getenv('ENTITY_DATABASE_NAME', '');\n", - "connect_to_db\n", - " \n", - "%sql $connect_to_db" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%%sql \n", - "select now() AT TIME ZONE 'PST' as current_date" + "engine = create_engine(connect_to_db)\n", + "\n", + "# Test connection\n", + "with engine.connect() as conn:\n", + " print(\"Connected successfully!\")" ] }, { @@ -121,80 +112,87 @@ "skipped_identifiers = []\n", "\n", "# loop through list of businesses to create filing\n", - "for identifier in businesses:\n", - " filing_details = %sql \\\n", - " SELECT f.id, f.filing_date \\\n", - " FROM businesses b \\\n", - " JOIN filings f ON b.id = f.business_id \\\n", - " WHERE f.filing_type = 'incorporationApplication' \\\n", - " AND b.identifier = :identifier\n", - " \n", - " if filing_details:\n", - " filing_id = filing_details[0]['id']\n", - " filing_date = filing_details[0]['filing_date']\n", + "with engine.connect() as conn:\n", + " for identifier in businesses:\n", + " filing_details_query = text(\"\"\"\n", + " SELECT f.id, f.filing_date \n", + " FROM businesses b \n", + " JOIN filings f ON b.id = f.business_id \n", + " WHERE f.filing_type = 'incorporationApplication' \n", + " AND b.identifier = :identifier\n", + " \"\"\")\n", + " filing_details_query_result = conn.execute(filing_details_query, {\"identifier\": identifier})\n", + " filing_details = filing_details_query_result.mappings().fetchone()\n", + " \n", + " if filing_details:\n", + " filing_id = filing_details['id']\n", + " filing_date = filing_details['filing_date']\n", "\n", - " formatted_filing_date = filing_date.strftime(\"%B %d, %Y\")\n", - " \n", - " draft_details = %sql \\\n", - " SELECT b.state, \\\n", - " (SELECT COUNT(1) \\\n", - " FROM filings f \\\n", - " WHERE f.business_id = b.id \\\n", - " AND f.status in ('DRAFT', 'PENDING')) <> 0 AS has_draft \\\n", - " FROM businesses b \\\n", - " WHERE b.identifier = :identifier\n", - " \n", - " state = None\n", - " has_draft = None\n", - " \n", - " if draft_details:\n", - " state = draft_details[0]['state']\n", - " has_draft = draft_details[0]['has_draft']\n", - " \n", - " if state == 'HISTORICAL' or has_draft:\n", - " skipped_identifiers.append(identifier)\n", - " continue\n", + " formatted_filing_date = filing_date.strftime(\"%B %d, %Y\")\n", + " \n", + " draft_details_query = text(\"\"\"\n", + " SELECT b.state, \n", + " (SELECT COUNT(1) \n", + " FROM filings f \n", + " WHERE f.business_id = b.id \n", + " AND f.status in ('DRAFT', 'PENDING')) <> 0 AS has_draft \n", + " FROM businesses b \n", + " WHERE b.identifier = :identifier\n", + " \"\"\")\n", + " draft_details_query_result = conn.execute(draft_details_query, {\"identifier\": identifier})\n", + " draft_details = draft_details_query_result.mappings().fetchone()\n", + " \n", + " state = None\n", + " has_draft = None\n", + " \n", + " if draft_details:\n", + " state = draft_details['state']\n", + " has_draft = draft_details['has_draft']\n", + " \n", + " if state == 'HISTORICAL' or has_draft:\n", + " skipped_identifiers.append(identifier)\n", + " continue\n", " \n", - " filing_data = {\n", - " \"filing\": {\n", - " \"header\": {\n", - " \"name\": \"registrarsNotation\",\n", - " \"date\": current_date,\n", - " \"certifiedBy\": \"system\"\n", - " },\n", - " \"business\": {\n", - " \"identifier\": identifier,\n", - " \"legalType\": \"BEN\"\n", - " },\n", - " \"registrarsNotation\": {\n", - " \"orderDetails\": \"BC benefit company statement contained in notice of articles as required under \" + \n", - " \"section 51.992 of the Business Corporations Act corrected from \" +\n", - " \"\\\"This company is a benefit company and, as such, has purposes that include conducting its business \" +\n", - " \" in a responsible and sustainable manner and promoting one or more public benefits\\\" to \" + \n", - " \"\\\"This company is a benefit company and, as such, is committed to conducting its business in a \" + \n", - " \"responsible and sustainable manner and promoting one or more public benefits\\\".\"\n", + " filing_data = {\n", + " \"filing\": {\n", + " \"header\": {\n", + " \"name\": \"registrarsNotation\",\n", + " \"date\": current_date,\n", + " \"certifiedBy\": \"system\"\n", + " },\n", + " \"business\": {\n", + " \"identifier\": identifier,\n", + " \"legalType\": \"BEN\"\n", + " },\n", + " \"registrarsNotation\": {\n", + " \"orderDetails\": \"BC benefit company statement contained in notice of articles as required under \" + \n", + " \"section 51.992 of the Business Corporations Act corrected from \" +\n", + " \"\\\"This company is a benefit company and, as such, has purposes that include conducting its business \" +\n", + " \" in a responsible and sustainable manner and promoting one or more public benefits\\\" to \" + \n", + " \"\\\"This company is a benefit company and, as such, is committed to conducting its business in a \" + \n", + " \"responsible and sustainable manner and promoting one or more public benefits\\\".\"\n", + " }\n", " }\n", " }\n", - " }\n", "\n", - " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", - " response = requests.post(filing_url, headers=headers, json=filing_data)\n", + " filing_url = urljoin(base_url, f\"/api/v2/businesses/{identifier}/filings\")\n", + " response = requests.post(filing_url, headers=headers, json=filing_data)\n", "\n", - " # Check the status code of the response\n", - " if response.status_code == 201:\n", - " successful_identifiers.append(identifier)\n", - " else:\n", - " failed_identifiers.append(identifier)\n", - " print(f\"Failed to make POST request. Status code: {response.status_code} for {identifier}\")\n", - "print('Successfully filed Registrar Notation for:', successful_identifiers) # Print the successful identifiers\n", - "print('Failed to file Registrar Notation for:', failed_identifiers) # Print the failed identifiers\n", - "print('Skipped to file Registrar Notation for:', skipped_identifiers) # Print the skipped identifiers\n" + " # Check the status code of the response\n", + " if response.status_code == 201:\n", + " successful_identifiers.append(identifier)\n", + " else:\n", + " failed_identifiers.append(identifier)\n", + " print(f\"Failed to make POST request. Status code: {response.status_code} for {identifier}\")\n", + " print('Successfully filed Registrar Notation for:', successful_identifiers) # Print the successful identifiers\n", + " print('Failed to file Registrar Notation for:', failed_identifiers) # Print the failed identifiers\n", + " print('Skipped to file Registrar Notation for:', skipped_identifiers) # Print the skipped identifiers" ] } ], "metadata": { "kernelspec": { - "display_name": "3.8.17", + "display_name": "3.11.7", "language": "python", "name": "python3" }, @@ -208,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.17" + "version": "3.11.7" } }, "nbformat": 4,