Esempio n. 1
0
    def get(legal_type, identifier, filing_type):
        """Return the complete filing info or historic (pre-bob-date=2019-03-08) filings."""
        try:
            if legal_type not in [x.value for x in Business.LearBusinessTypes]:
                return jsonify({'message': 'Must provide a valid legal type.'
                                }), HTTPStatus.BAD_REQUEST

            # get optional parameters (event_id / year)
            event_id = request.args.get('eventId', None)
            year = request.args.get('year', None)
            if year:
                year = int(year)

            # convert identifier if BC legal_type
            if legal_type in Business.CORP_TYPE_CONVERSION[
                    Business.LearBusinessTypes.BCOMP.value]:
                identifier = identifier[-7:]

            # get business
            corp_types = Business.CORP_TYPE_CONVERSION[legal_type]
            business = Business.find_by_identifier(identifier, corp_types)

            # get filings history from before bob-date
            if filing_type == 'historic':
                historic_filings_info = Filing.get_historic_filings(
                    business=business)
                return jsonify(historic_filings_info)

            # else get filing
            filing = Filing()
            filing.business = business
            filing.filing_type = filing_type
            filing.event_id = event_id
            filing = Filing.get_filing(filing=filing, year=year)
            return jsonify(filing.as_dict())

        except GenericException as err:  # pylint: disable=duplicate-code
            return jsonify({'message': err.error}), err.status_code

        except Exception as err:  # pylint: disable=broad-except; want to catch all errors
            # general catch-all exception
            current_app.logger.error(err.with_traceback(None))
            return jsonify({
                'message':
                'Error when trying to retrieve business record from COLIN'
            }), HTTPStatus.INTERNAL_SERVER_ERROR
Esempio n. 2
0
    def post(legal_type, identifier, **kwargs):
        """Create a new filing."""
        # pylint: disable=unused-argument,too-many-branches; filing_type is only used for the get
        try:
            if legal_type not in [x.value for x in Business.LearBusinessTypes]:
                return jsonify({'message': 'Must provide a valid legal type.'
                                }), HTTPStatus.BAD_REQUEST

            json_data = request.get_json()
            if not json_data:
                return jsonify({'message': 'No input data provided'
                                }), HTTPStatus.BAD_REQUEST

            # validate schema
            is_valid, errors = validate(json_data,
                                        'filing',
                                        validate_schema=True)
            if not is_valid:
                for err in errors:
                    print(err.message)
                return jsonify({'message': 'Error: Invalid Filing schema'
                                }), HTTPStatus.BAD_REQUEST

            json_data = json_data.get('filing', None)

            # ensure that the business in the AR matches the business in the URL
            if identifier != json_data['business']['identifier']:
                return jsonify({
                    'message':
                    'Error: Identifier in URL does not match identifier in filing data'
                }), HTTPStatus.BAD_REQUEST

            # convert identifier if BC legal_type
            if legal_type in Business.CORP_TYPE_CONVERSION[
                    Business.LearBusinessTypes.BCOMP.value]:
                identifier = identifier[-7:]

            corp_types = Business.CORP_TYPE_CONVERSION[legal_type]
            if json_data.get('correction', None):
                filing_list = {'correction': json_data['correction']}
            else:
                filing_list = {
                    'changeOfAddress':
                    json_data.get('changeOfAddress', None),
                    'changeOfDirectors':
                    json_data.get('changeOfDirectors', None),
                    'annualReport':
                    json_data.get('annualReport', None),
                    'incorporationApplication':
                    json_data.get('incorporationApplication', None),
                    'alteration':
                    json_data.get('alteration', None),
                    'transition':
                    json_data.get('transition', None)
                }

            # Filter out null-values in the filing_list dictionary
            filing_list = {
                k: v
                for k, v in filing_list.items() if filing_list[k]
            }
            print('filing list: ', filing_list)
            try:
                # get db connection and start a session, in case we need to roll back
                con = DB.connection
                con.begin()
                filings_added = FilingInfo._add_filings(
                    con, json_data, filing_list, identifier, corp_types)

                # return the completed filing data
                completed_filing = Filing()
                # get business info again - could have changed since filings were applied
                completed_filing.business = Business.find_by_identifier(
                    identifier, corp_types, con)
                completed_filing.body = {}
                for filing_info in filings_added:
                    sub_filing = Filing()
                    sub_filing.business = completed_filing.business
                    sub_filing.filing_type = filing_info['filing_type']
                    sub_filing.event_id = filing_info['event_id']
                    sub_filing = Filing.get_filing(filing=sub_filing, con=con)

                    if completed_filing.header:
                        completed_filing.header['colinIds'].append(
                            sub_filing.event_id)
                        # annual report is the only filing with sub filings underneath it
                        if sub_filing.filing_type == 'annualReport':
                            completed_filing.header['name'] = 'annualReport'
                    else:
                        completed_filing.header = sub_filing.header
                    completed_filing.body.update(
                        {sub_filing.filing_type: sub_filing.body})

                # success! commit the db changes
                con.commit()
                return jsonify(completed_filing.as_dict()), HTTPStatus.CREATED

            except Exception as db_err:
                current_app.logger.error(
                    'failed to file - rolling back partial db changes.')
                if con:
                    con.rollback()
                raise db_err

        except Exception as err:  # pylint: disable=broad-except; want to catch all errors
            # general catch-all exception
            current_app.logger.error(err.with_traceback(None))
            return jsonify({
                'message':
                f'Error when trying to file for business {identifier}'
            }), HTTPStatus.INTERNAL_SERVER_ERROR