Exemplo n.º 1
0
def full_access_to_name_request(request: Request) -> bool:
    """Returns that the request contains the headers required to fully access this NR."""
    nr = request.headers.get('BCREG-NR', '')
    nrl = request.headers.get('BCREG-NRL', '')
    email = request.headers.get('BCREG-User-Email', '')
    phone = request.headers.get('BCREG-User-Phone', '')

    current_app.logger.debug('NR: %s, NRL: %s, Email: %s, Phone: %s', nr, nrl,
                             email, phone)

    nr = set_to_none(nr, ['none', 'null', 'nan'])
    nrl = set_to_none(nrl, ['none', 'null', 'nan'])
    email = set_to_none(email, ['none', 'null', 'nan'])
    phone = set_to_none(phone, ['none', 'null', 'nan'])

    if nr and not RequestDAO.validNRFormat(nr):
        nr = nr.replace(' ', '')
        nr = nr.replace('NR', '')
        nr = 'NR ' + nr

    if not (name_request := RequestDAO.find_by_nr(nr)):
        if not (name_request := RequestDAO.find_by_nr(nrl)):
            current_app.logger.debug(
                'Failed to find NR - NR: %s, NRL: %s, Email: %s, Phone: %s',
                nr, nrl, email, phone)
            return False
Exemplo n.º 2
0
def test_add_request_names(client, jwt, app):
    """
    Setup:
    Test:
    Validate:
    :param client:
    :param jwt:
    :param app:
    :return:
    """
    do_test_cleanup()

    # Initialize the service
    nr_svc = NameRequestService()
    """
    Test adding two new names 
    """
    # We will need a base NR
    nr = build_nr(State.DRAFT, {}, [test_names_no_id[0]])
    # We can't save the NR without an NR Num
    nr.nrNum = 'NR L000001'
    # Save to DB so PK sequences are updated
    nr.save_to_db()
    db.session.flush()

    nr = Request.find_by_nr(nr.nrNum)

    # Set data to the service, all we need to test is names so just provide what's necessary
    nr_svc.request_data = {
        'names': [
            # Same as test name 1
            test_names_no_id[1],
            test_names_no_id[2]
        ]
    }

    # Build the names
    nr = nr_svc.map_request_names(nr)

    nr.save_to_db()

    nr = Request.find_by_nr(nr.nrNum)

    # Convert to dict
    nr = nr.json()

    assert nr is not None
    # Test the names
    assert_names_are_mapped_correctly(nr_svc.request_data.get('names'),
                                      nr.get('names'))

    # Clean up
    do_test_cleanup()
Exemplo n.º 3
0
    def get(nr, choice, analysis_type, *args, **kwargs):
        start = request.args.get('start', RequestsAnalysis.START)
        rows = request.args.get('rows', RequestsAnalysis.ROWS)

        if analysis_type not in ANALYTICS_VALID_ANALYSIS:
            return jsonify(
                message=
                '{analysis_type} is not a valid analysis type for that name choice'
                .format(analysis_type=analysis_type)), 404

        nrd = RequestDAO.find_by_nr(nr)

        if not nrd:
            return jsonify(message='{nr} not found'.format(nr=nr)), 404

        nrd_name = nrd.names.filter_by(choice=choice).one_or_none()

        if not nrd_name:
            return jsonify(message='Name choice:{choice} not found for {nr}'.
                           format(nr=nr, choice=choice)), 404

        if analysis_type in RestrictedWords.RESTRICTED_WORDS:
            results, msg, code = RestrictedWords.get_restricted_words_conditions(
                nrd_name.name)

        else:
            results, msg, code = SolrQueries.get_results(analysis_type,
                                                         nrd_name.name,
                                                         start=start,
                                                         rows=rows)

        if code:
            return jsonify(message=msg), code
        return jsonify(results), 200
Exemplo n.º 4
0
    def fetch_nro_request_and_copy_to_namex_request(self, user: User, nr_number: str, name_request: Request = None) \
            -> Request:
        """Utility function to gather up and copy a Request from NRO to a NameX Request Object
           The request is NOT persisted in this helper method
        """
        try:
            cursor = self.connection.cursor()

            if name_request:
                nr = name_request
                nr_num = nr.nrNum
            else:
                nr_num = nr_number
                nr = Request.find_by_nr(nr_num)
                if not nr:
                    nr = Request()

            nr_header = get_nr_header(cursor, nr_num)

            if not nr_header:
                current_app.logger.info(
                    'Attempting to fetch Request:{} from NRO, but does not exist'
                    .format(nr_num))
                return None
            current_app.logger.debug('fetched nr_header: {}'.format(nr_header))

            # get all the request segments from NRO
            nr_submitter = get_nr_submitter(cursor, nr_header['request_id'])
            nr_applicant = get_nr_requester(cursor, nr_header['request_id'])
            nr_ex_comments = get_exam_comments(cursor, nr_header['request_id'])
            nr_nwpta = get_nwpta(cursor, nr_header['request_id'])
            nr_names = get_names(cursor, nr_header['request_id'])

            current_app.logger.debug('completed all gets')

        except Exception as err:
            current_app.logger.debug(
                'unable to load nr_header: {}'.format(nr_num),
                err.with_traceback(None))
            return None

        add_nr_header(nr, nr_header, nr_submitter, user)
        current_app.logger.debug('completed header for {}'.format(nr.nrNum))
        if nr_applicant:
            add_applicant(nr, nr_applicant)
            current_app.logger.debug('completed applicants for {}'.format(
                nr.nrNum))
        if nr_ex_comments:
            add_comments(nr, nr_ex_comments)
            current_app.logger.debug('completed comments for {}'.format(
                nr.nrNum))
        if nr_nwpta:
            add_nwpta(nr, nr_nwpta)
            current_app.logger.debug('completed nwpta for {}'.format(nr.nrNum))
        if nr_names:
            add_names(nr, nr_names)
            current_app.logger.debug('completed names for {}'.format(nr.nrNum))

        return nr
Exemplo n.º 5
0
    def delete(nr):
        nrd = RequestDAO.find_by_nr(nr)
        # even if not found we still return a 204, which is expected spec behaviour
        if nrd:
            nrd.stateCd = State.CANCELLED
            nrd.save_to_db()

        return '', 204
Exemplo n.º 6
0
def is_processable(msg: dict):
    """Determine if message is processable using message type of msg."""
    with FLASK_APP.app_context():
        if msg and is_names_event_msg_type(msg) \
           and (nr_num := msg.get('data', {})
                             .get('request', {})
                             .get('nrNum', None)) \
           and (nr := RequestDAO.find_by_nr(nr_num)) \
           and nr.entity_type_cd not in ('FR', 'GP'):
            return True
Exemplo n.º 7
0
    def save_request(cls, name_request, on_success=None):
        try:
            name_request.save_to_db()
            if on_success:
                on_success()

            return Request.find_by_nr(name_request.nrNum)

        except Exception as err:
            raise SaveNameRequestError(err)
Exemplo n.º 8
0
    def post(nr_num):
        try:
            # TODO: Validate NR string format
            # if not RequestDAO.validNRFormat(nr_num):
            #    return jsonify(message='NR number is not in a valid format \'NR 9999999\''), 400

            nr_draft = RequestDAO.find_by_nr(nr_num)
            if not nr_draft:
                # Should this be a 400 or 404... hmmm
                return jsonify(message='{nr_num} not found'.format(nr_num=nr_num)), 400

            json_input = request.get_json()
            if not json_input:
                return jsonify(message=MSG_BAD_REQUEST_NO_JSON_BODY), 400
            elif isinstance(json_input, str):
                json_input = json.loads(json_input)

            # Grab the info we need off the request
            payment_info = json_input.get('paymentInfo')
            filing_info = json_input.get('filingInfo')
            business_info = json_input.get('businessInfo')

            # Create our payment request
            req = PaymentRequest(
                payment_info=payment_info,
                filing_info=filing_info,
                business_info=business_info
            )

            payment_response = create_payment(req)
            if not payment_response:
                raise PaymentServiceError(message=MSG_ERROR_CREATING_RESOURCE)

            if payment_response and payment_response.status_code == PaymentStatusCode.CREATED.value:
                # Save the payment info to Postgres
                payment = PaymentDAO()
                payment.nrId = nr_draft.id
                payment.payment_token = str(payment_response.id)
                payment.payment_completion_date = payment_response.created_on
                payment.payment_status_code = PaymentState.CREATED.value
                payment.save_to_db()

                data = jsonify(payment_response.to_dict())
                response = make_response(data, 200)
                return response

        except PaymentServiceError as err:
            return handle_exception(err, err.message, 500)
        except SBCPaymentException as err:
            return handle_exception(err, err.message, 500)
        except SBCPaymentError as err:
            return handle_exception(err, err.message, 500)
        except Exception as err:
            return handle_exception(err, err, 500)
Exemplo n.º 9
0
async def test_update_payment_record(
        app, session, test_name, action_code, start_request_state,
        end_request_state, start_priority, end_priority, start_datetime,
        days_after_start_datetime, start_payment_state, end_payment_state,
        start_payment_date, end_payment_has_value, error):
    """Assert that the update_payment_record works as expected."""
    from namex.models import Request, State, Payment
    from namex_pay.worker import update_payment_record

    print(test_name)

    now = datetime.utcnow()

    with freeze_time(now):
        # setup
        PAYMENT_TOKEN = 'dog'
        NR_NUMBER = 'NR B000001'
        name_request = Request()
        name_request.nrNum = NR_NUMBER
        name_request.stateCd = start_request_state
        name_request._source = 'NRO'
        name_request.expirationDate = start_datetime
        name_request.priorityCd = start_priority
        name_request.save_to_db()

        payment = Payment()
        payment.nrId = name_request.id
        payment._payment_token = PAYMENT_TOKEN
        payment._payment_status_code = start_payment_state
        payment.payment_action = action_code
        payment.furnished = False
        payment._payment_completion_date = start_payment_date
        payment.save_to_db()

        # run test
        if error:  # expecting it to raise an error
            with pytest.raises(error):
                await update_payment_record(payment)
        else:
            # else it was processable
            if not (payment_final := await update_payment_record(payment)):
                payment_final = payment

            nr_final = Request.find_by_nr(NR_NUMBER)

            assert nr_final.stateCd == end_request_state
            assert nr_final.priorityCd == end_priority
            assert nr_final.expirationDate == (
                start_datetime
                or now) + timedelta(days=days_after_start_datetime)
            assert eval(
                f'payment_final.payment_completion_date {end_payment_has_value} None'
            )
            assert payment_final.payment_status_code == end_payment_state
Exemplo n.º 10
0
    def post(self, *args, **kwargs):
        json_input = request.get_json()
        if not json_input:
            return {"message": "No input data provided"}, 400

        nr_num = json_input['nameRequest']
        current_app.logger.debug('attempting to load: {}'.format(nr_num))
        if not validNRFormat(nr_num):
            return {"message": "Valid NR format required - 'NR 9999999'"}, 400

        if Request.find_by_nr(nr_num):
            return {
                "message":
                "{nr} already exists in namex, unable to create a duplicate".
                format(nr=nr_num)
            }, 409

        conn = db.get_engine(bind='nro')

        nr_header = get_nr_header(conn, nr_num)
        current_app.logger.debug('nr_header: {}'.format(nr_header))
        if not nr_header:
            return {
                "message":
                "{nr} not found, unable to complete extraction to new system".
                format(nr=nr_num)
            }, 404

        nr_submitter = get_nr_submitter(conn, nr_header['request_id'])
        nr_applicant = get_nr_requester(conn, nr_header['request_id'])
        nr_ex_comments = get_exam_comments(conn, nr_header['request_id'])
        nr_nwpat = get_nwpta(conn, nr_header['request_id'])
        nr_names = get_names(conn, nr_header['request_id'])

        user = User.find_by_username(current_app.config['NRO_SERVICE_ACCOUNT'])

        #Create NR
        new_nr = Request()
        add_nr_header(new_nr, nr_header, nr_submitter, user)
        add_applicant(new_nr, nr_applicant)
        add_comments(new_nr, nr_ex_comments)
        add_nwpta(new_nr, nr_nwpat)
        add_names(new_nr, nr_names)

        new_nr.save_to_db()

        return {
            "message": "{nr} has been successfully copied".format(nr=nr_num)
        }, 200
Exemplo n.º 11
0
    def common(nr, choice):
        """:returns: object, code, msg
        """
        if not RequestDAO.validNRFormat(nr):
            return None, None, jsonify(
                {'message': 'NR is not a valid format \'NR 9999999\''}), 400

        nrd = RequestDAO.find_by_nr(nr)
        if not nrd:
            return None, None, jsonify(
                {"message": "{nr} not found".format(nr=nr)}), 404

        name = nrd.names.filter_by(choice=choice).one_or_none()
        if not name:
            return None, None, jsonify({
                "message":
                "Choice {choice} for {nr} not found".format(choice=choice,
                                                            nr=nr)
            }), 404

        return nrd, name, None, 200
Exemplo n.º 12
0
    def patch(nr, *args, **kwargs):
        """  Patches the NR, only STATE can be changed with some business rules around roles/scopes

        :param nr (str): NameRequest Number in the format of 'NR 000000000'
        :param args:  __futures__
        :param kwargs: __futures__
        :return: 200 - success; 40X for errors

        :HEADER: Valid JWT Bearer Token for a valid REALM
        :JWT Scopes: - USER.APPROVER, USER.EDITOR, USER.VIEWONLY

        APPROVERS: Can change from almost any state, other than CANCELLED, EXPIRED and ( COMPLETED not yet furnished )
        EDITOR: Can't change to a COMPLETED state (ACCEPTED, REJECTED, CONDITION)
        VIEWONLY: Can't change anything, so that are bounced
        """

        # do the cheap check first before the more expensive ones
        #check states
        json_input = request.get_json()
        if not json_input:
            return jsonify({'message': 'No input data provided'}), 400

        # Currently only state changes are supported by patching
        # all these checks to get removed to marshmallow
        state = json_input.get('state', None)
        if not state:
            return jsonify({"message": "state not set"}), 406

        if state not in State.VALID_STATES:
            return jsonify({"message": "not a valid state"}), 406

        #check user scopes
        if not (jwt.requires_roles(User.EDITOR)
                or jwt.requires_roles(User.APPROVER)):
            raise AuthError(
                {
                    "code": "Unauthorized",
                    "description": "You don't have access to this resource."
                }, 403)

        if (state in (State.APPROVED,
                     State.REJECTED,
                     State.CONDITIONAL))\
                and not jwt.requires_roles(User.APPROVER):
            return jsonify({
                "message":
                "Only Names Examiners can set state: {}".format(state)
            }), 428

        try:
            nrd = RequestDAO.find_by_nr(nr)
            if not nrd:
                return jsonify({"message": "NR not found"}), 404

            user = User.find_by_jwtToken(g.jwt_oidc_token_info)
            if not user:
                user = User.create_from_jwtToken(g.jwt_oidc_token_info)

            #NR is in a final state, but maybe the user wants to pull it back for corrections
            if nrd.stateCd in State.COMPLETED_STATE:
                if not jwt.requires_roles(User.APPROVER):
                    return jsonify({
                        "message":
                        "Only Names Examiners can alter completed Requests"
                    }), 401

                if nrd.furnished == RequestDAO.REQUEST_FURNISHED:
                    return jsonify({
                        "message":
                        "Request has already been furnished and cannot be altered"
                    }), 409

                if state != State.INPROGRESS:
                    return jsonify({
                        "message":
                        "Completed unfurnished Requests can only be set to an INPROGRESS state"
                    }), 400

            elif state in State.RELEASE_STATES:
                if nrd.userId != user.id or nrd.stateCd != State.INPROGRESS:
                    return jsonify({
                        "message":
                        "The Request must be INPROGRESS and assigned to you before you can change it."
                    }), 401

            existing_nr = RequestDAO.get_inprogress(user)
            if existing_nr:
                existing_nr.stateCd = State.HOLD
                existing_nr.save_to_db()

            nrd.stateCd = state
            nrd.userId = user.id
            nrd.save_to_db()

        except NoResultFound as nrf:
            # not an error we need to track in the log
            return jsonify({"message": "Request:{} not found".format(nr)}), 404
        except Exception as err:
            current_app.logger.error(
                "Error when patching NR:{0} Err:{1}".format(nr, err))
            return jsonify({"message": "NR had an internal error"}), 404

        return jsonify({'message': 'Request:{} - patched'.format(nr)}), 200
Exemplo n.º 13
0
def job(app, namex_db, nro_connection, user, max_rows=100):

    row_count = 0

    try:
        ora_con = nro_connection
        result, col_names = job_result_set(ora_con, max_rows)

        for r in result:

            row_count += 1

            row = ora_row_to_dict(col_names, r)

            nr_num = row['nr_num']
            nr = Request.find_by_nr(nr_num)
            action = row['action']

            current_app.logger.debug(
                'processing: {}, NameX state: {}, action: {}'.format(
                    nr_num, None if (not nr) else nr.stateCd, action))

            if nr and (nr.stateCd != State.DRAFT):
                if action != 'X':
                    success = update_feeder_row(
                        ora_con,
                        id=row['id'],
                        status='C',
                        send_count=1 + 0 if
                        (row['send_count'] is None) else row['send_count'],
                        error_message='Ignored - Request: not processed')
                    ora_con.commit()
                    continue
            try:
                nr = nro.fetch_nro_request_and_copy_to_namex_request(
                    user, nr_number=nr_num, name_request=nr)

                namex_db.session.add(nr)
                EventRecorder.record(user,
                                     Event.UPDATE_FROM_NRO,
                                     nr, {},
                                     save_to_session=True)

                success = update_feeder_row(
                    ora_con,
                    id=row['id'],
                    status='C',
                    send_count=1 + 0 if
                    (row['send_count'] is None) else row['send_count'],
                    error_message=None)

                if success:
                    ora_con.commit()
                    namex_db.session.commit()
                else:
                    raise Exception()

            except Exception as err:
                current_app.logger.error(err.with_traceback(None))
                success = update_feeder_row(
                    ora_con,
                    id=row['id'],
                    status=row['status'],
                    send_count=1 + 0 if
                    (row['send_count'] is None) else row['send_count'],
                    error_message=err.with_traceback(None))
                namex_db.session.rollback()
                ora_con.commit()

        return row_count

    except Exception as err:
        current_app.logger.error('Update Failed:', err.with_traceback(None))
        return -1
Exemplo n.º 14
0
    def put(nr, *args, **kwargs):

        # do the cheap check first before the more expensive ones
        json_input = request.get_json()
        if not json_input:
            return jsonify(message='No input data provided'), 400
        current_app.logger.debug(json_input)

        nr_num = json_input.get('nrNum', None)
        if nr_num and nr_num != nr:
            return jsonify(
                message='Data contains a different NR# than this resource'
            ), 400

        state = json_input.get('state', None)
        if not state:
            return jsonify({"message": "state not set"}), 406

        if state not in State.VALID_STATES:
            return jsonify({"message": "not a valid state"}), 406

        #check user scopes
        if not (jwt.requires_roles(User.EDITOR)
                or jwt.requires_roles(User.APPROVER)):
            raise AuthError(
                {
                    "code": "Unauthorized",
                    "description": "You don't have access to this resource."
                }, 403)

        if (state in (State.APPROVED,
                     State.REJECTED,
                     State.CONDITIONAL))\
                and not jwt.requires_roles(User.APPROVER):
            return jsonify(message='Only Names Examiners can set state: {}'.
                           format(state)), 428

        try:
            nr_d = RequestDAO.find_by_nr(nr)
            if not nr_d:
                return jsonify(message='NR not found'), 404

            user = User.find_by_jwtToken(g.jwt_oidc_token_info)
            if not user:
                user = User.create_from_jwtToken(g.jwt_oidc_token_info)

            #NR is in a final state, but maybe the user wants to pull it back for corrections
            if nr_d.stateCd in State.COMPLETED_STATE:
                if not jwt.requires_roles(User.APPROVER):
                    return jsonify(
                        message=
                        'Only Names Examiners can alter completed Requests'
                    ), 401

                if nr_d.furnished == RequestDAO.REQUEST_FURNISHED:
                    return jsonify(
                        message=
                        'Request has already been furnished and cannot be altered'
                    ), 409

                if state != State.INPROGRESS:
                    return jsonify(
                        message=
                        'Completed unfurnished Requests can only be set to an INPROGRESS state'
                    ), 400

            elif state in State.RELEASE_STATES:
                if nr_d.userId != user.id or nr_d.stateCd != State.INPROGRESS:
                    return jsonify(
                        message=
                        'The Request must be INPROGRESS and assigned to you before you can change it.'
                    ), 401
            elif nr_d.userId != user.id or nr_d.stateCd != State.INPROGRESS:
                return jsonify(
                    message=
                    'The Request must be INPROGRESS and assigned to you before you can change it.'
                ), 401

            # update request header
            request_header_schema.load(json_input, instance=nr_d, partial=True)
            nr_d.stateCd = state
            nr_d.userId = user.id

            # update applicants
            applicants_d = nr_d.applicants.one_or_none()
            if applicants_d:
                appl = json_input.get('applicants', None)
                if appl:
                    errm = applicant_schema.validate(appl, partial=False)
                    if errm:
                        return jsonify(errm)

                    applicant_schema.load(appl,
                                          instance=applicants_d,
                                          partial=False)
                else:
                    applicants_d.delete_from_db()

            ### NAMES ###
            for nrd_name in nr_d.names.all():
                for in_name in json_input['names']:
                    if nrd_name.choice == in_name['choice']:

                        errors = names_schema.validate(in_name, partial=False)
                        if errors:
                            return jsonify(errors), 400

                        names_schema.load(in_name,
                                          instance=nrd_name,
                                          partial=False)
            ### END names ###

            ### COMMENTS ###

            # we only add new comments, we do not change existing comments
            # - we can find new comments in json as those with no ID

            for in_comment in json_input['comments']:
                is_new_comment = False
                try:
                    if in_comment['id'] is None or in_comment['id'] == 0:
                        is_new_comment = True
                except KeyError:
                    is_new_comment = True

                if is_new_comment and in_comment['comment'] is not None:
                    new_comment = Comment()
                    new_comment.comment = in_comment['comment']
                    new_comment.examiner = user
                    new_comment.nrId = nr_d.id

            ### END comments ###

            ### NWPTA ###

            for nrd_nwpta in nr_d.partnerNS.all():
                for in_nwpta in json_input['nwpta']:
                    if nrd_nwpta.partnerJurisdictionTypeCd == in_nwpta[
                            'partnerJurisdictionTypeCd']:

                        errors = nwpta_schema.validate(in_nwpta, partial=False)
                        if errors:
                            return jsonify(errors), 400

                        nwpta_schema.load(in_nwpta,
                                          instance=nrd_nwpta,
                                          partial=False)
            ### END nwpta ###

            ### Finally save the entire graph
            nr_d.save_to_db()

        except ValidationError as ve:
            return jsonify(ve.messages)

        except NoResultFound as nrf:
            # not an error we need to track in the log
            return jsonify(message='Request:{} not found'.format(nr)), 404

        except Exception as err:
            current_app.logger.error(
                "Error when replacing NR:{0} Err:{1}".format(nr, err))
            return jsonify(message='NR had an internal error'), 500

        current_app.logger.debug(nr_d.json())
        return jsonify(nr_d.json()), 200
Exemplo n.º 15
0
    def post(*args, **kwargs):

        app_config = current_app.config.get('SOLR_SYNONYMS_API_URL', None)
        if not app_config:
            current_app.logger.error('ENV is not set')
            return None, 'Internal server error', 500

        test_env = 'prod'
        if test_env in app_config:
            current_app.logger.info(
                'Someone is trying to post a new request. Not available.')
            return jsonify(
                {'message': 'Not Implemented in the current environment'}), 501

        json_data = request.get_json()
        if not json_data:
            current_app.logger.error("Error when getting json input")
            return jsonify({'message': 'No input data provided'}), 400

        try:

            restricted = VirtualWordConditionService()
        except Exception as error:
            current_app.logger.error(
                "'Error initializing VirtualWordCondition Service: Error:{0}".
                format(error))
            return jsonify({"message":
                            "Virtual Word Condition Service error"}), 404

        try:
            user = User.find_by_username('name_request_service_account')
            user_id = user.id
        except Exception as error:
            current_app.logger.error(
                "Error getting user id: Error:{0}".format(error))
            return jsonify({"message": "Get User Error"}), 404

        try:
            name_request = Request()
        except Exception as error:
            current_app.logger.error(
                "Error initializing name_request object Error:{0}".format(
                    error))
            return jsonify({"message": "Name Request object error"}), 404

        try:
            nr_num = generate_nr()
            nr_id = get_request_sequence()
        except Exception as error:
            current_app.logger.error(
                "Error getting nr number. Error:{0}".format(error))
            return jsonify({"message": "Error getting nr number"}), 404

        #set the request attributes
        try:
            name_request.id = nr_id
            name_request.submittedDate = datetime.utcnow()
            name_request.requestTypeCd = set_request_type(
                json_data['entity_type'], json_data['request_action'])
            name_request.nrNum = nr_num
        except Exception as error:
            current_app.logger.error(
                "Error setting request header attributes. Error:{0}".format(
                    error))
            return jsonify(
                {"message": "Error setting request header attributes"}), 404

        try:

            if (json_data['stateCd'] == 'COND-RESERVE'):
                name_request.consentFlag = 'Y'

            if json_data['stateCd'] in [State.RESERVED, State.COND_RESERVE]:
                name_request.expirationDate = create_expiry_date(
                    start=name_request.submittedDate,
                    expires_in_days=56,
                    tz=timezone('UTC'))

            name_request.stateCd = json_data['stateCd']
            name_request.entity_type_cd = json_data['entity_type']
            name_request.request_action_cd = json_data['request_action']
        except Exception as error:
            current_app.logger.error(
                "Error setting reserve state and expiration date. Error:{0}".
                format(error))
            return jsonify(
                {"message":
                 "Error setting reserve state and expiration date"}), 404

        #set this to name_request_service_account
        name_request.userId = user_id
        try:
            lang_comment = add_language_comment(json_data['english'], user_id,
                                                nr_id)
            name_request.comments.append(lang_comment)
        except Exception as error:
            current_app.logger.error(
                "Error setting language comment. Error:{0}".format(error))
            return jsonify({"message": "Error setting language comment."}), 404

        try:
            if json_data['nameFlag'] == True:
                name_comment = add_name_comment(user_id, nr_id)
                name_request.comments.append(name_comment)

        except Exception as error:
            current_app.logger.error(
                "Error setting person name comment. Error:{0}".format(error))
            return jsonify({"message":
                            "Error setting person name comment."}), 404

        try:
            if json_data['submit_count'] is None:
                name_request.submitCount = 1
            else:
                name_request.submitCount = +1
        except Exception as error:
            current_app.logger.error(
                "Error setting submit count. Error:{0}".format(error))
            return jsonify({"message": "Error setting submit count."}), 404

        try:
            if json_data['stateCd'] == State.DRAFT:
                # set request header attributes
                name_request = set_draft_attributes(name_request, json_data,
                                                    user_id)
                name_request.save_to_db()
                nrd = Request.find_by_nr(name_request.nrNum)
                try:
                    # set applicant attributes
                    nrd_app = set_applicant_attributes(json_data, nr_id)
                    nrd.applicants.append(nrd_app)

                except Exception as error:
                    current_app.logger.error(
                        "Error setting applicant. Error:{0}".format(error))
                    return jsonify({"message":
                                    "Error setting applicant."}), 404

        except Exception as error:
            current_app.logger.error(
                "Error setting DRAFT attributes. Error:{0}".format(error))
            return jsonify({"message": "Error setting DRAFT attributes."}), 404

        try:

            if json_data['stateCd'] in [
                    State.RESERVED, State.COND_RESERVE, State.DRAFT
            ]:
                name_request.save_to_db()
                nrd = Request.find_by_nr(name_request.nrNum)

        except Exception as error:
            current_app.logger.error(
                "Error saving reservation to db. Error:{0}".format(error))
            return jsonify({"message": "Error saving reservation to db."}), 404

        try:
            nrd = Request.find_by_nr(name_request.nrNum)
        except Exception as error:
            current_app.logger.error(
                "Error retrieving the New NR from the db. Error:{0}".format(
                    error))
            return jsonify(
                {"message": "Error retrieving the New NR from the db."}), 404

        try:
            for name in json_data.get('names', None):
                try:
                    submitted_name = Name()
                    name_id = get_name_sequence()
                    submitted_name.id = name_id
                except Exception as error:
                    current_app.logger.error(
                        "Error on submitted Name object. Error:{0}".format(
                            error))
                    return jsonify(
                        {"message":
                         "Error on submitted_name and sequence."}), 404

                #common name attributes
                try:
                    submitted_name.choice = name['choice']
                    submitted_name.name = name['name']

                    if (name['name_type_cd']):
                        submitted_name.name_type_cd = name['name_type_cd']
                    else:
                        submitted_name.name_type_cd = 'CO'

                    if (json_data['stateCd'] == State.DRAFT):
                        submitted_name.state = 'NE'
                    else:
                        submitted_name.state = json_data['stateCd']

                    if name['designation']:
                        submitted_name.designation = name['designation']

                    submitted_name.nrId = nr_id
                except Exception as error:
                    current_app.logger.error(
                        "Error on common name attributes. Error:{0}".format(
                            error))
                    return jsonify(
                        {"message": "Error on common name attributes."}), 404

                decision_text = None

                if json_data['stateCd'] in [
                        State.RESERVED, State.COND_RESERVE
                ]:
                    try:
                        # only capturing one conflict
                        if (name['conflict1_num']):
                            submitted_name.conflict1_num = name[
                                'conflict1_num']
                        if name['conflict1']:
                            submitted_name.conflict1 = name['conflict1']
                        #conflict text same as Namex
                        decision_text = 'Consent is required from ' + name[
                            'conflict1'] + '\n' + '\n'
                    except Exception as error:
                        current_app.logger.error(
                            "Error on reserved conflict info. Error:{0}".
                            format(error))
                        return jsonify(
                            {"message":
                             "Error on reserved conflict info."}), 404

                else:
                    try:
                        submitted_name.conflict1_num = None
                        submitted_name.conflict1 = None
                    except Exception as error:
                        current_app.logger.error(
                            "Error on draft empty conflict info. Error:{0}".
                            format(error))
                        return jsonify(
                            {"message":
                             "Error on draft empty conflict info."}), 404

                consent_list = name['consent_words']
                if len(consent_list) > 0:
                    for consent in consent_list:
                        try:
                            cnd_instructions = None
                            if consent != "" or len(consent) > 0:
                                cnd_instructions = restricted.get_word_condition_instructions(
                                    consent)
                        except Exception as error:
                            current_app.logger.error(
                                "Error on get consent word. Consent Word[0], Error:{1}"
                                .format(consent, error))
                            return jsonify(
                                {"message":
                                 "Error on get consent words."}), 404

                        try:
                            if (decision_text is None):
                                decision_text = cnd_instructions + '\n'
                            else:
                                decision_text += consent + '- ' + cnd_instructions + '\n'

                            submitted_name.decision_text = decision_text
                        except Exception as error:
                            current_app.logger.error(
                                "Error on adding consent words to decision. Error:{0}"
                                .format(error))
                            return jsonify({
                                "message":
                                "Error on adding consent words to decision text"
                            }), 404
                try:
                    nrd.names.append(submitted_name)
                except Exception as error:
                    current_app.logger.error(
                        "Error appending names. Error:{0}".format(error))
                    return jsonify({"message": "Error appending names"}), 404

            try:
                #save names
                nrd.save_to_db()
            except Exception as error:
                current_app.logger.error(
                    "Error saving the whole nr and names. Error:{0}".format(
                        error))
                return jsonify({"message":
                                "Error saving names to the db"}), 404

        except Exception as error:
            current_app.logger.error(
                "Error setting name. Error:{0}".format(error))
            return jsonify({"message": "Error setting name."}), 404

        #TODO: Need to add verification that the save was successful.

    #update solr for reservation
        try:
            if (json_data['stateCd'] in ['RESERVED', 'COND-RESERVE']):
                solr_name = nrd.names[0].name
                solr_docs = []
                nr_doc = {
                    "id":
                    name_request.nrNum,
                    "name":
                    solr_name,
                    "source":
                    "NR",
                    "start_date":
                    name_request.submittedDate.strftime("%Y-%m-%dT%H:%M:00Z")
                }

                solr_docs.append(nr_doc)
                update_solr('possible.conflicts', solr_docs)
        except Exception as error:
            current_app.logger.error(
                "Error updating solr for reservation. Error:{0}".format(error))
            return jsonify({"message":
                            "Error updating solr for reservation."}), 404

        current_app.logger.debug(name_request.json())
        return jsonify(name_request.json()), 200
Exemplo n.º 16
0
def process_delete_from_solr(state_change_msg: dict):  # pylint: disable=too-many-locals, , too-many-branches
    """Process names update via Solr feeder api."""
    logger.debug('names processing: %s', state_change_msg)
    nr_num = state_change_msg.get('nrNum', None)
    nr = RequestDAO.find_by_nr(nr_num)
    send_to_solr_delete(nr)
Exemplo n.º 17
0
def job(app, namex_db, nro_connection, user, max_rows=100):
    """Process the NRs that have been updated in the NamesDB.

    Most updates will go away as NRO (the legacy UI for the NamesDB) is decommissioned.

    The following states allow the following changes:

    - all changes allowed: DRAFT, PENDING_PAYMENT

    - no changes allowed: INPROGRESS, REFUND_REQUESTED, REJECTED, EXPIRED, HISTORICAL, COMPLETED

    - set cancelled state: CANCELLED

    - all changes, except for state: HOLD

    - consumed info only: RESERVED, COND_RESERVE, APPROVED, CONDITIONAL
    """
    row_count = 0

    try:
        ora_con = nro_connection
        # get the NRs from Oracle NamesDB of interest
        result, col_names = job_result_set(ora_con, max_rows)

        for r in result:

            row_count += 1

            row = ora_row_to_dict(col_names, r)

            nr_num = row['nr_num']
            nr = Request.find_by_nr(nr_num)
            action = row['action']

            current_app.logger.debug(
                'processing: {}, NameX state: {}, action: {}'.format(
                    nr_num, None if (not nr) else nr.stateCd, action))

            # NO CHANGES ALLOWED
            if nr and (nr.stateCd in [
                    State.INPROGRESS, State.REFUND_REQUESTED, State.REJECTED,
                    State.EXPIRED, State.HISTORICAL, State.COMPLETED
            ]):
                success = update_feeder_row(
                    ora_con,
                    row_id=row['id'],
                    status='C',
                    send_count=1 + 0 if
                    (row['send_count'] is None) else row['send_count'],
                    error_message='Ignored - Request: not processed')
                ora_con.commit()
                # continue to next row
                current_app.logger.info(
                    'skipping: {}, NameX state: {}, action: {}'.format(
                        nr_num, None if (not nr) else nr.stateCd, action))
                continue

            # ignore existing NRs not in a completed state or draft, or in a completed state and not furnished
            if nr and (
                    nr.stateCd not in State.COMPLETED_STATE + [State.DRAFT] or
                (nr.stateCd in State.COMPLETED_STATE and nr.furnished == 'N')):
                success = update_feeder_row(
                    ora_con,
                    row_id=row['id'],
                    status='C',
                    send_count=1 + 0 if
                    (row['send_count'] is None) else row['send_count'],
                    error_message='Ignored - Request: not processed')
                ora_con.commit()
                continue
            # for any NRs in a completed state or new NRs not existing in NameX
            else:  # pylint: disable=R1724: Unnecessary "else"
                try:
                    # get submitter
                    ora_cursor = ora_con.cursor()
                    nr_header = get_nr_header(ora_cursor, nr_num)
                    nr_submitter = get_nr_submitter(ora_cursor,
                                                    nr_header['request_id'])
                    # get pending payments
                    pending_payments = []
                    if nr:
                        pending_payments = [
                            x for x in nr.payments.all()
                            if x.payment_status_code ==
                            PaymentStatusCode.CREATED.value
                        ]
                    # ignore if:
                    # - NR does not exist and NR originated in namex (handles racetime condition for when it is still in the process of saving)
                    # - NR has a pending update from namex (pending payment)
                    if (not nr and nr_submitter
                            and nr_submitter.get('submitter', '')
                            == 'namex') or (nr and len(pending_payments) > 0):
                        success = update_feeder_row(
                            ora_con,
                            row_id=row['id'],
                            status='C',
                            send_count=1 + 0 if
                            (row['send_count'] is None) else row['send_count'],
                            error_message='Ignored - Request: not processed')
                        ora_con.commit()
                    else:
                        nr = nro.fetch_nro_request_and_copy_to_namex_request(
                            user, nr_number=nr_num, name_request=nr)

                        namex_db.session.add(nr)
                        EventRecorder.record(user,
                                             Event.UPDATE_FROM_NRO,
                                             nr,
                                             nr.json(),
                                             save_to_session=True)
                        current_app.logger.debug(
                            'EventRecorder should have been saved to by now, although not committed'
                        )
                        success = update_feeder_row(
                            ora_con,
                            row_id=row['id'],
                            status='C',
                            send_count=1 + 0 if
                            (row['send_count'] is None) else row['send_count'],
                            error_message=None)

                        if success:
                            ora_con.commit()
                            current_app.logger.debug('Oracle commit done')
                            namex_db.session.commit()
                            current_app.logger.debug('Postgresql commit done')
                        else:
                            raise Exception()

                except Exception as err:
                    current_app.logger.error(err.with_traceback(None))
                    success = update_feeder_row(
                        ora_con,
                        row_id=row['id'],
                        status=row['status'],
                        send_count=1 + 0 if
                        (row['send_count'] is None) else row['send_count'],
                        error_message=err.with_traceback(None))
                    namex_db.session.rollback()
                    ora_con.commit()

        return row_count

    except Exception as err:
        current_app.logger.error('Update Failed:', err.with_traceback(None))
        return -1
Exemplo n.º 18
0
def job(app, namex_db, nro_connection, user, max_rows=100):

    row_count = 0

    try:
        ora_con = nro_connection
        result, col_names = job_result_set(ora_con, max_rows)

        for r in result:

            row_count += 1

            row = ora_row_to_dict(col_names, r)

            nr_num = row['nr_num']
            nr = Request.find_by_nr(nr_num)
            action = row['action']

            current_app.logger.debug('processing: {}, NameX state: {}, action: {}'
                                     .format(
                nr_num,
                None if (not nr) else nr.stateCd,
                action
            ))

            if nr and (nr.stateCd != State.DRAFT):

                # do NOT ignore updates of completed NRs, since those are CONSUME transactions -
                # the only kind that gets into the namex_feeder table for completed NRs
                if nr.stateCd in State.COMPLETED_STATE and action == 'U':
                    pass

                elif action != 'X':
                    success = update_feeder_row(ora_con
                                                ,id=row['id']
                                                ,status='C'
                                                ,send_count=1 + 0 if (row['send_count'] is None) else row['send_count']
                                                , error_message='Ignored - Request: not processed')
                    ora_con.commit()
                    continue
            try:
                nr = nro.fetch_nro_request_and_copy_to_namex_request(user, nr_number=nr_num, name_request=nr)

                namex_db.session.add(nr)
                EventRecorder.record(user, Event.UPDATE_FROM_NRO, nr, {}, save_to_session=True)
                current_app.logger.debug('EventRecorder should have been saved to by now, although not committed')

                success = update_feeder_row(ora_con
                                            , id=row['id']
                                            , status='C'
                                            , send_count=1 + 0 if (row['send_count'] is None) else row['send_count']
                                            , error_message=None)

                if success:
                    ora_con.commit()
                    current_app.logger.debug('Oracle commit done')
                    namex_db.session.commit()
                    current_app.logger.debug('Postgresql commit done')
                else:
                    raise Exception()

            except Exception as err:
                current_app.logger.error(err.with_traceback(None))
                success = update_feeder_row(ora_con
                                            , id=row['id']
                                            , status=row['status']
                                            , send_count=1 + 0 if (row['send_count'] is None) else row['send_count']
                                            , error_message=err.with_traceback(None))
                namex_db.session.rollback()
                ora_con.commit()

        return row_count

    except Exception as err:
        current_app.logger.error('Update Failed:', err.with_traceback(None))
        return -1
Exemplo n.º 19
0
                                "{0}:{1}/{2}".format(Config.ORA_HOST, Config.ORA_PORT, Config.ORA_NAME))

    ora_cursor = ora_con.cursor()

    # get list of NRs that need to be processed
    r_query = db.engine.execute("select nr_num from get_decision_data_table_tracker "
                                "where success is NULL "
                                "FETCH FIRST {} ROWS ONLY".format(max_rows))
    records = r_query.fetchall()

    for nr_num in records:
        nr_num = nr_num[0]
        current_app.logger.info(nr_num)

        # get NR
        nr = Request.find_by_nr(nr_num)

        # if there is no NR in Namex for this NR, log issue in table
        if not nr:
            db.engine.execute("update get_decision_data_table_tracker "
                                          "set success=false, message='{}'"
                                          "where nr_num = '{}'"
                                          .format("NR not found in Namex [1]", nr_num))
            continue

        try:

            # get name data from NRO
            nro_names = get_names_from_nro(ora_cursor, nr_num)

            if not nro_names:
Exemplo n.º 20
0
def job(app, namex_db, nro_connection, user, max_rows=100):

    row_count = 0
    datafix_status = None

    try:
        ora_con = nro_connection
        result, col_names = job_result_set(ora_con, max_rows)

        for r in result:

            row_count += 1
            row = ora_row_to_dict(col_names, r)
            #stuff from the datafix table (from namesp, CPRD)
            nr_num = row['nr_num']
            corp_num = row['corp_num']
            corp_name = row['name']

            #check to see if there are any
            ora_con1 = nro_connection
            name_count_results, col_ni_count = get_name_count(
                ora_con1, corp_num, corp_name)
            ni_count_row = ora_row_to_dict(col_ni_count, name_count_results)

            #no mathcing name instnace rows
            test = ni_count_row['occurence']
            if test[0] == 0:

                #current_app.logger.error(err.with_traceback(None))
                success = update_datafix_row(ora_con,
                                             id=row['id'],
                                             nr_num=nr_num,
                                             status='BAD')
            else:

                skipped = 0
                #check for name_instance corp rows
                ora_con2 = nro_connection
                name_results, col_ni = get_name_instance_rows(
                    ora_con2, corp_num, corp_name)

                for ni in name_results:

                    skipped = skipped + 1
                    ni_row = ora_row_to_dict(col_ni, ni)
                    if ni_row['nr_num'] != nr_num:
                        nr_num = ni_row['nr_num']

                    nr = Request.find_by_nr(nr_num)

                    if (nr is None and skipped < test[0]):
                        continue

                    current_app.logger.debug(
                        'processing: {}, NameX state: {}'.format(
                            nr_num, None if (not nr) else nr.stateCd[0:9]))
                    try:
                        nr = nro.fetch_nro_request_and_copy_to_namex_request(
                            user, nr_number=nr_num, name_request=nr)

                        nr._source = 'NRO'
                        nr.furnished = 'Y'
                        #for ones that are mistakenely set as HISTORICAL, set to APPROVED as this is an active corp

                        namex_db.session.add(nr)
                        EventRecorder.record(user,
                                             Event.UPDATE_FROM_NRO,
                                             nr, {},
                                             save_to_session=True)
                        current_app.logger.debug(
                            'EventRecorder should have been saved to by now, although not committed'
                        )

                        datafix_status = None if (not nr) else nr.stateCd[0:9]

                        success = update_datafix_row(ora_con,
                                                     id=row['id'],
                                                     nr_num=nr_num,
                                                     status=datafix_status)

                        if success:
                            ora_con.commit()
                            current_app.logger.debug('Oracle commit done')
                            namex_db.session.commit()
                            current_app.logger.debug('Postgresql commit done')
                        else:
                            raise Exception()

                    except Exception as err:
                        current_app.logger.error(err.with_traceback(None))
                        success = update_datafix_row(ora_con,
                                                     id=row['id'],
                                                     nr_num=nr_num,
                                                     status='ERROR')
                        namex_db.session.rollback()
                        ora_con.commit()

        return row_count

    except Exception as err:
        current_app.logger.error('Update Failed:', err.with_traceback(None))
        return -1