//! //! use std::str::FromStr; use crate::compositions; use crate::repositories; use beteran_common_rust as bcr; use beteran_protobuf_rust as bpr; use diesel::{ r2d2::{ConnectionManager, Pool}, PgConnection, }; use prost::Message; /// pub struct Service { connection_broker: nats::asynk::Connection, queue_broker: String, pool: Pool>, site_repository: repositories::site::repository::Repository, site_composition: compositions::site::composition::Composition, member_bank_account_repository: repositories::member_bank_account::repository::Repository, } impl std::fmt::Debug for Service { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.debug_struct("Service of service.member.service.identity") .finish() } } impl Service { /// pub fn new( connection_broker: nats::asynk::Connection, queue_broker: String, pool: Pool>, ) -> Service { Service { connection_broker, queue_broker, pool, site_repository: repositories::site::repository::Repository::new(), site_composition: compositions::site::composition::Composition::new(), member_bank_account_repository: repositories::member_bank_account::repository::Repository::new(), } } pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { futures::try_join!( self.create_member_bank_account(), self.list_member_bank_accounts(), self.get_member_bank_account(), self.update_member_bank_account(), self.delete_member_bank_account(), ) .map(|_| ()) } fn check_site( &self, conn: &diesel::PgConnection, url: Option, site_id: uuid::Uuid, ) -> Result { match self .site_composition .select_by_url(conn, url, site_id) .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, }) })? { Some(s) => Ok(s), None => Err(bcr::error::rpc::Error::InvalidParams( bcr::error::rpc::InvalidParams { message: "invalid site_url information".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "client.site_url".to_string(), value: "".to_string(), error_type: bcr::error::rpc::InvalidParamsType::None, message: "".to_string(), }, }, )), } } async fn create_member_bank_account(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( bpr::ss::member_bank_account::SUBJECT_CREATE_MEMBER_BANK_ACCOUNT, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { let req = bpr::ss::member_bank_account::CreateMemberBankAccountRequest::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 req.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 request = match req.request { Some(r) => r, None => { return Err(bcr::error::rpc::Error::InvalidParams( bcr::error::rpc::InvalidParams { message: "invalid request information".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "request".to_string(), value: "".to_string(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: "".to_string(), }, }, )); } }; let member_id = uuid::Uuid::from_str(request.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: request.member_id.clone(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: e.to_string(), }, }) })?; let bank_id = uuid::Uuid::from_str(request.bank_id.as_str()).map_err(|e| { bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { message: "invalid bank_id param".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "bank_id".to_string(), value: request.bank_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 s = self .member_bank_account_repository .insert( &conn, &repositories::member_bank_account::models::NewMemberBankAccount { member_id, bank_id, name: request.name, account_number: request.account_number, exchange_password: request.exchange_password, memo: request.memo, }, ) .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, }) })?; message .respond( bpr::ss::member_bank_account::CreateMemberBankAccountResponse { error: None, result: Some( bpr::ss::member_bank_account::create_member_bank_account_response::Result { member_bank_account: Some( bpr::models::member_bank_account::MemberBankAccount::from(&s), ), }, ), } .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::ss::member_bank_account::CreateMemberBankAccountResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } .encode_to_vec(), ) .await?; } } Ok(()) } async fn list_member_bank_accounts(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( bpr::ss::member_bank_account::SUBJECT_LIST_MEMBER_BANK_ACCOUNTS, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { let req = bpr::ss::member_bank_account::ListMemberBankAccountsRequest::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 req.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 request = match req.request { Some(r) => r, None => { return Err(bcr::error::rpc::Error::InvalidParams( bcr::error::rpc::InvalidParams { message: "invalid request information".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "request".to_string(), value: "".to_string(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: "".to_string(), }, }, )); } }; let search = match request.search { Some(s) => { let member_id = match s.member_id { Some(d) => match uuid::Uuid::from_str(d.as_str()) { Ok(dd) => Some(dd), Err(e) => { return Err(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: d.clone(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: e.to_string(), }, }, )); } }, None => None, }; let bank_id = match s.bank_id { Some(d) => match uuid::Uuid::from_str(d.as_str()) { Ok(dd) => Some(dd), Err(e) => { return Err(bcr::error::rpc::Error::InvalidParams( bcr::error::rpc::InvalidParams { message: "invalid bank_id param".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "bank_id".to_string(), value: d.clone(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: e.to_string(), }, }, )); } }, None => None, }; Some(repositories::member_bank_account::models::FindAllSearch { member_id, bank_id, name_like: s.name_like, account_number_like: s.account_number_like, memo_like: s.memo_like, }) } None => None, }; 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 find_all = repositories::member_bank_account::models::FindAll { search, pagination: request .pagination .as_ref() .map(|d| bcr::models::pagination::Pagination::from(d)), sorts: Some( request .sorts .iter() .map(|d| beteran_common_rust::models::pagination::Sort::from(d)) .collect(), ), }; let count = self .member_bank_account_repository .select_all_count(&conn, &find_all) .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 list = self .member_bank_account_repository .select_all(&conn, &find_all) .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, }) })?; message .respond( bpr::ss::member_bank_account::ListMemberBankAccountsResponse { error: None, result: Some( bpr::ss::member_bank_account::list_member_bank_accounts_response::Result { member_bank_accounts: list .iter() .map(|d| bpr::models::member_bank_account::MemberBankAccount::from(d)) .collect(), }, ), } .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::ss::member_bank_account::ListMemberBankAccountsResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } .encode_to_vec(), ) .await?; } } Ok(()) } async fn get_member_bank_account(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( bpr::ss::member_bank_account::SUBJECT_GET_MEMBER_BANK_ACCOUNT, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { let req = bpr::ss::member_bank_account::GetMemberBankAccountRequest::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 req.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 request = match req.request { Some(r) => r, None => { return Err(bcr::error::rpc::Error::InvalidParams( bcr::error::rpc::InvalidParams { message: "invalid request information".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "request".to_string(), value: "".to_string(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: "".to_string(), }, }, )); } }; let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { message: "invalid id param".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "id".to_string(), value: request.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 s = self .member_bank_account_repository .select(&conn, id) .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, }) })?; message .respond( bpr::ss::member_bank_account::GetMemberBankAccountResponse { error: None, result: Some( bpr::ss::member_bank_account::get_member_bank_account_response::Result { member_bank_account: s .map(|d| bpr::models::member_bank_account::MemberBankAccount::from(&d)), }, ), } .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::ss::member_bank_account::GetMemberBankAccountResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } .encode_to_vec(), ) .await?; } } Ok(()) } async fn update_member_bank_account(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( bpr::ss::member_bank_account::SUBJECT_UPDATE_MEMBER_BANK_ACCOUNT, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { let req = bpr::ss::member_bank_account::UpdateMemberBankAccountRequest::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 req.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 request = match req.request { Some(r) => r, None => { return Err(bcr::error::rpc::Error::InvalidParams( bcr::error::rpc::InvalidParams { message: "invalid request information".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "request".to_string(), value: "".to_string(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: "".to_string(), }, }, )); } }; let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { message: "invalid id param".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "id".to_string(), value: request.id.clone(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: e.to_string(), }, }) })?; let bank_id = uuid::Uuid::from_str(request.bank_id.as_str()).map_err(|e| { bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { message: "invalid id param".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "bank_id".to_string(), value: request.bank_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 _affected = self .member_bank_account_repository .update( &conn, id, &repositories::member_bank_account::models::ModifyMemberBankAccount { bank_id, name: request.name, account_number: request.account_number, exchange_password: request.exchange_password, memo: request.memo, }, ) .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 s = self .member_bank_account_repository .select(&conn, id) .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, }) })?; message .respond( bpr::ss::member_bank_account::UpdateMemberBankAccountResponse { error: None, result: Some( bpr::ss::member_bank_account::update_member_bank_account_response::Result { member_bank_account: s .map(|d| bpr::models::member_bank_account::MemberBankAccount::from(&d)), }, ), } .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::ss::member_bank_account::UpdateMemberBankAccountResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } .encode_to_vec(), ) .await?; } } Ok(()) } async fn delete_member_bank_account(&self) -> Result<(), Box> { let s = self .connection_broker .queue_subscribe( bpr::ss::member_bank_account::SUBJECT_DELETE_MEMBER_BANK_ACCOUNT, self.queue_broker.as_str(), ) .await?; while let Some(message) = s.next().await { if let Err(e) = async { let req = bpr::ss::member_bank_account::DeleteMemberBankAccountRequest::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 req.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 request = match req.request { Some(r) => r, None => { return Err(bcr::error::rpc::Error::InvalidParams( bcr::error::rpc::InvalidParams { message: "invalid request information".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "request".to_string(), value: "".to_string(), error_type: bcr::error::rpc::InvalidParamsType::Required, message: "".to_string(), }, }, )); } }; let id = uuid::Uuid::from_str(request.id.as_str()).map_err(|e| { bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams { message: "invalid id param".to_string(), detail: bcr::error::rpc::InvalidParamsDetail { location: "request".to_string(), param: "id".to_string(), value: request.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 _affected = self .member_bank_account_repository .delete(&conn, id) .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, }) })?; message .respond( bpr::ss::member_bank_account::DeleteMemberBankAccountResponse { error: None, result: Some( bpr::ss::member_bank_account::delete_member_bank_account_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::ss::member_bank_account::DeleteMemberBankAccountResponse { error: Some(bpr::protobuf::rpc::Error::from(e)), result: None, } .encode_to_vec(), ) .await?; } } Ok(()) } }