|
(defn- is-card-blocked? |
|
[{:keys [account]}] |
|
(when (:cardIsBlocked account) |
|
"CARD BLOCKED")) |
|
|
|
(defn- has-no-limit? |
|
[{:keys [account transaction]}] |
|
(when (> (:amount transaction) (:limit account)) |
|
"NO LIMIT AVAILABLE")) |
|
|
|
(defn- is-first-transaction? |
|
[last-transactions] |
|
(empty? last-transactions)) |
|
|
|
(defn- is-transaction-amount-above-90%-limit? |
|
[account transaction] |
|
(> (:amount transaction) (* (:limit account) 0.9))) |
|
|
|
(defn- is-transaction-amount-allowed? |
|
[{:keys [account transaction last-transactions]}] |
|
(when (and (is-first-transaction? last-transactions) |
|
(is-transaction-amount-above-90%-limit? account transaction)) |
|
"TRANSACTION ABOVE 90% LIMIT IN FIRST TRANSACTION")) |
|
|
|
(defn- is-merchant-on-deny-list? |
|
[{:keys [account transaction]}] |
|
(when (contains? (:denyList account) (:merchant transaction)) |
|
"MERCHANT ON DENY LIST")) |
|
|
|
(defn- more-than-10-transactions-on-the-same-merchant? |
|
[{:keys [last-transactions transaction]}] |
|
(when (-> (filter #(= (:merchant transaction) (:merchant %)) last-transactions) |
|
(count) |
|
(> 10)) |
|
"MORE THAN 10 TRANSACTIONS ON THE SAME MERCHANT")) |
|
|
|
(defn authorize |
|
[account transaction last-transactions] |
|
(let [args {:account account |
|
:transaction transaction |
|
:last-transactions last-transactions} |
|
denied-reasons (keep #(% args) [has-no-limit? |
|
is-card-blocked? |
|
is-transaction-amount-allowed? |
|
more-than-10-transactions-on-the-same-merchant? |
|
is-merchant-on-deny-list?])] |
|
(if (empty? denied-reasons) |
|
{:approved true |
|
:deniedReasons [] |
|
:newLimit (- (:limit account) (:amount transaction))} |
|
{:approved false |
|
:deniedReasons (into [] denied-reasons) |
|
:newLimit (:limit account)}))) |