# Business rules

Business rules management lets your team instantly update business rules without relying on engineering or redeploying your app.

## Problem

Without business rules management, rules are typically hardcoded. This results in several challenges:

* Compliance, product, and ops teams:
  * Don't have visibility on business rules and so cannot verify their correctness.
  * Can't safely update business rules themselves.
  * Consume engineering resources with requests to update business rules.
* Updates to business rules are slow as they require code changes and app redeploys.
* There's no single source of truth for business rules across different codebases.
* Business rules can't be tuned with experimentation to improve metrics while controlling risk.

## Solution

Define business rules in Hypertune flags instead of hardcoding them:

```graphql
type BusinessRules {
  isApplicantEligibleForLoan(
    creditScore: Int!,
    annualIncomeUsd: Boolean!,
    debtToIncomeRatio: Float!,
    loanAmountRequestedUsd: Int!,
    riskScore: Int!,
    loanToValueRatio: Float!
  ): Boolean!
  
  claimRequiresAdjusterReview(
    claimAmount: Int!,
    policyCoverageLimit: Int!
  ): Boolean!
}
```

Then reference them in your code:

{% code title="app/api/submit-loan-application/route.ts" %}

```typescript
import { waitUntil } from '@vercel/functions'
import { NextResponse } from 'next/server'
import getHypertune from '@/lib/getHypertune'
import submitLoan from '@/lib/submitLoan'

export const runtime = 'edge'

export async function POST(request: Request) {
  const hypertune = await getHypertune({ isRouteHandler: true })

  const {
    applicantId,
    creditScore,
    annualIncomeUsd,
    debtToIncomeRatio,
    loanAmountRequestedUsd,
    riskScore,
    loanToValueRatio,
  } = await request.json()

  const isEligible = hypertune
    .businessRules()
    .isApplicantEligibleForLoan({
      args: {
        creditScore,
        annualIncomeUsd,
        debtToIncomeRatio,
        loanAmountRequestedUsd,
        riskScore,
        loanToValueRatio,
      },
      fallback: false,
    })

  waitUntil(hypertune.flushLogs())

  if (!isEligible) {
    return NextResponse.json(
      {
        error: 'Loan application denied',
        message: `Unfortunately, your loan application does not meet our eligibility criteria at this time. Please contact our support team for more information.`,
      },
      { status: 403 }
    )
  }

  const applicationId = await submitLoan(
    applicantId,
    loanAmountRequestedUsd
  )

  return NextResponse.json({
    success: true,
    message: `Congratulations! Your loan application has been approved. Our team will contact you within 24 hours to complete the process.`,
    applicationId,
  })
}
```

{% endcode %}

This empowers compliance, product, and ops to view and instantly update business rules from the Hypertune dashboard without any code changes or redeploys:

<figure><img src="https://2048905609-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWa3rQLiu4JZhBRkiyoKz%2Fuploads%2FIluDVdS2c3aD6pmADPvR%2Flocalhost_3000_projects_3357_main_draft_logic_setup%3D0%26selected_field_path%3Droot%253EbusinessRules%253EisApplicantEligibleForLoan%26schema_mode%3Dcode.png?alt=media&#x26;token=279f7afa-bdbf-4182-9f54-9961fcc4922f" alt="" width="375"><figcaption></figcaption></figure>

## Benefits

* Compliance, product, and ops teams:
  * Have visibility on business rules so can verify their correctness.
  * Can safely update business rules themselves.
  * Don't need to bother engineering with requests to update business rules.
* Business rules can be updated instantly without any code changes or app redeploys.
* Business rules are consolidated into a single source of truth across different codebases.
* Business rules can be tuned with experimentation to improve metrics while controlling risk.

## ROI

These benefits help teams:

* Stay compliant.
* Control risk.
* Improve key business metrics.
* Move faster with the same headcount.
