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 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 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