Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions jobs/correction-ben-statement/.env.example
Original file line number Diff line number Diff line change
@@ -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=
170 changes: 85 additions & 85 deletions jobs/correction-ben-statement/add_corrections_alterations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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!\")"
]
},
{
Expand Down Expand Up @@ -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"
},
Expand All @@ -218,7 +218,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.17"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
Loading