Esempio n. 1
0
def get_replies(flyer_id):
    data = request.get_json()
    logger.info("Data recieved: %s", data)

    if "sender" not in data:
        msg = "No sender provided for reply."
        logger.info(msg)
        return create_response(status=422, message=msg)

    flyer = SearchFlyer.query.get(flyer_id)
    sender = data['sender']
    data['content'] = f'{sender} se quiere contactar con vos por tu aviso.'
    data['flyer_id'] = flyer_id
    data['recipient'] = flyer.created_by
    data['status'] = 'sent'

    # create SQLAlchemy Object
    message = Message(**data)

    # commit it to database
    db.session.add_all([message])
    db.session.commit()

    submit_notification(
        flyer.created_by, {
            'title': '¡Respondieron tu aviso!',
            'body': f'{sender} se quiere contactar con vos por tu aviso.',
            'flyer_id': flyer_id
        })

    return create_response(
        message=f"Successfully created message with id: {message.id}",
        data={"message": message.to_dict()})
Esempio n. 2
0
def post_message():
    message = Message(message=json.loads(request.data))
    channel_repo = ChannelRepo(current_app.config['CHANNEL_REPO_CONF'])
    channel_queue_repo = ChannelQueueRepo(current_app.config['CHANNEL_QUEUE_REPO_CONF'])
    use_case = ReceiveMessageUseCase(channel_repo, channel_queue_repo)
    use_case.receive(message)
    message_data = message.to_dict()
    return JsonResponse(message_data, status=200)
def add_message():
    data = request.form.to_dict()
    subjects = [
        "[Kiva] New required document",
        "[Kiva] Document reviewed",
        "[Kiva] Document uploaded",
    ]

    # If to_fp is true, then this notification is meant for the fp
    if "to_fp" not in data:
        return create_response(
            status=400, message="No boolean to_fp provided for new message")

    # Get a PM id if it's not provided
    if "pm_id" not in data:
        if "fp_id" not in data:
            return create_response(
                status=400, message="No FP or PM ID provided for new message")
        data["pm_id"] = FieldPartner.query.get(data["fp_id"]).pm_id

    # Because we can't get the FP ID from PM, we need the FP ID explicity when it's to FP
    # Otherwise, the fp_id field can be empty
    if "fp_id" not in data and data.to_fp:
        return create_response(
            status=400, message="No FP ID provided for new message to FP")

    if "doc_id" not in data:
        return create_response(
            status=400, message="No document ID provided for new message")

    # Default to reviewed because it has 2 statuses
    message_type = MessageType.REVIEWED_DOC

    # Using statuses to determine the message type, lowercasing for message
    status = Document.query.get(data["doc_id"]).status.lower()
    if status == "missing":
        message_type = MessageType.NEW_DOC
    if status == "pending":
        message_type = MessageType.UPLOADED_DOC

    # Getting names for the message contents
    docclass_name = DocumentClass.query.get(
        Document.query.get(data["doc_id"]).docClassID).name

    organization = ""
    if "fp_id" in data:
        organization = FieldPartner.query.get(data["fp_id"]).org_name

    contents = [
        f"Your Portfolio Manager has added a new required document: {docclass_name}.",  # document class name
        f"Your document, {docclass_name}, has been reviewed and was {status}.",  # document class name, status [approved/rejected]
        f"Your Field Partner from {organization} has uploaded a document for {docclass_name}.",  # organization, document class name
    ]

    if "reason" in data:
        reason = data.get("reason")
        contents[
            1] = f"Your document, {docclass_name}, has been reviewed and was {status} for the following reason: {reason}."

    # Add the contents as a description field
    data["description"] = contents[message_type.value]

    if data["to_fp"] == "true" or data["to_fp"] == "false":
        data["to_fp"] = data["to_fp"] == "true"
    elif data["to_fp"] == "True" or data["to_fp"] == "False":
        data["to_fp"] = data["to_fp"] == "True"
    else:
        data["to_fp"] == bool(data["to_fp"])

    recipient = (FieldPartner.query.get(data["fp_id"]) if data["to_fp"] else
                 PortfolioManager.query.get(data["pm_id"]))

    # Send the email
    mail = Mail(current_app)

    email = Flask_Message(
        sender=os.getenv("GMAIL_NAME"),
        subject=subjects[message_type.value],
        recipients=[recipient.email],
        body=contents[message_type.value],
    )

    mail.send(email)
    new_message = Message(data)
    ret = new_message.to_dict()

    db.session.add(new_message)
    db.session.commit()

    return create_response(data={"message": ret})