Exemplo n.º 1
0
    def _save_filing(client_request: LocalProxy, business_identifier: str,
                     user: User,
                     filing_id: int) -> Tuple[Business, Filing, dict, int]:
        """Save the filing to the ledger.

        If not successful, a dict of errors is returned.

        Returns: {
            Business: business model object found for the identifier provided
            Filing: filing model object for the submitted filing
            dict: a dict of errors
            int: the HTTPStatus error code
        }
        """
        json_input = client_request.get_json()
        if not json_input:
            return None, None, {'message':
                                f'No filing json data in body of post for {business_identifier}.'}, \
                HTTPStatus.BAD_REQUEST

        business = Business.find_by_identifier(business_identifier)
        if not business:
            return None, None, {
                'message': f'{business_identifier} not found'
            }, HTTPStatus.NOT_FOUND

        if client_request.method == 'PUT':
            rv = db.session.query(Business, Filing). \
                filter(Business.id == Filing.business_id). \
                filter(Business.identifier == business_identifier). \
                filter(Filing.id == filing_id). \
                one_or_none()
            if not rv:
                return None, None, {
                    'message': f'{business_identifier} no filings found'
                }, HTTPStatus.NOT_FOUND
            filing = rv[1]
        else:
            filing = Filing()
            filing.business_id = business.id

        try:
            filing.submitter_id = user.id
            filing.filing_date = datetime.datetime.utcnow()
            filing.filing_json = json_input
            filing.save()
        except BusinessException as err:
            return None, None, {'error': err.error}, err.status_code

        return business, filing, None, None
Exemplo n.º 2
0
    def _validate_filing_json(client_request: LocalProxy) -> Tuple[dict, int]:
        """Assert that the json is a valid filing.

        Returns: {
            dict: a dict, success message or array of errors
            int: the HTTPStatus error code
        }
        """
        valid, err = rsbc_schemas.validate(client_request.get_json(), 'filing')

        if valid:
            return {'message': 'Filing is valid'}, HTTPStatus.OK

        errors = []
        for error in err:
            errors.append({'path': '/'.join(error.path), 'error': error.message})
        return errors, HTTPStatus.BAD_REQUEST
Exemplo n.º 3
0
    def _save_filing(client_request: LocalProxy, business_identifier: str,
                     user: User,
                     filing_id: int) -> Tuple[Business, Filing, dict, int]:
        """Save the filing to the ledger.

        If not successful, a dict of errors is returned.

        Returns: {
            Business: business model object found for the identifier provided
            Filing: filing model object for the submitted filing
            dict: a dict of errors
            int: the HTTPStatus error code
        }
        """
        json_input = client_request.get_json()
        if not json_input:
            return None, None, {'message':
                                f'No filing json data in body of post for {business_identifier}.'}, \
                HTTPStatus.BAD_REQUEST

        business = Business.find_by_identifier(business_identifier)
        if not business:
            return None, None, {
                'message': f'{business_identifier} not found'
            }, HTTPStatus.NOT_FOUND

        if client_request.method == 'PUT':
            rv = db.session.query(Business, Filing). \
                filter(Business.id == Filing.business_id). \
                filter(Business.identifier == business_identifier). \
                filter(Filing.id == filing_id). \
                one_or_none()
            if not rv:
                return None, None, {
                    'message': f'{business_identifier} no filings found'
                }, HTTPStatus.NOT_FOUND
            filing = rv[1]
        else:
            filing = Filing()
            filing.business_id = business.id

        try:
            filing.submitter_id = user.id
            filing.filing_json = json_input
            if user.username == 'coops-updater-job':
                try:
                    filing.filing_date = datetime.datetime.fromisoformat(
                        filing.filing_json['filing']['header']['date'])
                    filing.colin_event_id = filing.filing_json['filing'][
                        'header']['colinId']
                except KeyError:
                    current_app.logger.error(
                        'Business:%s missing filing/header values, unable to save',
                        business.identifier)
                    return None, None, {
                        'message': 'missing filing/header values'
                    }, HTTPStatus.BAD_REQUEST
            else:
                filing.filing_date = datetime.datetime.utcnow()
            filing.save()
        except BusinessException as err:
            return None, None, {'error': err.error}, err.status_code

        return business, filing, None, None
Exemplo n.º 4
0
    def _save_filing(
        client_request: LocalProxy,  # pylint: disable=too-many-return-statements,too-many-branches
        business_identifier: str,
        user: User,
        filing_id: int
    ) -> Tuple[Union[Business, RegistrationBootstrap], Filing, dict, int]:
        """Save the filing to the ledger.

        If not successful, a dict of errors is returned.

        Returns: {
            Business: business model object found for the identifier provided
            Filing: filing model object for the submitted filing
            dict: a dict of errors
            int: the HTTPStatus error code

        @TODO refactor to a set of single putpose routines
        }
        """
        json_input = client_request.get_json()
        if not json_input:
            return None, None, {'message':
                                f'No filing json data in body of post for {business_identifier}.'}, \
                HTTPStatus.BAD_REQUEST

        if business_identifier.startswith('T'):
            # bootstrap filing
            bootstrap = RegistrationBootstrap.find_by_identifier(
                business_identifier)
            business = None
            if not bootstrap:
                return None, None, {
                    'message': f'{business_identifier} not found'
                }, HTTPStatus.NOT_FOUND
            if client_request.method == 'PUT':
                rv = db.session.query(Filing). \
                    filter(Filing.temp_reg == business_identifier). \
                    filter(Filing.id == filing_id). \
                    one_or_none()
                if not rv:
                    return None, None, {
                        'message': f'{business_identifier} no filings found'
                    }, HTTPStatus.NOT_FOUND
                filing = rv
            else:
                filing = Filing()
                filing.temp_reg = bootstrap.identifier
                if not json_input['filing'].get('business'):
                    json_input['filing']['business'] = {}
                json_input['filing']['business'][
                    'identifier'] = bootstrap.identifier

        else:
            # regular filing for a business
            business = Business.find_by_identifier(business_identifier)
            if not business:
                return None, None, {
                    'message': f'{business_identifier} not found'
                }, HTTPStatus.NOT_FOUND

            if client_request.method == 'PUT':
                rv = db.session.query(Business, Filing). \
                    filter(Business.id == Filing.business_id). \
                    filter(Business.identifier == business_identifier). \
                    filter(Filing.id == filing_id). \
                    one_or_none()
                if not rv:
                    return None, None, {
                        'message': f'{business_identifier} no filings found'
                    }, HTTPStatus.NOT_FOUND
                filing = rv[1]
            else:
                filing = Filing()
                filing.business_id = business.id

        try:
            filing.submitter_id = user.id
            filing.filing_json = json_input
            filing.source = filing.filing_json['filing']['header'].get(
                'source', Filing.Source.LEAR.value)
            if filing.source == Filing.Source.COLIN.value:
                try:
                    filing.filing_date = datetime.datetime.fromisoformat(
                        filing.filing_json['filing']['header']['date'])
                    for colin_id in filing.filing_json['filing']['header'][
                            'colinIds']:
                        colin_event_id = ColinEventId()
                        colin_event_id.colin_event_id = colin_id
                        filing.colin_event_ids.append(colin_event_id)
                except KeyError:
                    current_app.logger.error(
                        'Business:%s missing filing/header values, unable to save',
                        business.identifier)
                    return None, None, {
                        'message': 'missing filing/header values'
                    }, HTTPStatus.BAD_REQUEST
            else:
                filing.filing_date = datetime.datetime.utcnow()

            # for any legal type, set effective date as set in json; otherwise leave as default
            filing.effective_date = \
                datetime.datetime.fromisoformat(filing.filing_json['filing']['header']['effectiveDate']) \
                if filing.filing_json['filing']['header'].get('effectiveDate', None) else datetime.datetime.utcnow()

            filing.save()
        except BusinessException as err:
            return None, None, {'error': err.error}, err.status_code

        return business or bootstrap, filing, None, None