def furnish_request_message(request: Request, option: str): # pylint: disable=redefined-outer-name """Send notification info to the mail queue.""" current_app.logger.debug( 'Start of the furnishing of request for %s nrNum=%s', option, request.nrNum) payload = create_cloud_event_msg( msg_id=str(uuid.uuid4()), msg_type='bc.registry.names.request', source=f'/requests/{request.nrNum}', time=datetime.utcfromtimestamp( time.time()).replace(tzinfo=timezone.utc).isoformat(), identifier=request.nrNum, json_data_body={'request': { 'nrNum': request.nrNum, 'option': option }}) current_app.logger.debug('About to publish email for %s nrNum=%s', option, request.nrNum) publish_email_message(payload) if option == 'before-expiry': request.notifiedBeforeExpiry = True elif option == 'expired': request.notifiedExpiry = True request.stateCd = State.EXPIRED request.save_to_db()
def test_get_next_oldest(client, jwt, app): # add NR to database from namex.models import Request as RequestDAO, State nr = RequestDAO() nr.nrNum = 'NR 0000001' nr.stateCd = State.DRAFT nr.save_to_db() for i in range(2, 12): nr = RequestDAO() nr.nrNum = 'NR {0:07d}'.format(i) nr.stateCd = State.DRAFT nr.save_to_db() # create JWT & setup header with a Bearer Token using the JWT token = jwt.create_jwt(claims, token_header) headers = {'Authorization': 'Bearer ' + token} # The message expected to be returned json_msg = jsonify(nameRequest='NR 0000001') # get the resource (this is the test) rv = client.get('/api/v1/requests/queues/@me/oldest', headers=headers) assert b'"nameRequest": "NR 0000001"' in rv.data
def test_add_nwpta(app, request, session, pns): # imports for just this test from namex.services.nro.request_utils import add_nwpta # SETUP # create an NR nr = Request() nr.activeUser = User('idir/bob', 'bob', 'last', 'idir', 'localhost') session.add(nr) session.commit() # Test add_nwpta(nr, pns) session.add(nr) session.commit() partners = nr.partnerNS.all() assert len(pns) == len(partners) for partner in partners: partner_found = False for p in pns: if p['partner_jurisdiction_type_cd'] == partner.partnerJurisdictionTypeCd: partner_found = True continue assert partner_found
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
def complete_reapply_payment(self, nr_model: RequestDAO, payment_id: int): """ Invoked when re-applying for an existing Name Request reservation. Extend the Name Request's expiration date by 56 days. If the request action is set to REH or REST, extend the expiration by an additional year (plus the default 56 days). :param nr_model: :param payment_id: :return: """ nr_svc = self.nr_service # Update the state of the payment payment = get_active_payment(nr_model, payment_id) sbc_payment_response = get_payment(payment.payment_token) # TODO: Throw errors if this fails! if sbc_payment_response.statusCode in [ PaymentStatusCode.COMPLETED.value, PaymentStatusCode.APPROVED.value ]: payment.payment_status_code = sbc_payment_response.statusCode payment.payment_completion_date = sbc_payment_response.createdOn payment.save_to_db() if nr_model.submitCount < 3: if nr_model.request_action_cd in [ RequestAction.REH.value, RequestAction.REN.value ]: # If request action is REH or REST extend by 1 year (+ 56 default) days, starting tomorrow nr_model = nr_svc.extend_expiry_date(nr_model, datetime.utcnow(), days=421) nr_model = nr_svc.update_request_submit_count(nr_model) else: # Extend expiry date by (default) 56 days, starting tomorrow nr_model = nr_svc.extend_expiry_date(nr_model, datetime.utcnow(), days=56) nr_model = nr_svc.update_request_submit_count(nr_model) nr_model.save_to_db() else: # TODO: Make a custom exception for this? raise PaymentServiceError( message= 'Submit count maximum of 3 retries has been reached!') # This (optionally) handles the updates for NRO and Solr, if necessary update_solr = False nr_model = self.update_records_in_network_services( nr_model, update_solr) # Update the actions, as things change once the payment is successful self.nr_service.current_state_actions = get_nr_state_actions( nr_model.stateCd, nr_model) # Record the event # EventRecorder.record(nr_svc.user, Event.PATCH + ' [re-apply]', nr_model, nr_svc.request_data) return nr_model
def test_request_add_applicant_existing(app, request, session, applicant1, applicant2): # imports for just this test from namex.models import Applicant from namex.services.nro.request_utils import add_applicant # SETUP # create an NR and add an applicant nr = Request() nr.activeUser = User('idir/bob', 'bob', 'last', 'idir', 'localhost') nr.applicants.append(Applicant(**applicant1)) session.add(nr) session.commit() # Test # Call add_applicant and then assert the new NR applicant matches our data add_applicant(nr, applicant2) session.add(nr) session.commit() appl = nr.applicants.one_or_none() nra = dict_to_json_keys(applicant2) a = appl.as_dict() if a.get('partyId'): a.pop('partyId') # check entire dict assert nra == a
def test_add_names_with_changes(app, request, session, previous_names, test_names): # imports for just this test from namex.services.nro.request_utils import add_names # SETUP # create an NR nr = Request() nr.activeUser = User('idir/bob', 'bob', 'last', 'idir', 'localhost') if previous_names: add_names(nr, previous_names) session.add(nr) session.commit() # Test add_names(nr, test_names) session.add(nr) session.commit() names = nr.names.all() assert len(test_names) == len(names) for name in names: name_found = False decision_data_intact = False for tn in test_names: if tn['name'] == name.name: name_found = True continue assert name_found
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
def test_reopen_event_history(client, jwt, app): from namex.models import Request as RequestDAO, State, Name as NameDAO, User, Event from namex.services import EventRecorder # add a user for the comment user = User('test-user', '', '', '43e6a245-0bf7-4ccf-9bd0-e7fb85fd18cc', 'https://sso-dev.pathfinder.gov.bc.ca/auth/realms/sbc') user.save_to_db() headers = create_header(jwt, [User.EDITOR]) nr = RequestDAO() nr.nrNum = 'NR 0000002' nr.stateCd = State.REJECTED nr.requestId = 1460775 nr._source = 'NRO' name1 = NameDAO() name1.choice = 1 name1.name = 'TEST NAME ONE' nr.names = [name1] nr.save_to_db() EventRecorder.record(user, Event.PATCH, nr, {}) nr.stateCd = State.INPROGRESS EventRecorder.record(user, Event.PUT, nr, { "additional": "additional", "furnished": "N" }) # get the resource (this is the test) rv = client.get('/api/v1/events/NR%200000002', headers=headers) assert rv.status_code == 200 assert b'"user_action": "Re-Open"' in rv.data
def test_nr_state_actions(): nr_model = Request() nr_model.expirationDate = datetime.utcnow() + timedelta(days=3) nr_model.priorityCd = None nr_model.priorityDate = None print('\n Draft state actions \n') print(repr(NameRequestDraftActions.list())) actions = get_nr_state_actions(State.DRAFT, nr_model) print(repr(actions)) print('\n Reserved state actions \n') print(repr(NameRequestReservedActions.list())) actions = get_nr_state_actions(State.RESERVED, nr_model) print(repr(actions)) print('\n Conditionally reserved state actions \n') print(repr(NameRequestReservedActions.list())) actions = get_nr_state_actions(State.COND_RESERVE, nr_model) print(repr(actions)) print('\n Conditional state actions \n') print(repr(NameRequestActiveActions.list())) actions = get_nr_state_actions(State.CONDITIONAL, nr_model) print(repr(actions)) print('\n Approved state actions \n') print(repr(NameRequestActiveActions.list())) actions = get_nr_state_actions(State.APPROVED, nr_model) print(repr(actions)) print('\n In Progress state actions \n') print(repr(NameRequestInProgressActions.list())) actions = get_nr_state_actions(State.INPROGRESS, nr_model) print(repr(actions)) print('\n Hold state actions \n') print(repr(NameRequestHoldActions.list())) actions = get_nr_state_actions(State.HOLD, nr_model) print(repr(actions)) print('\n Historical state actions \n') print(repr(NameRequestHistoricalActions.list())) actions = get_nr_state_actions(State.HISTORICAL, nr_model) print(repr(actions)) print('\n Cancelled state actions \n') print(repr(NameRequestCancelledActions.list())) actions = get_nr_state_actions(State.CANCELLED, nr_model) print(repr(actions)) print('\n Rejected state actions \n') print(repr(NameRequestActiveRejectedActions.list())) actions = get_nr_state_actions(State.REJECTED, nr_model) print(repr(actions))
def create_nr(nr_num: str, request_state: str, names: list, names_state: list): now = datetime.utcnow() with freeze_time(now): name_request = Request() name_request.nrNum = nr_num name_request.stateCd = request_state name_request._source = 'NRO' name_request.expirationDate = add_years(now, 1) name_request.entity_type_cd = 'CR' # name_request.priorityCd = start_priority name_request.save_to_db() for index, name in enumerate(names): name_state = names_state[index] choice = index + 1 name_obj = Name() name_obj.nrId = name_request.id name_obj.name = name name_obj.state = name_state name_obj.choice = choice name_obj.name_type_cd = 'CO' name_obj.save_to_db() return name_request
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
def test_add_new_comment_to_nr(client, jwt, app): from namex.models import Request as RequestDAO, State, Name as NameDAO, Comment as CommentDAO, User, \ Event as EventDAO from sqlalchemy import desc #add a user for the comment user = User('test-user', '', '', '43e6a245-0bf7-4ccf-9bd0-e7fb85fd18cc', 'https://sso-dev.pathfinder.gov.bc.ca/auth/realms/sbc') user.save_to_db() nr = RequestDAO() nr.nrNum = 'NR 0000002' nr.stateCd = State.INPROGRESS nr.requestId = 1460775 name1 = NameDAO() name1.choice = 1 name1.name = 'TEST NAME ONE' nr.names = [name1] nr.save_to_db() comment1 = CommentDAO() comment1.comment = 'This is the first Comment' comment1.nr_id = nr.id comment1.examinerId = nr.userId nr.comments = [comment1] nr.save_to_db() # create JWT & setup header with a Bearer Token using the JWT token = jwt.create_jwt(claims, token_header) headers = { 'Authorization': 'Bearer ' + token, 'content-type': 'application/json' } # get the resource so we have a template for the request: rv = client.get('/api/v1/requests/NR%200000002', headers=headers) assert rv.status_code == 200 # assert we're starting with just one name: data = json.loads(rv.data) assert len(data['comments']) == 1 new_comment = {"comment": "The 13th comment entered by the user."} rv = client.post('/api/v1/requests/NR%200000002/comments', data=json.dumps(new_comment), headers=headers) assert b'"comment": "The 13th comment entered by the user."' in rv.data assert 200 == rv.status_code event_results = EventDAO.query.filter_by(nrId=nr.id).order_by( EventDAO.eventDate.desc()).first_or_404() assert event_results.action == 'post' assert event_results.eventJson[0:11] == '{"comment":'
def complete_reservation_payment(self, nr_model: RequestDAO, payment_id: int): """ Invoked when completing an in-progress Name Request reservation. :param nr_model: :param payment_id: :return: """ nr_svc = self.nr_service # Update the state of the payment payment = get_active_payment(nr_model, payment_id) sbc_payment_response = get_payment(payment.payment_token) # TODO: Throw errors if this fails! if sbc_payment_response.statusCode in [ PaymentStatusCode.COMPLETED.value, PaymentStatusCode.APPROVED.value ]: payment.payment_status_code = sbc_payment_response.statusCode payment.payment_completion_date = sbc_payment_response.createdOn payment.save_to_db() # This handles updates if the NR state is DRAFT, COND_RESERVE or RESERVED # If the state is COND_RESERVE update state to CONDITIONAL # If the state is RESERVED update state to APPROVED # Then update the name request as required if nr_model.stateCd == State.DRAFT: # If the state is DRAFT, leave it as a DRAFT nr_model = self.update_nr(nr_model, State.DRAFT, self.handle_nr_approve) if nr_model.stateCd == State.COND_RESERVE: # If the state is COND_RESERVE update state to CONDITIONAL, and update the name request as required nr_model = self.update_nr(nr_model, State.CONDITIONAL, self.handle_nr_approve) elif nr_model.stateCd == State.RESERVED: # If the state is RESERVED update state to APPROVED, and update the name request as required nr_model = self.update_nr(nr_model, State.APPROVED, self.handle_nr_approve) # Save the name request nr_model.save_to_db() # Record the event EventRecorder.record(nr_svc.user, Event.PATCH + ' [payment completed] RESERVE', nr_model, nr_model.json()) # Update the actions, as things change once the payment is successful self.nr_service.current_state_actions = get_nr_state_actions( nr_model.stateCd, nr_model) return nr_model
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()
def test_comment_where_no_user(client, jwt, app): from namex.models import Request as RequestDAO, State, Name as NameDAO, Comment as CommentDAO, User nr = RequestDAO() nr.nrNum = 'NR 0000002' nr.stateCd = State.INPROGRESS nr.requestId = 1460775 nr._source = 'NRO' name1 = NameDAO() name1.choice = 1 name1.name = 'TEST NAME ONE' nr.names = [name1] nr.save_to_db() # create JWT & setup header with a Bearer Token using the JWT token = jwt.create_jwt(claims, token_header) headers = { 'Authorization': 'Bearer ' + token, 'content-type': 'application/json' } new_comment = {"comment": "The 13th comment entered by the user."} rv = client.post('/api/v1/requests/NR%200000002/comments', data=json.dumps(new_comment), headers=headers) assert 404 == rv.status_code
def test_get_inprogress_event_history(client, jwt, app): from namex.models import Request as RequestDAO, State, Name as NameDAO, User, Event from namex.services import EventRecorder # add a user for the comment user = User('test-user', '', '', '43e6a245-0bf7-4ccf-9bd0-e7fb85fd18cc', 'https://sso-dev.pathfinder.gov.bc.ca/auth/realms/sbc') user.save_to_db() # create JWT & setup header with a Bearer Token using the JWT token = jwt.create_jwt(claims, token_header) headers = { 'Authorization': 'Bearer ' + token, 'content-type': 'application/json' } nr = RequestDAO() nr.nrNum = 'NR 0000002' nr.stateCd = State.INPROGRESS nr.requestId = 1460775 nr._source = 'NRO' name1 = NameDAO() name1.choice = 1 name1.name = 'TEST NAME ONE' nr.names = [name1] nr.save_to_db() EventRecorder.record(user, Event.PATCH, nr, {}) # get the resource (this is the test) rv = client.get('/api/v1/events/NR%200000002', headers=headers) assert rv.status_code == 200 assert b'"user_action": "Load NR"' in rv.data
def test_get_expiry_days(client, test_name, days, action_cd, request_type): """ Test that get_expiry_date method returns a either 56 or 421 days """ mock_nr = RequestDAO() # Set defaults, if these exist in the provided data they will be overwritten mock_nr.stateCd = State.APPROVED mock_nr.request_action_cd = action_cd mock_nr.requestTypeCd = request_type mock_nr.expirationDate = None mock_expiry_days = int(nr_svc.get_expiry_days(mock_nr)) assert mock_expiry_days == days
def complete_upgrade_payment(self, nr_model: RequestDAO, payment_id: int): """ Invoked when upgrading an existing Name Request reservation to PRIORITY status. :param nr_model: :param payment_id: :return: """ nr_svc = self.nr_service if nr_model.stateCd not in [State.DRAFT, State.PENDING_PAYMENT]: raise PaymentServiceError( message= 'Error upgrading Name Request, request is in an invalid state!' ) # Update the state of the payment payment = get_active_payment(nr_model, payment_id) sbc_payment_response = get_payment(payment.payment_token) # TODO: Throw errors if this fails! if sbc_payment_response.statusCode in [ PaymentStatusCode.COMPLETED.value, PaymentStatusCode.APPROVED.value ]: payment.payment_status_code = sbc_payment_response.statusCode payment.payment_completion_date = sbc_payment_response.createdOn payment.save_to_db() nr_model.priorityCd = 'Y' nr_model.priorityDate = datetime.utcnow() # Save the name request nr_model.save_to_db() # This (optionally) handles the updates for NRO and Solr, if necessary update_solr = False nr_model = self.update_records_in_network_services( nr_model, update_solr) # Update the actions, as things change once the payment is successful self.nr_service.current_state_actions = get_nr_state_actions( nr_model.stateCd, nr_model) # Record the event EventRecorder.record(nr_svc.user, Event.PATCH + ' [payment completed] UPGRADE', nr_model, nr_model.json()) return nr_model
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
def create_name_request(self): """ # !Important! All new name requests should be initially set to the DRAFT state. # Use apply_state_change on the name_request to transition to any other state. :return: """ try: name_request = Request() self.generate_nr_keys() name_request.stateCd = State.DRAFT except Exception as err: raise CreateNameRequestError(err) return name_request
def test_has_consumed_name(): """Assert has_consumed_name.""" from namex.models import Name, Request as RequestDAO, State name = Name() name.choice = 1 name.name = 'TEST' name.state = 'APPROVED' nr = RequestDAO() nr.nrNum = 'NR 0000001' nr.stateCd = State.CONSUMED nr.names.append(name) nr.save_to_db() assert nr.has_consumed_name is True
def request_refund(self, nr_model: RequestDAO, payment_id: int): """ Processes a SINGLE refund request. This is different from the 'refund' in the NameRequest resource PATCH namerequests/{nrId}/REQUEST_REFUND which cancels the NR and refunds any associated payments. :param nr_model: :param payment_id: :return: """ # Handle the payments valid_states = [ PaymentState.APPROVED.value, PaymentState.COMPLETED.value, PaymentState.PARTIAL.value ] if nr_model.stateCd not in [State.DRAFT]: raise PaymentServiceError( message='Invalid NR state for cancel and refund') # Cancel any payments associated with the NR for payment in nr_model.payments.all(): if payment.payment_status_code in valid_states and payment.id == payment_id: # refund_payment(payment.payment_token, {'reason': 'Name Request user requested refund'}) refund_payment(payment.payment_token, {}) payment.payment_status_code = PaymentState.REFUND_REQUESTED.value payment.save_to_db() nr_svc = self.nr_service EventRecorder.record( nr_svc.user, Event.PATCH + f' [payment refunded] {payment.payment_action}', nr_model, nr_model.json()) return nr_model
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
def update_nr_name_search(mapper, connection, target): """Add any changes to the name to the request.nameSearch column.""" from namex.models import Request name = target nr = Request.find_by_id(name.nrId) if nr: # get the names associated with the NR names_q = connection.execute( f""" SELECT names.name from names JOIN requests on requests.id = names.nr_id WHERE requests.id={nr.id} """ ) # format the names into a string like: |1<name1>|2<name2>|3<name3> names = [x[0] for x in names_q.all()] name_search = '' for item, index in zip(names, range(len(names))): name_search += f'|{index + 1}{item}{index + 1}|' # update the name_search field of the nr with the formatted string connection.execute( """ UPDATE requests SET name_search=%s WHERE id=%s """, ('(' + name_search + ')', nr.id) )
def get(): if not (jwt.requires_roles(User.EDITOR) or jwt.requires_roles(User.APPROVER)): return jsonify({ "message": "Error: You do not have access to the Name Request queue." }), 403 try: user = User.find_by_jwtToken(g.jwt_oidc_token_info) current_app.logger.debug('find user') if not user: user = User.create_from_jwtToken(g.jwt_oidc_token_info) nr = RequestDAO.get_queued_oldest(user) except SQLAlchemyError as err: # TODO should put some span trace on the error message current_app.logger.error(err.with_traceback(None)) return jsonify({ 'message': 'An error occurred getting the next Name Request.' }), 500 except AttributeError as err: current_app.logger.error(err) return jsonify( {'message': 'There are no Name Requests to work on.'}), 404 return jsonify(nameRequest='{}'.format(nr)), 200
def test_add_nr_header_set_state(state_type_cd, nr_names, expected): from namex.services.nro.request_utils import add_names, add_nr_header # the correct state for a Request that is completed in NRO is determined by the Name states nr = Request() user = User('idir/bob', 'bob', 'last', 'idir', 'localhost') nr_submitter = None nr_header = { 'priority_cd': 'N', 'state_type_cd': state_type_cd, 'nr_num': 'NR 0000001', 'request_id': 1, 'previous_request_id': None, 'submit_count': 0, 'request_type_cd': 'REQ', 'expiration_date': None, 'additional_info': None, 'nature_business_info': 'N/A', 'xpro_jurisdiction': None, 'submitted_date': EPOCH_DATETIME, 'last_update': EPOCH_DATETIME } add_nr_header(nr, nr_header, nr_submitter, user) add_names(nr, nr_names) assert nr.stateCd == expected
def test_add_nr_header_with_priority(priority_cd, expected): from namex.services.nro.request_utils import add_nr_header nr = Request() user = User('idir/bob', 'bob', 'last', 'idir', 'localhost') nr_submitter = None nr_header = { 'priority_cd': priority_cd, 'state_type_cd': 'H', 'nr_num': 'NR 0000001', 'request_id': 1, 'previous_request_id': None, 'submit_count': 0, 'request_type_cd': 'REQ', 'expiration_date': None, 'additional_info': None, 'nature_business_info': 'N/A', 'xpro_jurisdiction': None, 'submitted_date': EPOCH_DATETIME, 'last_update': EPOCH_DATETIME } add_nr_header(nr, nr_header, nr_submitter, user) assert nr.priorityCd == expected
def get_statistics(cls): # For now not using this to improve performance # response_values = [cls.get_approved_names_counter(), # cls.get_waiting_time_priority_queue(unit=UnitTime.HR.value), # cls.get_waiting_time_regular_queue(unit=UnitTime.DAY.value)] oldest_draft = Request.get_oldest_draft() todays_date = get_utc_now().date() submitted_date = oldest_draft.submittedDate.date() # note that busday_count does not count the end date provided delta = np.busday_count(submitted_date, todays_date) delta = int(delta) # add one to waiting time to account for specific scenarios if np.is_busday(todays_date) or delta == 0: delta += 1 response_values = [ 0, 0, #cls.get_waiting_time_priority_queue(unit=UnitTime.HR.value), delta ] response = query_result_to_dict(response_keys, response_values) return response
def handle_patch_rollback(self, nr_model: Request, action: str): """ Roll back the Name Request. :param nr_model: :param action: :return: """ nr_svc = self.nr_service # This handles updates if the NR state is 'patchable' nr_model = self.update_nr(nr_model, State.CANCELLED, self.handle_nr_patch) # Only update the record in NRO if it's a real NR, otherwise the record won't exist if not is_temp_nr_num(nr_model.nrNum): # This handles the updates for NRO and Solr, if necessary # self.update_records_in_network_services(nr_model, update_solr=True) nr_model = self.update_request_in_nro(nr_model, self.save_nr) # Delete in solr for temp or real NR because it is cancelled if nr_model.entity_type_cd in [ 'CR', 'UL', 'BC', 'CP', 'PA', 'XCR', 'XUL', 'XCP', 'CC', 'FI', 'XCR', 'XUL', 'XCP' ]: SOLR_CORE = 'possible.conflicts' self.delete_solr_doc(SOLR_CORE, nr_model.nrNum) # Record the event EventRecorder.record(nr_svc.user, Event.PATCH + ' [rollback]', nr_model, nr_model.json()) return nr_model