diff --git a/poetry.lock b/poetry.lock index 8b54ff8..063995d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. [[package]] name = "alembic" @@ -126,6 +126,50 @@ files = [ docs = ["Sphinx (>=5.3.0,<5.4.0)", "sphinx-rtd-theme (>=1.2.2)", "sphinxcontrib-asyncio (>=0.3.0,<0.4.0)"] test = ["flake8 (>=6.1,<7.0)", "uvloop (>=0.15.3)"] +[[package]] +name = "black" +version = "24.8.0" +description = "The uncompromising code formatter." +optional = false +python-versions = ">=3.8" +files = [ + {file = "black-24.8.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:09cdeb74d494ec023ded657f7092ba518e8cf78fa8386155e4a03fdcc44679e6"}, + {file = "black-24.8.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:81c6742da39f33b08e791da38410f32e27d632260e599df7245cccee2064afeb"}, + {file = "black-24.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:707a1ca89221bc8a1a64fb5e15ef39cd755633daa672a9db7498d1c19de66a42"}, + {file = "black-24.8.0-cp310-cp310-win_amd64.whl", hash = "sha256:d6417535d99c37cee4091a2f24eb2b6d5ec42b144d50f1f2e436d9fe1916fe1a"}, + {file = "black-24.8.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:fb6e2c0b86bbd43dee042e48059c9ad7830abd5c94b0bc518c0eeec57c3eddc1"}, + {file = "black-24.8.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:837fd281f1908d0076844bc2b801ad2d369c78c45cf800cad7b61686051041af"}, + {file = "black-24.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:62e8730977f0b77998029da7971fa896ceefa2c4c4933fcd593fa599ecbf97a4"}, + {file = "black-24.8.0-cp311-cp311-win_amd64.whl", hash = "sha256:72901b4913cbac8972ad911dc4098d5753704d1f3c56e44ae8dce99eecb0e3af"}, + {file = "black-24.8.0-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:7c046c1d1eeb7aea9335da62472481d3bbf3fd986e093cffd35f4385c94ae368"}, + {file = "black-24.8.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:649f6d84ccbae73ab767e206772cc2d7a393a001070a4c814a546afd0d423aed"}, + {file = "black-24.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:2b59b250fdba5f9a9cd9d0ece6e6d993d91ce877d121d161e4698af3eb9c1018"}, + {file = "black-24.8.0-cp312-cp312-win_amd64.whl", hash = "sha256:6e55d30d44bed36593c3163b9bc63bf58b3b30e4611e4d88a0c3c239930ed5b2"}, + {file = "black-24.8.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:505289f17ceda596658ae81b61ebbe2d9b25aa78067035184ed0a9d855d18afd"}, + {file = "black-24.8.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:b19c9ad992c7883ad84c9b22aaa73562a16b819c1d8db7a1a1a49fb7ec13c7d2"}, + {file = "black-24.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1f13f7f386f86f8121d76599114bb8c17b69d962137fc70efe56137727c7047e"}, + {file = "black-24.8.0-cp38-cp38-win_amd64.whl", hash = "sha256:f490dbd59680d809ca31efdae20e634f3fae27fba3ce0ba3208333b713bc3920"}, + {file = "black-24.8.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:eab4dd44ce80dea27dc69db40dab62d4ca96112f87996bca68cd75639aeb2e4c"}, + {file = "black-24.8.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:3c4285573d4897a7610054af5a890bde7c65cb466040c5f0c8b732812d7f0e5e"}, + {file = "black-24.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:9e84e33b37be070ba135176c123ae52a51f82306def9f7d063ee302ecab2cf47"}, + {file = "black-24.8.0-cp39-cp39-win_amd64.whl", hash = "sha256:73bbf84ed136e45d451a260c6b73ed674652f90a2b3211d6a35e78054563a9bb"}, + {file = "black-24.8.0-py3-none-any.whl", hash = "sha256:972085c618ee94f402da1af548a4f218c754ea7e5dc70acb168bfaca4c2542ed"}, + {file = "black-24.8.0.tar.gz", hash = "sha256:2500945420b6784c38b9ee885af039f5e7471ef284ab03fa35ecdde4688cd83f"}, +] + +[package.dependencies] +click = ">=8.0.0" +mypy-extensions = ">=0.4.3" +packaging = ">=22.0" +pathspec = ">=0.9.0" +platformdirs = ">=2" + +[package.extras] +colorama = ["colorama (>=0.4.3)"] +d = ["aiohttp (>=3.7.4)", "aiohttp (>=3.7.4,!=3.9.0)"] +jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] +uvloop = ["uvloop (>=0.15.2)"] + [[package]] name = "boto3" version = "1.35.14" @@ -930,6 +974,7 @@ optional = false python-versions = ">=3.9" files = [ {file = "pandas-2.2.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:90c6fca2acf139569e74e8781709dccb6fe25940488755716d1d354d6bc58bce"}, + {file = "pandas-2.2.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:c7adfc142dac335d8c1e0dcbd37eb8617eac386596eb9e1a1b77791cf2498238"}, {file = "pandas-2.2.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4abfe0be0d7221be4f12552995e58723c7422c80a659da13ca382697de830c08"}, {file = "pandas-2.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8635c16bf3d99040fdf3ca3db669a7250ddf49c55dc4aa8fe0ae0fa8d6dcc1f0"}, {file = "pandas-2.2.2-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:40ae1dffb3967a52203105a077415a86044a2bea011b5f321c6aa64b379a3f51"}, @@ -943,12 +988,14 @@ files = [ {file = "pandas-2.2.2-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:0cace394b6ea70c01ca1595f839cf193df35d1575986e484ad35c4aeae7266c1"}, {file = "pandas-2.2.2-cp311-cp311-win_amd64.whl", hash = "sha256:873d13d177501a28b2756375d59816c365e42ed8417b41665f346289adc68d24"}, {file = "pandas-2.2.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:9dfde2a0ddef507a631dc9dc4af6a9489d5e2e740e226ad426a05cabfbd7c8ef"}, + {file = "pandas-2.2.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:e9b79011ff7a0f4b1d6da6a61aa1aa604fb312d6647de5bad20013682d1429ce"}, {file = "pandas-2.2.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1cb51fe389360f3b5a4d57dbd2848a5f033350336ca3b340d1c53a1fad33bcad"}, {file = "pandas-2.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:eee3a87076c0756de40b05c5e9a6069c035ba43e8dd71c379e68cab2c20f16ad"}, {file = "pandas-2.2.2-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:3e374f59e440d4ab45ca2fffde54b81ac3834cf5ae2cdfa69c90bc03bde04d76"}, {file = "pandas-2.2.2-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:43498c0bdb43d55cb162cdc8c06fac328ccb5d2eabe3cadeb3529ae6f0517c32"}, {file = "pandas-2.2.2-cp312-cp312-win_amd64.whl", hash = "sha256:d187d355ecec3629624fccb01d104da7d7f391db0311145817525281e2804d23"}, {file = "pandas-2.2.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:0ca6377b8fca51815f382bd0b697a0814c8bda55115678cbc94c30aacbb6eff2"}, + {file = "pandas-2.2.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:9057e6aa78a584bc93a13f0a9bf7e753a5e9770a30b4d758b8d5f2a62a9433cd"}, {file = "pandas-2.2.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:001910ad31abc7bf06f49dcc903755d2f7f3a9186c0c040b827e522e9cef0863"}, {file = "pandas-2.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:66b479b0bd07204e37583c191535505410daa8df638fd8e75ae1b383851fe921"}, {file = "pandas-2.2.2-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:a77e9d1c386196879aa5eb712e77461aaee433e54c68cf253053a73b7e49c33a"}, @@ -1024,6 +1071,33 @@ polars = ["polars (>=0.20.0)"] pyspark = ["pyspark[connect] (>=3.2.0)"] strategies = ["hypothesis (>=6.92.7)"] +[[package]] +name = "pathspec" +version = "0.12.1" +description = "Utility library for gitignore style pattern matching of file paths." +optional = false +python-versions = ">=3.8" +files = [ + {file = "pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08"}, + {file = "pathspec-0.12.1.tar.gz", hash = "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712"}, +] + +[[package]] +name = "platformdirs" +version = "4.3.3" +description = "A small Python package for determining appropriate platform-specific dirs, e.g. a `user data dir`." +optional = false +python-versions = ">=3.8" +files = [ + {file = "platformdirs-4.3.3-py3-none-any.whl", hash = "sha256:50a5450e2e84f44539718293cbb1da0a0885c9d14adf21b77bae4e66fc99d9b5"}, + {file = "platformdirs-4.3.3.tar.gz", hash = "sha256:d4e0b7d8ec176b341fb03cb11ca12d0276faa8c485f9cd218f613840463fc2c0"}, +] + +[package.extras] +docs = ["furo (>=2024.8.6)", "proselint (>=0.14)", "sphinx (>=8.0.2)", "sphinx-autodoc-typehints (>=2.4)"] +test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=8.3.2)", "pytest-cov (>=5)", "pytest-mock (>=3.14)"] +type = ["mypy (>=1.11.2)"] + [[package]] name = "pluggy" version = "1.5.0" @@ -1072,6 +1146,7 @@ files = [ {file = "psycopg2_binary-2.9.9-cp311-cp311-win32.whl", hash = "sha256:dc4926288b2a3e9fd7b50dc6a1909a13bbdadfc67d93f3374d984e56f885579d"}, {file = "psycopg2_binary-2.9.9-cp311-cp311-win_amd64.whl", hash = "sha256:b76bedd166805480ab069612119ea636f5ab8f8771e640ae103e05a4aae3e417"}, {file = "psycopg2_binary-2.9.9-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:8532fd6e6e2dc57bcb3bc90b079c60de896d2128c5d9d6f24a63875a95a088cf"}, + {file = "psycopg2_binary-2.9.9-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:b0605eaed3eb239e87df0d5e3c6489daae3f7388d455d0c0b4df899519c6a38d"}, {file = "psycopg2_binary-2.9.9-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8f8544b092a29a6ddd72f3556a9fcf249ec412e10ad28be6a0c0d948924f2212"}, {file = "psycopg2_binary-2.9.9-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:2d423c8d8a3c82d08fe8af900ad5b613ce3632a1249fd6a223941d0735fce493"}, {file = "psycopg2_binary-2.9.9-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:2e5afae772c00980525f6d6ecf7cbca55676296b580c0e6abb407f15f3706996"}, @@ -1080,6 +1155,8 @@ files = [ {file = "psycopg2_binary-2.9.9-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:cb16c65dcb648d0a43a2521f2f0a2300f40639f6f8c1ecbc662141e4e3e1ee07"}, {file = "psycopg2_binary-2.9.9-cp312-cp312-musllinux_1_1_ppc64le.whl", hash = "sha256:911dda9c487075abd54e644ccdf5e5c16773470a6a5d3826fda76699410066fb"}, {file = "psycopg2_binary-2.9.9-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:57fede879f08d23c85140a360c6a77709113efd1c993923c59fde17aa27599fe"}, + {file = "psycopg2_binary-2.9.9-cp312-cp312-win32.whl", hash = "sha256:64cf30263844fa208851ebb13b0732ce674d8ec6a0c86a4e160495d299ba3c93"}, + {file = "psycopg2_binary-2.9.9-cp312-cp312-win_amd64.whl", hash = "sha256:81ff62668af011f9a48787564ab7eded4e9fb17a4a6a74af5ffa6a457400d2ab"}, {file = "psycopg2_binary-2.9.9-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:2293b001e319ab0d869d660a704942c9e2cce19745262a8aba2115ef41a0a42a"}, {file = "psycopg2_binary-2.9.9-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:03ef7df18daf2c4c07e2695e8cfd5ee7f748a1d54d802330985a78d2a5a6dca9"}, {file = "psycopg2_binary-2.9.9-cp37-cp37m-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:0a602ea5aff39bb9fac6308e9c9d82b9a35c2bf288e184a816002c9fae930b77"}, @@ -1783,7 +1860,7 @@ files = [ ] [package.dependencies] -greenlet = {version = "!=0.4.17", optional = true, markers = "python_version < \"3.13\" and (platform_machine == \"win32\" or platform_machine == \"WIN32\" or platform_machine == \"AMD64\" or platform_machine == \"amd64\" or platform_machine == \"x86_64\" or platform_machine == \"ppc64le\" or platform_machine == \"aarch64\") or extra == \"asyncio\""} +greenlet = {version = "!=0.4.17", optional = true, markers = "python_version < \"3.13\" and (platform_machine == \"aarch64\" or platform_machine == \"ppc64le\" or platform_machine == \"x86_64\" or platform_machine == \"amd64\" or platform_machine == \"AMD64\" or platform_machine == \"win32\" or platform_machine == \"WIN32\") or extra == \"asyncio\""} typing-extensions = ">=4.6.0" [package.extras] @@ -2101,4 +2178,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = ">=3.12,<4" -content-hash = "99e6712f74ce8db5dfb593ef9dbc266e9e1d7c76dcfff181ff9fd6e8d550cd25" +content-hash = "fedf0cb1dfe88abb1fee7ba0d01caa844499be62891c12790b5bf8492e7fd2d0" diff --git a/pyproject.toml b/pyproject.toml index 98ebd1b..f6fdf71 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,13 @@ regtech-api-commons = {git = "https://github.com/cfpb/regtech-api-commons.git"} sbl-filing-api = {git = "https://github.com/cfpb/sbl-filing-api.git"} regtech-user-fi-management = {git = "https://github.com/cfpb/regtech-user-fi-management.git"} boto3 = "^1.34.149" +black = "^24.8.0" + +[tool.black] +line-length = 120 + +[tool.ruff] +line-length = 120 [tool.poetry.group.dev.dependencies] diff --git a/src/regtech_cleanup_api/entities/engine/engine.py b/src/regtech_cleanup_api/entities/engine/engine.py index f008410..d50e47f 100644 --- a/src/regtech_cleanup_api/entities/engine/engine.py +++ b/src/regtech_cleanup_api/entities/engine/engine.py @@ -54,9 +54,7 @@ async def get_filing_session(): echo=institution_settings.db_logging, poolclass=NullPool, ).execution_options(schema_translate_map={None: institution_settings.inst_db_schema}) -InstitutionSessionLocal = sessionmaker( - autocommit=False, autoflush=False, bind=user_fi_engine -) +InstitutionSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=user_fi_engine) filing_engine = create_engine( diff --git a/src/regtech_cleanup_api/entities/repos/filing_repo.py b/src/regtech_cleanup_api/entities/repos/filing_repo.py index a6eb975..c1a3f00 100644 --- a/src/regtech_cleanup_api/entities/repos/filing_repo.py +++ b/src/regtech_cleanup_api/entities/repos/filing_repo.py @@ -25,9 +25,7 @@ def get_user_action_ids( filing_user_action_id = [] if not just_submissions: filing = submission_repo.get_filing(session, lei, period_code) - filing_user_action_id = ( - [filing.creator_id] if filing.creator_id else [] - ) + filing_user_action_id = [filing.creator_id] if filing.creator_id else [] submissions = submission_repo.get_submissions(session, lei, period_code) user_action_ids = list( set( @@ -82,7 +80,5 @@ def delete_contact_info(session: Session, lei: str = None, period_code: str = No def delete_helper(session: Session, table_obj: T, table_id: Any): - session.query(table_obj).filter(table_obj.id == table_id).delete( - synchronize_session="fetch" - ) + session.query(table_obj).filter(table_obj.id == table_id).delete(synchronize_session="fetch") session.commit() diff --git a/src/regtech_cleanup_api/entities/repos/institution_repo.py b/src/regtech_cleanup_api/entities/repos/institution_repo.py index 16393c1..490ce44 100644 --- a/src/regtech_cleanup_api/entities/repos/institution_repo.py +++ b/src/regtech_cleanup_api/entities/repos/institution_repo.py @@ -8,19 +8,13 @@ from sqlalchemy import text -def delete_domains_by_lei( - session: Session, lei: str -) -> List[FinancialInstitutionDomainDao] | None: - session.query(FinancialInstitutionDomainDao).filter( - FinancialInstitutionDomainDao.lei == lei - ).delete() +def delete_domains_by_lei(session: Session, lei: str) -> List[FinancialInstitutionDomainDao] | None: + session.query(FinancialInstitutionDomainDao).filter(FinancialInstitutionDomainDao.lei == lei).delete() session.commit() return {"OK": True} -def delete_sbl_type_by_lei( - session: Session, lei: str -) -> List[SblTypeMappingDao] | None: +def delete_sbl_type_by_lei(session: Session, lei: str) -> List[SblTypeMappingDao] | None: session.query(SblTypeMappingDao).filter(SblTypeMappingDao.lei == lei).delete() # deleting from history tables del_hist_stmt = text("DELETE from fi_to_type_mapping_history where fi_id = :fi_id") @@ -30,9 +24,7 @@ def delete_sbl_type_by_lei( def delete_institution(session: Session, lei: str) -> FinancialInstitutionDao | None: - session.query(FinancialInstitutionDao).filter( - FinancialInstitutionDao.lei == lei - ).delete() + session.query(FinancialInstitutionDao).filter(FinancialInstitutionDao.lei == lei).delete() # deleting from history tables del_hist_stmt = text("DELETE from financial_institutions_history where lei = :lei") diff --git a/src/regtech_cleanup_api/entities/repos/submission_repo.py b/src/regtech_cleanup_api/entities/repos/submission_repo.py index 9d78b11..fe2bef6 100644 --- a/src/regtech_cleanup_api/entities/repos/submission_repo.py +++ b/src/regtech_cleanup_api/entities/repos/submission_repo.py @@ -12,6 +12,10 @@ T = TypeVar("T") +def get_filings(session: Session, lei: str) -> List[FilingDAO]: + return query_helper(session, FilingDAO, lei=lei) + + def get_filing(session: Session, lei: str, filing_period: str) -> FilingDAO: result = query_helper(session, FilingDAO, lei=lei, filing_period=filing_period) return result[0] if result else None @@ -25,9 +29,7 @@ def query_helper(session: Session, table_obj: T, **filter_args) -> List[T]: return session.query(table_obj).all() -def get_submissions( - session: Session, lei: str = None, filing_period: str = None -) -> List[SubmissionDAO]: +def get_submissions(session: Session, lei: str = None, filing_period: str = None) -> List[SubmissionDAO]: filing_id = None if lei and filing_period: filing = get_filing(session, lei=lei, filing_period=filing_period) diff --git a/src/regtech_cleanup_api/main.py b/src/regtech_cleanup_api/main.py index 90e2220..cdefdb2 100644 --- a/src/regtech_cleanup_api/main.py +++ b/src/regtech_cleanup_api/main.py @@ -18,8 +18,7 @@ ) import uvicorn -from regtech_cleanup_api.routers.filing_cleanup import router as filing_router -from regtech_cleanup_api.routers.institution_cleanup import router as institution_router +from regtech_cleanup_api.routers.cleanup import router as cleanup_router from regtech_cleanup_api.config import kc_settings log = logging.getLogger() @@ -50,8 +49,7 @@ ) -app.include_router(institution_router, prefix="/v1/institution_cleanup") -app.include_router(filing_router, prefix="/v1/filing_cleanup") +app.include_router(cleanup_router, prefix="/v1/cleanup") if __name__ == "__main__": diff --git a/src/regtech_cleanup_api/routers/cleanup.py b/src/regtech_cleanup_api/routers/cleanup.py new file mode 100644 index 0000000..f29fcf0 --- /dev/null +++ b/src/regtech_cleanup_api/routers/cleanup.py @@ -0,0 +1,67 @@ +import logging + +from concurrent.futures import ProcessPoolExecutor +from http import HTTPStatus + +from fastapi import Depends, Request, Response, status +from regtech_api_commons.api.exceptions import RegTechHttpException +from regtech_api_commons.api.router_wrapper import Router +from typing import Annotated + +from regtech_cleanup_api.entities.engine.engine import ( + get_filing_session, + get_institution_session, +) + +from sqlalchemy.orm import Session + +from regtech_api_commons.api.dependencies import verify_user_lei_relation + +import regtech_cleanup_api.entities.repos.submission_repo as submission_repo + +from regtech_cleanup_api.routers.institution_cleanup import ( + delete_helper as institution_delete_helper, +) +from regtech_cleanup_api.routers.filing_cleanup import ( + delete_helper as filing_delete_helper, +) + +from regtech_cleanup_api.services.validation import is_valid_cleanup_lei + +logger = logging.getLogger(__name__) + + +def set_institution_db(request: Request, session: Session = Depends(get_institution_session)): + request.state.institution_db_session = session + + +def set_filing_db(request: Request, session: Annotated[Session, Depends(get_filing_session)]): + request.state.filing_db_session = session + + +executor = ProcessPoolExecutor() +router = Router( + dependencies=[ + Depends(set_filing_db), + Depends(set_institution_db), + Depends(verify_user_lei_relation), + ] +) + + +@router.delete("/{lei}") +def delete_all_things(request: Request, lei: str): + if not is_valid_cleanup_lei(lei): + raise RegTechHttpException( + HTTPStatus.NOT_ACCEPTABLE, + name="Not Test LEI", + detail=f"{lei} not valid test lei.", + ) + else: + institution_delete_helper(lei, request.state.institution_db_session) + + filings = submission_repo.get_filings(request.state.filing_db_session, lei) + for f in filings: + filing_delete_helper(f.lei, f.filing_period, request.state.filing_db_session) + + return Response(status_code=status.HTTP_204_NO_CONTENT) diff --git a/src/regtech_cleanup_api/routers/filing_cleanup.py b/src/regtech_cleanup_api/routers/filing_cleanup.py index 7b043e8..6f7cdaa 100644 --- a/src/regtech_cleanup_api/routers/filing_cleanup.py +++ b/src/regtech_cleanup_api/routers/filing_cleanup.py @@ -29,78 +29,85 @@ def set_db(request: Request, session: Annotated[Session, Depends(get_filing_sess router = Router(dependencies=[Depends(set_db), Depends(verify_user_lei_relation)]) -@router.delete("/institutions/{lei}/filings/{period_code}") +@router.delete("/filing/{lei}/{period_code}") def delete_filing(request: Request, lei: str, period_code: str): - if is_valid_cleanup_lei(lei): + if not is_valid_cleanup_lei(lei): + raise RegTechHttpException( + status_code=HTTPStatus.NOT_ACCEPTABLE, + name="Invalid LEI", + detail="Not a valid LEI", + ) + else: try: session = request.state.db_session - try: - repo.delete_contact_info(session, lei, period_code) - except Exception as e: - raise RegTechHttpException( - status_code=HTTPStatus.INTERNAL_SERVER_ERROR, - name="Contact Info Delete Failed", - detail="Failed to delete contact info", - ) from e - - try: - user_action_ids = repo.get_user_action_ids(session, lei, period_code) - except Exception as e: - raise RegTechHttpException( - status_code=HTTPStatus.INTERNAL_SERVER_ERROR, - name="Missing User Action Data", - detail="Failed to get user action data", - ) from e + delete_helper(lei, period_code, session) - try: - repo.delete_submissions(session, lei, period_code) - except Exception as e: - raise RegTechHttpException( - status_code=HTTPStatus.INTERNAL_SERVER_ERROR, - name="Submission Delete Failed", - detail="Failed to delete submission data", - ) from e + except Exception as e: + raise RegTechHttpException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + name="Delete Filing Server Error", + detail=f"Server error while trying to delete filing for LEI {lei}.", + ) from e - try: - repo.delete_filing(session, lei, period_code) - except Exception as e: - raise RegTechHttpException( - status_code=HTTPStatus.INTERNAL_SERVER_ERROR, - name="Filing Delete Failed", - detail="Failed to delete filing data", - ) from e - try: - repo.delete_user_actions(session, user_action_ids) - except Exception as e: - raise RegTechHttpException( - status_code=HTTPStatus.INTERNAL_SERVER_ERROR, - name="User Action Delete Failed", - detail="Failed to delete user action data", - ) from e +def delete_helper(lei: str, period_code: str, session: Session): + try: + repo.delete_contact_info(session, lei, period_code) + except Exception as e: + raise RegTechHttpException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + name="Contact Info Delete Failed", + detail="Failed to delete contact info", + ) from e + + try: + user_action_ids = repo.get_user_action_ids(session, lei, period_code) + except Exception as e: + raise RegTechHttpException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + name="Missing User Action Data", + detail="Failed to get user action data", + ) from e + + try: + repo.delete_submissions(session, lei, period_code) + except Exception as e: + raise RegTechHttpException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + name="Submission Delete Failed", + detail="Failed to delete submission data", + ) from e + + try: + repo.delete_filing(session, lei, period_code) + except Exception as e: + raise RegTechHttpException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + name="Filing Delete Failed", + detail="Failed to delete filing data", + ) from e + + try: + repo.delete_user_actions(session, user_action_ids) + except Exception as e: + raise RegTechHttpException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + name="User Action Delete Failed", + detail="Failed to delete user action data", + ) from e - delete_from_storage(period_code, lei) + delete_from_storage(period_code, lei) - except Exception as e: - raise e - else: - raise RegTechHttpException( - status_code=HTTPStatus.NOT_ACCEPTABLE, - name="Invalid LEI", - detail="Not a valid LEI", - ) return Response(status_code=status.HTTP_202_ACCEPTED) -@router.delete("/institutions/{lei}/submissions/{period_code}") +@router.delete("/submissions/{lei}/{period_code}") def delete_submissions(request: Request, lei: str, period_code: str): if is_valid_cleanup_lei(lei): try: session = request.state.db_session try: - user_action_ids = repo.get_user_action_ids( - session, lei=lei, period_code=period_code, submissions=True - ) + user_action_ids = repo.get_user_action_ids(session, lei=lei, period_code=period_code, submissions=True) except Exception as e: raise RegTechHttpException( status_code=HTTPStatus.INTERNAL_SERVER_ERROR, diff --git a/src/regtech_cleanup_api/routers/institution_cleanup.py b/src/regtech_cleanup_api/routers/institution_cleanup.py index db20467..c5d8a76 100644 --- a/src/regtech_cleanup_api/routers/institution_cleanup.py +++ b/src/regtech_cleanup_api/routers/institution_cleanup.py @@ -31,7 +31,7 @@ def set_db(request: Request, session: Session = Depends(get_institution_session) @router.delete( - "/{lei}", + "/institution/{lei}", response_model=FinancialInstitutionWithRelationsDto, dependencies=[Depends(verify_user_lei_relation)], ) @@ -43,29 +43,33 @@ def delete_institution(request: Request, lei: str): detail=f"{lei} not valid test lei.", ) else: - delete_domains = repo.delete_domains_by_lei(request.state.db_session, lei) - if not delete_domains: - logger.error(f"Domain(s) for LEI {lei} not deleted.") + return delete_helper(lei, request.state.db_session) - delete_sbl_type = repo.delete_sbl_type_by_lei(request.state.db_session, lei) - if not delete_sbl_type: - logger.error(f"sbl type(s) for LEI {lei} not deleted.") - res = repo.delete_institution(request.state.db_session, lei) - if not res: +def delete_helper(lei: str, session: Session): + delete_domains = repo.delete_domains_by_lei(session, lei) + if not delete_domains: + logger.error(f"Domain(s) for LEI {lei} not deleted.") + + delete_sbl_type = repo.delete_sbl_type_by_lei(session, lei) + if not delete_sbl_type: + logger.error(f"sbl type(s) for LEI {lei} not deleted.") + + res = repo.delete_institution(session, lei) + if not res: + raise RegTechHttpException( + HTTPStatus.NOT_FOUND, + name="Institution to be deleted Not Found", + detail=f"{lei} not found.", + ) + else: + try: + oauth2_admin.delete_group(lei) + except Exception: raise RegTechHttpException( HTTPStatus.NOT_FOUND, - name="Institution to be deleted Not Found", - detail=f"{lei} not found.", + name="Group Not Found", + detail=f"The group to be deleted {lei} not found.", ) - else: - try: - oauth2_admin.delete_group(lei) - except Exception: - raise RegTechHttpException( - HTTPStatus.NOT_FOUND, - name="Group Not Found", - detail=f"The group to be deleted {lei} not found.", - ) - return Response(status_code=status.HTTP_204_NO_CONTENT) + return Response(status_code=status.HTTP_204_NO_CONTENT) diff --git a/src/regtech_cleanup_api/services/file_handler.py b/src/regtech_cleanup_api/services/file_handler.py index a41fd9a..e69a443 100644 --- a/src/regtech_cleanup_api/services/file_handler.py +++ b/src/regtech_cleanup_api/services/file_handler.py @@ -20,9 +20,7 @@ def delete(path: str): s3 = boto3.resource("s3") s3_objects = s3.meta.client.list_objects(Bucket=bucket, Prefix=path) if "Contents" in s3_objects: - object_keys = { - "Objects": [{"Key": k["Key"]} for k in s3_objects["Contents"]] - } + object_keys = {"Objects": [{"Key": k["Key"]} for k in s3_objects["Contents"]]} try: s3.meta.client.delete_objects(Bucket=bucket, Delete=object_keys) except Exception as e: