nextjs-challenge / backend / app.py
app.py
Raw
from flask import Flask
from flask import request
import json

app = Flask(__name__)

# Definition of invoice class
class Invoice:
    def __init__(self, amount: float, department: str, _manager_approval_required: bool) -> None:
        self.amount = amount
        self.department = department
        self.manager_approval_required = _manager_approval_required

    # Definition of conditions to be used in the process_invoice function
    def invoice_amount_greater_than(self, threshold: float, onSuccess, onFailure):
        if self.amount > threshold:
            return onSuccess
        else:
            return onFailure
    
    def department_is(self, _department: str, onSuccess, onFailure):
        if self.department == _department:
            return onSuccess
        else:
            return onFailure
    
    def needs_manager_approval(self, onSuccess, onFailure):
        if self.manager_approval_required:
            return onSuccess
        else:
            return onFailure
    
    # Definition of actions to be used in the process_invoice function
    def send_for_approval_slack(self, approver: str) -> str:
        return f"Sending approval request to {approver} via Slack."
    
    def send_for_approval_email(self, approver: str) -> str:
        return f"Sending approval request to {approver} via email."

def process_invoice(invoice: Invoice) -> str:
    # Workflow for processing the invoice
    result = invoice.invoice_amount_greater_than(10000,
    invoice.department_is("marketing",
        invoice.send_for_approval_email("CMO"), # amount > 10000 & department = marketing
        invoice.send_for_approval_slack("CFO") # amount > 10000 & department != marketing
    ),
    invoice.invoice_amount_greater_than(5000,
        invoice.needs_manager_approval(
            invoice.send_for_approval_email("Finance manager"), # amount < 10000 & amount > 5000 & requires_manager_approval = true
            invoice.send_for_approval_slack("Finance team member") # amount < 10000 & amount > 5000 & requires_manager_approval = false
        ),
        invoice.send_for_approval_slack("Finance team member") # amount < 10000 & amount < 5000
        )
    )
    return result


@app.route("/", methods=["POST"])
def receiveData():
    data = request.data
    
    # Validate the data
    if not data:
        return "No data received"
    
    try:
        data_json = json.loads(data)
        amount = data_json["amount"]
        # Amount is received as a string, so converting it to float, e.g. '$ 23,000.20' -> 23000.20
        amount = float(amount.replace('$', '').replace(',', '').replace(' ', ''))
        department = data_json["department"]
        manager_approval_required = data_json["managerApprovalRequired"]
        invoice = Invoice(amount, department, manager_approval_required)
        
        print("Data received:")
        print(f"Amount: {amount}, Department: {department}, Requires Manager Approval: {manager_approval_required}")
        
        print('Processing invoice...')
        result = process_invoice(invoice)
        print('Action:', result)
        
        # TODO: Save the invoice in the database
        
        jsonified = json.dumps({"result": result})
        return jsonified
    except Exception as e:
        print("Error occurred:", e)
        return "Error occurred"

# to test the code, the following curl command can be used
# curl -X POST http://127.0.0.1:5000/ -H "Content-Type: application/json" -d '{"amount": 2000, "department": "marketing", "manager_approval_required": true}'