示例#1
0
def test_custom_display_text(test_client, init_database, menu_name, language, expecteds,
                             real_at_idx, length, unexpected, menu_nr):
    with db.session.no_autoflush:
        start_state = UssdSessionFactory(state="menu_name")

        mapping = fake_transfer_mapping(length)
        real_usage1 = TransferUsage.find_or_create('Education')
        real_usage1.translations = {'en': 'Education', 'sw': 'Elimu'}
        real_usage2 = TransferUsage.find_or_create('Health')
        real_usage2.translations = {'en': 'Health', 'sw': 'Afya'}
        mapping[real_at_idx] = KenyaUssdStateMachine.make_usage_mapping(real_usage1)
        mapping[real_at_idx + 1] = KenyaUssdStateMachine.make_usage_mapping(real_usage2)
        user = standard_user()
        user.preferred_language = language

        start_state.session_data = {
            'transfer_usage_mapping': mapping,
            'usage_menu': menu_nr,
            'usage_index_stack': [0, 8]
        }

        start_state.user = user

        menu = UssdMenu(name=menu_name, display_key="ussd.kenya.{}".format(menu_name))
        resulting_menu = KenyaUssdProcessor.custom_display_text(
            menu, start_state)

        for expected in expecteds:
            assert expected in resulting_menu
        if unexpected is not None:
            assert unexpected not in resulting_menu
def fake_transfer_mapping(length: int):
    mapping = []
    transfer_usage = TransferUsage.find_or_create("Food")
    db.session.commit()
    for i in range(length):
        mapping.append(KenyaUssdStateMachine.make_usage_mapping(transfer_usage))

    return mapping
def test_send_token(mocker, test_client, init_database,
                    initialised_blockchain_network, lang, mock_sms_apis,
                    token1_symbol, token2_symbol, recipient_balance,
                    expected_send_msg, expected_receive_msg):

    org = OrganisationFactory(country_code='KE')
    sender = UserFactory(preferred_language=lang,
                         phone=phone(),
                         first_name="Bob",
                         last_name="Foo",
                         default_organisation=org)
    token1 = Token.query.filter_by(symbol=token1_symbol).first()
    create_transfer_account_for_user(sender, token1, 20000)

    recipient = UserFactory(preferred_language=lang,
                            phone=phone(),
                            first_name="Joe",
                            last_name="Bar",
                            default_organisation=org)
    token2 = Token.query.filter_by(symbol=token2_symbol).first()
    create_transfer_account_for_user(recipient, token2, 30000)

    def mock_convert(exchange_contract, from_token, to_token, from_amount,
                     signing_address):
        if from_token.symbol == "SM1":
            return from_amount * 1.5
        else:
            return from_amount * 0.75

    mocker.patch('server.bt.get_conversion_amount', mock_convert)

    TransferUsage.find_or_create("Alf DVDs")
    TokenProcessor.send_token(sender, recipient, 1000, "1", 1)
    assert default_transfer_account(sender).balance == 19000
    assert default_transfer_account(recipient).balance == recipient_balance

    messages = mock_sms_apis

    assert len(messages) == 2
    sent_message = messages[0]
    assert sent_message['phone'] == sender.phone
    assert expected_send_msg in sent_message['message']
    received_message = messages[1]
    assert received_message['phone'] == recipient.phone
    assert expected_receive_msg in received_message['message']
def test_send_directory_listing(mocker, test_client, init_database):
    session = UssdSessionFactory(state="directory_listing")
    session.session_data = {'transfer_usage_mapping': fake_transfer_mapping(6), 'usage_menu': 0}
    user = standard_user()
    user.phone = phone()
    state_machine = KenyaUssdStateMachine(session, user)
    transfer_usage = TransferUsage.find_or_create("Food")

    send_directory_listing = mocker.MagicMock()
    mocker.patch('server.ussd_tasker.send_directory_listing', send_directory_listing)

    state_machine.feed_char('2')
    assert state_machine.state == 'complete'
    send_directory_listing.assert_called_with(user, transfer_usage)
示例#5
0
def proccess_create_or_modify_user_request(attribute_dict,
                                           organisation=None,
                                           allow_existing_user_modify=False,
                                           is_self_sign_up=False,
                                           modify_only=False):
    """
    Takes a create or modify user request and determines the response. Normally what's in the top level API function,
    but here it's one layer down because there's multiple entry points for 'create user':
    - The admin api
    - The register api

    :param attribute_dict: attributes that can be supplied by the request maker
    :param organisation:  what organisation the request maker belongs to. The created user is bound to the same org
    :param allow_existing_user_modify: whether to return an error when the user already exists for the supplied IDs
    :param is_self_sign_up: does the request come from the register api?
    :param modify_only: whether to allow the creation of a  new user
    :return: An http response
    """

    if not attribute_dict.get('custom_attributes'):
        attribute_dict['custom_attributes'] = {}

    user_id = attribute_dict.get('user_id')

    email = attribute_dict.get('email')
    phone = attribute_dict.get('phone')

    account_types = attribute_dict.get('account_types', [])
    if isinstance(account_types, str):
        account_types = account_types.split(',')

    referred_by = attribute_dict.get('referred_by')

    blockchain_address = attribute_dict.get('blockchain_address')

    provided_public_serial_number = attribute_dict.get('public_serial_number')

    uuid = attribute_dict.get('uuid')

    require_identifier = attribute_dict.get('require_identifier', True)

    if not user_id:
        # Extract ID from Combined User ID and Name String if it exists
        try:
            user_id_name_string = attribute_dict.get('user_id_name_string')

            user_id_str = user_id_name_string and user_id_name_string.split(
                ':')[0]

            if user_id_str:
                user_id = int(user_id_str)

        except SyntaxError:
            pass

    if not blockchain_address and provided_public_serial_number:

        try:
            blockchain_address = to_checksum_address(
                provided_public_serial_number)

            # Since it's actually an ethereum address set the provided public serial number to None
            # so it doesn't get used as a transfer card
            provided_public_serial_number = None
        except Exception:
            pass

    require_transfer_card_exists = attribute_dict.get(
        'require_transfer_card_exists',
        g.active_organisation.require_transfer_card)

    public_serial_number = (provided_public_serial_number
                            or attribute_dict.get('payment_card_qr_code')
                            or attribute_dict.get('payment_card_barcode'))

    location = attribute_dict.get('location')  # address location

    # Yes, we know "GPS" refers to a technology, but "gps_location" is less ambiguous for end users than "geo_location"
    gps_location = attribute_dict.get(
        'gps_location')  # geo location as str of lat, lng

    use_precreated_pin = attribute_dict.get('use_precreated_pin')
    use_last_4_digits_of_id_as_initial_pin = attribute_dict.get(
        'use_last_4_digits_of_id_as_initial_pin')

    transfer_account_name = attribute_dict.get('transfer_account_name')
    first_name = attribute_dict.get('first_name')
    last_name = attribute_dict.get('last_name')

    business_usage_name = attribute_dict.get('business_usage_name')
    business_usage_id = None
    if business_usage_name:
        usage = TransferUsage.find_or_create(business_usage_name)
        business_usage_id = usage.id

    preferred_language = attribute_dict.get('preferred_language')

    primary_user_identifier = attribute_dict.get('primary_user_identifier')
    primary_user_pin = attribute_dict.get('primary_user_pin')

    initial_disbursement = attribute_dict.get('initial_disbursement', None)
    if not account_types:
        account_types = ['beneficiary']
    roles_to_set = []
    for at in account_types:
        if at not in g.active_organisation.valid_roles:
            raise Exception(
                f'{at} not a valid role for this organisation. Please choose one of the following: {g.active_organisation.valid_roles}'
            )
        roles_to_set.append((ASSIGNABLE_TIERS[at], at))

    chain = get_chain()
    if current_app.config['CHAINS'][chain]['IS_USING_BITCOIN']:
        try:
            base58.b58decode_check(blockchain_address)
        except ValueError:
            response_object = {
                'message':
                'Blockchain Address {} Not Valid'.format(blockchain_address)
            }
            return response_object, 400

    if isinstance(phone, bool):
        phone = None

    if phone and not is_self_sign_up:
        # phone has already been parsed if self sign up
        try:
            phone = proccess_phone_number(phone)
        except NumberParseException as e:
            response_object = {'message': 'Invalid Phone Number: ' + str(e)}
            return response_object, 400

    # Work out if there's an existing transfer account to bind to
    existing_transfer_account = None
    if primary_user_identifier:
        primary_user, _ = find_user_from_public_identifier(
            primary_user_identifier)

        if not primary_user or not primary_user.verify_password(
                primary_user_pin):
            response_object = {'message': 'Primary User not Found'}
            return response_object, 400

        if not primary_user.verify_password(primary_user_pin):
            response_object = {'message': 'Invalid PIN for Primary User'}
            return response_object, 400

        primary_user_transfer_account = primary_user.transfer_account

        if not primary_user_transfer_account:
            response_object = {
                'message': 'Primary User has no transfer account'
            }
            return response_object, 400

    if not (phone or email or public_serial_number or blockchain_address
            or user_id or uuid or not require_identifier):
        response_object = {'message': 'Must provide a unique identifier'}
        return response_object, 400

    if use_precreated_pin and not public_serial_number:
        response_object = {
            'message':
            'Must provide public serial number to use a transfer card or pre-created pin'
        }
        return response_object, 400

    if public_serial_number:
        public_serial_number = str(public_serial_number)

        if use_precreated_pin or require_transfer_card_exists:
            transfer_card = TransferCard.query.filter_by(
                public_serial_number=public_serial_number).first()

            if not transfer_card:
                response_object = {'message': 'Transfer card not found'}
                return response_object, 400

    business_usage = None
    if business_usage_id:
        business_usage = TransferUsage.query.get(business_usage_id)
        if not business_usage:
            response_object = {
                'message':
                f'Business Usage not found for id {business_usage_id}'
            }
            return response_object, 400

    referred_by_user, _ = find_user_from_public_identifier(referred_by)

    if referred_by and not referred_by_user:
        response_object = {
            'message':
            f'Referrer user not found for public identifier {referred_by}'
        }
        return response_object, 400

    existing_user, _ = find_user_from_public_identifier(
        email, phone, public_serial_number, blockchain_address, uuid)

    if not existing_user and user_id:
        existing_user = User.query.get(user_id)

    if modify_only and existing_user is None:
        response_object = {'message': 'User not found'}
        return response_object, 404

    if existing_user:
        if not allow_existing_user_modify:
            response_object = {'message': 'User already exists for Identifier'}
            return response_object, 400

        try:

            user = update_transfer_account_user(
                existing_user,
                first_name=first_name,
                last_name=last_name,
                preferred_language=preferred_language,
                phone=phone,
                email=email,
                public_serial_number=public_serial_number,
                use_precreated_pin=use_precreated_pin,
                existing_transfer_account=existing_transfer_account,
                roles=roles_to_set,
                business_usage=business_usage)

            set_location_conditionally(user, location, gps_location)

            if referred_by_user:
                user.referred_by.clear(
                )  # otherwise prior referrals will remain...
                user.referred_by.append(referred_by_user)

            set_custom_attributes(attribute_dict, user)
            flag_modified(user, "custom_attributes")

            db.session.commit()

            response_object = {
                'message': 'User Updated',
                'data': {
                    'user': user_schema.dump(user).data
                }
            }

            return response_object, 200

        except Exception as e:
            response_object = {'message': str(e)}

            return response_object, 400

    user = create_transfer_account_user(
        first_name=first_name,
        last_name=last_name,
        preferred_language=preferred_language,
        phone=phone,
        email=email,
        public_serial_number=public_serial_number,
        uuid=uuid,
        organisation=organisation,
        blockchain_address=blockchain_address,
        transfer_account_name=transfer_account_name,
        use_precreated_pin=use_precreated_pin,
        use_last_4_digits_of_id_as_initial_pin=
        use_last_4_digits_of_id_as_initial_pin,
        existing_transfer_account=existing_transfer_account,
        roles=roles_to_set,
        is_self_sign_up=is_self_sign_up,
        business_usage=business_usage,
        initial_disbursement=initial_disbursement)

    set_location_conditionally(user, location, gps_location)

    if referred_by_user:
        user.referred_by.append(referred_by_user)

    if attribute_dict.get('gender'):
        attribute_dict['custom_attributes']['gender'] = attribute_dict.get(
            'gender')

    if attribute_dict.get('bio'):
        attribute_dict['custom_attributes']['bio'] = attribute_dict.get('bio')

    set_custom_attributes(attribute_dict, user)

    if is_self_sign_up and attribute_dict.get('deviceInfo', None) is not None:
        save_device_info(device_info=attribute_dict.get('deviceInfo'),
                         user=user)
    send_onboarding_sms_messages(user)
    # Location fires an async task that needs to know user ID
    db.session.flush()

    if phone:
        if is_self_sign_up:
            send_one_time_code(phone=phone, user=user)
            return {
                'message': 'User Created. Please verify phone number.',
                'otp_verify': True
            }, 200

        elif current_app.config['ONBOARDING_SMS']:
            try:
                send_onboarding_sms_messages(user)
            except Exception as e:
                print(e)
                sentry_sdk.capture_exception(e)
                pass

    response_object = {
        'message': 'User Created',
        'data': {
            'user': user_schema.dump(user).data
        }
    }

    return response_object, 200
示例#6
0
def generate_timeseries_metrics(create_organisation):
    # Generates metrics over timeline
    # User1 and User2 made today
    user1 = create_transfer_account_user(first_name='Ricky',
                                         phone="+19025551234",
                                         organisation=create_organisation,
                                         roles=[('BENEFICIARY', 'beneficiary')
                                                ])
    user1.created = zero_time
    user1.default_transfer_account.is_approved = True
    user1.default_transfer_account._make_initial_disbursement(100, True)
    user1._location = 'Sunnyvale'
    attribute_dict = {'custom_attributes': {}}
    attribute_dict['custom_attributes']['colour'] = 'red'
    attribute_dict['custom_attributes']['chips'] = 'Dressed All Over'
    set_custom_attributes(attribute_dict, user1)
    user1.lat = 44.675447
    user1.lng = -63.594995

    user2 = create_transfer_account_user(first_name='Bubbles',
                                         phone="+19025551235",
                                         organisation=create_organisation)
    user2.created = zero_time
    user2.default_transfer_account.is_approved = True
    user2.default_transfer_account._make_initial_disbursement(200, True)
    user2._location = 'Sunnyvale'
    attribute_dict = {'custom_attributes': {}}
    attribute_dict['custom_attributes']['colour'] = 'red'
    attribute_dict['custom_attributes']['chips'] = 'Chicken'
    user2.lat = 44.675447
    user2.lng = -63.594995

    set_custom_attributes(attribute_dict, user2)

    # user3 made yesterday
    user3 = create_transfer_account_user(first_name='Julian',
                                         phone="+19025531230",
                                         organisation=create_organisation)
    user3.default_transfer_account.is_approved = True
    disburse = user3.default_transfer_account._make_initial_disbursement(
        210, True)
    user3.created = zero_time
    user3.created = user3.created - timedelta(days=1) + timedelta(hours=6)
    disburse.created = user3.created - timedelta(days=1) + timedelta(hours=3)
    user3._location = 'Dartmouth'
    attribute_dict = {'custom_attributes': {}}
    attribute_dict['custom_attributes']['colour'] = 'blue'
    attribute_dict['custom_attributes']['chips'] = 'Barbecue'
    set_custom_attributes(attribute_dict, user3)
    user3.lat = 44.668055
    user3.lng = -63.580829

    # user4 made 4 days ago
    user4 = create_transfer_account_user(first_name='Randy',
                                         phone="+19025511230",
                                         organisation=create_organisation)
    user4.created = zero_time
    user4.default_transfer_account.is_approved = True
    disburse = user4.default_transfer_account._make_initial_disbursement(
        201, True)
    user4.created = user4.created - timedelta(days=4) + timedelta(hours=23)
    disburse.created = user4.created - timedelta(days=4) + timedelta(hours=1)
    user4._location = 'Lower Sackville'
    attribute_dict = {'custom_attributes': {}}
    attribute_dict['custom_attributes']['colour'] = 'blue'
    attribute_dict['custom_attributes']['chips'] = 'Barbecue'
    set_custom_attributes(attribute_dict, user4)
    user4.lat = 44.770061
    user4.lng = -63.692723

    # user5/user6 made 10 days ago
    user5 = create_transfer_account_user(first_name='Cory',
                                         phone="+19011531230",
                                         organisation=create_organisation)
    user5.created = zero_time
    user5.default_transfer_account.is_approved = True
    disburse = user5.default_transfer_account._make_initial_disbursement(
        202, True)
    user5.created = user5.created - timedelta(days=10) + timedelta(hours=2)
    disburse.created = user5.created - timedelta(days=10) + timedelta(hours=5)
    user5._location = 'Truro'
    attribute_dict = {'custom_attributes': {}}
    attribute_dict['custom_attributes']['colour'] = 'green'
    attribute_dict['custom_attributes']['chips'] = 'Dressed All Over'
    set_custom_attributes(attribute_dict, user5)
    user5.lat = 45.368075
    user5.lng = -63.256207

    user6 = create_transfer_account_user(first_name='Trevor',
                                         phone="+19025111230",
                                         organisation=create_organisation)
    user6.created = zero_time
    user6.default_transfer_account.is_approved = True
    disburse = user6.default_transfer_account._make_initial_disbursement(
        204, True)
    attribute_dict = {'custom_attributes': {}}
    attribute_dict['custom_attributes']['colour'] = 'red'
    attribute_dict['custom_attributes']['chips'] = 'Jalapeno'
    set_custom_attributes(attribute_dict, user6)
    user6.lat = 44.368363
    user6.lng = -64.526330

    db.session.commit()
    tu1 = TransferUsage.find_or_create("Pepperoni")
    tu2 = TransferUsage.find_or_create("Jalepeno Chips")
    tu3 = TransferUsage.find_or_create("Shopping Carts")
    tu1.created = zero_time - timedelta(days=15) + timedelta(hours=22)
    tu2.created = zero_time - timedelta(days=15) + timedelta(hours=2)
    tu3.created = zero_time - timedelta(days=15) + timedelta(hours=1)

    p1 = make_payment_transfer(
        100,
        create_organisation.token,
        send_user=user1,
        send_transfer_account=user1.default_transfer_account,
        receive_user=user2,
        receive_transfer_account=user2.default_transfer_account,
        transfer_use=str(int(tu1.id)))
    p1.created = zero_time + timedelta(hours=3)

    p2 = make_payment_transfer(
        25,
        create_organisation.token,
        send_user=user3,
        send_transfer_account=user3.default_transfer_account,
        receive_user=user4,
        receive_transfer_account=user4.default_transfer_account,
        transfer_use=str(int(tu1.id)))
    p2.created = zero_time
    p2.created = p2.created - timedelta(days=1) + timedelta(hours=7)

    p3 = make_payment_transfer(
        5,
        create_organisation.token,
        send_user=user4,
        send_transfer_account=user4.default_transfer_account,
        receive_user=user2,
        receive_transfer_account=user2.default_transfer_account,
        transfer_use=str(int(tu2.id)))
    p3.created = zero_time
    p3.created = p3.created - timedelta(days=1) + timedelta(hours=22)

    p4 = make_payment_transfer(
        20,
        create_organisation.token,
        send_user=user5,
        send_transfer_account=user5.default_transfer_account,
        receive_user=user6,
        receive_transfer_account=user6.default_transfer_account,
        transfer_use=str(int(tu3.id)))
    p4.created = zero_time
    p4.created = p4.created - timedelta(days=4) + timedelta(hours=1)

    p5 = make_payment_transfer(
        20,
        create_organisation.token,
        send_user=user6,
        send_transfer_account=user6.default_transfer_account,
        receive_user=user5,
        receive_transfer_account=user5.default_transfer_account,
        transfer_use=str(int(tu2.id)))
    p5.created = zero_time
    p5.created = p5.created - timedelta(days=6) + timedelta(hours=23)
    db.session.commit()
示例#7
0
def proccess_create_or_modify_user_request(
    attribute_dict,
    organisation=None,
    allow_existing_user_modify=False,
    is_self_sign_up=False,
    modify_only=False,
):
    """
    Takes a create or modify user request and determines the response. Normally what's in the top level API function,
    but here it's one layer down because there's multiple entry points for 'create user':
    - The admin api
    - The register api

    :param attribute_dict: attributes that can be supplied by the request maker
    :param organisation:  what organisation the request maker belongs to. The created user is bound to the same org
    :param allow_existing_user_modify: whether to return and error when the user already exists for the supplied IDs
    :param is_self_sign_up: does the request come from the register api?
    :return: An http response
    """

    if not attribute_dict.get('custom_attributes'):
        attribute_dict['custom_attributes'] = {}

    user_id = attribute_dict.get('user_id')

    email = attribute_dict.get('email')
    phone = attribute_dict.get('phone')

    referred_by = attribute_dict.get('referred_by')

    blockchain_address = attribute_dict.get('blockchain_address')

    provided_public_serial_number = attribute_dict.get('public_serial_number')

    if not blockchain_address and provided_public_serial_number:

        try:
            blockchain_address = to_checksum_address(
                provided_public_serial_number)

            # Since it's actually an ethereum address set the provided public serial number to None
            # so it doesn't get used as a transfer card
            provided_public_serial_number = None
        except Exception:
            pass

    require_transfer_card_exists = attribute_dict.get(
        'require_transfer_card_exists',
        g.active_organisation.require_transfer_card)

    public_serial_number = (provided_public_serial_number
                            or attribute_dict.get('payment_card_qr_code')
                            or attribute_dict.get('payment_card_barcode'))

    location = attribute_dict.get('location')  # address location
    geo_location = attribute_dict.get(
        'geo_location')  # geo location as str of lat, lng

    if geo_location:
        geo = geo_location.split(' ')
        lat = geo[0]
        lng = geo[1]
    else:
        # TODO: Work out how this passed tests when this wasn't definied properly!?!
        lat = None
        lng = None

    use_precreated_pin = attribute_dict.get('use_precreated_pin')
    use_last_4_digits_of_id_as_initial_pin = attribute_dict.get(
        'use_last_4_digits_of_id_as_initial_pin')

    transfer_account_name = attribute_dict.get('transfer_account_name')
    first_name = attribute_dict.get('first_name')
    last_name = attribute_dict.get('last_name')

    business_usage_name = attribute_dict.get('business_usage_name')
    business_usage_id = None
    if business_usage_name:
        usage = TransferUsage.find_or_create(business_usage_name)
        business_usage_id = usage.id

    preferred_language = attribute_dict.get('preferred_language')

    primary_user_identifier = attribute_dict.get('primary_user_identifier')
    primary_user_pin = attribute_dict.get('primary_user_pin')

    initial_disbursement = attribute_dict.get('initial_disbursement', None)

    is_vendor = attribute_dict.get('is_vendor', None)
    if is_vendor is None:
        is_vendor = attribute_dict.get('vendor', False)

    is_tokenagent = attribute_dict.get('is_tokenagent', False)
    is_groupaccount = attribute_dict.get('is_groupaccount', False)

    # is_beneficiary defaults to the opposite of is_vendor
    is_beneficiary = attribute_dict.get(
        'is_beneficiary', not is_vendor and not is_tokenagent
        and not is_groupaccount)

    if current_app.config['IS_USING_BITCOIN']:
        try:
            base58.b58decode_check(blockchain_address)
        except ValueError:
            response_object = {
                'message':
                'Blockchain Address {} Not Valid'.format(blockchain_address)
            }
            return response_object, 400

    if isinstance(phone, bool):
        phone = None

    if phone and not is_self_sign_up:
        # phone has already been parsed if self sign up
        try:
            phone = proccess_phone_number(phone)
        except NumberParseException as e:
            response_object = {'message': 'Invalid Phone Number: ' + str(e)}
            return response_object, 400

    # Work out if there's an existing transfer account to bind to
    existing_transfer_account = None
    if primary_user_identifier:
        primary_user = find_user_from_public_identifier(
            primary_user_identifier)

        if not primary_user or not primary_user.verify_password(
                primary_user_pin):
            response_object = {'message': 'Primary User not Found'}
            return response_object, 400

        if not primary_user.verify_password(primary_user_pin):
            response_object = {'message': 'Invalid PIN for Primary User'}
            return response_object, 400

        primary_user_transfer_account = primary_user.transfer_account

        if not primary_user_transfer_account:
            response_object = {
                'message': 'Primary User has no transfer account'
            }
            return response_object, 400

    if not (phone or email or public_serial_number or blockchain_address):
        response_object = {'message': 'Must provide a unique identifier'}
        return response_object, 400

    if use_precreated_pin and not public_serial_number:
        response_object = {
            'message':
            'Must provide public serial number to use a transfer card or pre-created pin'
        }
        return response_object, 400

    if public_serial_number:
        public_serial_number = str(public_serial_number)

        if use_precreated_pin or require_transfer_card_exists:
            transfer_card = TransferCard.query.filter_by(
                public_serial_number=public_serial_number).first()

            if not transfer_card:
                response_object = {'message': 'Transfer card not found'}
                return response_object, 400

    business_usage = None
    if business_usage_id:
        business_usage = TransferUsage.query.get(business_usage_id)
        if not business_usage:
            response_object = {
                'message':
                f'Business Usage not found for id {business_usage_id}'
            }
            return response_object, 400

    referred_by_user = find_user_from_public_identifier(referred_by)

    if referred_by and not referred_by_user:
        response_object = {
            'message':
            f'Referrer user not found for public identifier {referred_by}'
        }
        return response_object, 400

    existing_user = find_user_from_public_identifier(email, phone,
                                                     public_serial_number,
                                                     blockchain_address)

    if modify_only:
        existing_user = User.query.get(user_id)

    if modify_only and existing_user is None:
        response_object = {'message': 'User not found'}
        return response_object, 404

    if existing_user:
        if not allow_existing_user_modify:
            response_object = {'message': 'User already exists for Identifier'}
            return response_object, 400

        try:

            user = update_transfer_account_user(
                existing_user,
                first_name=first_name,
                last_name=last_name,
                preferred_language=preferred_language,
                phone=phone,
                email=email,
                location=location,
                public_serial_number=public_serial_number,
                use_precreated_pin=use_precreated_pin,
                existing_transfer_account=existing_transfer_account,
                is_beneficiary=is_beneficiary,
                is_vendor=is_vendor,
                is_tokenagent=is_tokenagent,
                is_groupaccount=is_groupaccount,
                business_usage=business_usage)

            if referred_by_user:
                user.referred_by.clear(
                )  # otherwise prior referrals will remain...
                user.referred_by.append(referred_by_user)

            set_custom_attributes(attribute_dict, user)
            flag_modified(user, "custom_attributes")

            db.session.commit()

            response_object = {
                'message': 'User Updated',
                'data': {
                    'user': user_schema.dump(user).data
                }
            }

            return response_object, 200

        except Exception as e:
            response_object = {'message': str(e)}

            return response_object, 400

    user = create_transfer_account_user(
        first_name=first_name,
        last_name=last_name,
        preferred_language=preferred_language,
        phone=phone,
        email=email,
        public_serial_number=public_serial_number,
        organisation=organisation,
        blockchain_address=blockchain_address,
        transfer_account_name=transfer_account_name,
        lat=lat,
        lng=lng,
        use_precreated_pin=use_precreated_pin,
        use_last_4_digits_of_id_as_initial_pin=
        use_last_4_digits_of_id_as_initial_pin,
        existing_transfer_account=existing_transfer_account,
        is_beneficiary=is_beneficiary,
        is_vendor=is_vendor,
        is_tokenagent=is_tokenagent,
        is_groupaccount=is_groupaccount,
        is_self_sign_up=is_self_sign_up,
        business_usage=business_usage,
        initial_disbursement=initial_disbursement)

    if referred_by_user:
        user.referred_by.append(referred_by_user)

    if attribute_dict.get('gender'):
        attribute_dict['custom_attributes']['gender'] = attribute_dict.get(
            'gender')

    if attribute_dict.get('bio'):
        attribute_dict['custom_attributes']['bio'] = attribute_dict.get('bio')

    set_custom_attributes(attribute_dict, user)

    if is_self_sign_up and attribute_dict.get('deviceInfo', None) is not None:
        save_device_info(device_info=attribute_dict.get('deviceInfo'),
                         user=user)
    send_onboarding_sms_messages(user)
    # Location fires an async task that needs to know user ID
    db.session.flush()

    if location:
        user.location = location

    if phone:
        if is_self_sign_up:
            send_one_time_code(phone=phone, user=user)
            return {
                'message': 'User Created. Please verify phone number.',
                'otp_verify': True
            }, 200

        elif current_app.config['ONBOARDING_SMS']:
            try:
                send_onboarding_sms_messages(user)
            except Exception as e:
                print(e)
                sentry_sdk.capture_exception(e)
                pass

    response_object = {
        'message': 'User Created',
        'data': {
            'user': user_schema.dump(user).data
        }
    }

    return response_object, 200
示例#8
0
def generate_timeseries_metrics(create_organisation):
    # Generates metrics over timeline
    # User1 and User2 made today
    user1 = create_transfer_account_user(first_name='Ricky',
                                         phone="+19025551234",
                                         organisation=create_organisation,
                                         roles=[('BENEFICIARY', 'beneficiary')
                                                ])
    user1.default_transfer_account.is_approved = True
    user1.default_transfer_account._make_initial_disbursement(100, True)
    user1._location = 'Sunnyvale'

    user2 = create_transfer_account_user(first_name='Bubbles',
                                         phone="+19025551235",
                                         organisation=create_organisation)
    user2.default_transfer_account.is_approved = True
    user2.default_transfer_account._make_initial_disbursement(200, True)
    user2._location = 'Sunnyvale'

    # user3 made yesterday
    user3 = create_transfer_account_user(first_name='Julian',
                                         phone="+19025531230",
                                         organisation=create_organisation)
    user3.default_transfer_account.is_approved = True
    disburse = user3.default_transfer_account._make_initial_disbursement(
        210, True)
    user3.created = user3.created - timedelta(days=1)
    disburse.created = user3.created - timedelta(days=1)
    user3._location = 'Dartmouth'

    # user4 made 4 days ago
    user4 = create_transfer_account_user(first_name='Randy',
                                         phone="+19025511230",
                                         organisation=create_organisation)
    user4.default_transfer_account.is_approved = True
    disburse = user4.default_transfer_account._make_initial_disbursement(
        201, True)
    user4.created = user4.created - timedelta(days=4)
    disburse.created = user4.created - timedelta(days=4)
    user4._location = 'Lower Sackville'

    # user5/user6 made 10 days ago
    user5 = create_transfer_account_user(first_name='Cory',
                                         phone="+19011531230",
                                         organisation=create_organisation)
    user5.default_transfer_account.is_approved = True
    disburse = user5.default_transfer_account._make_initial_disbursement(
        202, True)
    user5.created = user5.created - timedelta(days=10)
    disburse.created = user5.created - timedelta(days=10)
    user5._location = 'Truro'

    user6 = create_transfer_account_user(first_name='Trevor',
                                         phone="+19025111230",
                                         organisation=create_organisation)
    user6.default_transfer_account.is_approved = True
    disburse = user6.default_transfer_account._make_initial_disbursement(
        204, True)
    user6.created = user6.created - timedelta(days=10)
    disburse.created = user6.created - timedelta(days=10)

    db.session.commit()

    tu1 = TransferUsage.find_or_create("Pepperoni")
    tu2 = TransferUsage.find_or_create("Jalepeno Chips")
    tu3 = TransferUsage.find_or_create("Shopping Carts")
    tu1.created = tu1.created - timedelta(days=15)
    tu2.created = tu1.created - timedelta(days=15)
    tu3.created = tu1.created - timedelta(days=15)

    p1 = make_payment_transfer(
        100,
        create_organisation.token,
        send_user=user1,
        send_transfer_account=user1.default_transfer_account,
        receive_user=user2,
        receive_transfer_account=user2.default_transfer_account,
        transfer_use=str(int(tu1.id)))

    p2 = make_payment_transfer(
        25,
        create_organisation.token,
        send_user=user3,
        send_transfer_account=user3.default_transfer_account,
        receive_user=user4,
        receive_transfer_account=user4.default_transfer_account,
        transfer_use=str(int(tu1.id)))
    p2.created = p2.created - timedelta(days=1)

    p3 = make_payment_transfer(
        5,
        create_organisation.token,
        send_user=user4,
        send_transfer_account=user4.default_transfer_account,
        receive_user=user2,
        receive_transfer_account=user2.default_transfer_account,
        transfer_use=str(int(tu2.id)))
    p3.created = p3.created - timedelta(days=1)

    p4 = make_payment_transfer(
        20,
        create_organisation.token,
        send_user=user5,
        send_transfer_account=user5.default_transfer_account,
        receive_user=user6,
        receive_transfer_account=user6.default_transfer_account,
        transfer_use=str(int(tu3.id)))
    p4.created = p4.created - timedelta(days=4)

    p4 = make_payment_transfer(
        20,
        create_organisation.token,
        send_user=user6,
        send_transfer_account=user6.default_transfer_account,
        receive_user=user5,
        receive_transfer_account=user5.default_transfer_account,
        transfer_use=str(int(tu2.id)))
    p4.created = p4.created - timedelta(days=6)
    db.session.commit()