From 4c785f254289f0999fc06f299e34ae45d599720e Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Mon, 29 Aug 2022 05:45:20 +0000 Subject: [PATCH] synchronization of member is modified --- src/main.rs | 7 +- src/synchronizations/member/synchronizer.rs | 94 +++++++++++++++++++-- 2 files changed, 93 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index b9d0bff..fd348dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,8 +73,11 @@ async fn main() -> Result<(), Box> { synchronizations::vendor::synchronizer::Synchronizer::new(pool.clone(), api_config.clone()); let game_synchronizer = synchronizations::game::synchronizer::Synchronizer::new(pool.clone(), api_config.clone()); - let member_synchronizer = - synchronizations::member::synchronizer::Synchronizer::new(pool.clone(), api_config.clone()); + let member_synchronizer = synchronizations::member::synchronizer::Synchronizer::new( + connection_server_broker.clone(), + pool.clone(), + api_config.clone(), + ); let member_account_synchronizer = synchronizations::member_account::synchronizer::Synchronizer::new( pool.clone(), diff --git a/src/synchronizations/member/synchronizer.rs b/src/synchronizations/member/synchronizer.rs index 20ed2ce..4694ab5 100644 --- a/src/synchronizations/member/synchronizer.rs +++ b/src/synchronizations/member/synchronizer.rs @@ -1,13 +1,19 @@ use crate::api; use crate::core; use crate::repositories; +use beteran_protobuf_rust as bpr; use diesel::{ r2d2::{ConnectionManager, Pool}, PgConnection, }; +use prost::Message; +use std::net::IpAddr; +use std::net::Ipv4Addr; +use std::str::FromStr; /// pub struct Synchronizer { + connection_broker: nats::asynk::Connection, pool: Pool>, api_config: core::config::ApiConfig, synchronization_history_repository: repositories::synchronization_history::repository::Repository, @@ -24,6 +30,7 @@ impl std::fmt::Debug for Synchronizer { impl std::clone::Clone for Synchronizer { fn clone(&self) -> Self { Self { + connection_broker: self.connection_broker.clone(), pool: self.pool.clone(), api_config: self.api_config.clone(), synchronization_history_repository: @@ -37,10 +44,12 @@ impl std::clone::Clone for Synchronizer { impl Synchronizer { /// pub fn new( + connection_broker: nats::asynk::Connection, pool: Pool>, api_config: core::config::ApiConfig, ) -> Synchronizer { Synchronizer { + connection_broker, pool, api_config: api_config.clone(), synchronization_history_repository: @@ -60,15 +69,88 @@ impl Synchronizer { let res = self.member_api.list_members(req).await?; for u in res.users { - let modify_member = repositories::member::models::ModifyMember { - balance: u.cash, - balance_bota: u.cash_bota, - oriental_play: u.oriental_play, - }; + if self + .member_repository + .select(&conn, u.id) + .expect("member_repository.select") + .is_none() + { + let ss_get_member_by_username_req = bpr::ss::member::GetMemberByUsernameRequest { + client: Some(bpr::models::core::network::Client { + client_ip: self + .connection_broker + .client_ip() + .unwrap_or(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))) + .to_string(), + site_url: None, + access_token: None, + }), + request: Some(bpr::ss::member::get_member_by_username_request::Request { + username: u.site_username.clone(), + }), + }; + + let ss_get_member_by_username_res_msg = self + .connection_broker + .request( + bpr::ss::member::SUBJECT_GET_MEMBER_BY_USERNAME, + ss_get_member_by_username_req.encode_to_vec(), + ) + .await + .expect("SUBJECT_GET_MEMBER_BY_USERNAME"); + + let ss_get_member_by_username_res = bpr::ss::member::GetMemberByUsernameResponse::decode( + ss_get_member_by_username_res_msg.data.as_slice(), + ) + .expect("GetMemberByUsernameResponse"); + + if let Some(e) = ss_get_member_by_username_res.error { + println!( + "bpr::ss::member::SUBJECT_GET_MEMBER_BY_USERNAME error: {}", + e + ); + continue; + } + + match ss_get_member_by_username_res.result { + Some(r) => match r.member { + Some(m) => { + let member_id = uuid::Uuid::from_str(m.id.as_str()).expect("uuid::Uuid"); + + self + .member_repository + .insert( + &conn, + &repositories::member::models::NewMember { + id: u.id, + member_id, + }, + ) + .expect("member insert"); + } + None => { + println!("member is not exist: {}", u.site_username); + continue; + } + }, + None => { + println!("member is not exist: {}", u.site_username); + continue; + } + }; + } self .member_repository - .update(&conn, u.id, &modify_member) + .update( + &conn, + u.id, + &repositories::member::models::ModifyMember { + balance: u.cash, + balance_bota: u.cash_bota, + oriental_play: u.oriental_play, + }, + ) .expect("member update"); }