async def process_name_events(event_message: Dict[(str, any)]):
    "Process name events.\n\n    1. Check if the NR already exists in entities table, if yes apply changes. If not create entity record.\n    2. Check if new status is DRAFT, if yes call pay-api and get the account details for the payments against the NR.\n    3. If an account is found, affiliate to that account.\n\n    Args:\n        event_message (object): cloud event message, sample below.\n            {\n                'specversion': '1.0.1',\n                'type': 'bc.registry.names.events',\n                'source': '/requests/6724165',\n                'id': id,\n                'time': '',\n                'datacontenttype': 'application/json',\n                'identifier': '781020202',\n                'data': {\n                    'request': {\n                        'nrNum': 'NR 5659951',\n                        'newState': 'APPROVED',\n                        'previousState': 'DRAFT'\n                    }\n                }\n            }\n    "
    logger.debug('>>>>>>>process_name_events>>>>>')
    request_data = event_message.get('data').get('request')
    nr_number = request_data['nrNum']
    nr_status = request_data['newState']
    nr_entity = EntityModel.find_by_business_identifier(nr_number)
    if (nr_entity is None):
        logger.info('Entity doesnt exist, creating a new entity.')
        nr_entity = EntityModel(business_identifier=nr_number, corp_type_code=CorpType.NR.value)
    nr_entity.status = nr_status
    nr_entity.name = request_data.get('name', '')
    nr_entity.last_modified_by = None
    nr_entity.last_modified = parser.parse(event_message.get('time'))
    if ((nr_status == 'DRAFT') and (AffiliationModel.find_affiliations_by_business_identifier(nr_number) is None)):
        logger.info('Status is DRAFT, getting invoices for account')
        invoices = RestService.get(f'{APP_CONFIG.PAY_API_URL}/payment-requests?businessIdentifier={nr_number}', token=RestService.get_service_account_token()).json()
        if (invoices and (auth_account_id := invoices['invoices'][0].get('paymentAccount').get('accountId')) and str(auth_account_id).isnumeric()):
            logger.info('Account ID received : %s', auth_account_id)
            org: OrgModel = db.session.query(OrgModel).filter((OrgModel.id == auth_account_id)).one_or_none()
            if org:
                nr_entity.pass_code_claimed = True
                logger.info('Creating affiliation between Entity : %s and Org : %s', nr_entity, org)
                affiliation: AffiliationModel = AffiliationModel(entity=nr_entity, org=org)
                affiliation.flush()
    nr_entity.save()
    logger.debug('<<<<<<<process_name_events<<<<<<<<<<')
Process name events.

1. Check if the NR already exists in entities table, if yes apply changes. If not create entity record.
2. Check if new status is DRAFT, if yes call pay-api and get the account details for the payments against the NR.
3. If an account is found, affiliate to that account.

Args:
    event_message (object): cloud event message, sample below.
        {
            'specversion': '1.0.1',
            'type': 'bc.registry.names.events',
            'source': '/requests/6724165',
            'id': id,
            'time': '',
            'datacontenttype': 'application/json',
            'identifier': '781020202',
            'data': {
                'request': {
                    'nrNum': 'NR 5659951',
                    'newState': 'APPROVED',
                    'previousState': 'DRAFT'
                }
            }
        }
async def process_name_events(event_message: Dict[(str, any)]):
    "Process name events.\n\n    1. Check if the NR already exists in entities table, if yes apply changes. If not create entity record.\n    2. Check if new status is DRAFT, if yes call pay-api and get the account details for the payments against the NR.\n    3. If an account is found, affiliate to that account.\n\n    Args:\n        event_message (object): cloud event message, sample below.\n            {\n                'specversion': '1.0.1',\n                'type': 'bc.registry.names.events',\n                'source': '/requests/6724165',\n                'id': id,\n                'time': ,\n                'datacontenttype': 'application/json',\n                'identifier': '781020202',\n                'data': {\n                    'request': {\n                        'nrNum': 'NR 5659951',\n                        'newState': 'APPROVED',\n                        'previousState': 'DRAFT'\n                    }\n                }\n            }\n    "
    logger.debug('>>>>>>>process_name_events>>>>>')
    request_data = event_message.get('data').get('request')
    nr_number = request_data['nrNum']
    nr_status = request_data['newState']
    nr_entity = EntityModel.find_by_business_identifier(nr_number)
    if (nr_entity is None):
        logger.info('Entity doesnt exist, creating a new entity.')
        nr_entity = EntityModel(business_identifier=nr_number, corp_type_code=CorpType.NR.value)
    nr_entity.status = nr_status
    nr_entity.name = request_data.get('name', )
    nr_entity.last_modified_by = None
    nr_entity.last_modified = parser.parse(event_message.get('time'))
    if ((nr_status == 'DRAFT') and (AffiliationModel.find_affiliations_by_business_identifier(nr_number) is None)):
        logger.info('Status is DRAFT, getting invoices for account')
        invoices = RestService.get(f'{APP_CONFIG.PAY_API_URL}/payment-requests?businessIdentifier={nr_number}', token=RestService.get_service_account_token()).json()
        if (invoices and (auth_account_id := invoices['invoices'][0].get('paymentAccount').get('accountId')) and str(auth_account_id).isnumeric()):
            logger.info('Account ID received : %s', auth_account_id)
            org: OrgModel = db.session.query(OrgModel).filter((OrgModel.id == auth_account_id)).one_or_none()
            if org:
                nr_entity.pass_code_claimed = True
                logger.info('Creating affiliation between Entity : %s and Org : %s', nr_entity, org)
                affiliation: AffiliationModel = AffiliationModel(entity=nr_entity, org=org)
                affiliation.flush()
    nr_entity.save()
    logger.debug('<<<<<<<process_name_events<<<<<<<<<<')