def on_get(self, req, res, did): rows = ValidationTx.objects(did=did) if rows: obj = [each.as_dict() for each in rows] self.on_success(res, obj) else: raise AppError( description="Cannot retrieve requests for the given did")
def on_post(self, req, res, confirmation_id): result = {} rows = ValidationTx.objects(id=confirmation_id) if rows: row = rows[0] row.isSavedOnProfile = True row.save() result = row.as_dict() self.on_success(res, result)
def get_stats_from_validationtx(provider_id): stats = {} validation_tx_rows = ValidationTx.objects(provider=provider_id) if validation_tx_rows: for validation_tx_row in validation_tx_rows: status = validation_tx_row.status if status in stats.keys(): stats[status] += 1 else: stats[status] = 1 return stats
def on_get(self, req, res, provider_id): rows = ValidationTx.objects(provider=provider_id) if rows: result = [] for row in rows: # if row.status == ValidationStatus.NEW: result.append(row.as_dict()) self.on_success(res, result) else: raise AppError( description="Cannot retrieve requests for the given provider_id" )
def on_post(self, req, res): data = req.media providerId = data["provider"] providersRows = Provider.objects(id=providerId) if providersRows: provider = [each.as_dict() for each in providersRows][0] else: raise AppError(description="Provider not found for the given ID") transactionSent = self.transaction_already_sent(data) result = {} if transactionSent: result["duplicate"] = True result["validationtx"] = transactionSent else: result["duplicate"] = False row = ValidationTx(did=data["did"].replace("did:elastos:", "").split("#")[0], provider=data["provider"], validationType=data["validationType"], requestParams=data["requestParams"], status=ValidationStatus.NEW, isSavedOnProfile=False, retries=0) row.save() doc = { "type": "email", "action": "create", "transactionId": '{}'.format(row.id), "params": data["requestParams"], 'did': data["did"] } redisBroker.send_validator_message(doc, provider["did"]) result["validationtx"] = row.as_dict() self.on_success(res, result)
def handle_response(doc): tx_type = doc["action"] if tx_type not in ["create", "cancel", "update"]: LOG.info(f'Transaction type "{tx_type}" passed is not valid') return provider_rows = Provider.objects(did=doc["validatorKey"]) if not provider_rows: LOG.info(f'Validator key "{doc["validatorKey"]} is invalid') return provider_row = provider_rows[0] transaction_rows = ValidationTx.objects(id=doc["transactionId"]) if not transaction_rows: LOG.info(f'Transaction ID "{doc["transactionId"]} not found') return transaction_row = transaction_rows[0] if transaction_row.provider != str(provider_row.id): LOG.info(f'Provider "{provider_row.name} cannot perform this action') return if tx_type == "create": if transaction_row.status != ValidationStatus.NEW: LOG.info( f'Transaction ID "{str(transaction_row.id)}" is already being processed' ) return transaction_row.status = ValidationStatus.IN_PROGRESS elif tx_type == "cancel": if transaction_row.status != ValidationStatus.CANCELATION_IN_PROGRESS: LOG.info( f'Transaction ID "{str(transaction_row.id)}" cannot be cancelled because no ' f'cancellation is in progress for this request') return transaction_row.status = ValidationStatus.CANCELED elif tx_type == "update": if transaction_row.status != ValidationStatus.IN_PROGRESS: LOG.info( f'Transaction ID "{str(transaction_row.id)}" is already being processed' ) return if doc["response"] != ValidationStatus.APPROVED and doc[ "response"] != ValidationStatus.REJECTED: LOG.info(f'Response status "{doc["response"]}" is invalid') return transaction_row.status = doc["response"] transaction_row.reason = doc["reason"] transaction_row.verifiedCredential = doc["verifiableCredential"] transaction_row.save()
def transaction_already_sent(self, data): time = datetime.utcnow() - timedelta(minutes=10) rows = ValidationTx.objects(did=data["did"].replace( "did:elastos:", "").split("#")[0], validationType=data["validationType"], provider=data["provider"], modified__gte=time) if rows: for row in rows: obj = row.as_dict() if obj["status"] == ValidationStatus.CANCELED or obj[ "status"] == ValidationStatus.CANCELATION_IN_PROGRESS: return None if obj["requestParams"] == data["requestParams"]: return obj return None
def on_get(self, req, res, provider_id): rows = ValidationTx.objects(provider=provider_id) if rows: result = {} for row in rows: status = row.status if status in result.keys(): result[status] += 1 else: result[status] = 1 self.on_success(res, result) else: raise AppError( description= "Cannot retrieve total request count for the given provider ID" )
def on_post(self, req, res, confirmation_id): rows = ValidationTx.objects(id=confirmation_id) if not rows: raise AppError(description="Validation not found") request = rows[0] if request.status == ValidationStatus.REJECTED: raise AppError(description="Validation is already rejected") elif request.status == ValidationStatus.APPROVED or request.status == ValidationStatus.REJECTED: raise AppError( description= "Validation cannot be rejected after it has been processed") elif request.status == ValidationStatus.NEW: request.status = ValidationStatus.REJECTED request.save() self.on_success(res, request.as_dict())
def on_get(self, req, res, provider_did): providerId = '' # Get provider id by provider DID rows = Provider.objects(did=provider_did) if rows: providerId = rows[0].id # Get validation requests using above provider id # TODO: remove redundancy as the below is quite similar to ValidationTxFromProviderId on_get if providerId: rows2 = ValidationTx.objects(provider=str(providerId)) if rows2: result = [] for row in rows2: result.append(row.as_dict()) self.on_success(res, result) else: raise AppError( description= "Cannot retrieve requests for the given provider_id")
def on_post(self, req, res, confirmation_id): rows = ValidationTx.objects(id=confirmation_id) if not rows: raise AppError(description="Validation not found") request = rows[0] if request.status == ValidationStatus.CANCELED: raise AppError(description="Validation is already canceled") elif request.status == ValidationStatus.APPROVED or request.status == ValidationStatus.REJECTED: raise AppError( description= "Validation cannot be canceled after it has been processed") elif request.status == ValidationStatus.NEW: request.status = ValidationStatus.CANCELED request.save() self.on_success(res, request.as_dict()) return providers = Provider.objects(id=request.provider) if not providers: raise AppError(description="Provider not found") redisBroker.send_validator_message( { "type": "email", "action": "cancel", "transactionId": f'{request.id}', }, providers[0].did) request.status = ValidationStatus.CANCELATION_IN_PROGRESS request.retries = 0 request.save() self.on_success(res, request.as_dict())