Beispiel #1
0
def convert_answers_to_payload_0_0_3(
    answer_store: AnswerStore,
    list_store: ListStore,
    schema: QuestionnaireSchema,
    full_routing_path: RoutingPath,
) -> list[Answer]:
    """
    Convert answers into the data format below
    'data': [
        {
            'value': 'Joe',
            'answer_id': 'first-name',
            'list_item_id': 'axkkdh'
        },
        {
            'value': 'Dimaggio',
            'answer_id': 'last-name',
            'list_item_id': 'axkkdh'
        },
        {
            'value': 'No',
            'answer_id': 'do-you-live-here'
        }
    ]

    For list answers, this method will query the list store and get all answers from the
    add list item block. If there are multiple list collectors for one list, they will have
    the same answer_ids, and will not be duplicated.

    Returns:
        A list of answer dictionaries.
    """
    answers_payload = AnswerStore()

    for routing_path in full_routing_path:
        for block_id in routing_path:
            if block := schema.get_block(block_id):
                block_type = block["type"]
                if block_type == "RelationshipCollector" and "unrelated_block" in block:
                    add_relationships_unrelated_answers(
                        answer_store=answer_store,
                        list_store=list_store,
                        schema=schema,
                        section_id=routing_path.section_id,
                        relationships_block=block,
                        answers_payload=answers_payload,
                    )

                if schema.is_list_block_type(
                        block_type) or schema.is_primary_person_block_type(
                            block_type):
                    add_list_collector_answers(
                        answer_store=answer_store,
                        list_store=list_store,
                        schema=schema,
                        list_collector_block=block,
                        answers_payload=answers_payload,
                    )

                answer_ids = schema.get_answer_ids_for_block(block_id)
                answers_in_block = answer_store.get_answers_by_answer_id(
                    answer_ids, list_item_id=routing_path.list_item_id)
                for answer_in_block in answers_in_block:
                    answers_payload.add_or_update(answer_in_block)