Example #1
0
def attach_beneficiary_import_details(
    beneficiary: users_models.User,
    application_id: int,
    source_id: int,
    source: BeneficiaryImportSources,
    details: str,
    status: ImportStatus = ImportStatus.CREATED,
) -> None:
    beneficiary_import = BeneficiaryImport.query.filter_by(
        applicationId=application_id,
        sourceId=source_id,
        source=source.value,
        beneficiary=beneficiary,
    ).one_or_none()
    if not beneficiary_import:
        beneficiary_import = BeneficiaryImport(
            applicationId=application_id,
            sourceId=source_id,
            source=source.value,
            beneficiary=beneficiary,
        )

    beneficiary_import.setStatus(status=status, detail=details)
    beneficiary_import.beneficiary = beneficiary

    pcapi_repository.repository.save(beneficiary_import)
Example #2
0
def to_rejected_model(beneficiary_pre_subscription: BeneficiaryPreSubscription, detail: str) -> BeneficiaryImport:
    beneficiary_import = BeneficiaryImport()

    beneficiary_import.applicationId = beneficiary_pre_subscription.application_id
    beneficiary_import.sourceId = beneficiary_pre_subscription.source_id
    beneficiary_import.source = beneficiary_pre_subscription.source
    beneficiary_import.setStatus(status=ImportStatus.REJECTED, detail=detail)

    return beneficiary_import
Example #3
0
def attach_beneficiary_import_details(
        beneficiary: User,
        beneficiary_pre_subscription: BeneficiaryPreSubscription) -> None:
    beneficiary_import = BeneficiaryImport()

    beneficiary_import.applicationId = beneficiary_pre_subscription.application_id
    beneficiary_import.sourceId = beneficiary_pre_subscription.source_id
    beneficiary_import.source = beneficiary_pre_subscription.source
    beneficiary_import.setStatus(status=ImportStatus.CREATED)

    beneficiary.beneficiaryImports = [beneficiary_import]
    def update_model(self, new_status_form: Form,
                     beneficiary_import: BeneficiaryImport) -> None:
        new_status = ImportStatus(new_status_form.status.data)

        if is_import_status_change_allowed(beneficiary_import.currentStatus,
                                           new_status):
            beneficiary_import.setStatus(new_status,
                                         detail=new_status_form.detail.data,
                                         author=current_user)
            repository.save(beneficiary_import)
        else:
            new_status_form.status.errors.append(
                IMPORT_STATUS_MODIFICATION_RULE)
Example #5
0
    def test_appends_a_status_to_a_new_beneficiary_import(self):
        # given
        one_second = timedelta(seconds=1)
        now = datetime.utcnow()
        beneficiary_import = BeneficiaryImport()

        # when
        beneficiary_import.setStatus(ImportStatus.CREATED)

        # then
        assert len(beneficiary_import.statuses) == 1
        assert beneficiary_import.currentStatus == ImportStatus.CREATED  # pylint: disable=comparison-with-callable
        assert beneficiary_import.detail is None
        assert now - one_second < beneficiary_import.statuses[
            0].date < now + one_second
Example #6
0
    def test_appends_a_status_to_a_beneficiary_import_with_existing_status(
            self):
        # given
        one_second = timedelta(seconds=1)
        now = datetime.utcnow()
        beneficiary_import = BeneficiaryImport()
        payment_status = BeneficiaryImportStatus()
        payment_status.status = ImportStatus.DUPLICATE
        payment_status.date = datetime.utcnow()
        beneficiary_import.statuses = [payment_status]

        # when
        beneficiary_import.setStatus(ImportStatus.REJECTED)

        # then
        assert len(beneficiary_import.statuses) == 2
        assert beneficiary_import.currentStatus == ImportStatus.REJECTED  # pylint: disable=comparison-with-callable
        assert beneficiary_import.detail is None
        assert now - one_second < beneficiary_import.statuses[
            1].date < now + one_second
Example #7
0
def create_beneficiary_import(user: users_models.User) -> None:
    if not user.beneficiaryFraudResult:
        raise exceptions.BeneficiaryFraudResultMissing()
    fraud_result: fraud_models.BeneficiaryFraudResult = user.beneficiaryFraudResult
    fraud_check = fraud_models.BeneficiaryFraudCheck.query.filter_by(
        user=user,
        type=fraud_models.FraudCheckType.EDUCONNECT,
    ).one_or_none()

    fraud_ko_reasons = fraud_result.reason_codes
    if fraud_models.FraudReasonCode.DUPLICATE_USER in fraud_ko_reasons:
        raise fraud_exceptions.DuplicateUser()

    if fraud_models.FraudReasonCode.AGE_NOT_VALID in fraud_ko_reasons:
        raise fraud_exceptions.UserAgeNotValid()

    if fraud_models.FraudReasonCode.INE_NOT_WHITELISTED in fraud_ko_reasons:
        raise fraud_exceptions.NotWhitelistedINE()

    if fraud_check.type != fraud_models.FraudCheckType.EDUCONNECT:
        raise NotImplementedError()

    if fraud_result.status != fraud_models.FraudStatus.OK:
        raise fraud_exceptions.FraudException()

    beneficiary_import = BeneficiaryImport(
        thirdPartyId=fraud_check.thirdPartyId,
        beneficiaryId=user.id,
        sourceId=None,
        source=BeneficiaryImportSources.educonnect.value,
        beneficiary=user,
        # TODO(viconnex): select the eligibilityType according to the subscription date
        eligibilityType=users_models.EligibilityType.UNDERAGE,
    )
    beneficiary_import.setStatus(ImportStatus.CREATED)
    pcapi_repository.repository.save(beneficiary_import)

    users_api.update_user_information_from_external_source(user, fraud_check.source_data(), commit=True)
Example #8
0
def attach_beneficiary_import_details(
    beneficiary: User,
    beneficiary_pre_subscription: BeneficiaryPreSubscription,
    status: ImportStatus = ImportStatus.CREATED,
) -> None:
    beneficiary_import = BeneficiaryImport.query.filter_by(
        applicationId=beneficiary_pre_subscription.application_id,
        sourceId=beneficiary_pre_subscription.source_id,
        source=beneficiary_pre_subscription.source,
        beneficiary=beneficiary,
    ).one_or_none()
    if not beneficiary_import:
        beneficiary_import = BeneficiaryImport()

        beneficiary_import.applicationId = beneficiary_pre_subscription.application_id
        beneficiary_import.sourceId = beneficiary_pre_subscription.source_id
        beneficiary_import.source = beneficiary_pre_subscription.source
        beneficiary_import.beneficiary = beneficiary

    beneficiary_import.setStatus(status=status)
    beneficiary_import.beneficiary = beneficiary

    repository.save(beneficiary_import)