def delete_user_and_transfer_account(self): """ Soft deletes a User and default Transfer account if no other users associated to it. Removes User PII Disables transfer card """ try: ta = self.default_transfer_account ta.delete_transfer_account_from_user(user=self) timenow = datetime.datetime.utcnow() self.deleted = timenow self.first_name = None self.last_name = None self.phone = None transfer_card = None try: transfer_card = TransferCard.get_transfer_card( self.public_serial_number) except NoTransferCardError as e: pass if transfer_card and not transfer_card.is_disabled: transfer_card.disable() except (ResourceAlreadyDeletedError, TransferAccountDeletionError) as e: raise e
def update_transfer_account_user(user, first_name=None, last_name=None, preferred_language=None, phone=None, email=None, public_serial_number=None, use_precreated_pin=False, existing_transfer_account=None, roles=None, default_organisation_id=None, business_usage=None): if first_name: user.first_name = first_name if last_name: user.last_name = last_name if preferred_language: user.preferred_language = preferred_language if phone: user.phone = phone if email: user.email = email if public_serial_number: user.public_serial_number = public_serial_number transfer_card = TransferCard.get_transfer_card(public_serial_number) user.default_transfer_account.transfer_card = transfer_card if transfer_card: transfer_card.update_transfer_card() else: transfer_card = None if default_organisation_id: user.default_organisation_id = default_organisation_id if use_precreated_pin and transfer_card: user.set_pin(transfer_card.PIN) if existing_transfer_account: user.transfer_accounts.append(existing_transfer_account) if business_usage: user.business_usage_id = business_usage.id # remove all roles before updating user.remove_all_held_roles() flag_modified(user, '_held_roles') if roles: for role in roles: user.set_held_role(role[0], role[1]) return user
def public_serial_number(self, public_serial_number): self._public_serial_number = public_serial_number try: transfer_card = TransferCard.get_transfer_card( public_serial_number) if transfer_card.user_id is None and transfer_card.nfc_serial_number is not None: # Card hasn't been used before, and has a nfc number attached self.nfc_serial_number = transfer_card.nfc_serial_number self.transfer_card = transfer_card except NoTransferCardError: pass
def post(self, nfc_serial_number, public_serial_number): post_data = request.get_json() public_serial_number = post_data.get('public_serial_number') nfc_serial_number = post_data.get('nfc_serial_number') if not public_serial_number or not nfc_serial_number: response_object = { 'message': 'Missing Data', } return make_response(jsonify(response_object)), 400 # serial numbers are case insensitive public_serial_number = re.sub(r'[\t\n\r]', '', str(public_serial_number)).upper() nfc_serial_number = nfc_serial_number.upper() if TransferCard.query.filter_by( public_serial_number=public_serial_number).first(): response_object = { 'message': 'Public Serial Number already used', } return make_response(jsonify(response_object)), 400 if TransferCard.query.filter_by( nfc_serial_number=nfc_serial_number).first(): response_object = { 'message': 'NFC Serial Number already used', } return make_response(jsonify(response_object)), 400 transfer_card = TransferCard(nfc_serial_number=nfc_serial_number, public_serial_number=public_serial_number) db.session.add(transfer_card) db.session.commit() response_object = { 'status': 'success', } return make_response(jsonify(response_object)), 201
def create_transfer_account_user(first_name=None, last_name=None, preferred_language=None, phone=None, email=None, public_serial_number=None, uuid=None, organisation: Organisation = None, token=None, blockchain_address=None, transfer_account_name=None, use_precreated_pin=False, use_last_4_digits_of_id_as_initial_pin=False, existing_transfer_account=None, roles=None, is_self_sign_up=False, business_usage=None, initial_disbursement=None): user = User(first_name=first_name, last_name=last_name, preferred_language=preferred_language, blockchain_address=blockchain_address, phone=phone, email=email, uuid=uuid, public_serial_number=public_serial_number, is_self_sign_up=is_self_sign_up, business_usage=business_usage) precreated_pin = None is_activated = False try: transfer_card = TransferCard.get_transfer_card(public_serial_number) except Exception as e: transfer_card = None if use_precreated_pin: precreated_pin = transfer_card.PIN is_activated = True elif use_last_4_digits_of_id_as_initial_pin: precreated_pin = str(public_serial_number or phone)[-4:] is_activated = False user.set_pin(precreated_pin, is_activated) if roles: for role in roles: user.set_held_role(role[0], role[1]) else: user.remove_all_held_roles() if not organisation: organisation = Organisation.master_organisation() user.add_user_to_organisation(organisation, is_admin=False) db.session.add(user) if existing_transfer_account: transfer_account = existing_transfer_account user.transfer_accounts.append(existing_transfer_account) else: transfer_account = TransferAccount( bound_entity=user, blockchain_address=blockchain_address, organisation=organisation) top_level_roles = [r[0] for r in roles or []] is_vendor = 'VENDOR' in top_level_roles is_beneficiary = 'BENEFICIARY' in top_level_roles transfer_account.name = transfer_account_name transfer_account.is_vendor = is_vendor transfer_account.is_beneficiary = is_beneficiary if transfer_card: transfer_account.transfer_card = transfer_card if token: transfer_account.token = token if not is_self_sign_up: transfer_account.approve_and_disburse( initial_disbursement=initial_disbursement) db.session.add(transfer_account) user.default_transfer_account = transfer_account return user
def test_create_user(test_client, authed_sempo_admin_user, init_database, create_transfer_account_user, mock_async_set_user_gps_from_location, user_phone_accessor, phone, use_card, business_usage_name, referred_by, gps_location, initial_disbursement, tier, status_code): if tier: authed_sempo_admin_user.set_held_role('ADMIN', tier) auth = get_complete_auth_token(authed_sempo_admin_user) else: auth = None # create the user who is referring create_transfer_account_user.phone = referred_by user_phone_accessor(create_transfer_account_user) payload = { 'first_name': 'John', 'last_name': 'Smith', 'bio': 'EasyMart', 'gender': 'female', 'phone': phone, 'initial_disbursement': initial_disbursement, 'location': 'Elwood', 'business_usage_name': business_usage_name, 'referred_by': user_phone_accessor(create_transfer_account_user) } if gps_location: payload['gps_location'] = gps_location if use_card: public_serial_number = f'{randint(0,999999):06}' new_card = TransferCard(public_serial_number=public_serial_number) init_database.session.add(new_card) init_database.session.commit() payload['public_serial_number'] = public_serial_number response = test_client.post( "/api/v1/user/", headers=dict( Authorization=auth, Accept='application/json' ), json=payload) assert response.status_code == status_code if response.status_code == 200: data = response.json['data'] assert isinstance(data['user'], object) assert data['user']['first_name'] == 'John' assert data['user']['last_name'] == 'Smith' assert data['user']['custom_attributes']['bio'] == 'EasyMart' assert data['user']['custom_attributes']['gender'] == 'female' assert data['user']['phone'] == proccess_phone_number(phone) assert data['user']['is_vendor'] is False assert data['user']['is_tokenagent'] is False assert data['user']['is_groupaccount'] is False assert data['user']['location'] == 'Elwood' assert data['user']['business_usage_id'] == init_database.session.query(TransferUsage)\ .filter_by(name=business_usage_name).first().id assert data['user']['referred_by'] == user_phone_accessor(create_transfer_account_user) if initial_disbursement is not None: assert data['user']['transfer_accounts'][0]['balance'] == initial_disbursement else: db_user = init_database.session.query(User).get(data['user']['id']) assert data['user']['transfer_accounts'][0]['balance'] == db_user.default_organisation.default_disbursement # Checks that we're calling the gps location fetching job, and passing the right data to it fn_inputs = mock_async_set_user_gps_from_location if gps_location: assert data['user']['lat'] == 12.02 assert data['user']['lng'] == -15.04 assert len(fn_inputs) == 0 else: args, kwargs = fn_inputs[-1] assert kwargs == {'user_id': data['user']['id'], 'location': 'Elwood'}
def create_transfer_account_user(first_name=None, last_name=None, preferred_language=None, phone=None, email=None, public_serial_number=None, organisation: Organisation = None, token=None, blockchain_address=None, transfer_account_name=None, lat=None, lng=None, use_precreated_pin=False, use_last_4_digits_of_id_as_initial_pin=False, existing_transfer_account=None, is_beneficiary=False, is_vendor=False, is_tokenagent=False, is_groupaccount=False, is_self_sign_up=False, business_usage=None, initial_disbursement=None): user = User(first_name=first_name, last_name=last_name, lat=lat, lng=lng, preferred_language=preferred_language, phone=phone, email=email, public_serial_number=public_serial_number, is_self_sign_up=is_self_sign_up, business_usage=business_usage) precreated_pin = None is_activated = False try: transfer_card = TransferCard.get_transfer_card(public_serial_number) except Exception as e: transfer_card = None if use_precreated_pin: precreated_pin = transfer_card.PIN is_activated = True elif use_last_4_digits_of_id_as_initial_pin: precreated_pin = str(public_serial_number or phone)[-4:] is_activated = False user.set_pin(precreated_pin, is_activated) if not is_vendor: vendor_tier = None elif existing_transfer_account: vendor_tier = 'vendor' else: vendor_tier = 'supervendor' user.set_held_role('VENDOR', vendor_tier) if is_tokenagent: user.set_held_role('TOKEN_AGENT', 'grassroots_token_agent') if is_groupaccount: user.set_held_role('GROUP_ACCOUNT', 'grassroots_group_account') if is_beneficiary: user.set_held_role('BENEFICIARY', 'beneficiary') if not organisation: organisation = Organisation.master_organisation() user.add_user_to_organisation(organisation, is_admin=False) db.session.add(user) if existing_transfer_account: transfer_account = existing_transfer_account user.transfer_accounts.append(existing_transfer_account) else: transfer_account = TransferAccount( bound_entity=user, blockchain_address=blockchain_address, organisation=organisation) transfer_account.name = transfer_account_name transfer_account.is_vendor = is_vendor transfer_account.is_beneficiary = is_beneficiary if transfer_card: transfer_account.transfer_card = transfer_card if token: transfer_account.token = token if not is_self_sign_up: transfer_account.approve_and_disburse( initial_disbursement=initial_disbursement) db.session.add(transfer_account) user.default_transfer_account = transfer_account return user
def update_transfer_account_user(user, first_name=None, last_name=None, preferred_language=None, phone=None, email=None, public_serial_number=None, location=None, lat=None, lng=None, use_precreated_pin=False, existing_transfer_account=None, is_beneficiary=False, is_vendor=False, is_tokenagent=False, is_groupaccount=False, default_organisation_id=None, business_usage=None): if first_name: user.first_name = first_name if last_name: user.last_name = last_name if preferred_language: user.preferred_language = preferred_language if phone: user.phone = phone if email: user.email = email if public_serial_number: user.public_serial_number = public_serial_number transfer_card = TransferCard.get_transfer_card(public_serial_number) user.default_transfer_account.transfer_card = transfer_card else: transfer_card = None if location: user.location = location if lat: user.lat = lat if lng: user.lng = lng if default_organisation_id: user.default_organisation_id = default_organisation_id if use_precreated_pin and transfer_card: user.set_pin(transfer_card.PIN) if existing_transfer_account: user.transfer_accounts.append(existing_transfer_account) if business_usage: user.business_usage_id = business_usage.id # remove all roles before updating user.remove_all_held_roles() flag_modified(user, '_held_roles') if not is_vendor: vendor_tier = None elif existing_transfer_account: vendor_tier = 'vendor' else: vendor_tier = 'supervendor' user.set_held_role('VENDOR', vendor_tier) if is_tokenagent: user.set_held_role('TOKEN_AGENT', 'grassroots_token_agent') if is_groupaccount: user.set_held_role('GROUP_ACCOUNT', 'grassroots_group_account') if is_beneficiary: user.set_held_role('BENEFICIARY', 'beneficiary') return user