diff --git a/Cargo.toml b/Cargo.toml index 3d534fa..ea0f2f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ prost = { version = "0" } nats = { version = "0" } base64 = { version = "0" } -beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.64-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.48-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.66-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.50-snapshot" } [build-dependencies] diff --git a/src/main.rs b/src/main.rs index 70a3274..f4e36eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod member_bank_deposit; mod member_bank_withdraw; mod member_class; mod member_level; +mod member_referrer; mod site; mod api; @@ -41,6 +42,8 @@ async fn main() -> Result<(), Box> { member_class::service::Service::new(connection_broker.clone(), queue_broker.clone()); let member_level_service = member_level::service::Service::new(connection_broker.clone(), queue_broker.clone()); + let member_referrer_service = + member_referrer::service::Service::new(connection_broker.clone(), queue_broker.clone()); let site_service = site::service::Service::new(connection_broker.clone(), queue_broker.clone()); let api_service = api::service::Service::new(connection_broker.clone(), queue_broker.clone()); @@ -56,6 +59,7 @@ async fn main() -> Result<(), Box> { member_bank_withdraw_service.subscribe(), member_class_service.subscribe(), member_level_service.subscribe(), + member_referrer_service.subscribe(), site_service.subscribe(), api_service.subscribe(), )?; diff --git a/src/member/service.rs b/src/member/service.rs index f1fd1f7..7aa127d 100644 --- a/src/member/service.rs +++ b/src/member/service.rs @@ -220,7 +220,7 @@ impl Service { site_id: s.site_id, member_class_id: s.member_class_id, member_level_id: s.member_level_id, - referrer_member_id: s.referrer_member_id, + parent_member_id: s.parent_member_id, username_like: s.username_like, nickname_like: s.nickname_like, mobile_phone_number_like: s.mobile_phone_number_like, @@ -679,7 +679,7 @@ impl Service { })); } - if let Some(r) = ss_delete_member_res.result { + if let Some(_r) = ss_delete_member_res.result { message .respond( bpr::c2se::member::DeleteMemberResponse { diff --git a/src/member_referrer/mod.rs b/src/member_referrer/mod.rs new file mode 100644 index 0000000..1f278a4 --- /dev/null +++ b/src/member_referrer/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/member_referrer/service.rs b/src/member_referrer/service.rs new file mode 100644 index 0000000..1ae467f --- /dev/null +++ b/src/member_referrer/service.rs @@ -0,0 +1,613 @@ +use beteran_common_rust as bcr; +use beteran_protobuf_rust as bpr; +use prost::Message; + +/// +pub struct Service { + connection_broker: nats::asynk::Connection, + queue_broker: String, +} + +impl std::fmt::Debug for Service { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("beteran-backend-server-edge::member_referrer::service::Service") + .finish() + } +} + +impl Service { + /// + pub fn new(connection_broker: nats::asynk::Connection, queue_broker: String) -> Service { + Service { + connection_broker, + queue_broker, + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!( + self.create_member_referrer(), + self.list_member_referrers(), + self.get_member_referrer(), + self.update_member_referrer(), + self.delete_member_referrer(), + ) + .map(|_| ()) + } + + fn get_client_in_header( + &self, + message: &nats::asynk::Message, + ) -> Result { + match &message.headers { + Some(headers) => { + let client = match headers.get(bpr::c2se::core::network::HEADER_CLIENT) { + Some(c) => { + let msg = base64::decode(c).map_err(|e| { + bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: format!("invalid header: {}", e), + }) + })?; + bpr::models::core::network::Client::decode(msg.as_slice()).map_err(|e| { + bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: format!("invalid header: {}", e), + }) + })? + } + None => { + return Err(bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: "invalid client information".to_string(), + })); + } + }; + + Ok(client) + } + None => Err(bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: "invalid header".to_string(), + })), + } + } + + async fn create_member_referrer(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member_referrer::SUBJECT_CREATE_MEMBER_REFERRER, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let client = self.get_client_in_header(&message)?; + + let req = + bpr::c2se::member_referrer::CreateMemberReferrerRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_create_member_referrer_req = bpr::ss::member_referrer::CreateMemberReferrerRequest { + client: Some(client), + request: Some( + bpr::ss::member_referrer::create_member_referrer_request::Request { + member_id: req.member_id, + }, + ), + }; + + let ss_create_member_referrer_res_msg = self + .connection_broker + .request( + bpr::ss::member_referrer::SUBJECT_CREATE_MEMBER_REFERRER, + ss_create_member_referrer_req.encode_to_vec(), + ) + .await + .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 ss_create_member_referrer_res = + bpr::ss::member_referrer::CreateMemberReferrerResponse::decode( + ss_create_member_referrer_res_msg.data.as_slice(), + ) + .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, + }) + })?; + + if let Some(e) = ss_create_member_referrer_res.error { + return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + })); + } + + if let Some(r) = ss_create_member_referrer_res.result { + message + .respond( + bpr::c2se::member_referrer::CreateMemberReferrerResponse { + error: None, + result: Some( + bpr::c2se::member_referrer::create_member_referrer_response::Result { + member_referrer: r.member_referrer, + }, + ), + } + .encode_to_vec(), + ) + .await + .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 + { + message + .respond( + bpr::c2se::member_referrer::CreateMemberReferrerResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn list_member_referrers(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member_referrer::SUBJECT_LIST_MEMBER_REFERRERS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let client = self.get_client_in_header(&message)?; + + let req = + bpr::c2se::member_referrer::ListMemberReferrersRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_list_member_referrers_req = bpr::ss::member_referrer::ListMemberReferrersRequest { + client: Some(client), + request: Some( + bpr::ss::member_referrer::list_member_referrers_request::Request { + pagination: req.pagination, + sorts: req.sorts, + search: match req.search { + Some(s) => Some( + bpr::ss::member_referrer::list_member_referrers_request::request::Search { + member_id: s.member_id, + can_use: s.can_use, + memo_like: s.memo_like, + }, + ), + None => None, + }, + }, + ), + }; + + let ss_list_member_referrers_res_msg = self + .connection_broker + .request( + bpr::ss::member_referrer::SUBJECT_LIST_MEMBER_REFERRERS, + ss_list_member_referrers_req.encode_to_vec(), + ) + .await + .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 ss_list_member_referrers_res = + bpr::ss::member_referrer::ListMemberReferrersResponse::decode( + ss_list_member_referrers_res_msg.data.as_slice(), + ) + .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, + }) + })?; + + if let Some(e) = ss_list_member_referrers_res.error { + return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + })); + } + + if let Some(r) = ss_list_member_referrers_res.result { + message + .respond( + bpr::c2se::member_referrer::ListMemberReferrersResponse { + error: None, + result: Some( + bpr::c2se::member_referrer::list_member_referrers_response::Result { + member_referrers: r.member_referrers, + }, + ), + } + .encode_to_vec(), + ) + .await + .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 + { + message + .respond( + bpr::c2se::member_referrer::ListMemberReferrersResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn get_member_referrer(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member_referrer::SUBJECT_GET_MEMBER_REFERRER, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let client = self.get_client_in_header(&message)?; + + let req = + bpr::c2se::member_referrer::GetMemberReferrerRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_get_member_referrer_req = bpr::ss::member_referrer::GetMemberReferrerRequest { + client: Some(client), + request: Some( + bpr::ss::member_referrer::get_member_referrer_request::Request { id: req.id }, + ), + }; + + let ss_get_member_referrer_res_msg = self + .connection_broker + .request( + bpr::ss::member_referrer::SUBJECT_UPDATE_MEMBER_REFERRER, + ss_get_member_referrer_req.encode_to_vec(), + ) + .await + .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 ss_get_member_referrer_res = + bpr::ss::member_referrer::GetMemberReferrerResponse::decode( + ss_get_member_referrer_res_msg.data.as_slice(), + ) + .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, + }) + })?; + + if let Some(e) = ss_get_member_referrer_res.error { + return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + })); + } + + if let Some(r) = ss_get_member_referrer_res.result { + message + .respond( + bpr::c2se::member_referrer::GetMemberReferrerResponse { + error: None, + result: Some( + bpr::c2se::member_referrer::get_member_referrer_response::Result { + member_referrer: r.member_referrer, + }, + ), + } + .encode_to_vec(), + ) + .await + .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 + { + message + .respond( + bpr::c2se::member_referrer::GetMemberReferrerResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn update_member_referrer(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member_referrer::SUBJECT_UPDATE_MEMBER_REFERRER, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let client = self.get_client_in_header(&message)?; + + let req = + bpr::c2se::member_referrer::UpdateMemberReferrerRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_update_member_referrer_req = bpr::ss::member_referrer::UpdateMemberReferrerRequest { + client: Some(client), + request: Some( + bpr::ss::member_referrer::update_member_referrer_request::Request { + id: req.id, + can_use: req.can_use, + memo: req.memo, + }, + ), + }; + + let ss_update_member_referrer_res_msg = self + .connection_broker + .request( + bpr::ss::member_referrer::SUBJECT_UPDATE_MEMBER_REFERRER, + ss_update_member_referrer_req.encode_to_vec(), + ) + .await + .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 ss_update_member_referrer_res = + bpr::ss::member_referrer::UpdateMemberReferrerResponse::decode( + ss_update_member_referrer_res_msg.data.as_slice(), + ) + .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, + }) + })?; + + if let Some(e) = ss_update_member_referrer_res.error { + return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + })); + } + + if let Some(r) = ss_update_member_referrer_res.result { + message + .respond( + bpr::c2se::member_referrer::UpdateMemberReferrerResponse { + error: None, + result: Some( + bpr::c2se::member_referrer::update_member_referrer_response::Result { + member_referrer: r.member_referrer, + }, + ), + } + .encode_to_vec(), + ) + .await + .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 + { + message + .respond( + bpr::c2se::member_referrer::UpdateMemberReferrerResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn delete_member_referrer(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member_referrer::SUBJECT_DELETE_MEMBER_REFERRER, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let client = self.get_client_in_header(&message)?; + + let req = + bpr::c2se::member_referrer::DeleteMemberReferrerRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_delete_member_referrer_req = bpr::ss::member_referrer::DeleteMemberReferrerRequest { + client: Some(client), + request: Some( + bpr::ss::member_referrer::delete_member_referrer_request::Request { id: req.id }, + ), + }; + + let ss_delete_member_referrer_res_msg = self + .connection_broker + .request( + bpr::ss::member_referrer::SUBJECT_DELETE_MEMBER_REFERRER, + ss_delete_member_referrer_req.encode_to_vec(), + ) + .await + .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 ss_delete_member_referrer_res = + bpr::ss::member_referrer::DeleteMemberReferrerResponse::decode( + ss_delete_member_referrer_res_msg.data.as_slice(), + ) + .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, + }) + })?; + + if let Some(e) = ss_delete_member_referrer_res.error { + return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + })); + } + + if let Some(r) = ss_delete_member_referrer_res.result { + message + .respond( + bpr::c2se::member_referrer::DeleteMemberReferrerResponse { + error: None, + result: Some( + bpr::c2se::member_referrer::delete_member_referrer_response::Result {}, + ), + } + .encode_to_vec(), + ) + .await + .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 + { + message + .respond( + bpr::c2se::member_referrer::DeleteMemberReferrerResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +}