Ejemplo n.º 1
0
def token_value_addendum_signed(message_flow_run_id, member, steps, end_id, end_message_flow_id, parent_message_key,
                                tag, result_key, flush_id, flush_message_flow_id, service_identity, user_details,
                                flow_params):
    parsed_tag = json.loads(tag)
    document_key = Document.create_key(parsed_tag['document_id'])
    last_step = steps[-1]
    assert isinstance(last_step, FormFlowStepTO)
    if last_step.answer_id != FormTO.POSITIVE:
        logging.error('User pressed cancel in the %s flow', FLOW_SIGN_TOKEN_VALUE_ADDENDUM)
        return None

    logging.info('Received signature for Document')

    sign_result = last_step.form_result.result.get_value()
    assert isinstance(sign_result, SignWidgetResultTO)
    user_detail = user_details[0]
    app_user = create_app_user_by_email(user_detail.email, user_detail.app_id)
    investment_keys = InvestmentAgreement.list_by_user(app_user).fetch(keys_only=True)
    multiply_agreements_tokens(document_key, sign_result, user_details[0], investment_keys)
def _create_token_value_agreement_if_needed(profile_key):
    profile = profile_key.get()  # type: TffProfile
    investments = [
        i for i in InvestmentAgreement.list_by_user(profile.app_user)
        if PaymentInfo.HAS_MULTIPLIED_TOKENS not in i.payment_info
        and i.creation_time <= FF_ENDED_TIMESTAMP
    ]
    statuses = [
        InvestmentAgreement.STATUS_PAID, InvestmentAgreement.STATUS_SIGNED
    ]
    canceled_or_started_investments = [
        i for i in investments if i.status not in statuses
    ]
    to_put, token_count = multiply_tokens_for_agreements(
        canceled_or_started_investments)
    azzert(token_count == 0, 'Expected token_count to be 0')
    logging.info('Updated %s agreements for user %s', len(to_put),
                 profile.username)
    if to_put:
        ndb.put_multi(to_put)
    has_document = any(d.type == DocumentType.TOKEN_VALUE_ADDENDUM.value
                       for d in Document.list_by_username(profile.username))
    if any(i.status in statuses for i in investments) and not has_document:
        create_token_value_agreement(profile.username)