aboutsummaryrefslogtreecommitdiffstats
path: root/crates/rule-executor/src/processor/rule/rule_901.rs
blob: 8dfe036344e96cc0726d1a1b7339a0343594354b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use anyhow::{Result, anyhow};
use determine_outcome::determine_outcome;
use opentelemetry_semantic_conventions::attribute;
use serde::Deserialize;
use sqlx::types::BigDecimal;
use time::OffsetDateTime;
use tracing::{Instrument, error, info_span, trace};
use tracing_opentelemetry::OpenTelemetrySpanExt;
use warden_core::{
    configuration::rule::RuleConfiguration,
    iso20022::TransactionType,
    message::{Payload, RuleResult},
};

use crate::{processor::rule::determine_outcome, state::AppHandle};

#[derive(Deserialize)]
pub struct Parameters {
    max_query_range: f64,
}

pub(super) async fn process_901(
    configuration: &RuleConfiguration,
    payload: &Payload,
    state: AppHandle,
) -> Result<RuleResult> {
    let mut rule_result = RuleResult {
        id: configuration.id.to_string(),
        version: configuration.version.to_string(),
        ..Default::default()
    };
    let c = configuration.configuration.as_ref();

    let bands = c
        .and_then(|value| {
            if value.bands.is_empty() {
                None
            } else {
                Some(&value.bands)
            }
        })
        .ok_or_else(|| anyhow!("no bands available"))?;

    let exit_conditions = c
        .and_then(|value| {
            if value.exit_conditions.is_empty() {
                None
            } else {
                Some(&value.exit_conditions)
            }
        })
        .ok_or_else(|| anyhow!("no exit conditions available"))?;

    let parameters = c
        .and_then(|value| value.parameters.as_ref())
        .ok_or_else(|| anyhow!("no parameters available"))?;

    let params: Parameters = serde_json::from_value(parameters.clone().into())
        .inspect_err(|e| error!("failed to deserailise params: {e:?}"))?;

    let unsuccessful_transaction = exit_conditions
        .iter()
        .find(|value| value.sub_rule_ref.eq(".x00"));

    if let Some(warden_core::message::payload::Transaction::Pacs002(pacs002_document)) =
        payload.transaction.as_ref()
    {
        let tx_sts = pacs002_document
            .f_i_to_f_i_pmt_sts_rpt
            .tx_inf_and_sts
            .first()
            .ok_or_else(|| anyhow::anyhow!("tx sts to be there"))?;

        if tx_sts.tx_sts().ne("ACCC") {
            let unsuccessful_transaction = unsuccessful_transaction
                .ok_or_else(|| anyhow::anyhow!("no unsuccessful transaction ref"))?;
            rule_result.reason = unsuccessful_transaction.reason.to_owned();
            rule_result.sub_rule_ref = unsuccessful_transaction.sub_rule_ref.to_owned();

            return Ok(rule_result);
        }

        let current_pacs002_timeframe: OffsetDateTime = pacs002_document
            .f_i_to_f_i_pmt_sts_rpt
            .grp_hdr
            .cre_dt_tm
            .try_into()?;

        let data_cache = payload
            .data_cache
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("data cache is missing"))?;

        let range = BigDecimal::try_from(params.max_query_range)?;

        let tx_tp = TransactionType::PACS002.to_string();

        let span = info_span!("rule.logic");
        span.set_attribute(attribute::DB_SYSTEM_NAME, "postgres");
        span.set_attribute(attribute::DB_OPERATION_NAME, "901");
        span.set_attribute("otel.kind", "client");

        trace!("executing rule query");
        let recent_transactions = sqlx::query_scalar!(
            "select count(*) from transaction_relationship tr
                 where tr.destination = $1
                   and tr.tx_tp = $2
                   and extract(epoch from ($3::timestamptz - tr.cre_dt_tm)) * 1000 <= $4
                   and tr.cre_dt_tm <= $3::timestamptz",
            data_cache.dbtr_acct_id,
            tx_tp,
            current_pacs002_timeframe,
            range,
        )
        .fetch_one(&state.services.postgres)
        .instrument(span)
        .await?
        .ok_or_else(|| anyhow::anyhow!("no data"))?;

        determine_outcome(recent_transactions, bands.as_ref(), &mut rule_result);

        Ok(rule_result)
    } else {
        Err(anyhow::anyhow!("no valid transaction"))
    }
}