def to_model(beneficiary_pre_subscription: BeneficiaryPreSubscription, user: Optional[User] = None) -> User: if not user: beneficiary = User() beneficiary.email = beneficiary_pre_subscription.email beneficiary.password = random_password() generate_reset_token(beneficiary, validity_duration_hours=THIRTY_DAYS_IN_HOURS) else: beneficiary = user beneficiary.dateOfBirth = beneficiary_pre_subscription.date_of_birth or beneficiary.dateOfBirth beneficiary.activity = beneficiary_pre_subscription.activity beneficiary.address = beneficiary_pre_subscription.address beneficiary.city = beneficiary_pre_subscription.city beneficiary.civility = beneficiary_pre_subscription.civility beneficiary.departementCode = beneficiary_pre_subscription.department_code beneficiary.firstName = beneficiary_pre_subscription.first_name beneficiary.hasSeenTutorials = False beneficiary.isAdmin = False beneficiary.lastName = beneficiary_pre_subscription.last_name beneficiary.phoneNumber = beneficiary_pre_subscription.phone_number beneficiary.postalCode = beneficiary_pre_subscription.postal_code beneficiary.publicName = beneficiary_pre_subscription.public_name beneficiary = users_api.activate_beneficiary(beneficiary, beneficiary_pre_subscription.deposit_source) users_api.attach_beneficiary_import_details(beneficiary, beneficiary_pre_subscription) return beneficiary
def execute(self, application_id: int) -> None: beneficiary_pre_subscription = get_application_by_id(application_id) user = find_user_by_email(beneficiary_pre_subscription.email) try: validate(beneficiary_pre_subscription, preexisting_account=user) except CantRegisterBeneficiary as cant_register_beneficiary_exception: self.beneficiary_repository.reject( beneficiary_pre_subscription, detail=str(cant_register_beneficiary_exception)) send_rejection_email_to_beneficiary_pre_subscription( beneficiary_pre_subscription=beneficiary_pre_subscription, beneficiary_is_eligible=isinstance( cant_register_beneficiary_exception, BeneficiaryIsADuplicate), send_email=send_raw_email, ) else: if not user: beneficiary = self.beneficiary_repository.save( beneficiary_pre_subscription) else: beneficiary = users_api.activate_beneficiary( user, beneficiary_pre_subscription.deposit_source) users_api.attach_beneficiary_import_details( user, beneficiary_pre_subscription) send_activation_email(user=beneficiary, send_email=send_raw_email)
def test_with_eligible_user(self): eligible_date = date.today() - relativedelta(years=18, days=30) user = users_factories.UserFactory(isBeneficiary=False, dateOfBirth=eligible_date) user = users_api.activate_beneficiary(user, "test") assert user.isBeneficiary assert len(user.deposits) == 1
def test_with_ineligible_user_raises_exception(self): user = users_factories.UserFactory.build(isBeneficiary=False) with pytest.raises(users_exceptions.NotEligible): users_api.activate_beneficiary(user, "test")