Skip to main content

flippico_cache/types/
channels.rs

1use redis::{FromRedisValue, RedisResult, ToRedisArgs, Value};
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, PartialEq, Debug, Deserialize)]
5pub enum SubscriptionChannel {
6    Amadeus,
7    Notifica,
8    BajkomatApi,
9    Shopify,
10}
11
12impl SubscriptionChannel {
13    pub fn get_channel(&self) -> &'static str {
14        match self {
15            SubscriptionChannel::Amadeus => "AMADEUS_CHANNEL",
16            SubscriptionChannel::Notifica => "NOTIFICA_CHANNEL",
17            SubscriptionChannel::BajkomatApi => "BAJKOMAT_API_CHANNEL",
18            SubscriptionChannel::Shopify => "SHOPIFY_CHANNEL",
19        }
20    }
21
22    pub fn from_channel(value: &str) -> Option<Self> {
23        [
24            SubscriptionChannel::Amadeus,
25            SubscriptionChannel::BajkomatApi,
26            SubscriptionChannel::Notifica,
27            SubscriptionChannel::Shopify,
28        ]
29        .into_iter()
30        .find(|variant| variant.get_channel() == value)
31    }
32}
33
34#[derive(Serialize, PartialEq, Debug, Deserialize)]
35pub enum ListChannel {
36    Amadeus,
37    Notifica,
38    BajkomatApi,
39    KsefGpt,
40    N8n,
41    Shopify,
42}
43
44impl ListChannel {
45    pub fn get_channel(&self) -> &'static str {
46        match self {
47            ListChannel::Amadeus => "AMADEUS_LIST",
48            ListChannel::Notifica => "NOTIFICA_LIST",
49            ListChannel::BajkomatApi => "BAJKOMAT_API_LIST",
50            ListChannel::KsefGpt => "KSEF_GPT_LISt",
51            ListChannel::Shopify => "SHOPIFY_LIST",
52            ListChannel::N8n => "N_8_N",
53        }
54    }
55    pub fn from_channel(value: &str) -> Option<Self> {
56        [
57            ListChannel::Amadeus,
58            ListChannel::BajkomatApi,
59            ListChannel::KsefGpt,
60            ListChannel::Notifica,
61            ListChannel::Shopify,
62            ListChannel::N8n,
63        ]
64        .into_iter()
65        .find(|variant| variant.get_channel() == value)
66    }
67}
68
69pub enum CacheSpace {
70    Amadeus,
71    Notifica,
72    BajkomatApi,
73    Shopify,
74    KsefGpt,
75}
76
77impl CacheSpace {
78    pub fn get_space(&self) -> &'static str {
79        match self {
80            CacheSpace::Amadeus => "AMADEUS_CACHE",
81            CacheSpace::Notifica => "NOTIFICA_CACHE",
82            CacheSpace::BajkomatApi => "BAJKOMAT_API_CACHE",
83            CacheSpace::Shopify => "SHOPIFY_CACHE",
84            CacheSpace::KsefGpt => "KSEFGPT_CACHE",
85        }
86    }
87}
88
89#[derive(Serialize, PartialEq, Debug, Deserialize)]
90pub struct ChannelMessage {
91    pub meta: Option<MessageMeta>,
92    pub channel: SubscriptionChannel,
93    pub body: Option<serde_json::Value>,
94}
95
96#[derive(Serialize, PartialEq, Debug, Deserialize)]
97pub struct MessageMeta {
98    pub app_name: String,
99}
100
101#[derive(Serialize, PartialEq, Debug, Deserialize)]
102pub struct ListMessage {
103    pub meta: Option<MessageMeta>,
104    pub body: Option<serde_json::Value>,
105}
106
107#[derive(Serialize, PartialEq, Debug, Deserialize)]
108pub struct CacheValue<T> {
109    pub meta: Option<MessageMeta>,
110    pub value: Option<T>,
111}
112
113impl From<String> for SubscriptionChannel {
114    fn from(value: String) -> Self {
115        SubscriptionChannel::from_channel(&value).expect("Invalid channel string")
116    }
117}
118
119impl FromRedisValue for ChannelMessage {
120    fn from_redis_value(v: &Value) -> RedisResult<Self> {
121        match v {
122            Value::Array(items) => {
123                if items.len() < 2 {
124                    return Err((redis::ErrorKind::TypeError, "Not enough items").into());
125                }
126                let channel: String = redis::from_redis_value(&items[0])?;
127                let mut body: Option<String> = None;
128                for chunk in items[1..].chunks(2) {
129                    if chunk.len() == 2 {
130                        // let key: String = redis::from_redis_value(&chunk[0])?;
131                        let val = redis::from_redis_value(&chunk[1])?;
132                        body = Some(val)
133                    }
134                }
135
136                Ok(ChannelMessage {
137                    channel: SubscriptionChannel::from(channel),
138                    // TODO to check if body is Some
139                    meta: None,
140                    body: serde_json::from_str(&body.unwrap()).unwrap(),
141                })
142            }
143            _ => Err((redis::ErrorKind::TypeError, "Unexpected Redis value").into()),
144        }
145    }
146}
147
148impl ToRedisArgs for ChannelMessage {
149    fn write_redis_args<W>(&self, out: &mut W)
150    where
151        W: ?Sized + redis::RedisWrite,
152    {
153        let msg = serde_json::to_vec(&self);
154        if let Ok(message) = msg {
155            out.write_arg(&message);
156        }
157    }
158}