165 lines
5.2 KiB
Rust
165 lines
5.2 KiB
Rust
use super::super::super::repositories;
|
|
use beteran_common_rust as bcr;
|
|
use beteran_protobuf_rust as bpr;
|
|
use diesel::{
|
|
r2d2::{ConnectionManager, Pool},
|
|
PgConnection,
|
|
};
|
|
use prost::Message;
|
|
use std::str::FromStr;
|
|
|
|
pub struct EventHandler {
|
|
connection_broker: nats::asynk::Connection,
|
|
queue_broker: String,
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
member_repository: repositories::member::repository::Repository,
|
|
}
|
|
|
|
impl std::fmt::Debug for EventHandler {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
f.debug_struct("EventHandler of members").finish()
|
|
}
|
|
}
|
|
|
|
impl EventHandler {
|
|
///
|
|
pub fn new(
|
|
connection_broker: nats::asynk::Connection,
|
|
queue_broker: String,
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
) -> EventHandler {
|
|
EventHandler {
|
|
connection_broker,
|
|
queue_broker,
|
|
pool,
|
|
member_repository: repositories::member::repository::Repository::new(),
|
|
}
|
|
}
|
|
|
|
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
|
|
futures::try_join!(self.event_after_signin()).map(|_| ())
|
|
}
|
|
|
|
async fn event_after_signin(&self) -> Result<(), Box<dyn std::error::Error>> {
|
|
let s = self
|
|
.connection_broker
|
|
.queue_subscribe(
|
|
bpr::ss::identity::EVENT_SUBJECT_AFTER_SIGNIN,
|
|
self.queue_broker.as_str(),
|
|
)
|
|
.await?;
|
|
|
|
while let Some(message) = s.next().await {
|
|
if let Err(e) = async {
|
|
let eve =
|
|
bpr::ss::identity::AfterSigninEvent::decode(message.data.as_slice()).map_err(|e| {
|
|
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
|
|
message: format!("invalid request: {}", e),
|
|
})
|
|
})?;
|
|
|
|
let client = match eve.client {
|
|
Some(c) => c,
|
|
None => {
|
|
return Err(bcr::error::rpc::Error::InvalidParams(
|
|
bcr::error::rpc::InvalidParams {
|
|
message: "invalid client information".to_string(),
|
|
detail: bcr::error::rpc::InvalidParamsDetail {
|
|
location: "request".to_string(),
|
|
param: "client".to_string(),
|
|
value: "".to_string(),
|
|
error_type: bcr::error::rpc::InvalidParamsType::Required,
|
|
message: "".to_string(),
|
|
},
|
|
},
|
|
));
|
|
}
|
|
};
|
|
let event = match eve.event {
|
|
Some(r) => r,
|
|
None => {
|
|
return Err(bcr::error::rpc::Error::InvalidParams(
|
|
bcr::error::rpc::InvalidParams {
|
|
message: "invalid event information".to_string(),
|
|
detail: bcr::error::rpc::InvalidParamsDetail {
|
|
location: "event".to_string(),
|
|
param: "event".to_string(),
|
|
value: "".to_string(),
|
|
error_type: bcr::error::rpc::InvalidParamsType::Required,
|
|
message: "".to_string(),
|
|
},
|
|
},
|
|
));
|
|
}
|
|
};
|
|
let member = match event.member {
|
|
Some(c) => c,
|
|
None => {
|
|
return Err(bcr::error::rpc::Error::InvalidParams(
|
|
bcr::error::rpc::InvalidParams {
|
|
message: "invalid client information".to_string(),
|
|
detail: bcr::error::rpc::InvalidParamsDetail {
|
|
location: "request".to_string(),
|
|
param: "client".to_string(),
|
|
value: "".to_string(),
|
|
error_type: bcr::error::rpc::InvalidParamsType::Required,
|
|
message: "".to_string(),
|
|
},
|
|
},
|
|
));
|
|
}
|
|
};
|
|
|
|
let id = uuid::Uuid::from_str(member.id.as_str()).map_err(|e| {
|
|
bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams {
|
|
message: "invalid member.id param".to_string(),
|
|
detail: bcr::error::rpc::InvalidParamsDetail {
|
|
location: "request".to_string(),
|
|
param: "member.id".to_string(),
|
|
value: member.id.clone(),
|
|
error_type: bcr::error::rpc::InvalidParamsType::Required,
|
|
message: e.to_string(),
|
|
},
|
|
})
|
|
})?;
|
|
|
|
let conn = self.pool.get().map_err(|e| {
|
|
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
|
|
code: bpr::protobuf::rpc::Error::SERVER_00,
|
|
message: format!("server {}", e),
|
|
data: None,
|
|
})
|
|
})?;
|
|
|
|
let last_signined_at = chrono::Utc::now().timestamp();
|
|
|
|
self
|
|
.member_repository
|
|
.update_last_signined_ip(
|
|
&conn,
|
|
id,
|
|
&repositories::member::models::ModifyMember4LastSignined {
|
|
last_signined_ip: client.client_ip,
|
|
last_signined_at,
|
|
},
|
|
)
|
|
.map_err(|e| {
|
|
bcr::error::rpc::Error::Server(bcr::error::rpc::Server {
|
|
code: bpr::protobuf::rpc::Error::SERVER_00,
|
|
message: format!("server {}", e),
|
|
data: None,
|
|
})
|
|
})?;
|
|
|
|
Ok::<(), bcr::error::rpc::Error>(())
|
|
}
|
|
.await
|
|
{
|
|
println!("error: {}", e);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|