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
use chrono::NaiveDateTime;
use ntex::http::header;
use ntex::web::types::Query;
use ntex::web::HttpRequest;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::enums::app_message::AppMessage;

pub type TheQueryParams = Query<QueryParams>;

#[derive(Deserialize, Clone, Default)]
pub struct QueryParams {
    pub search: Option<String>,
    pub limit: Option<i64>,
    pub page: Option<i64>,
    pub per_page: Option<i64>,
    pub status: Option<String>,
    pub stage: Option<String>,
    pub network_id: Option<Uuid>,
    pub category_id: Option<Uuid>,
}

#[derive(Deserialize)]
pub struct IdPathParam {
    pub id: String,
}

#[derive(Deserialize)]
pub struct IdAsUuid {
    pub id: Uuid,
}

#[cfg(feature = "feat-validator")]
#[derive(Deserialize, validator::Validate)]
pub struct ReasonPayload {
    #[validate(length(min = 3, max = 1500))]
    pub reason: String,
}

impl QueryParams {
    pub fn search(&self) -> Option<String> {
        self.search.clone()
    }

    pub fn search_query(&self) -> String {
        self.search.clone().unwrap_or(String::from(""))
    }

    pub fn search_query_like(&self) -> String {
        format!("%{}%", self.search_query())
    }

    pub fn limit(&self) -> i64 {
        let limit = self.limit.unwrap_or(10);
        match limit > 50 {
            true => 50,
            false => limit,
        }
    }

    pub fn curr_page(&self) -> i64 {
        self.page.unwrap_or(1)
    }

    pub fn per_page(&self) -> i64 {
        let limit = self.per_page.unwrap_or(10);
        match limit > 50 {
            true => 50,
            false => limit,
        }
    }
}

pub fn get_ip_and_ua(req: &HttpRequest) -> (Option<String>, Option<String>) {
    let user_agent = req
        .headers()
        .get(header::USER_AGENT)
        .map(|u| u.to_str().unwrap().to_string());

    let ip_address = req
        .connection_info()
        .remote()
        .map(|v| v.to_string())
        .unwrap_or(req.peer_addr().map(|s| s.to_string()).unwrap());

    (Some(ip_address), user_agent)
}

#[allow(dead_code)]
pub fn date_from_unsafe_input(date: &str, field_name: &str) -> Result<NaiveDateTime, AppMessage> {
    NaiveDateTime::parse_from_str(format!("{} 00:00:00", date).as_str(), "%Y-%m-%d %H:%M:%S")
        .map_err(|e| {
            AppMessage::WarningMessageString(format!(
                "Invalid {} input value({}), please make sure it's valid date; {}",
                field_name, date, e
            ))
        })
}

#[derive(Serialize, Deserialize, Clone)]
pub struct HttpHeaderItem {
    pub name: String,
    pub value: String,
}