Ejemplo n.º 1
0
async def add_broadcast_template_db(broadcast_template: NewBroadcastTemplate,
                                    current_user: CurrentUserSchema) -> str:
    doc = {
        "updated_at": get_local_datetime_now(),
        "created_at": get_local_datetime_now(),
        "updated_by": ObjectId(current_user.userId),
        "created_by": ObjectId(current_user.userId),
        "is_active": True,
        "platforms": broadcast_template.platforms,
        "flow": broadcast_template.flow,
        "name": broadcast_template.name
    }

    result = await template_collection.insert_one(doc)
    return f"Added {1 if result.acknowledged else 0} broadcast template."
Ejemplo n.º 2
0
async def add_flows_to_db_from_question(flow: NewFlow,
                                        current_user: CurrentUserSchema):
    """
    From question page, there is a similar method for flow page add_flows_to_db_from_flow
    """
    doc = {
        "updated_at": get_local_datetime_now(),
        "topic": flow.topic,
        "created_at": get_local_datetime_now(),
        "updated_by": ObjectId(current_user.userId),
        "type": flow.type,
        "is_active": True,
        "created_by": ObjectId(current_user.userId),
        "flow": flow.flow_items
    }
    result = await collection.insert_one(doc)
    return result.inserted_id
Ejemplo n.º 3
0
async def process_flow(flow: FlowItemCreateIn, current_user, *,
                       method: RequestMethod):
    doc = {
        "name": flow.name,
        "updated_at": get_local_datetime_now(),
        "created_at": get_local_datetime_now(),
        "updated_by": ObjectId(current_user.userId),
        "type": 'storyboard',
        "is_active": flow.is_active,
        "created_by": ObjectId(current_user.userId),
        "flow": [format_flow_to_database_format(f) for f in flow.flow]
    }

    if method == RequestMethod.EDIT:
        keys_to_remove = ["created_at", "created_by"]
        for key in keys_to_remove:
            doc.pop(key)
    return doc
Ejemplo n.º 4
0
async def skip_message_db(message: SkipMessage,
                          current_user: CurrentUserSchema) -> str:
    query = {"_id": ObjectId(message.id)}

    set_query = {
        "updated_at": get_local_datetime_now(),
        "updated_by": ObjectId(current_user.userId),
        "adminportal.graded": True,
        "adminportal.answer": None
    }
    result = await message_collection.update_one(query, {'$set': set_query})

    return f"Skipped {result.modified_count} question."
Ejemplo n.º 5
0
async def update_broadcast_template_db(
        template_id: str, broadcast_template: NewBroadcastTemplate,
        current_user: CurrentUserSchema) -> str:
    doc = {
        "updated_at": get_local_datetime_now(),
        "updated_by": ObjectId(current_user.userId),
        "platforms": broadcast_template.platforms,
        "flow": broadcast_template.flow,
        "name": broadcast_template.name,
        "is_active": broadcast_template.is_active
    }
    result = await template_collection.update_one(
        {"_id": ObjectId(template_id)}, {"$set": doc})
    return f"Updated {1 if result.acknowledged else 0} broadcast template."
Ejemplo n.º 6
0
async def remove_flow_db(flow_ids: list[str],
                         current_user: CurrentUserSchema) -> str:
    query = {
        "_id": {
            "$in": [ObjectId(f) for f in flow_ids]
        },
        "is_active": True
    }

    set_query = {
        "updated_at": get_local_datetime_now(),
        "updated_by": ObjectId(current_user.userId),
        "is_active": False
    }
    result = await collection.update_many(query, {'$set': set_query})

    return f"Removed {result.modified_count} flows."
Ejemplo n.º 7
0
async def update_message_db(message_item: UpdateMessageResponse,
                            current_user: CurrentUserSchema,
                            language: str = 'EN') -> str:
    query = {"_id": ObjectId(message_item.id)}
    result1 = result2 = result3 = 0

    # add selected answer to message if it's not same with original response/graded response
    message_from_db = await message_collection.find_one(query)
    graded_response = message_from_db.get('adminportal', {}).get('answer')
    original_response = message_from_db.get('chatbot', {}).get('qnid')

    response = graded_response or original_response
    if not graded_response and response == message_item.new_response:
        return 'No questions updated'

    updated_info_query = {
        "updated_at": get_local_datetime_now(),
        "updated_by": ObjectId(current_user.userId),
    }
    # add graded response to message
    set_message_query = updated_info_query | {
        "adminportal.graded": True,
        "adminportal.answer": ObjectId(message_item.new_response)
    }
    result1 = await message_collection.update_one(query,
                                                  {'$set': set_message_query})

    # delete variation from main question and add variation to new question
    query = {
        "_id": ObjectId(response),
        "alternate_questions.text": Regex(f"^{escape(message_item.text)}$",
                                          "i"),
        "is_active": True
    }
    if question_db := await question_collection.find_one(
            query):  # remove variation if found match
        for idx, v in enumerate(question_db['alternate_questions']):
            if v['text'].lower() == message_item.text.lower():
                question_db['alternate_questions'].pop(idx)
                question_db |= updated_info_query
                result2 = await question_collection.replace_one(
                    {"_id": question_db['_id']}, question_db)
                break