{
      "rejected": {
                        "key": "rejected",
                        "description": [
                            "If the application is ever aborted or rejected, then case has failed, else passed",
                            "If the field is not checked, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.status_logs.status",
                                        "primaryColumn": "application_id",
                                        "dbCondition": [
                                            "where status in ('rejected', 'aborted')",
                                            "order by created_at",
                                            "limit 1"
                                        ]
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "list",
                                    "details": {
                                        "value": [
                                            "rejected",
                                            "aborted"
                                        ],
                                        "condition": "notInList"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "max_loan_amount": {
                        "key": "max_loan_amount",
                        "description": [
                            "If the application\u2019s loan amount <= Max loan amount, then the rule has passed, else fail",
                            "Eg. If the requested loan amount is 500000 and the max loan amount is 250000, then it has failed this rule"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.consumer_application.loan_amount",
                                        "primaryColumn": "id",
                                        "condition": "lessThanEqual"
                                    }
                                }
                            ]
                        },
                        "priority": 2
                    },
    "min_loan_amount": {
                        "key": "min_loan_amount",
                        "description": [
                            "If the application\u2019s loan amount >= Min loan amount, then the rule has passed, else fail",
                            "Eg. If the requested loan amount is 5000 and the min loan amount is 25000, then it has failed this rule"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.consumer_application.loan_amount",
                                        "primaryColumn": "id",
                                        "condition": "greaterThanEqual"
                                    }
                                }
                            ]
                        },
                        "priority": 2
                    },
    "tenure": {
                        "key": "net_tenure",
                        "description": [
                            "If the application\u2019s net tenure <= net tenure field, then the rule has passed, else failed"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.emi_calculation.remaining_emi",
                                        "primaryColumn": "application_id",
                                        "dbCondition": [
                                            "order by created_at desc",
                                            "limit 1"
                                        ],
                                        "condition": "lessThanEqual"
                                    }
                                }
                            ]
                        },
                        "priority": 3
                    },
    "score": {
                        "key": "credit_score",
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.credit_report.Score",
                                        "primaryColumn": "application_id",
                                        "dbCondition": [
                                            "where applicant_type = 1"
                                        ],
                                        "condition": "greaterThanEqual"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "cf_foir": {
                        "key": "cfFoir",
                        "description": [
                            "If the application\u2019s CF FOIR > max CF FOIR, then the rule has failed, else pass"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.credit_report.cfFoir",
                                        "primaryColumn": "application_id",
                                        "condition": "lessThanEqual"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "foir": {
                        "key": "foir",
                        "description": [
                            "If the application\u2019s FOIR > max FOIR, then the rule has failed, else pass"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.credit_report.FoirIncludingCFEMI",
                                        "primaryColumn": "application_id",
                                        "condition": "lessThanEqual"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "monthly_income": {
                        "key": "monthly_income",
                        "description": [
                            "If the application\u2019s monthly income >= Monthly Income, then the rule has passed, else failed"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.consumer_application.monthly_income",
                                        "primaryColumn": "id",
                                        "condition": "greaterThanEqual"
                                    }
                                }
                            ]
                        }
                    },
    "age": {
                        "key": "age",
                        "description": [
                            "If the application\u2019s age >= Min Age, then pass else fail",
                            "If the value is null, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.consumer_application.dob",
                                        "primaryColumn": "id",
                                        "condition": "greaterThanEqual",
                                        "function": {
                                            "call": "dateDiffInYear(var1, var2)",
                                            "arg": [
                                                "db.dob",
                                                "fun.getCurrentDate"
                                            ]
                                        }
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "ageAtLastEMI": {
                        "key": "ageAtLastEMI",
                        "description": [
                            "Calculate the date difference between last EMI date and the DoB",
                            "If this age is > input, then fail, else pass",
                            "If the field is blank, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.consumer_application.dob",
                                        "dbRaw": "select ca.dob, date(DATE_ADD(emi_calculation.emi_date, INTERVAL emi_calculation.remaining_emi month)) as last_emi_date from consumer_application as ca left join consumer_application_element cae on ca.id = cae.application_id left join emi_calculation on emi_calculation.application_id = cae.application_id where ca.id = {sys.input.identifier} limit 1",
                                        "condition": "lessThanEqual",
                                        "function": {
                                            "call": "dateDiffInYear(var1, var2)",
                                            "arg": [
                                                "db.dob",
                                                "db.last_emi_date"
                                            ]
                                        }
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "employee_status": {
                        "key": "employee_status",
                        "description": [
                            "If the application\u2019s employment is present in the selected dropdown, then the rule is passed else failed",
                            "If none are selected in the dropdown, then this rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.consumer_application.employee_status",
                                        "primaryColumn": "id"
                                    },
                                    "optional": 1
                                }
                            ],
                            "against": [
                                {
                                    "type": "key",
                                    "details": {
                                        "condition": "inList"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "check_duplicates": {
                        "key": "de_dupe_check",
                        "description": [
                            "If the application\u2019s PAN card \/ Mobile Number \/ Email ID is present previously then the case has failed, else passed",
                            "If the field is not checked, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf..checker",
                                        "dbRaw": "select case when identifier_details.email is not null and identifier_details.mobile_number is not null and identifier_details.pan_card is not null then max(case when email_match.email is null and mobile_number_match.mobile_number is null and pan_match.pan_card is null then 1 else 0 end) else 0 end as checker from ( select id, email, mobile_number, pan_card from consumer_application where id = {sys.input.identifier}) identifier_details left join (select email from consumer_application where id !={sys.input.identifier} and datediff(curdate(), created_at)<=90 and is_deleted is null) email_match on email_match.email = identifier_details.email left join (select mobile_number from consumer_application where id !={sys.input.identifier} and datediff(curdate(), created_at)<=90 and is_deleted is null) mobile_number_match on mobile_number_match.mobile_number = identifier_details.mobile_number left join (select pan_card from consumer_application where id !={sys.input.identifier} and datediff(curdate(), created_at)<=90 and is_deleted is null) pan_match on pan_match.pan_card = identifier_details.pan_card",
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "guarantor": {
                        "key": "guarantor",
                        "description": [
                            "If the application has zero loans, then pass",
                            "If the application has at least 1 non-guarantor loan then pass else fail",
                            "If the field is not checked, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf..decision",
                                        "dbRaw": "select max(case when foir.OwnershipType is null or foir.OwnershipType!='Guarantor' then 1 else 0 end) as decision from (select id from consumer_application where consumer_application.id ={sys.input.identifier}) consumer_application left join (select foir.application_id , foir.OwnershipType from foir where deleted_at is null AND foir.accounttype!='Credit Fair EMI' and foir.applicant_type = {sys.input.applicant_type}) foir on foir.application_id = consumer_application.id",
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "caseType": {
                        "key": "case_type_check",
                        "description": [
                            "If any of the mentioned values are present in the DPD of the tradeline then fail, else pass",
                            "If the field is blank, then the rule is to be skipped",
                            "Eg. Inputs -> PWOS,SET"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.null.null",
                                        "dbRaw": "SELECT JSON_ARRAYAGG(JSON_OBJECT('dpd', foir.dpd)) AS foir_data FROM foir WHERE application_id = {sys.input.identifier} and foir.applicant_type = {sys.input.applicant_type}",
                                        "primaryColumn": "application_id",
                                        "function": {
                                            "call": "case_type_check(var1, var2)",
                                            "arg": [
                                                "sys.input.case_type",
                                                "db.foir_data"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "dpd_count_above_zero": {
                        "key": "dpd",
                        "fallback": {
                            "rule": "dpd_check_equifax"
                        },
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "s3",
                                    "details": {
                                        "value": "CAIS_Account_DETAILS",
                                        "config_accessors": "bureau",
                                        "function": {
                                            "call": "dpd_check_experian(var1, var2, var3)",
                                            "arg": [
                                                "sys.input.dpd_years_above_0",
                                                "sys.input.dpd_count_above_0",
                                                "s3.CAIS_Account_DETAILS"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "dpd_years_above_zero": {
                        "key": "dpd",
                        "fallback": {
                            "rule": "dpd_check_equifax"
                        },
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "s3",
                                    "details": {
                                        "value": "CAIS_Account_DETAILS",
                                        "config_accessors": "bureau",
                                        "function": {
                                            "call": "dpd_check_experian(var1, var2, var3)",
                                            "arg": [
                                                "sys.input.dpd_years_above_0",
                                                "sys.input.dpd_count_above_0",
                                                "s3.CAIS_Account_DETAILS"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "dpd_check_equifax": {
                        "key": "dpd_check_equifax",
                        "fallback": true,
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "s3",
                                    "details": {
                                        "value": "sch:Account",
                                        "config_accessors": "bureau",
                                        "function": {
                                            "call": "dpd_check_equifax(var1, var2, var3)",
                                            "arg": [
                                                "sys.input.dpd_years_above_0",
                                                "sys.input.dpd_count_above_0",
                                                "s3.sch:Account"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "past_dues": {
                        "key": "past_due_amount",
                        "description": [
                            "If total past due amount >= input, then fail else pass",
                            "If the field is blank, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.credit_report.TotalPastDue",
                                        "primaryColumn": "application_id",
                                        "condition": "lessThanEqual"
                                    },
                                    "optional": 1

                                }
                            ]
                        },
                        "priority": 1
                    },
    "pre_dpd_count": {
                        "key": "pre_dpd_check",
                        "description": [
                            "3 Inputs:",
                            [
                                "Year (Numeric)",
                                "Keywords (Characters)",
                                "Count (Numeric)"
                            ],
                            "Logic:",
                            [
                                "Extract all the tradelines opened in last [Year]",
                                "For each tradeline sum the number of times DPD is not part of [Keywords]",
                                "If this sum >= count, then fail else pass",
                                "If all the above fields are blank then skip"
                            ]
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.null.null",
                                        "dbRaw": "select JSON_ARRAYAGG(JSON_OBJECT('DateOpened', DATE_FORMAT(foir.DateOpened, '%Y-%m-%d'), 'created_at', DATE_FORMAT(foir.created_at, '%Y-%m-%d'), 'dpd', foir.dpd)) as foir_data from foir where application_id = {sys.input.identifier} and foir.applicant_type = {sys.input.applicant_type}",
                                        "primaryColumn": "application_id",
                                        "function": {
                                            "call": "pre_dpd_check(var1, var2, var3, var4)",
                                            "arg": [
                                                "sys.input.pre_dpd_count",
                                                "sys.input.pre_dpd_years",
                                                "sys.input.pre_dpd_keywords",
                                                "db.foir_data"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "pre_dpd_years": {
                        "key": "pre_dpd_check",
                        "description": [
                            "3 Inputs:",
                            [
                                "Year (Numeric)",
                                "Keywords (Characters)",
                                "Count (Numeric)"
                            ],
                            "Logic:",
                            [
                                "Extract all the tradelines opened in last [Year]",
                                "For each tradeline sum the number of times DPD is not part of [Keywords]",
                                "If this sum >= count, then fail else pass",
                                "If all the above fields are blank then skip"
                            ]
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.null.null",
                                        "dbRaw": "select JSON_ARRAYAGG(JSON_OBJECT('DateOpened', DATE_FORMAT(foir.DateOpened, '%Y-%m-%d'), 'created_at', DATE_FORMAT(foir.created_at, '%Y-%m-%d'), 'dpd', foir.dpd)) as foir_data from foir where application_id = {sys.input.identifier} and foir.applicant_type = {sys.input.applicant_type}",
                                        "primaryColumn": "application_id",
                                        "function": {
                                            "call": "pre_dpd_check(var1, var2, var3, var4)",
                                            "arg": [
                                                "sys.input.pre_dpd_count",
                                                "sys.input.pre_dpd_years",
                                                "sys.input.pre_dpd_keywords",
                                                "db.foir_data"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "pre_dpd_keywords": {
                        "key": "pre_dpd_check",
                        "description": [
                            "3 Inputs:",
                            [
                                "Year (Numeric)",
                                "Keywords (Characters)",
                                "Count (Numeric)"
                            ],
                            "Logic:",
                            [
                                "Extract all the tradelines opened in last [Year]",
                                "For each tradeline sum the number of times DPD is not part of [Keywords]",
                                "If this sum >= count, then fail else pass",
                                "If all the above fields are blank then skip"
                            ]
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.null.null",
                                        "dbRaw": "select JSON_ARRAYAGG(JSON_OBJECT('DateOpened', DATE_FORMAT(foir.DateOpened, '%Y-%m-%d'), 'created_at', DATE_FORMAT(foir.created_at, '%Y-%m-%d'), 'dpd', foir.dpd)) as foir_data from foir where application_id = {sys.input.identifier} and foir.applicant_type = {sys.input.applicant_type}",
                                        "primaryColumn": "application_id",
                                        "function": {
                                            "call": "pre_dpd_check(var1, var2, var3, var4)",
                                            "arg": [
                                                "sys.input.pre_dpd_count",
                                                "sys.input.pre_dpd_years",
                                                "sys.input.pre_dpd_keywords",
                                                "db.foir_data"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "post_dpd_count": {
                        "key": "post_dpd_check",
                        "description": [
                            "3 Inputs:",
                            [
                                "Year (Numeric)",
                                "Keywords (Characters)",
                                "Count (Numeric)"
                            ],
                            "Logic:",
                            [
                                "Extract all the tradelines opened before [Year]",
                                "For each tradeline sum the number of times DPD is not part of [Keywords]",
                                "If this sum >= count, then fail else pass",
                                "If all the above fields are blank then skip"
                            ]
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.null.null",
                                        "dbRaw": "select JSON_ARRAYAGG(JSON_OBJECT('DateOpened', DATE_FORMAT(foir.DateOpened, '%Y-%m-%d'), 'created_at', DATE_FORMAT(foir.created_at, '%Y-%m-%d'), 'dpd', foir.dpd)) as foir_data from foir where application_id = {sys.input.identifier} and foir.applicant_type = {sys.input.applicant_type}",
                                        "primaryColumn": "application_id",
                                        "function": {
                                            "call": "post_dpd_check(var1, var2, var3, var4)",
                                            "arg": [
                                                "sys.input.post_dpd_count",
                                                "sys.input.post_dpd_year",
                                                "sys.input.post_dpd_keywords",
                                                "db.foir_data"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "post_dpd_year": {
                        "key": "post_dpd_check",
                        "description": [
                            "3 Inputs:",
                            [
                                "Year (Numeric)",
                                "Keywords (Characters)",
                                "Count (Numeric)"
                            ],
                            "Logic:",
                            [
                                "Extract all the tradelines opened before [Year]",
                                "For each tradeline sum the number of times DPD is not part of [Keywords]",
                                "If this sum >= count, then fail else pass",
                                "If all the above fields are blank then skip"
                            ]
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.null.null",
                                        "dbRaw": "select JSON_ARRAYAGG(JSON_OBJECT('DateOpened', DATE_FORMAT(foir.DateOpened, '%Y-%m-%d'), 'created_at', DATE_FORMAT(foir.created_at, '%Y-%m-%d'), 'dpd', foir.dpd)) as foir_data from foir where application_id = {sys.input.identifier} and foir.applicant_type = {sys.input.applicant_type}",
                                        "primaryColumn": "application_id",
                                        "function": {
                                            "call": "post_dpd_check(var1, var2, var3, var4)",
                                            "arg": [
                                                "sys.input.post_dpd_count",
                                                "sys.input.post_dpd_year",
                                                "sys.input.post_dpd_keywords",
                                                "db.foir_data"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "post_dpd_keywords": {
                        "key": "post_dpd_check",
                        "description": [
                            "3 Inputs:",
                            [
                                "Year (Numeric)",
                                "Keywords (Characters)",
                                "Count (Numeric)"
                            ],
                            "Logic:",
                            [
                                "Extract all the tradelines opened before [Year]",
                                "For each tradeline sum the number of times DPD is not part of [Keywords]",
                                "If this sum >= count, then fail else pass",
                                "If all the above fields are blank then skip"
                            ]
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.null.null",
                                        "dbRaw": "select JSON_ARRAYAGG(JSON_OBJECT('DateOpened', DATE_FORMAT(foir.DateOpened, '%Y-%m-%d'), 'created_at', DATE_FORMAT(foir.created_at, '%Y-%m-%d'), 'dpd', foir.dpd)) as foir_data from foir where application_id = {sys.input.identifier} and foir.applicant_type = {sys.input.applicant_type}",
                                        "primaryColumn": "application_id",
                                        "function": {
                                            "call": "post_dpd_check(var1, var2, var3, var4)",
                                            "arg": [
                                                "sys.input.post_dpd_count",
                                                "sys.input.post_dpd_year",
                                                "sys.input.post_dpd_keywords",
                                                "db.foir_data"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "track_record":  {
                        "key": "track_record",
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf..valid_tradeline_exists",
                                        "dbRaw": "select max(case when ownershiptype = 'Individual' AND (((DateClosed is null or DateClosed>=DATE_SUB(curdate(), INTERVAL 3 year)) and accounttype not in ( 'Gold Loan', 'Loan against Shares\/Securities')) OR (DateOpened<=DATE_SUB(curdate(), INTERVAL 6 month) and (accounttype = 'Credit Card'))) AND min_appr_amt<=approvedamount then 1 else 0 end) as valid_tradeline_exists from (select consumer_application.id ,least((approved_amount *0.5) , 50000) as min_appr_amt from consumer_application where id = {sys.input.identifier} ) consumer_application left join (select foir.application_id , foir.OwnershipType , foir.DateClosed , foir.AccountType , foir.DateOpened , foir.ApprovedAmount from foir where deleted_at is null and foir.applicant_type = {sys.input.applicant_type}) foir on foir.application_id = consumer_application.id",
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "average_balance": {
                        "key": "average_balance",
                        "value": {
                            "source": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.emi_calculation.emi",
                                        "primaryColumn": "application_id",
                                        "dbSelect": [
                                            "2*emi"
                                        ],
                                        "dbCondition": [
                                            "order by created_at desc",
                                            "limit 1"]
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.finbit_datas.average_balance",
                                        "primaryColumn": "application_id",
                                        "condition": "greaterThanEqual"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "negative_area": {
                    "key": "negative_area",
                    "description": [
                        "If application’s current address / application's permanent address / co-application’s address is part of the level 1 negative area list, then fail else pass",
                        "If the field is not checked, then the rule is to be skipped"
                    ],
                    "value": {
                        "source": [
                            {
                                "type": "text",
                                "details": {
                                    "value": 1
                                }
                            }
                        ],
                        "against": [
                            {
                                "type": "db",
                                "details": {
                                    "value": "sourceFile.cf..checker",
                                    "dbRaw": "SELECT CASE WHEN consumer_application.current_address_pincode is not null and consumer_application.permanent_address_pincode is not null then CASE WHEN current_check.pincode IS NULL AND permanent_check.pincode IS NULL AND coapp_check.pincode IS NULL THEN 1 ELSE 0 end else 0 END AS checker FROM consumer_application LEFT JOIN locations current_check ON current_check.pincode COLLATE utf8mb4_unicode_ci = consumer_application.current_address_pincode COLLATE utf8mb4_unicode_ci LEFT JOIN locations permanent_check ON permanent_check.pincode COLLATE utf8mb4_unicode_ci = consumer_application.permanent_address_pincode COLLATE utf8mb4_unicode_ci LEFT JOIN locations coapp_check ON coapp_check.pincode COLLATE utf8mb4_unicode_ci = consumer_application.co_applicant_address_pincode COLLATE utf8mb4_unicode_ci WHERE consumer_application.id = {sys.input.identifier} LIMIT 1",
                                    "condition": "equal"
                                }
                            }
                        ]
                    },
                    "priority": 4
                    },
    "bureau_surrogate": {
                        "key": "surrogate_check_experian",
                        "fallback": {
                            "rule": "surrogate_check_equifax"
                            },
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "s3",
                                    "details": {
                                        "value": ["CAIS_Account_DETAILS", "ReportDate"],
                                        "config_accessors": "bureau",
                                        "function": {
                                            "call": "check_surrogate_rule_batch_experian(var1, var2, var3, var4)",
                                            "arg": [
                                                "sys.input.loan_amount",
                                                "s3.ReportDate",
                                                "sys.input.surrogate_percentage",
                                                "s3.CAIS_Account_DETAILS"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "surrogate_check_equifax": {
                        "key": "surrogate_check_equifax",
                        "fallback": true,
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "s3",
                                    "details": {
                                        "value": ["sch:Account", "sch:Date"],
                                        "config_accessors": "bureau",
                                        "function": {
                                            "call": "check_surrogate_rule_batch_equifax(var1, var2, var3, var4)",
                                            "arg": [
                                                "sys.input.loan_amount",
                                                "s3.sch:Date",
                                                "sys.input.surrogate_percentage",
                                                "s3.sch:Account"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "accountType": {
                        "key": "account_type_check",
                        "description": [
                            "If >= 3 open gold loans are present then fail, else pass",
                            "If the field is not checked, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.null.null",
                                        "dbRaw": "SELECT JSON_ARRAYAGG(JSON_OBJECT('AccountType', foir.AccountType,'DateClosed', case when foir.DateClosed is null then '-' else foir.DateClosed end)) AS foir_data FROM foir WHERE application_id = {sys.input.identifier} AND foir.AccountType = 'Gold Loan' AND foir.applicant_type = {sys.input.applicant_type} limit 1",
                                        "function": {
                                            "call": "account_type_check(var1, var2, var3)",
                                            "arg": [
                                                "db.foir_data"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "no_owned_house": {
                        "key": "no_owned_house_check",
                        "description": [
                            "If current address is not present OR",
                            "Else If current address is present but isn’t",
                            [
                                "OWNED",
                                "PARENTS HOUSE",
                                "Self Owned",
                                "Parent Owned"
                            ],
                            "Then fail, else pass",
                            "If the field is not checked, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.consumer_application.current_address_status",
                                        "primaryColumn": "id"
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "list",
                                    "details": {
                                        "value": [
                                            "OWNED",
                                            "PARENTS HOUSE",
                                            "Self Owned",
                                            "Parent Owned"
                                        ],
                                        "condition": "inList"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "zero_downpayment": {
                        "key": "zero_downpayment_check",
                        "description": [
                            "If EMI is not generated, then mention reason as “EMI not generated” and fail",
                            "Else if EMI is generated and upfront from customer is <= 0, then fail as “zero downpayment”",
                            "Else Pass",
                            "If the field is not checked, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.emi_calculation.upfront_from_customer",
                                        "primaryColumn": "application_id",
                                        "dbCondition": [
                                            "order by created_at desc",
                                            "limit 1"],
                                        "condition": "greaterThan"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "fetch_gst": {
                        "key": "fetch_gst_using_pan",
                        "description": [
                            "If GST verification is checked then if both of the below conditions is true:",
                            [
                                "If applicant’s employment type is 'business', 'self employed - business', 'self employed - professional'",
                                "GST authStatus is ‘Inactive’"
                            ],
                            "Then the case has failed, else passed",
                            "If the field is not checked, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf..active_status",
                                        "dbRaw": "select case when consumer_application.employee_status in ('business', 'self employed - business', 'self employed - professional') then (case when data is not null then (case when json_unquote(json_extract(data, '$.result[0].authStatus')) ='Inactive' then 0 else 1 end)else 0 end) else 1 end as active_status from (select * from consumer_application where consumer_application.id = {sys.input.identifier}) consumer_application left join (select * from (select *, row_number() over(partition by application_id order by created_at desc) row_no from gst_pan_data where deleted_at is null) gst_pan_data where row_no = 1) gst_pan_data on gst_pan_data.application_id = consumer_application.id",
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 4
                    },
    "term_loan_amount_paid": {
                        "key": "term_loan_check",
                        "description": [
                            "Loan period is > 0 Years",
                            "Loan is either ['Auto Loan', 'Housing Loan', 'Personal Loan', 'Consumer Loan', 'Education Loan', 'Loan to Professional', 'Two-wheeler Loan', 'Commercial Vehicle Loan', 'Used Car Loan', 'Business Loan-Priority Sector-Small Business', 'Business Loan - Priority Sector- Agriculture', 'Business Loan - Priority Sector- Others', 'Business Non-Funded Credit Facility', 'Business Non-Funded Credit Facility - Priority', 'Staff Loan']",
                            "The loan should be active/open",
                            "Extract the maximum of  Amount Paid ",
                            "If the max(Amount Paid) > requested loan amount * [amount paid], then pass else fail",
                            "If the input in the field is not present, then the rule is to be skipped"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "text",
                                    "details": {
                                        "value": 1
                                    }
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.null.null",
                                        "dbRaw": "SELECT IF (credit_report.Score < 10, 1, 0) as is_thin_file , JSON_ARRAYAGG( JSON_OBJECT( 'DateOpened', case when DATE_FORMAT(foir.DateOpened, '%Y-%m-%d') is null then '-' else DATE_FORMAT(foir.DateOpened, '%Y-%m-%d') end , 'DateClosed', case when DATE_FORMAT(foir.DateClosed , '%Y-%m-%d') is null then '-' else DATE_FORMAT(foir.DateClosed, '%Y-%m-%d') end , 'dpd', foir.dpd, 'Balance', foir.Balance , 'ApprovedAmount', foir.ApprovedAmount , 'AccountType', REPLACE(REPLACE(foir.AccountType, ']', ''), '[', '') ) ) AS foir_data FROM credit_report inner join (select * from foir where deleted_at is null) foir on foir.application_id = credit_report.application_id WHERE credit_report.application_id = {sys.input.identifier} AND credit_report.applicant_type = {sys.input.applicant_type} AND credit_report.deleted_at is null",
                                        "primaryColumn": "application_id",
                                        "function": {
                                            "call": "is_term_loan(var1, var2, var3, var4)",
                                            "arg": [
                                                "db.foir_data",
                                                "db.is_thin_file",
                                                "sys.config.term_loans",
                                                "fun.getCurrentDate",
                                                "sys.input.term_loan_amount_paid_percentage",
                                                "sys.input.loan_amount"
                                            ]
                                        },
                                        "condition": "equal"
                                    }
                                }
                            ]
                        },
                        "priority": 1
                    },
    "is_pincode_approved":  {
                    "key": "is_pincode_approved",
                    "description": [
                        "Both the condition is true: If current address pincode is part of the approved pincode list and If permanent address pincode is past of the approved pincode list Then the case has passed, else failed"
                    ],
                    "value": {
                        "source": [
                            {
                                "type": "text",
                                "details": {
                                    "value": 1
                                }
                            }
                        ],
                        "against": [
                            {
                                "type": "db",
                                "details": {
                                    "value": "sourceFile.cf..checker",
                                    "dbRaw": "select case when current_check.pin_codes is not null and permanent_check.pin_codes is not null then 1 else 0 end as checker from consumer_application left join approved_pin_codes current_check on current_check.pin_codes = consumer_application.current_address_pincode and current_check.deleted_at is null left join approved_pin_codes permanent_check on permanent_check.pin_codes = consumer_application.permanent_address_pincode and permanent_check.deleted_at is null where consumer_application.id = {sys.input.identifier} limit 1",
                                    "condition": "equal"
                                }
                            }
                        ]
                    },
                    "priority": 4
                    },
    "bank_validation": {
                    "key": "bank_validation",
                    "description": [
                        "Both the condition is true: If current address pincode is part of the approved pincode list and If permanent address pincode is past of the approved pincode list Then the case has passed, else failed"
                    ],
                    "value": {
                        "source": [
                            {
                                "type": "text",
                                "details": {
                                    "value": 1
                                }
                            }
                        ],
                        "against": [
                            {
                                "type": "db",
                                "details": {
                                    "value": "sourceFile.cf.null.null",
                                    "dbRaw": "SELECT consumer_application.id AS application_id, consumer_application.name, CASE WHEN JSON_VALID(karza_response) and karza_response like '%bankResponse%' THEN JSON_UNQUOTE(JSON_EXTRACT(karza_response, '$.result.bankResponse')) ELSE '' END AS transaction_status, CASE WHEN JSON_VALID(karza_response) and karza_response like '%accountName%' THEN JSON_UNQUOTE(JSON_EXTRACT(karza_response, '$.result.accountName')) ELSE '' END AS account_name FROM bank LEFT JOIN consumer_application ON consumer_application.id = bank.application_id WHERE application_id = {sys.input.identifier} ORDER BY bank.created_at DESC LIMIT 1;",
                                    "primaryColumn": "application_id",
                                    "function": {
                                        "call": "bank_validation_check(var1, var2, var3, var4)",
                                        "arg": [
                                            "sys.input.bank_validation",
                                            "db.transaction_status",
                                            "db.name",
                                            "db.account_name"
                                        ]
                                    },
                                    "condition": "equal"
                                }
                            }
                        ]
                    },
                    "priority": 4
                    },
    "epf_verification": {
                    "key": "employment_verification",
                    "description": [
                        "If EPF verification is checked then in the EPF-report check if any of the below conditions is true:",
                        [
                            "isEmployed is false",
                            "uanNameMatch is false",
                            "employerName doesn’t match the name mentioned in our application"
                        ],
                        "Then the application has failed, else pass",
                        "If the field is not checked then the rule is to be skipped"
                    ],
                    "value": {
                        "source": [
                            {
                                "type": "text",
                                "details": {
                                    "value": 1
                                }
                            }
                        ],
                        "against": [
                            {
                                "type": "db",
                                "details": {
                                    "value": "sourceFile.cf.employment_verification.data",
                                    "primaryColumn": "application_id",
                                    "function": {
                                        "call": "is_employment_verification(var1)",
                                        "arg": [
                                            "db.data"
                                        ]
                                    },
                                    "condition": "equal"
                                }
                            }
                        ]
                    },
                    "priority": 4
                },
    "credit_card_amount_paid": {
                    "key": "credit_card_amount",
                    "description": [
                        "Extract all the open credit cards",
                        "Calculate Amount Paid",
                        "Identify the maximum Amount Paid",
                        "If the max(Amount Paid) > requested loan amount * [amount paid], then pass else fail",
                        "If the input in the field is not present, then the rule is to be skipped"
                    ],
                    "value": {
                        "source": [
                            {
                                "type": "text",
                                "details": {
                                    "value": 1
                                }
                            }
                        ],
                        "against": [
                            {
                                "type": "db",
                                "details": {
                                    "value": "sourceFile.cf.null.null",
                                    "dbRaw": "SELECT ca.loan_amount,JSON_ARRAYAGG(JSON_OBJECT('AccountType', foir.AccountType ,'ApprovedAmount', foir.ApprovedAmount ,'Balance', foir.Balance ,'DateClosed', case when foir.DateClosed is null then '-' else foir.DateClosed end ,'InstallmentAmount', foir.InstallmentAmount)) AS foir_data FROM consumer_application AS ca INNER JOIN foir ON foir.application_id = ca.id WHERE ca.id = {sys.input.identifier} AND foir.AccountType = 'Credit Card' and foir.applicant_type = {sys.input.applicant_type}",
                                    "function": {
                                        "call": "credit_card_check(var1, var2, var3)",
                                        "arg": [
                                            "sys.input.credit_card_percentage",
                                            "db.foir_data",
                                            "db.loan_amount"
                                        ]
                                    },
                                    "condition": "equal"
                                }
                            }
                        ]
                    },
                    "priority": 4
                },
    "power_savings" : {
                        "key": "inverter_power_savings",
                        "description": [
                            "Check if the EMI should be less than or equal to the power saving"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "db",
                                    "details": {
                                        "value": "sourceFile.cf.emi_calculation.emi",
                                        "primaryColumn": "application_id",
                                        "dbCondition": [
                                            "order by created_at desc",
                                            "limit 1"
                                        ],
                                        "condition": "lessThanEqual"
                                    }
                                }
                            ]
                        },
                        "priority": 2
                    },
                    "min_banking_months": {
                        "key": "min_banking_months",
                        "description": [
                            "Checks if the bank statement has enough history",
                            "Pass if Actual Months >= Configured Months"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "s3",
                                    "details": {
                                        "config_accessors": "pirimid",
                                        "value": "analytics",
                                        "function": {
                                            "call": "get_banking_month_count(var1)",
                                            "arg": [
                                                "s3.analytics"
                                            ]
                                        },
                                        "condition": "greaterThanEqual"
                                    }
                                }
                            ],
                            "message": "Insufficient banking history duration"
                        },
                        "priority": 3
                    },
                    "name_match_threshold_pct": {
                        "key": "name_match_threshold_pct",
                        "description": [
                            "Fuzzy matches Applicant Name vs Bank Account Name",
                            "Pass if Match Score >= Configured Threshold"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "s3",
                                    "details": {
                                        "config_accessors": "pirimid",
                                        "value": "analytics",
                                        "function": {
                                            "call": "get_name_match_score(var1, var2)",
                                            "arg": [
                                                "s3.analytics",
                                                "sys.input.applicant_name"
                                            ]
                                        },
                                        "condition": "greaterThanEqual"
                                    }
                                }
                            ],
                            "message": "Bank account name does not match applicant name"
                        },
                        "priority": 2
                    },
                    "emi_bounce_rule": {
                        "key": "emi_bounce_rule",
                        "description": [
                            "Counts EMI bounces in the last X months",
                            "Pass if Actual Bounces <= Configured Limit"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "s3",
                                    "details": {
                                        "config_accessors": "pirimid",
                                        "value": "analytics",
                                        "function": {
                                            "call": "get_recent_bounce_count(var1, var2)",
                                            "arg": [
                                                "s3.analytics",
                                                "sys.input.emi_bounce_lookback_months"
                                            ]
                                        },
                                        "condition": "lessThanEqual"
                                    }
                                }
                            ],
                            "message": "Too many recent EMI bounces"
                        },
                        "priority": 1
                    },
                    "speculative_txn_cap_pct": {
                        "key": "speculative_txn_cap_pct",
                        "description": [
                            "Calculates speculative spend as % of income",
                            "Pass if Actual % <= Configured Cap"
                        ],
                        "value": {
                            "source": [
                                {
                                    "type": "key"
                                }
                            ],
                            "against": [
                                {
                                    "type": "s3",
                                    "details": {
                                        "config_accessors": "pirimid",
                                        "value": "analytics",
                                        "function": {
                                            "call": "get_speculative_spend_percentage(var1, var2)",
                                            "arg": [
                                                "s3.analytics",
                                                "sys.input.declared_monthly_income"
                                            ]
                                        },
                                        "condition": "lessThanEqual"
                                    }
                                }
                            ],
                            "message": "Speculative transaction volume exceeds allowed limit"
                        },
                        "priority": 2
                    }
}