Skip to content

Commit

Permalink
24839 - script to add corrections and related changes (#3168)
Browse files Browse the repository at this point in the history
* 24839 - script to add corrections and related changes

* 24839 - remove unused file

* 24839 - remove unused data

* 24839 - updated queries

* 24839 - remove commented out code

* 24629 - fix linting issue

* 24629 - remove uneeded code

* 24629 - add correction check

* 24629 - fix correction format share class logic
  • Loading branch information
ketaki-deodhar authored Jan 9, 2025
1 parent 9f33aad commit 9ca5ad0
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 22 deletions.
159 changes: 159 additions & 0 deletions jobs/correction-ben-statement/add_corrections.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Add Correction filing for All active existing companies"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b> Purpose: Add Corrections filing for all active existing BENs.</b>\n",
"\n",
"This is a one time (python) script to be run at a given date/time.<br>\n",
"Set the configuration (client_id, client_secret, url(s)) for a scpecific environment.<br>\n",
"Get access token for authorization.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Access token returned successfully\n"
]
}
],
"source": [
"import requests\n",
"import os\n",
"from datetime import datetime\n",
"\n",
"# token_url, client_id, client_secret, base_url - update based on environment\n",
"token_url = os.getenv('ACCOUNT_SVC_AUTH_URL')\n",
"client_id = os.getenv('ACCOUNT_SVC_CLIENT_ID')\n",
"client_secret = os.getenv('ACCOUNT_SVC_CLIENT_SECRET')\n",
"base_url = os.getenv('LEGAL_API_BASE_URL')\n",
"\n",
"header = {\n",
" \"Content-Type\": \"application/x-www-form-urlencoded\"\n",
"}\n",
"\n",
"data = 'grant_type=client_credentials'\n",
"\n",
"res = requests.post(token_url, data, auth=(client_id, client_secret), headers=header)\n",
"\n",
"# Check the status code of the response\n",
"if res.status_code == 200:\n",
" print(\"Access token returned successfully\")\n",
" token = res.json()[\"access_token\"]\n",
"else:\n",
" print(f\"Failed to make POST request. Status code: {res.status_code}\")\n",
" print(res.text) # Print the error message if the request fails\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Call API (POST) endpoint to createCorrection filing with details as Ben correction statement for businesses."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Correction created successfully for BC0871147\n",
"Correction created successfully for BC0871183\n",
"Correction created successfully for BC0871186\n"
]
}
],
"source": [
"from urllib.parse import urljoin\n",
"from corrections_output import correction_businesses\n",
"\n",
"current_date = datetime.now().date().isoformat()\n",
"headers = {\n",
" 'Content-Type': 'application/json',\n",
" 'Authorization': 'Bearer ' + token\n",
"}\n",
"\n",
"# loop through list of businesses to create filing\n",
"for correction_businesse in correction_businesses:\n",
" identifier = correction_businesse[0]\n",
" filind_id = correction_businesse[1]\n",
" correction_filing_data = {\n",
" \"filing\": {\n",
" \"header\": {\n",
" \"name\": \"correction\",\n",
" \"date\": current_date,\n",
" \"certifiedBy\": \"system\"\n",
" },\n",
" \"business\": {\n",
" \"identifier\": identifier,\n",
" \"legalType\": \"BC\"\n",
" },\n",
" \"correction\": {\n",
" \"details\": \"First correction\",\n",
" \"correctedFilingId\": filind_id,\n",
" \"correctedFilingType\": \"incorporationApplication\",\n",
" \"comment\": f\"\"\"Correction for Incorporation Application filed on {current_date} \\n\n",
" BC benefit company statement contained in notice of articles as required under section \n",
" 51.992 of the Business Corporations Act corrected from “This company is a benefit company \n",
" and, as such, has purposes that include conducting its business in a responsible and \n",
" 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 \n",
" a responsible and sustainable manner and promoting one or more public benefits”.\"\"\"\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",
" print(f\"Correction created successfully for {identifier}\")\n",
" else:\n",
" print(f\"Failed to make POST request. Status code: {rv.status_code}\")\n",
" print(rv.text) # Print the error message if the request fails\n",
" \n"
]
}
],
"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.8.17"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
13 changes: 6 additions & 7 deletions jobs/correction-ben-statement/add_registrars_notation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"outputs": [],
"source": [
"from urllib.parse import urljoin\n",
"from data import ben_businesses\n",
"from rn_output import rn_businesses\n",
"\n",
"current_date = datetime.now().date().isoformat()\n",
"headers = {\n",
Expand All @@ -75,7 +75,7 @@
"}\n",
"\n",
"# loop through list of businesses to create filing\n",
"for ben in ben_businesses:\n",
"for business in rn_businesses:\n",
" filing_data = {\n",
" \"filing\": {\n",
" \"header\": {\n",
Expand All @@ -84,7 +84,7 @@
" \"certifiedBy\": \"system\"\n",
" },\n",
" \"business\": {\n",
" \"identifier\": ben,\n",
" \"identifier\": business,\n",
" \"legalType\": \"BEN\"\n",
" },\n",
" \"registrarsNotation\": {\n",
Expand All @@ -98,16 +98,15 @@
" }\n",
" }\n",
"\n",
" filing_url = urljoin(base_url, f\"/api/v2/businesses/{ben}/filings\")\n",
" filing_url = urljoin(base_url, f\"/api/v2/businesses/{business}/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",
" print(f\"Registrars Notation cretaed successfully for {ben}\")\n",
" print(f\"Registrars Notation created successfully for {business}\")\n",
" else:\n",
" print(f\"Failed to make POST request. Status code: {response.status_code}\")\n",
" print(response.text) # Print the error message if the request fails\n",
" \n"
" print(response.text) # Print the error message if the request fails\n"
]
}
],
Expand Down
31 changes: 31 additions & 0 deletions jobs/correction-ben-statement/convert_corrections_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pandas as pd

# Function to convert CSV to array of arrays using pandas
def convert_csv_to_array_of_arrays(csv_filename):
# Read the CSV file into a pandas DataFrame
df = pd.read_csv(csv_filename)

# Convert the DataFrame to a list of lists (array of arrays)
rows_array = df.values.tolist()

return rows_array

# Write the array of arrays to a Python file
def write_array_to_python_file(array, output_filename):
with open(output_filename, 'w') as f:
f.write('correction_businesses = [\n') # Start the Python array
for row in rows_array:
f.write(f' {row},\n') # Write each row as a list
f.write(']\n') # End the Python array

# Specify your input and output filenames
csv_filename = 'corrections_results.csv'
output_filename = 'corrections_output.py'

# Convert CSV to array of arrays
rows_array = convert_csv_to_array_of_arrays(csv_filename)

# Write the result to a Python file
write_array_to_python_file(rows_array, output_filename)

print(f"Data has been written to {output_filename}")
32 changes: 32 additions & 0 deletions jobs/correction-ben-statement/convert_registrar_notation_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import csv

# Function to read CSV and convert to a Python array
def csv_to_python_array(file_path):
array = []

# Open the CSV file and read its contents
with open(file_path, mode='r', newline='') as file:
reader = csv.reader(file)
for row in reader:
array.extend(row) # Add each element from the row to the array

return array

# Function to write the Python array to a file, with each element on a new row
def write_to_python_file(array, output_file):
with open(output_file, 'w') as file:
file.write('rn_businesses = [\n') # Start the array in Python format
for element in array:
file.write(f" '{element}',\n") # Write each element in the array
file.write(']\n') # End the array in Python format

input_csv = 'registrar_notation_result.csv'
output_python_file = 'rn_output.py'

# Convert CSV to Python array
python_array = csv_to_python_array(input_csv)

# Write the array to a Python file
write_to_python_file(python_array, output_python_file)

print(f"Python array has been written to {output_python_file}")
5 changes: 5 additions & 0 deletions jobs/correction-ben-statement/corrections_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
correction_businesses = [
['BC0871147', 131528],
['BC0871183', 133390],
['BC0871186', 139687],
]
4 changes: 4 additions & 0 deletions jobs/correction-ben-statement/corrections_results.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"identifier","id"
"BC1218818",110441
"BC1218819",110445
"BC1218820",110446
7 changes: 0 additions & 7 deletions jobs/correction-ben-statement/data.py

This file was deleted.

17 changes: 17 additions & 0 deletions jobs/correction-ben-statement/queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- query to get all businesses (BENs) for Registrar's Notation
select b.identifier
from businesses b
where b.legal_type = 'BEN'
order by b.identifier asc;

-- query to get all ACTIVE businesses (BENs) for Corrections
select b.identifier, f.id
from businesses b join filings f on b.id = f.business_id
where b.legal_type = 'BEN' and f.filing_type = 'incorporationApplication' and b.state = 'ACTIVE'
order by b.identifier asc;

-- query to get all ACTIVE businesses (BENs) which have "in progress" drafts
select b.identifier, f.id, f.filing_type
from businesses b join filings f on b.id = f.business_id
where b.legal_type = 'BEN' and b.state = 'ACTIVE' and f.status = 'DRAFT'
order by b.identifier asc;
4 changes: 4 additions & 0 deletions jobs/correction-ben-statement/registrar_notation_result.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"BC1230101"
"BC1230102"
"BC1230104"

5 changes: 5 additions & 0 deletions jobs/correction-ben-statement/rn_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rn_businesses = [
'BC1230101',
'BC1230102',
'BC1230104',
]
19 changes: 11 additions & 8 deletions legal-api/src/legal_api/reports/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,16 +1073,17 @@ def _format_correction_data(self, filing):

def _format_name_request_data(self, filing, versioned_business: Business):
name_request_json = filing.get('correction').get('nameRequest', {})
filing['nameRequest'] = name_request_json
prev_legal_name = versioned_business.legal_name
if name_request_json:
filing['nameRequest'] = name_request_json
prev_legal_name = versioned_business.legal_name

if name_request_json and not (new_legal_name := name_request_json.get('legalName')):
new_legal_name = Business.generate_numbered_legal_name(name_request_json['legalType'],
versioned_business.identifier)
if name_request_json and not (new_legal_name := name_request_json.get('legalName')):
new_legal_name = Business.generate_numbered_legal_name(name_request_json['legalType'],
versioned_business.identifier)

if new_legal_name and prev_legal_name != new_legal_name:
filing['previousLegalName'] = prev_legal_name
filing['newLegalName'] = new_legal_name
if new_legal_name and prev_legal_name != new_legal_name:
filing['previousLegalName'] = prev_legal_name
filing['newLegalName'] = new_legal_name

def _format_name_translations_data(self, filing, prev_completed_filing: Filing):
filing['listOfTranslations'] = filing['correction'].get('nameTranslations', [])
Expand Down Expand Up @@ -1165,6 +1166,8 @@ def _format_party_data(self, filing, prev_completed_filing: Filing):
filing['ceasedParties'] = parties_deleted

def _format_share_class_data(self, filing, prev_completed_filing: Filing): # pylint: disable=too-many-locals; # noqa: E501;
if filing.get('correction').get('shareStructure') is None:
return
filing['shareClasses'] = filing.get('correction').get('shareStructure', {}).get('shareClasses')
dates = filing['correction']['shareStructure'].get('resolutionDates', [])
formatted_dates = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def correct_business_data(business: Business, # pylint: disable=too-many-locals
def update_parties(business: Business, parties: list, correction_filing_rec: Filing):
"""Create a new party or get them if they already exist."""
# Cease the party roles not present in the edit request
if parties is None:
return
end_date_time = datetime.datetime.utcnow()
parties_to_update = [party.get('officer').get('id') for party in parties if
party.get('officer').get('id') is not None]
Expand Down

0 comments on commit 9ca5ad0

Please sign in to comment.