def resolve_as_complete(self, batch_uuid=None): if self.transfer_status not in [ None, TransferStatusEnum.PENDING, TransferStatusEnum.PARTIAL ]: raise Exception( f'Resolve called multiple times for transfer {self.id}') try: self.check_sender_transfer_limits() except TransferLimitError as e: # Sempo admins can always bypass limits, allowing for things like emergency moving of funds etc if hasattr(g, 'user') and AccessControl.has_suffient_role( g.user.roles, {'ADMIN': 'sempoadmin'}): self.add_message(f'Warning: {e}') else: raise e self.resolved_date = datetime.datetime.utcnow() self.transfer_status = TransferStatusEnum.COMPLETE self.blockchain_status = BlockchainStatus.PENDING self.update_balances() if (datetime.datetime.utcnow() - self.created).seconds > 5: clear_metrics_cache() rebuild_metrics_cache() if self.transfer_type == TransferTypeEnum.PAYMENT and self.transfer_subtype == TransferSubTypeEnum.DISBURSEMENT: if self.recipient_user and self.recipient_user.transfer_card: self.recipient_user.transfer_card.update_transfer_card() if batch_uuid: self.batch_uuid = batch_uuid if self.fiat_ramp and self.transfer_type in [ TransferTypeEnum.DEPOSIT, TransferTypeEnum.WITHDRAWAL ]: self.fiat_ramp.resolve_as_complete()
def post(self): """ This endpoint erases the cache for the current org. Use this after you alter the past so the cache can rebuild itself """ count = metrics_cache.clear_metrics_cache() response_object = { 'status': 'success', 'message': 'Cache erased', 'data': { 'removed_entries': count, } } return make_response(jsonify(response_object)), 200
def make_transfers(disbursement_id, auto_resolve=False): send_transfer_account = g.user.default_organisation.queried_org_level_transfer_account from server.models.user import User from server.models.transfer_account import TransferAccount from server.models.disbursement import Disbursement disbursement = db.session.query(Disbursement).filter( Disbursement.id == disbursement_id).first() for idx, ta in enumerate(disbursement.transfer_accounts): try: user = ta.primary_user if disbursement.transfer_type == 'DISBURSEMENT': transfer = make_payment_transfer( disbursement.disbursement_amount, send_user=g.user, receive_user=db.session.query(User).filter( User.id == user.id).first(), send_transfer_account=send_transfer_account, receive_transfer_account=db.session.query(TransferAccount). filter(TransferAccount.id == ta.id).first(), transfer_subtype=TransferSubTypeEnum.DISBURSEMENT, transfer_mode=TransferModeEnum.WEB, automatically_resolve_complete=False, ) if disbursement.transfer_type == 'RECLAMATION': transfer = make_payment_transfer( disbursement.disbursement_amount, send_user=db.session.query(User).filter( User.id == user.id).first(), send_transfer_account=db.session.query(TransferAccount). filter(TransferAccount.id == ta.id).first(), transfer_subtype=TransferSubTypeEnum.RECLAMATION, transfer_mode=TransferModeEnum.WEB, require_recipient_approved=False, automatically_resolve_complete=False, ) if disbursement.transfer_type == 'BALANCE': transfer = make_target_balance_transfer( disbursement.disbursement_amount, db.session.query(User).filter(User.id == user.id).first(), automatically_resolve_complete=False, transfer_mode=TransferModeEnum.WEB, ) disbursement.credit_transfers.append(transfer) if auto_resolve and disbursement.state == 'APPROVED': transfer.approvers = disbursement.approvers transfer.add_approver_and_resolve_as_completed() except Exception as e: disbursement.errors = disbursement.errors + [ str(ta) + ': ' + str(e) ] db.session.commit() percent_complete = ( (idx + 1) / len(disbursement.transfer_accounts)) * 100 yield { 'message': 'success' if percent_complete == 100 else 'pending', 'percent_complete': math.floor(percent_complete), 'data': { 'credit_transfers': credit_transfers_schema.dump( disbursement.credit_transfers).data } } clear_metrics_cache() rebuild_metrics_cache()