|
11 | 11 | from regtech_api_commons.api.exceptions import RegTechHttpException
|
12 | 12 | from regtech_api_commons.models.auth import AuthenticatedUser
|
13 | 13 |
|
| 14 | +from sbl_filing_api.entities.models.dao import FilingDAO |
14 | 15 | from sbl_filing_api.entities.models.model_enums import UserActionType
|
15 | 16 | from sbl_filing_api.services import submission_processor
|
16 | 17 | from sbl_filing_api.services.multithread_handler import handle_submission
|
| 18 | +from sbl_filing_api.config import request_action_validations |
17 | 19 | from typing import Annotated, List
|
18 | 20 |
|
19 | 21 | from sbl_filing_api.entities.engine.engine import get_session
|
|
39 | 41 |
|
40 | 42 | from sbl_filing_api.services.request_handler import send_confirmation_email
|
41 | 43 |
|
| 44 | +from sbl_filing_api.services.request_action_validator import UserActionContext, validate_user_action, set_context |
| 45 | + |
42 | 46 | logger = logging.getLogger(__name__)
|
43 | 47 |
|
44 | 48 |
|
@@ -72,92 +76,57 @@ async def get_filings(request: Request, period_code: str):
|
72 | 76 | return await repo.get_filings(request.state.db_session, user.institutions, period_code)
|
73 | 77 |
|
74 | 78 |
|
75 |
| -@router.post("/institutions/{lei}/filings/{period_code}", response_model=FilingDTO) |
| 79 | +@router.post( |
| 80 | + "/institutions/{lei}/filings/{period_code}", |
| 81 | + response_model=FilingDTO, |
| 82 | + dependencies=[ |
| 83 | + Depends(set_context({UserActionContext.PERIOD, UserActionContext.FILING})), |
| 84 | + Depends(validate_user_action(request_action_validations.filing_create, "Filing Create Forbidden")), |
| 85 | + ], |
| 86 | +) |
76 | 87 | @requires("authenticated")
|
77 | 88 | async def post_filing(request: Request, lei: str, period_code: str):
|
78 |
| - period = await repo.get_filing_period(request.state.db_session, filing_period=period_code) |
79 |
| - |
80 |
| - if period: |
81 |
| - filing = await repo.get_filing(request.state.db_session, lei, period_code) |
82 |
| - if filing: |
83 |
| - raise RegTechHttpException( |
84 |
| - status_code=status.HTTP_409_CONFLICT, |
85 |
| - name="Filing Creation Conflict", |
86 |
| - detail=f"Filing already exists for Filing Period {period_code} and LEI {lei}", |
87 |
| - ) |
88 |
| - creator = None |
89 |
| - try: |
90 |
| - creator = await repo.add_user_action( |
91 |
| - request.state.db_session, |
92 |
| - UserActionDTO( |
93 |
| - user_id=request.user.id, |
94 |
| - user_name=request.user.name, |
95 |
| - user_email=request.user.email, |
96 |
| - action_type=UserActionType.CREATE, |
97 |
| - ), |
98 |
| - ) |
99 |
| - except Exception: |
100 |
| - logger.exception("Error while trying to create the filing.creator UserAction.") |
101 |
| - raise RegTechHttpException( |
102 |
| - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
103 |
| - name="Filing.creator UserAction error", |
104 |
| - detail="Error while trying to create the filing.creator UserAction.", |
105 |
| - ) |
106 |
| - |
107 |
| - try: |
108 |
| - return await repo.create_new_filing(request.state.db_session, lei, period_code, creator_id=creator.id) |
109 |
| - except Exception: |
110 |
| - raise RegTechHttpException( |
111 |
| - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
112 |
| - name="Filing Creation Error", |
113 |
| - detail=f"An error occurred while creating a filing for LEI {lei} and Filing Period {period_code}.", |
114 |
| - ) |
| 89 | + creator = None |
| 90 | + try: |
| 91 | + creator = await repo.add_user_action( |
| 92 | + request.state.db_session, |
| 93 | + UserActionDTO( |
| 94 | + user_id=request.user.id, |
| 95 | + user_name=request.user.name, |
| 96 | + user_email=request.user.email, |
| 97 | + action_type=UserActionType.CREATE, |
| 98 | + ), |
| 99 | + ) |
| 100 | + except Exception: |
| 101 | + logger.exception("Error while trying to create the filing.creator UserAction.") |
| 102 | + raise RegTechHttpException( |
| 103 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 104 | + name="Filing.creator UserAction error", |
| 105 | + detail="Error while trying to create the filing.creator UserAction.", |
| 106 | + ) |
115 | 107 |
|
116 |
| - else: |
| 108 | + try: |
| 109 | + return await repo.create_new_filing(request.state.db_session, lei, period_code, creator_id=creator.id) |
| 110 | + except Exception: |
117 | 111 | raise RegTechHttpException(
|
118 |
| - status_code=status.HTTP_404_NOT_FOUND, |
119 |
| - name="Filing Period Not Found", |
120 |
| - detail=f"The period ({period_code}) does not exist, therefore a Filing can not be created for this period.", |
| 112 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 113 | + name="Filing Creation Error", |
| 114 | + detail=f"An error occurred while creating a filing for LEI {lei} and Filing Period {period_code}.", |
121 | 115 | )
|
122 | 116 |
|
123 | 117 |
|
124 |
| -@router.put("/institutions/{lei}/filings/{period_code}/sign", response_model=FilingDTO) |
| 118 | +@router.put( |
| 119 | + "/institutions/{lei}/filings/{period_code}/sign", |
| 120 | + response_model=FilingDTO, |
| 121 | + dependencies=[ |
| 122 | + Depends(set_context({UserActionContext.INSTITUTION, UserActionContext.FILING})), |
| 123 | + Depends(validate_user_action(request_action_validations.sign_and_submit, "Filing Action Forbidden")), |
| 124 | + ], |
| 125 | +) |
125 | 126 | @requires("authenticated")
|
126 | 127 | async def sign_filing(request: Request, lei: str, period_code: str):
|
127 |
| - filing = await repo.get_filing(request.state.db_session, lei, period_code) |
128 |
| - if not filing: |
129 |
| - raise RegTechHttpException( |
130 |
| - status_code=status.HTTP_404_NOT_FOUND, |
131 |
| - name="Filing Not Found", |
132 |
| - detail=f"There is no Filing for LEI {lei} in period {period_code}, unable to sign a non-existent Filing.", |
133 |
| - ) |
134 |
| - latest_sub = await repo.get_latest_submission(request.state.db_session, lei, period_code) |
135 |
| - if not latest_sub or latest_sub.state != SubmissionState.SUBMISSION_ACCEPTED: |
136 |
| - raise RegTechHttpException( |
137 |
| - status_code=status.HTTP_403_FORBIDDEN, |
138 |
| - name="Filing Action Forbidden", |
139 |
| - detail=f"Cannot sign filing. Filing for {lei} for period {period_code} does not have a latest submission the SUBMISSION_ACCEPTED state.", |
140 |
| - ) |
141 |
| - if filing.is_voluntary is None: |
142 |
| - raise RegTechHttpException( |
143 |
| - status_code=status.HTTP_403_FORBIDDEN, |
144 |
| - name="Filing Action Forbidden", |
145 |
| - detail=f"Cannot sign filing. Filing for {lei} for period {period_code} does not have a selection of is_voluntary defined.", |
146 |
| - ) |
147 |
| - if not filing.contact_info: |
148 |
| - raise RegTechHttpException( |
149 |
| - status_code=status.HTTP_403_FORBIDDEN, |
150 |
| - name="Filing Action Forbidden", |
151 |
| - detail=f"Cannot sign filing. Filing for {lei} for period {period_code} does not have contact info defined.", |
152 |
| - ) |
153 |
| - """ |
154 |
| - if not filing.institution_snapshot_id: |
155 |
| - return JSONResponse( |
156 |
| - status_code=status.HTTP_403_FORBIDDEN, |
157 |
| - content=f"Cannot sign filing. Filing for {lei} for period {period_code} does not have institution snapshot id defined.", |
158 |
| - ) |
159 |
| - """ |
160 |
| - |
| 128 | + filing: FilingDAO = request.state.context["filing"] |
| 129 | + latest_sub = (await filing.awaitable_attrs.submissions)[0] |
161 | 130 | sig = await repo.add_user_action(
|
162 | 131 | request.state.db_session,
|
163 | 132 | UserActionDTO(
|
|
0 commit comments