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)
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)
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
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
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
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 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)
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)
def save_beneficiary_import_with_status( status: ImportStatus, application_id: int, source_id: int, source: BeneficiaryImportSources, detail: str = None, user: User = None, ) -> None: existing_beneficiary_import = BeneficiaryImport.query.filter_by( applicationId=application_id).first() beneficiary_import = existing_beneficiary_import or BeneficiaryImport() if not beneficiary_import.beneficiary: beneficiary_import.beneficiary = user beneficiary_import.applicationId = application_id beneficiary_import.sourceId = source_id beneficiary_import.source = source.value beneficiary_import.setStatus(status=status, detail=detail, author=None) repository.save(beneficiary_import)
def create_beneficiary_import( application_id: int = 99, date: datetime = datetime.utcnow(), detail: str = None, idx: int = None, source_id: int = settings.DMS_OLD_ENROLLMENT_PROCEDURE_ID, source: str = BeneficiaryImportSources.demarches_simplifiees.value, status: ImportStatus = ImportStatus.CREATED, user: User = None, ) -> BeneficiaryImport: import_status = BeneficiaryImportStatus() import_status.date = date import_status.detail = detail import_status.status = status beneficiary_import = BeneficiaryImport() beneficiary_import.id = idx beneficiary_import.beneficiary = user beneficiary_import.applicationId = application_id beneficiary_import.sourceId = source_id beneficiary_import.source = source beneficiary_import.statuses = [import_status] return beneficiary_import
def save_beneficiary_import_with_status( status: ImportStatus, application_id: int, source_id: int, source: BeneficiaryImportSources, detail: str = None, user: User = None, ) -> BeneficiaryImport: # FIXME (dbaty, 2021-04-22): see comment above about the non-uniqueness of application_id existing_beneficiary_import = BeneficiaryImport.query.filter_by( applicationId=application_id).first() beneficiary_import = existing_beneficiary_import or BeneficiaryImport() if not beneficiary_import.beneficiary: beneficiary_import.beneficiary = user beneficiary_import.applicationId = application_id beneficiary_import.sourceId = source_id beneficiary_import.source = source.value beneficiary_import.setStatus(status=status, detail=detail, author=None) repository.save(beneficiary_import) return beneficiary_import