{
  "identifier": "2962",
  "rules": {
    "grade-1": [
      {
        "key": "rejected",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.status_logs.status",
                "primaryColumn": "application_id",
                "dbCondition": [
                  "where status='rejected'",
                  "order by created_at",
                  "limit 1"
                ]
              }
            }
          ],
          "against": [
            {
              "type": "text",
              "details": {
                "value": "rejected",
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 1
      },
      {
        "key": "pan_verfication",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.credit_report.panValidation",
                "primaryColumn": "application_id",
                "dbCondition": [
                  "where applicant_type='1'"
                ],
                "dbSelect": [
                  "REPLACE(JSON_EXTRACT(panValidation, '$.\"sch:PANStatus\"'), '\"', '')"
                ]
              }
            }
          ],
          "against": [
            {
              "type": "text",
              "details": {
                "value": "SUCCESS",
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 1
      },
      {
        "key": "max_loan_amount",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.loan_amount",
                "primaryColumn": "id",
                "condition": "greaterThanEqual"
              }
            }
          ]
        },
        "priority": 2
      },
      {
        "key": "min_loan_amount",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.loan_amount",
                "primaryColumn": "id",
                "condition": "lessThanEqual"
              }
            }
          ]
        },
        "priority": 2
      },
      {
        "key": "net_tenure",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.emi_calculation.remaining_emi",
                "primaryColumn": "application_id",
                "condition": "lessThanEqual"
              }
            }
          ]
        },
        "priority": 3
      },
      {
        "key": "credit_score",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.credit_report.Score",
                "primaryColumn": "application_id",
                "condition": "greaterThanEqual"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "foir",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.credit_report.cfFoir",
                "primaryColumn": "application_id",
                "condition": "lessThanEqual"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "monthly_income",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.monthly_income",
                "primaryColumn": "id",
                "condition": "lessThanEqual"
              }
            }
          ]
        }
      },
      {
        "key": "age",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.dob",
                "primaryColumn": "id",
                "condition": "lessThanEqual",
                "function": {
                  "call": "dateDiffInYear(var1, var2)",
                  "arg": [
                    "db.dob",
                    "fun.getCurrentDate"
                  ]
                }
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "employee_status",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.employee_status",
                "primaryColumn": "id"
              }
            }
          ],
          "against": [
            {
              "type": "list",
              "details": {
                "value": [
                  "SALARIED",
                  "UNEMPLOYED",
                  "HOUSEWIFE"
                ],
                "condition": "inList"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "average_balance",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.emi_calculation.emi",
                "primaryColumn": "application_id",
                "dbSelect": [
                  "2*emi"
                ]
              }
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.finbit_datas.average_balance",
                "primaryColumn": "application_id",
                "condition": "greaterThanEqual"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "negative_area",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.current_address_pincode",
                "primaryColumn": "id"
              }
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.pincodes.pincode",
                "dbCondition": [
                  "where pincode='db.source.current_address_pincode' limit 1"
                ],
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "BureauScore",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "s3",
              "details": {
                "value": "sch:Value",
                "config_accessors": "bureau",
                "condition": "greaterThanEqual"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "Surrogate",
        "value": {
          "source": [
            {
              "type": "text",
              "details": {
                "value": 1
              }
            }
          ],
          "against": [
            {
              "type": "s3",
              "details": {
                "value": "sch:Account",
                "config_accessors": "bureau",
                "function": {
                  "call": "check_surrogate_rule_batch(var1, var2, var3, var4)",
                  "arg": [
                    "sys.input.loan_amount",
                    "sys.input.bureau_created_at",
                    "sys.input.surrogate_percentage",
                    "s3.sch:Account"
                  ]
                },
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "merchant_eligibility",
        "value": {
          "source": [
            {
              "type": "text",
              "details": {
                "value": 1
              }
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.pincodes.pincode",
                "dbRaw": "select count(*) as app_count, SUM(approved_amount) as total_approved_amount from `consumer_application` where `merchant_id` = (select merchant_id from consumer_application where id = {sys.input.id}) and `state` in ('approved', 'disbursement checklist done', 'ready for disbursement', 'disbursed', 'nach registered', 'closed')",
                "function": {
                  "call": "is_merchant_eligible(var1, var2)",
                  "arg": [
                    "sys.input.loan_amount",
                    "db.total_approved_amount"
                  ]
                },
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 4
      }
    ],
    "grade-2": [
      {
        "key": "rejected",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.status_logs.status",
                "primaryColumn": "application_id",
                "dbCondition": [
                  "where status='rejected'",
                  "order by created_at",
                  "limit 1"
                ]
              }
            }
          ],
          "against": [
            {
              "type": "text",
              "details": {
                "value": "rejected",
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 1
      },
      {
        "key": "pan_verfication",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.credit_report.panValidation",
                "primaryColumn": "application_id",
                "dbCondition": [
                  "where applicant_type='1'"
                ],
                "dbSelect": [
                  "REPLACE(JSON_EXTRACT(panValidation, '$.\"sch:PANStatus\"'), '\"', '')"
                ]
              }
            }
          ],
          "against": [
            {
              "type": "text",
              "details": {
                "value": "SUCCESS",
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 1
      },
      {
        "key": "max_loan_amount",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.loan_amount",
                "primaryColumn": "id",
                "condition": "greaterThanEqual"
              }
            }
          ]
        },
        "priority": 2
      },
      {
        "key": "min_loan_amount",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.loan_amount",
                "primaryColumn": "id",
                "condition": "lessThanEqual"
              }
            }
          ]
        },
        "priority": 2
      },
      {
        "key": "net_tenure",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.emi_calculation.remaining_emi",
                "primaryColumn": "application_id",
                "condition": "lessThanEqual"
              }
            }
          ]
        },
        "priority": 3
      },
      {
        "key": "credit_score",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.credit_report.Score",
                "primaryColumn": "application_id",
                "condition": "greaterThanEqual"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "foir",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.credit_report.cfFoir",
                "primaryColumn": "application_id",
                "condition": "lessThanEqual"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "monthly_income",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.monthly_income",
                "primaryColumn": "id",
                "condition": "lessThanEqual"
              }
            }
          ]
        }
      },
      {
        "key": "age",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.dob",
                "primaryColumn": "id",
                "condition": "lessThanEqual",
                "function": {
                  "call": "dateDiffInYear(var1, var2)",
                  "arg": [
                    "db.dob",
                    "fun.getCurrentDate"
                  ]
                }
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "employee_status",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.employee_status",
                "primaryColumn": "id"
              }
            }
          ],
          "against": [
            {
              "type": "list",
              "details": {
                "value": [
                  "SALARIED",
                  "UNEMPLOYED",
                  "HOUSEWIFE"
                ],
                "condition": "inList"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "average_balance",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.emi_calculation.emi",
                "primaryColumn": "application_id",
                "dbSelect": [
                  "2*emi"
                ]
              }
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.finbit_datas.average_balance",
                "primaryColumn": "application_id",
                "condition": "greaterThanEqual"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "negative_area",
        "value": {
          "source": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.consumer_application.current_address_pincode",
                "primaryColumn": "id"
              }
            }
          ],
          "against": [
            {
              "type": "db",
              "details": {
                "value": "sourceFile.cf.pincodes.pincode",
                "dbCondition": [
                  "where pincode='db.source.current_address_pincode' limit 1"
                ],
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "BureauScore",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "s3",
              "details": {
                "value": "sch:Value",
                "config_accessors": "bureau",
                "condition": "greaterThanEqual"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "Surrogate",
        "value": {
          "source": [
            {
              "type": "text",
              "details": {
                "value": 1
              }
            }
          ],
          "against": [
            {
              "type": "s3",
              "details": {
                "value": "sch:Account",
                "config_accessors": "bureau",
                "function": {
                  "call": "check_surrogate_rule_batch(var1, var2, var3, var4)",
                  "arg": [
                    "sys.input.loan_amount",
                    "sys.input.bureau_created_at",
                    "sys.input.surrogate_percentage",
                    "s3.sch:Account"
                  ]
                },
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 4
      },
      {
        "key": "dpd",
        "value": {
          "source": [
            {
              "type": "key"
            }
          ],
          "against": [
            {
              "type": "s3",
              "details": {
                "value": "sch:Account",
                "config_accessors": "bureau",
                "function": {
                  "call": "get_dpd_from_date(var1, var2)",
                  "arg": [
                    "sys.input.bureau_pull_date",
                    "s3.sch:Account"
                  ]
                },
                "condition": "equal"
              }
            }
          ]
        },
        "priority": 4
      }
    ]
  },
  "storage": {},
  "autoload": {
    "config": {
      "s3": {
        "AWS_ACCESS_KEY_ID": "::getENV",
        "AWS_SECRET_ACCESS_KEY": "::getENV",
        "AWS_BUCKET_NAME": "creditfair-customer-documents-test"
      },
      "files": {
        "bureau": {
          "type": "s3",
          "misc": {
            "file": "{identifier}/{identifier}_response.json",
            "type": "experien"
          }
        }
      }
    }
  },
  "functions": {
    "lang": [
      {
        "php": [
          {
            "dateDiffInYear()": "private function dateDiffInYear($key, $key2){return (date('Y', strtotime($key2)) - date('Y',strtotime($key1))); }",
            "getCurrentDate()": "private function getCurrentDate(){return date('Y-m-d'); }"
          }
        ],
        "python": [
          {
            "dateDiffInYear()": "def dateDiffInYear(date1, date2):\n    from datetime import datetime\n    date1 = datetime.strptime(date1, \"%Y-%m-%d\")\n    date2 = datetime.strptime(date2, \"%Y-%m-%d\")\n    delta = date2 - date1\n    return int(delta.days / 365)\n",
            "getCurrentDate()": "from datetime import date\n\ndef getCurrentDate():\n    return date.today().strftime(\"%Y-%m-%d\")\n",
            "getSurrogate": "def getSurrogate(application, surrogate_percentage, foir):\n    from datetime import datetime\n    return int((1 if foir['sch:AccountType'] in ['Home Loan', 'Business Loan', 'Auto Loan'] else 0) and (1 if (datetime.strptime(foir.DateOpened, '%Y-%m-%d %H:%M:%S') - datetime.strptime(application.creditReport[0].created_at, '%Y-%m-%d %H:%M:%S')).days // 30 >= 12 else 0) and (1 if (datetime.strptime(foir.DateClosed, '%Y-%m-%d %H:%M:%S') - datetime.strptime(application.creditReport[0].created_at, '%Y-%m-%d %H:%M:%S')).days // 30 <= 60 else 0) and (1 if (foir.ApprovedAmount - foir.Balance) * surrogate_percentage / 100 >= application.approved_amount else 0))",
            "check_surrogate_rule_batch": "def check_surrogate_rule_batch(approved_amount, created_at, surrogate_percentage, accounts):\n    surrogate_checks = [check_surrogate_rule(approved_amount, created_at, surrogate_percentage, account) for account in accounts[0]]\n\n    if sum(surrogate_checks) == 0:\n        return 1\n    return 0",
            "parse_date": "def parse_date(date_str):\n    from datetime import datetime\n    if not date_str:\n        return ''\n    return datetime.strptime(date_str, '%Y-%m-%d')",
            "calculate_bureau_months": "def calculate_bureau_months(date1, date2):\n    if not date1 or not date2:\n        return 0\n    return (date1 - date2).days // 30",
            "check_loan_type": "def check_loan_type(loan_type, surrogate_loans):\n    return 1 if loan_type in surrogate_loans else 0",
            "check_open_date": "def check_open_date(bureau_open):\n    return 1 if bureau_open >= 12 else 0",
            "loan_sanction_amount": "def loan_sanction_amount(foir):\n    return foir['sch:SanctionAmount'] if 'sch:SanctionAmount' in foir else foir['sch:HighCredit']",
            "check_close_date": "def check_close_date(bureau_close):\n    return 1 if bureau_close <= 60 else 0",
            "check_amount": "def check_amount(sanctioned_percentage, requested_loan_amount):\n    return 1 if sanctioned_percentage >= requested_loan_amount else 0",
            "check_surrogate_rule": "def check_surrogate_rule(approved_amount, created_at, surrogate_percentage, foir):\n    print(foir['sch:AccountType'])\n\n    surrogate_loans = ['Home Loan', 'Business Loan', 'Auto Loan']\n\n    requested_loan_amount = approved_amount\n    bureau_pull_date = parse_date(created_at)\n    loan_type = foir['sch:AccountType']\n    loan_open_date = parse_date(foir['sch:DateOpened'] if 'sch:DateOpened' in foir else '')\n    loan_closed_date = parse_date(foir['sch:DateClosed'] if 'sch:DateClosed' in foir else '')\n    sanctioned_amount = float(loan_sanction_amount(foir)) - float(foir['sch:Balance'])\n    bureau_open = calculate_bureau_months(loan_open_date, bureau_pull_date)\n    bureau_close = calculate_bureau_months(loan_closed_date, bureau_pull_date)\n    sanctioned_percentage = sanctioned_amount * surrogate_percentage / 100\n\n    loan_type_check = check_loan_type(loan_type, surrogate_loans)\n    open_date_check = check_open_date(bureau_open)\n    close_date_check = check_close_date(bureau_close)\n    amount_check = check_amount(sanctioned_percentage, requested_loan_amount)\n\n    combined_check = 1 if all([loan_type_check, open_date_check, close_date_check, amount_check]) else 0\n    return combined_check",
            "get_max": "def getMax(lists):\n    numeric_values = [int(item) for sublist in lists.values() for item in sublist if item.isdigit()]\n    if numeric_values:\n        return max(numeric_values)\n    else:\n        return 0",
            "get_dpd_from_date": "from datetime import datetime\n\ndef get_dpd_from_date(from_date, accounts):\n    dpdLists = {}\n    for account in accounts[0]:\n        if account['sch:Open'] == 'Yes':\n            dpdData = [\n                remove_special_symbols(MonthWise['sch:PaymentStatus'])\n                for index, History in account['sch:History48Months'].items()\n                for MonthWise in History\n                if datetime.strptime(MonthWise['@key'], '%m-%y') > datetime.strptime(from_date, '%Y-%m-%d')\n            ]\n            dpdLists[account['@seq']] = dpdData\n    return getMax(dpdLists)",
            "remove_special_symbols": "import re\n\ndef remove_special_symbols(input_string):\n    pattern = r'[^\\w\\s]'\n    result = re.sub(pattern, '', input_string)\n    return result",
            "is_merchant_eligible": "def is_merchant_eligible(loan_amount, total_amount):\n    print('is_merchant_eligible')\n    if loan_amount > total_amount:\n        return 0\n    return 1"
          }
        ]
      }
    ]
  },
  "sources": {
    "sourceFile": {
      "cf": "cfwebbeta",
      "loc": "line_of_credit"
    },
    "connections": [
      {
        "db": "cfwebbeta",
        "status": true,
        "table": [
          {
            "consumer_application": [
              "loan_amount",
              "monthly_income",
              "dob",
              "employee_status"
            ],
            "emi_calculation": [
              "remaining_emi"
            ],
            "credit_report": [
              "Score",
              "panValidation"
            ],
            "foir": [
              "CFFoir"
            ],
            "status_logs": [
              "rejected"
            ]
          }
        ]
      },
      {
        "db": "line_of_credit",
        "table": [
          {
            "applications": [
              "loan_amount"
            ]
          }
        ]
      }
    ]
  }
}