Compare commits

..

27 Commits

Author SHA1 Message Date
48eb399736 version up 2022-09-16 16:47:55 +00:00
f5f996c283 version up 2022-09-16 10:35:15 +00:00
d1ddb9cb2d version up 2022-09-07 04:17:44 +00:00
922a06ebf8 version up 2022-09-06 12:36:46 +00:00
4ea20da177 version up 2022-09-05 14:43:22 +00:00
2b259f63e6 total_count is added 2022-09-01 17:29:50 +00:00
44fc11abe6 version up 2022-09-01 09:45:07 +00:00
4f63c0065f version up 2022-09-01 03:55:13 +00:00
3792d38f48 version up 2022-08-31 13:41:24 +00:00
0e12f1042d version up 2022-08-31 09:01:53 +00:00
8051c83f7e version up 2022-08-30 08:50:39 +00:00
2066a469dc get_game_url is added 2022-08-29 09:38:51 +00:00
fbb13adaa9 version up 2022-08-29 04:18:11 +00:00
430c0bc94a bug fixed 2022-08-28 16:34:52 +00:00
994e405717 .devcontainer is changed 2022-08-28 15:14:45 +00:00
8b18af247c version up 2022-08-28 03:18:57 +00:00
182cdc736f refactoring 2022-08-27 08:00:41 +00:00
c87e49817b member, member_bank_account are modified 2022-08-24 07:16:03 +00:00
af6bbc8a4f version up 2022-08-22 08:37:57 +00:00
826d4f6111 version up 2022-08-22 07:33:28 +00:00
9464f6d8a6 signinWithoutSecurityCode is added 2022-08-20 13:06:04 +00:00
7b6f03f51f version up 2022-08-20 13:02:52 +00:00
30d9ca8334 member_referrer is added 2022-08-20 11:18:55 +00:00
9d41ff3bc0 development server is changed 2022-08-19 08:02:44 +00:00
1d64849cdf version up 2022-08-19 07:50:49 +00:00
7742baf450 configuration of devcontainer is changed 2022-08-05 16:30:35 +09:00
315bc1fdb8 initialized 2022-08-04 22:57:50 +09:00
29 changed files with 4805 additions and 0 deletions

9
.devcontainer/Dockerfile Normal file
View File

@ -0,0 +1,9 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/debian/.devcontainer/base.Dockerfile
# [Choice] Debian version (use bullseye on local arm64/Apple Silicon): bullseye, buster
ARG VARIANT="buster"
FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
# ** [Optional] Uncomment this section to install additional packages. **
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>

View File

@ -0,0 +1,66 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/ubuntu
{
"name": "beteran-frontend-server-edge",
"build": {
"dockerfile": "Dockerfile",
"args": {
// Use the VARIANT arg to pick a Debian OS version: buster, bullseye
// Use bullseye when on local on arm64/Apple Silicon.
"VARIANT": "bullseye"
}
},
"runArgs": [
"--cap-add=SYS_PTRACE",
"--security-opt",
"seccomp=unconfined"
],
// Use this environment variable if you need to bind mount your local source code into a new container.
"remoteEnv": {
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}"
},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"lldb.verboseLogging": true,
"lldb.executable": "/usr/bin/lldb",
// VS Code don't watch files under ./target
"files.watcherExclude": {
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/target/**": true
},
"rust-analyzer.checkOnSave.command": "clippy",
"editor.tabSize": 2,
"editor.insertSpaces": true,
"editor.formatOnSave": true
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"donjayamanne.githistory",
"eamodio.gitlens",
"mhutchie.git-graph",
"ms-azuretools.vscode-docker",
"mutantdino.resourcemonitor",
"rust-lang.rust-analyzer",
"serayuzgur.crates",
"tamasfe.even-better-toml",
"vadimcn.vscode-lldb"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "uname -a",
"postCreateCommand": "bash ./.devcontainer/scripts/postCreateCommand.sh",
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
"features": {
"git": "latest",
"rust": "latest"
}
}

View File

@ -0,0 +1,5 @@
[toolchain]
channel = "stable"
profile = "minimal"
components = ["clippy", "rustfmt"]
targets = []

View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -e

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.DS_Store
/build
# Added by cargo
/target
Cargo.lock

75
.rustfmt.toml Normal file
View File

@ -0,0 +1,75 @@
# https://rust-lang.github.io/rustfmt/?version=v1.4.38&search=
array_width = 60
attr_fn_like_width = 70
binop_separator = "Front" # "Front", "Back"
blank_lines_lower_bound = 0
blank_lines_upper_bound = 1
brace_style = "SameLineWhere" #"AlwaysNextLine", "PreferSameLine", "SameLineWhere"
chain_width = 60
color = "Auto" #"Auto", "Always", "Never"
combine_control_expr = true # true, false
comment_width = 80
condense_wildcard_suffixes = false # true, false
control_brace_style = "AlwaysSameLine" # "AlwaysNextLine", "AlwaysSameLine", "ClosingNextLine"
disable_all_formatting = false # true, false
edition = "2015" # "2015", "2018", "2021"
empty_item_single_line = true # true, false
enum_discrim_align_threshold = 0
error_on_line_overflow = false # true, false
error_on_unformatted = false # true, false
fn_args_layout = "Tall" # "Compressed", "Tall", "Vertical"
fn_call_width = 60
fn_single_line = false # true, false
force_explicit_abi = true # true, false
force_multiline_blocks = false # true, false
format_code_in_doc_comments = false # true, false
format_generated_files = false # true, false
format_macro_matchers = false # true, false
format_macro_bodies = true # true, false
format_strings = false # true, false
group_imports = "Preserve" # "Preserve", "StdExternalCrate"
hard_tabs = false # true, false
hex_literal_case = "Preserve" # "Upper", "Lower"
hide_parse_errors = false # true, false
ignore = []
imports_indent = "Block" # "Block", "Visual"
imports_layout = "Mixed" # "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
indent_style = "Block" # "Block", "Visual"
inline_attribute_width = 0
license_template_path = ""
match_arm_blocks = true # true, false
match_arm_leading_pipes = "Never" # "Always", "Never", "Preserve"
match_block_trailing_comma = false # true, false
max_width = 100
merge_derives = true # true, false
imports_granularity = "Preserve" # "Preserve", "Crate", "Module", "Item", "One"
merge_imports = false # true, false
newline_style = "Auto" # "Auto", "Native", "Unix", "Windows"
normalize_comments = false # true, false
normalize_doc_attributes = false # true, false
overflow_delimited_expr = false # true, false
remove_nested_parens = true # true, false
reorder_impl_items = false # true, false
reorder_imports = true # true, false
reorder_modules = true # true, false
report_fixme = "Never" # "Always", "Unnumbered", "Never"
report_todo = "Never" # "Always", "Unnumbered", "Never"
skip_children = false # true, false
single_line_if_else_max_width = 50
space_after_colon = true # true, false
space_before_colon = false # true, false
spaces_around_ranges = false # true, false
struct_field_align_threshold = 0
struct_lit_single_line = true # true, false
struct_lit_width = 18
struct_variant_width = 35
tab_spaces = 2
trailing_comma = "Vertical" # "Always", "Never", "Vertical"
trailing_semicolon = true # true, false
type_punctuation_density = "Wide" # "Compressed", "Wide"
unstable_features = false # true, false
use_field_init_shorthand = false # true, false
use_small_heuristics = "Default" # "Default", "Off", "Max"
use_try_shorthand = false # true, false
where_single_line = false # true, false
wrap_comments = false # true, false

53
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,53 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'beteran-frontend-server-edge'",
"cargo": {
"args": [
"build",
"--bin=beteran-frontend-server-edge",
"--package=beteran-frontend-server-edge"
],
"filter": {
"name": "beteran-frontend-server-edge",
"kind": "bin"
}
},
"env": {
"URL_BROKER": "nats://192.168.50.205:4222",
"QUEUE_BROKER": "bet.beteran",
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'beteran-frontend-server-edge'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=beteran-frontend-server-edge",
"--package=beteran-frontend-server-edge"
],
"filter": {
"name": "beteran-frontend-server-edge",
"kind": "bin"
}
},
"env": {
"URL_BROKER": "nats://192.168.50.205:4222",
"QUEUE_BROKER": "bet.beteran",
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

25
Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "beteran-frontend-server-edge"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
name = "beteran-frontend-server-edge"
path = "./src/main.rs"
[dependencies]
base64 = { version = "0" }
futures = { version = "0", default-features = false, features = [
"async-await",
] }
nats = { version = "0" }
prost = { version = "0" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.99-snapshot" }
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.85-snapshot" }
[build-dependencies]

9
resources/public.pem Normal file
View File

@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw/GvSFLy8K5uT79REy5l
JtIS+Y9uAP1SJEyLvAqWfdPh9YO5QCnYPp1uCqL3xWozkhrQWUbaYkO+65vjmYcG
BMlzO+7KvZcHh4wr8ybA03M6Yy6JSbnXRQvVm2Bvp2Z1B0dkT11CtZxiaNywYFvM
hh5+zEbsrbFkzN5zI+hNpyg4uaaQY5Oy2jGW3zuOIy/i0qzs3pCHWDAk0d5hpGvf
lUIj7BOQtl7CcFoB8tMp6F2fi8t9m/Qa0yvQz8CNf7pdDqprUlm509wKzB4HgX8a
G5SMwMlfuQjvT3jQNLRnHJFDghdZuCaop8IKf4PVGjuLo0SEebYzVEYfzLQrHemo
RwIDAQAB
-----END PUBLIC KEY-----

1
src/api/game/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod service;

341
src/api/game/service.rs Normal file
View File

@ -0,0 +1,341 @@
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-frontend-server-edge::game::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<dyn std::error::Error>> {
futures::try_join!(self.list_games(), self.get_game_url(),).map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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 get_member_session_with_renewal(
&self,
client: &bpr::models::core::network::Client,
) -> Result<Option<bpr::models::member_session::MemberSession>, bcr::error::rpc::Error> {
let ss_get_member_session_req = bpr::ss::member_session::GetMemberSessionWithRenewalRequest {
client: Some(client.clone()),
request: Some(bpr::ss::member_session::get_member_session_with_renewal_request::Request {}),
};
let ss_get_member_session_res_msg = self
.connection_broker
.request(
bpr::ss::member_session::SUBJECT_GET_MEMBER_SESSION_WITH_RENEWAL,
ss_get_member_session_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_session_res =
bpr::ss::member_session::GetMemberSessionWithRenewalResponse::decode(
ss_get_member_session_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_session_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_session_res.result {
return Ok(r.member_session);
}
Ok(None)
}
async fn list_games(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::api::game::SUBJECT_LIST_GAMES,
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::api::game::ListGamesRequest::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_games_req = bpr::ss::api::game::ListGamesRequest {
client: Some(client),
request: Some(bpr::ss::api::game::list_games_request::Request {
pagination: req.pagination,
sorts: req.sorts,
search: match req.search {
Some(s) => Some(bpr::ss::api::game::list_games_request::request::Search {
parent_id: s.parent_id,
key_like: s.key_like,
category_like: s.category_like,
platform_like: s.platform_like,
game_type_like: s.game_type_like,
}),
None => None,
},
}),
};
let ss_list_games_res_msg = self
.connection_broker
.request(
bpr::ss::api::game::SUBJECT_LIST_GAMES,
ss_list_games_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_games_res =
bpr::ss::api::game::ListGamesResponse::decode(ss_list_games_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_games_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_games_res.result {
message
.respond(
bpr::c2se::api::game::ListGamesResponse {
error: None,
result: Some(bpr::c2se::api::game::list_games_response::Result {
total_count: r.total_count,
games: r.games,
}),
}
.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::api::game::ListGamesResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn get_game_url(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::api::game::SUBJECT_GET_GAME_URL,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::api::game::GetGameUrlRequest::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_game_url_req = bpr::ss::api::game::GetGameUrlRequest {
client: Some(client),
request: Some(bpr::ss::api::game::get_game_url_request::Request {
vendor_key: req.vendor_key,
game_key: req.game_key,
member_id: member_session.member_id,
}),
};
let ss_get_game_url_res_msg = self
.connection_broker
.request(
bpr::ss::api::game::SUBJECT_GET_GAME_URL,
ss_get_game_url_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_game_url_res =
bpr::ss::api::game::GetGameUrlResponse::decode(ss_get_game_url_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_game_url_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_game_url_res.result {
message
.respond(
bpr::c2se::api::game::GetGameUrlResponse {
error: None,
result: Some(bpr::c2se::api::game::get_game_url_response::Result {
user_id: r.user_id,
balance: r.balance,
url: r.url,
}),
}
.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::api::game::GetGameUrlResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

2
src/api/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod game;
pub mod vendor;

1
src/api/vendor/mod.rs vendored Normal file
View File

@ -0,0 +1 @@
pub mod service;

180
src/api/vendor/service.rs vendored Normal file
View File

@ -0,0 +1,180 @@
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-frontend-server-edge::vendor::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<dyn std::error::Error>> {
futures::try_join!(self.list_vendors(),).map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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 list_vendors(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::api::vendor::SUBJECT_LIST_VENDORS,
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::api::vendor::ListVendorsRequest::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_vendors_req = bpr::ss::api::vendor::ListVendorsRequest {
client: Some(client),
request: Some(bpr::ss::api::vendor::list_vendors_request::Request {
pagination: req.pagination,
sorts: req.sorts,
search: match req.search {
Some(s) => Some(
bpr::ss::api::vendor::list_vendors_request::request::Search {
name_like: s.name_like,
company_id: s.company_id,
vendor_id: s.vendor_id,
key_like: s.key_like,
category_like: s.category_like,
is_enable: s.is_enable,
},
),
None => None,
},
}),
};
let ss_list_vendors_res_msg = self
.connection_broker
.request(
bpr::ss::api::vendor::SUBJECT_LIST_VENDORS,
ss_list_vendors_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_vendors_res = bpr::ss::api::vendor::ListVendorsResponse::decode(
ss_list_vendors_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_vendors_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_vendors_res.result {
message
.respond(
bpr::c2se::api::vendor::ListVendorsResponse {
error: None,
result: Some(bpr::c2se::api::vendor::list_vendors_response::Result {
total_count: r.total_count,
vendors: r.vendors,
}),
}
.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::api::vendor::ListVendorsResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

1
src/bank/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod service;

273
src/bank/service.rs Normal file
View File

@ -0,0 +1,273 @@
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-frontend-server-edge::bank::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<dyn std::error::Error>> {
futures::try_join!(self.list_banks(), self.get_bank(),).map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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 list_banks(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::bank::SUBJECT_LIST_BANKS,
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::bank::ListBanksRequest::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_banks_req = bpr::ss::bank::ListBanksRequest {
client: Some(client),
request: Some(bpr::ss::bank::list_banks_request::Request {
pagination: req.pagination,
sorts: req.sorts,
search: match req.search {
Some(s) => Some(bpr::ss::bank::list_banks_request::request::Search {
name_like: s.name_like,
show: s.show,
can_use: s.can_use,
memo_like: s.memo_like,
}),
None => None,
},
}),
};
let ss_list_banks_res_msg = self
.connection_broker
.request(
bpr::ss::bank::SUBJECT_LIST_BANKS,
ss_list_banks_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_banks_res = bpr::ss::bank::ListBanksResponse::decode(
ss_list_banks_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_banks_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_banks_res.result {
message
.respond(
bpr::c2se::bank::ListBanksResponse {
error: None,
result: Some(bpr::c2se::bank::list_banks_response::Result {
total_count: r.total_count,
banks: r.banks,
}),
}
.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::bank::ListBanksResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn get_bank(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::bank::SUBJECT_GET_BANK,
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::bank::GetBankRequest::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_bank_req = bpr::ss::bank::GetBankRequest {
client: Some(client),
request: Some(bpr::ss::bank::get_bank_request::Request { id: req.id }),
};
let ss_get_bank_res_msg = self
.connection_broker
.request(
bpr::ss::bank::SUBJECT_GET_BANK,
ss_get_bank_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_bank_res = bpr::ss::bank::GetBankResponse::decode(
ss_get_bank_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_bank_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_bank_res.result {
message
.respond(
bpr::c2se::bank::GetBankResponse {
error: None,
result: Some(bpr::c2se::bank::get_bank_response::Result { bank: r.bank }),
}
.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::bank::GetBankResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

1
src/identity/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod service;

600
src/identity/service.rs Normal file
View File

@ -0,0 +1,600 @@
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-frontend-server-edge::identity::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<dyn std::error::Error>> {
futures::try_join!(
self.check_username_for_duplication(),
self.check_nickname_for_duplication(),
self.captcha(),
self.signin(),
self.signin_without_security_code(),
)
.map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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 check_username_for_duplication(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::identity::SUBJECT_CHECK_USERNAME_FOR_DUPLICATION,
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::identity::CheckUsernameForDuplicationRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let ss_check_username_for_duplication_req =
bpr::ss::identity::CheckUsernameForDuplicationRequest {
client: Some(client),
request: Some(
bpr::ss::identity::check_username_for_duplication_request::Request {
username: req.username,
},
),
};
let ss_check_username_for_duplication_res_msg = self
.connection_broker
.request(
bpr::ss::identity::SUBJECT_CHECK_USERNAME_FOR_DUPLICATION,
ss_check_username_for_duplication_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_check_username_for_duplication_res =
bpr::ss::identity::CheckUsernameForDuplicationResponse::decode(
ss_check_username_for_duplication_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_check_username_for_duplication_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_check_username_for_duplication_res.result {
message
.respond(
bpr::c2se::identity::CheckUsernameForDuplicationResponse {
error: None,
result: Some(
bpr::c2se::identity::check_username_for_duplication_response::Result {
duplicated: r.duplicated,
},
),
}
.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::identity::CheckUsernameForDuplicationResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn check_nickname_for_duplication(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::identity::SUBJECT_CHECK_NICKNAME_FOR_DUPLICATION,
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::identity::CheckNicknameForDuplicationRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let ss_check_nickname_for_duplication_req =
bpr::ss::identity::CheckNicknameForDuplicationRequest {
client: Some(client),
request: Some(
bpr::ss::identity::check_nickname_for_duplication_request::Request {
nickname: req.nickname,
},
),
};
let ss_check_nickname_for_duplication_res_msg = self
.connection_broker
.request(
bpr::ss::identity::SUBJECT_CHECK_NICKNAME_FOR_DUPLICATION,
ss_check_nickname_for_duplication_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_check_nickname_for_duplication_res =
bpr::ss::identity::CheckNicknameForDuplicationResponse::decode(
ss_check_nickname_for_duplication_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_check_nickname_for_duplication_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_check_nickname_for_duplication_res.result {
message
.respond(
bpr::c2se::identity::CheckNicknameForDuplicationResponse {
error: None,
result: Some(
bpr::c2se::identity::check_nickname_for_duplication_response::Result {
duplicated: r.duplicated,
},
),
}
.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::identity::CheckNicknameForDuplicationResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn captcha(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::identity::SUBJECT_CAPTCHA,
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::identity::CaptchaRequest::decode(message.data.as_slice()).map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let ss_captcha_req = bpr::ss::identity::CaptchaRequest {
client: Some(client),
request: Some(bpr::ss::identity::captcha_request::Request {}),
};
let ss_captcha_res_msg = self
.connection_broker
.request(
bpr::ss::identity::SUBJECT_CAPTCHA,
ss_captcha_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_captcha_res = bpr::ss::identity::CaptchaResponse::decode(
ss_captcha_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_captcha_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_captcha_res.result {
message
.respond(
bpr::c2se::identity::CaptchaResponse {
error: None,
result: Some(bpr::c2se::identity::captcha_response::Result {
security_code_hash: r.security_code_hash,
image: r.image,
}),
}
.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::identity::CaptchaResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn signin(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::identity::SUBJECT_SIGNIN,
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::identity::SigninRequest::decode(message.data.as_slice()).map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let ss_signin_req = bpr::ss::identity::SigninRequest {
client: Some(client),
request: Some(bpr::ss::identity::signin_request::Request {
security_code_hash: req.security_code_hash,
security_code: req.security_code,
username: req.username,
password: req.password,
}),
};
let ss_signin_res_msg = self
.connection_broker
.request(
bpr::ss::identity::SUBJECT_SIGNIN,
ss_signin_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_signin_res = bpr::ss::identity::SigninResponse::decode(
ss_signin_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_signin_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_signin_res.result {
message
.respond(
bpr::c2se::identity::SigninResponse {
error: None,
result: Some(bpr::c2se::identity::signin_response::Result {
access_token: r.access_token,
}),
}
.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::identity::SigninResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn signin_without_security_code(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::identity::SUBJECT_SIGNIN_WITHOUT_SECURITY_CODE,
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::identity::SigninWithoutSecurityCodeRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let ss_signin_without_security_code_req =
bpr::ss::identity::SigninWithoutSecurityCodeRequest {
client: Some(client),
request: Some(
bpr::ss::identity::signin_without_security_code_request::Request {
username: req.username,
password: req.password,
},
),
};
let ss_signin_without_security_code_res_msg = self
.connection_broker
.request(
bpr::ss::identity::SUBJECT_SIGNIN_WITHOUT_SECURITY_CODE,
ss_signin_without_security_code_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_signin_without_security_code_res =
bpr::ss::identity::SigninWithoutSecurityCodeResponse::decode(
ss_signin_without_security_code_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_signin_without_security_code_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_signin_without_security_code_res.result {
message
.respond(
bpr::c2se::identity::SigninWithoutSecurityCodeResponse {
error: None,
result: Some(
bpr::c2se::identity::signin_without_security_code_response::Result {
access_token: r.access_token,
},
),
}
.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::identity::SigninWithoutSecurityCodeResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

60
src/main.rs Normal file
View File

@ -0,0 +1,60 @@
use std::env;
mod api;
mod bank;
mod identity;
mod member;
mod member_bank_account;
mod member_bank_deposit;
mod member_bank_withdraw;
mod member_referrer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url_broker = match env::var_os("URL_BROKER") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
let queue_broker = match env::var_os("QUEUE_BROKER") {
Some(v) => v.into_string().unwrap(),
None => "".to_string(),
};
let broker_opts = nats::asynk::Options::new();
let connection_broker = broker_opts.connect(url_broker).await?;
let bank_service = bank::service::Service::new(connection_broker.clone(), queue_broker.clone());
let identity_service =
identity::service::Service::new(connection_broker.clone(), queue_broker.clone());
let member_service =
member::service::Service::new(connection_broker.clone(), queue_broker.clone());
let member_bank_account_service =
member_bank_account::service::Service::new(connection_broker.clone(), queue_broker.clone());
let member_bank_deposit_service =
member_bank_deposit::service::Service::new(connection_broker.clone(), queue_broker.clone());
let member_bank_withdraw_service =
member_bank_withdraw::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 api_vendor_service =
api::vendor::service::Service::new(connection_broker.clone(), queue_broker.clone());
let api_game_service =
api::game::service::Service::new(connection_broker.clone(), queue_broker.clone());
println!("Server edge[beteran-frontend-server-edge] is started");
futures::try_join!(
bank_service.subscribe(),
identity_service.subscribe(),
member_service.subscribe(),
member_bank_account_service.subscribe(),
member_bank_deposit_service.subscribe(),
member_bank_withdraw_service.subscribe(),
member_referrer_service.subscribe(),
api_vendor_service.subscribe(),
api_game_service.subscribe(),
)?;
Ok(())
}

1
src/member/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod service;

874
src/member/service.rs Normal file
View File

@ -0,0 +1,874 @@
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-frontend-server-edge::identity::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<dyn std::error::Error>> {
futures::try_join!(
self.create_member(),
self.get_member(),
self.get_member_by_username(),
self.update_member(),
self.update_member_for_password(),
self.delete_member(),
)
.map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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 get_member_session_with_renewal(
&self,
client: &bpr::models::core::network::Client,
) -> Result<Option<bpr::models::member_session::MemberSession>, bcr::error::rpc::Error> {
let ss_get_member_session_req = bpr::ss::member_session::GetMemberSessionWithRenewalRequest {
client: Some(client.clone()),
request: Some(bpr::ss::member_session::get_member_session_with_renewal_request::Request {}),
};
let ss_get_member_session_res_msg = self
.connection_broker
.request(
bpr::ss::member_session::SUBJECT_GET_MEMBER_SESSION_WITH_RENEWAL,
ss_get_member_session_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_session_res =
bpr::ss::member_session::GetMemberSessionWithRenewalResponse::decode(
ss_get_member_session_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_session_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_session_res.result {
return Ok(r.member_session);
}
Ok(None)
}
async fn create_member(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member::SUBJECT_CREATE_MEMBER,
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::CreateMemberRequest::decode(message.data.as_slice()).map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let bank_account = match req.bank_account {
Some(a) => Some(
bpr::ss::member::create_member_request::request::BankAccount {
bank_id: a.bank_id,
name: a.name,
account_number: a.account_number,
exchange_password: a.exchange_password,
},
),
None => None,
};
let game_setting =
req.game_setting.map(
|a| bpr::ss::member::create_member_request::request::GameSetting {
can_bet_casino: a.can_bet_casino,
can_bet_slot: a.can_bet_slot,
can_bet_powerball: a.can_bet_powerball,
can_bet_powerladder: a.can_bet_powerladder,
can_bet_eos: a.can_bet_eos,
can_bet_bogglepowerball: a.can_bet_bogglepowerball,
},
);
let settlement_setting = req.settlement_setting.map(|a| {
bpr::ss::member::create_member_request::request::SettlementSetting {
can_exchange: a.can_exchange,
can_first_charge_comp: a.can_first_charge_comp,
can_per_charge_comp: a.can_per_charge_comp,
manual_payment_type_for_partner: a.manual_payment_type_for_partner,
settlement_type: a.settlement_type,
rate_casino: a.rate_casino,
rate_casino_loosing: a.rate_casino_loosing,
rate_casino_bacara: a.rate_casino_bacara,
rate_casino_roulette: a.rate_casino_roulette,
rate_casino_dragon_tiger: a.rate_casino_dragon_tiger,
rate_slot: a.rate_slot,
rate_slot_loosing: a.rate_slot_loosing,
rate_powerball_single: a.rate_powerball_single,
rate_powerball_combo: a.rate_powerball_combo,
rate_powerladder_single: a.rate_powerladder_single,
rate_powerladder_combo: a.rate_powerladder_combo,
rate_eos_single: a.rate_eos_single,
rate_eos_combo: a.rate_eos_combo,
rate_bogglepowerball_single: a.rate_bogglepowerball_single,
rate_bogglepowerball_combo: a.rate_bogglepowerball_combo,
}
});
let ss_create_member_req = bpr::ss::member::CreateMemberRequest {
client: Some(client),
request: Some(bpr::ss::member::create_member_request::Request {
site_id: req.site_id,
member_level_id: req.member_level_id,
mobile_phone_number: req.mobile_phone_number,
member_class_id: req.member_class_id,
referrer_member_username: req.referrer_member_username,
username: req.username,
password: req.password,
nickname: req.nickname,
state: bpr::models::member::MemberState::Pending as i32,
bank_account,
game_setting,
settlement_setting,
}),
};
let ss_create_member_res_msg = self
.connection_broker
.request(
bpr::ss::member::SUBJECT_CREATE_MEMBER,
ss_create_member_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_res =
bpr::ss::member::CreateMemberResponse::decode(ss_create_member_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_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_res.result {
message
.respond(
bpr::c2se::member::CreateMemberResponse {
error: None,
result: Some(bpr::c2se::member::create_member_response::Result {
member: r.member,
}),
}
.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::CreateMemberResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn get_member(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member::SUBJECT_GET_MEMBER,
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::GetMemberRequest::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_req = bpr::ss::member::GetMemberRequest {
client: Some(client),
request: Some(bpr::ss::member::get_member_request::Request { id: req.id }),
};
let ss_get_member_res_msg = self
.connection_broker
.request(
bpr::ss::member::SUBJECT_GET_MEMBER,
ss_get_member_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_res =
bpr::ss::member::GetMemberResponse::decode(ss_get_member_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_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_res.result {
message
.respond(
bpr::c2se::member::GetMemberResponse {
error: None,
result: Some(bpr::c2se::member::get_member_response::Result { member: r.member }),
}
.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::GetMemberResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn get_member_by_username(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member::SUBJECT_GET_MEMBER_BY_USERNAME,
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::GetMemberByUsernameRequest::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_by_username_req = bpr::ss::member::GetMemberByUsernameRequest {
client: Some(client),
request: Some(bpr::ss::member::get_member_by_username_request::Request {
username: req.username,
}),
};
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
.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_by_username_res = bpr::ss::member::GetMemberByUsernameResponse::decode(
ss_get_member_by_username_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_by_username_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_by_username_res.result {
message
.respond(
bpr::c2se::member::GetMemberByUsernameResponse {
error: None,
result: Some(bpr::c2se::member::get_member_by_username_response::Result {
member: r.member,
}),
}
.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::GetMemberByUsernameResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn update_member(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member::SUBJECT_UPDATE_MEMBER,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req =
bpr::c2se::member::UpdateMemberRequest::decode(message.data.as_slice()).map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let bank_account = match req.bank_account {
Some(a) => Some(
bpr::ss::member::update_member_request::request::BankAccount {
id: a.id,
bank_id: a.bank_id,
name: a.name,
account_number: a.account_number,
memo: a.memo,
},
),
None => None,
};
let game_setting = match req.game_setting {
Some(a) => Some(
bpr::ss::member::update_member_request::request::GameSetting {
id: a.id,
can_bet_casino: a.can_bet_casino,
can_bet_slot: a.can_bet_slot,
can_bet_powerball: a.can_bet_powerball,
can_bet_powerladder: a.can_bet_powerladder,
can_bet_eos: a.can_bet_eos,
can_bet_bogglepowerball: a.can_bet_bogglepowerball,
},
),
None => None,
};
let settlement_setting = match req.settlement_setting {
Some(a) => Some(
bpr::ss::member::update_member_request::request::SettlementSetting {
id: a.id,
can_exchange: a.can_exchange,
can_first_charge_comp: a.can_first_charge_comp,
can_per_charge_comp: a.can_per_charge_comp,
manual_payment_type_for_partner: a.manual_payment_type_for_partner,
settlement_type: a.settlement_type,
rate_casino: a.rate_casino,
rate_casino_loosing: a.rate_casino_loosing,
rate_casino_bacara: a.rate_casino_bacara,
rate_casino_roulette: a.rate_casino_roulette,
rate_casino_dragon_tiger: a.rate_casino_dragon_tiger,
rate_slot: a.rate_slot,
rate_slot_loosing: a.rate_slot_loosing,
rate_powerball_single: a.rate_powerball_single,
rate_powerball_combo: a.rate_powerball_combo,
rate_powerladder_single: a.rate_powerladder_single,
rate_powerladder_combo: a.rate_powerladder_combo,
rate_eos_single: a.rate_eos_single,
rate_eos_combo: a.rate_eos_combo,
rate_bogglepowerball_single: a.rate_bogglepowerball_single,
rate_bogglepowerball_combo: a.rate_bogglepowerball_combo,
},
),
None => None,
};
let ss_update_member_req = bpr::ss::member::UpdateMemberRequest {
client: Some(client),
request: Some(bpr::ss::member::update_member_request::Request {
id: member_session.member_id.clone(),
site_id: req.site_id,
member_level_id: req.member_level_id,
mobile_phone_number: req.mobile_phone_number,
bank_account,
game_setting,
settlement_setting,
}),
};
let ss_update_member_res_msg = self
.connection_broker
.request(
bpr::ss::member::SUBJECT_UPDATE_MEMBER,
ss_update_member_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_res =
bpr::ss::member::UpdateMemberResponse::decode(ss_update_member_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_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_res.result {
message
.respond(
bpr::c2se::member::UpdateMemberResponse {
error: None,
result: Some(bpr::c2se::member::update_member_response::Result {
member: r.member,
}),
}
.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::UpdateMemberResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn update_member_for_password(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member::SUBJECT_UPDATE_MEMBER_FOR_PASSWORD,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req =
bpr::c2se::member::UpdateMemberForPasswordRequest::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_req = bpr::ss::member::UpdateMemberForPasswordRequest {
client: Some(client),
request: Some(
bpr::ss::member::update_member_for_password_request::Request {
id: member_session.member_id.clone(),
password: req.password,
},
),
};
let ss_update_member_res_msg = self
.connection_broker
.request(
bpr::ss::member::SUBJECT_UPDATE_MEMBER_FOR_PASSWORD,
ss_update_member_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_res = bpr::ss::member::UpdateMemberForPasswordResponse::decode(
ss_update_member_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_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_res.result {
message
.respond(
bpr::c2se::member::UpdateMemberForPasswordResponse {
error: None,
result: Some(bpr::c2se::member::update_member_for_password_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::UpdateMemberForPasswordResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn delete_member(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member::SUBJECT_DELETE_MEMBER,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req =
bpr::c2se::member::DeleteMemberRequest::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_req = bpr::ss::member::DeleteMemberRequest {
client: Some(client),
request: Some(bpr::ss::member::delete_member_request::Request {
id: member_session.member_id.clone(),
}),
};
let ss_delete_member_res_msg = self
.connection_broker
.request(
bpr::ss::member::SUBJECT_DELETE_MEMBER,
ss_delete_member_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_res =
bpr::ss::member::DeleteMemberResponse::decode(ss_delete_member_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_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_res.result {
message
.respond(
bpr::c2se::member::DeleteMemberResponse {
error: None,
result: Some(bpr::c2se::member::delete_member_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::DeleteMemberResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

View File

@ -0,0 +1 @@
pub mod service;

View File

@ -0,0 +1,559 @@
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-frontend-server-edge::member_bank_account::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<dyn std::error::Error>> {
futures::try_join!(
self.create_member_bank_account(),
self.get_member_bank_account(),
self.update_member_bank_account(),
self.update_member_bank_account_for_exchange_password(),
)
.map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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 get_member_session_with_renewal(
&self,
client: &bpr::models::core::network::Client,
) -> Result<Option<bpr::models::member_session::MemberSession>, bcr::error::rpc::Error> {
let ss_get_member_session_req = bpr::ss::member_session::GetMemberSessionWithRenewalRequest {
client: Some(client.clone()),
request: Some(bpr::ss::member_session::get_member_session_with_renewal_request::Request {}),
};
let ss_get_member_session_res_msg = self
.connection_broker
.request(
bpr::ss::member_session::SUBJECT_GET_MEMBER_SESSION_WITH_RENEWAL,
ss_get_member_session_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_session_res =
bpr::ss::member_session::GetMemberSessionWithRenewalResponse::decode(
ss_get_member_session_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_session_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_session_res.result {
return Ok(r.member_session);
}
Ok(None)
}
async fn create_member_bank_account(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::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 client = self.get_client_in_header(&message)?;
let req = bpr::c2se::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 ss_create_member_bank_account_req =
bpr::ss::member_bank_account::CreateMemberBankAccountRequest {
client: Some(client),
request: Some(
bpr::ss::member_bank_account::create_member_bank_account_request::Request {
member_id: req.member_id,
bank_id: req.bank_id,
name: req.name,
account_number: req.account_number,
exchange_password: req.exchange_password,
memo: req.memo,
},
),
};
let ss_create_member_bank_account_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_account::SUBJECT_CREATE_MEMBER_BANK_ACCOUNT,
ss_create_member_bank_account_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_bank_account_res =
bpr::ss::member_bank_account::CreateMemberBankAccountResponse::decode(
ss_create_member_bank_account_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_bank_account_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_bank_account_res.result {
message
.respond(
bpr::c2se::member_bank_account::CreateMemberBankAccountResponse {
error: None,
result: Some(
bpr::c2se::member_bank_account::create_member_bank_account_response::Result {
member_bank_account: r.member_bank_account,
},
),
}
.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_bank_account::CreateMemberBankAccountResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn get_member_bank_account(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::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 client = self.get_client_in_header(&message)?;
let req = bpr::c2se::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 ss_get_member_bank_account_req =
bpr::ss::member_bank_account::GetMemberBankAccountRequest {
client: Some(client),
request: Some(
bpr::ss::member_bank_account::get_member_bank_account_request::Request { id: req.id },
),
};
let ss_get_member_bank_account_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_account::SUBJECT_GET_MEMBER_BANK_ACCOUNT,
ss_get_member_bank_account_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_bank_account_res =
bpr::ss::member_bank_account::GetMemberBankAccountResponse::decode(
ss_get_member_bank_account_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_bank_account_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_bank_account_res.result {
message
.respond(
bpr::c2se::member_bank_account::GetMemberBankAccountResponse {
error: None,
result: Some(
bpr::c2se::member_bank_account::get_member_bank_account_response::Result {
member_bank_account: r.member_bank_account,
},
),
}
.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_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<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::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 client = self.get_client_in_header(&message)?;
let req = bpr::c2se::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 ss_update_member_bank_account_req =
bpr::ss::member_bank_account::UpdateMemberBankAccountRequest {
client: Some(client),
request: Some(
bpr::ss::member_bank_account::update_member_bank_account_request::Request {
id: req.id,
bank_id: req.bank_id,
name: req.name,
account_number: req.account_number,
memo: req.memo,
},
),
};
let ss_update_member_bank_account_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_account::SUBJECT_UPDATE_MEMBER_BANK_ACCOUNT,
ss_update_member_bank_account_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_bank_account_res =
bpr::ss::member_bank_account::UpdateMemberBankAccountResponse::decode(
ss_update_member_bank_account_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_bank_account_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_bank_account_res.result {
message
.respond(
bpr::c2se::member_bank_account::UpdateMemberBankAccountResponse {
error: None,
result: Some(
bpr::c2se::member_bank_account::update_member_bank_account_response::Result {
member_bank_account: r.member_bank_account,
},
),
}
.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_bank_account::UpdateMemberBankAccountResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn update_member_bank_account_for_exchange_password(
&self,
) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_account::SUBJECT_UPDATE_MEMBER_BANK_ACCOUNT_FOR_EXCHANGE_PASSWORD,
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_bank_account::UpdateMemberBankAccountForExchangePasswordRequest::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_bank_account_req = bpr::ss::member_bank_account::UpdateMemberBankAccountForExchangePasswordRequest {
client: Some(client),
request: Some(bpr::ss::member_bank_account::update_member_bank_account_for_exchange_password_request::Request{
id: req.id,
exchange_password: req.exchange_password,
})
};
let ss_update_member_bank_account_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_account::SUBJECT_UPDATE_MEMBER_BANK_ACCOUNT_FOR_EXCHANGE_PASSWORD,
ss_update_member_bank_account_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_bank_account_res = bpr::ss::member_bank_account::UpdateMemberBankAccountForExchangePasswordResponse::decode(
ss_update_member_bank_account_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_bank_account_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_bank_account_res.result {
message
.respond(
bpr::c2se::member_bank_account::UpdateMemberBankAccountForExchangePasswordResponse {
error: None,
result: Some(bpr::c2se::member_bank_account::update_member_bank_account_for_exchange_password_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_bank_account::UpdateMemberBankAccountForExchangePasswordResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

View File

@ -0,0 +1 @@
pub mod service;

View File

@ -0,0 +1,724 @@
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-frontend-server-edge::member_bank_deposit::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<dyn std::error::Error>> {
futures::try_join!(
self.create_member_bank_deposit(),
self.list_member_bank_deposits(),
self.get_member_bank_deposit(),
self.update_member_bank_deposit(),
self.delete_member_bank_deposit(),
)
.map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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 get_member_session_with_renewal(
&self,
client: &bpr::models::core::network::Client,
) -> Result<Option<bpr::models::member_session::MemberSession>, bcr::error::rpc::Error> {
let ss_get_member_session_req = bpr::ss::member_session::GetMemberSessionWithRenewalRequest {
client: Some(client.clone()),
request: Some(bpr::ss::member_session::get_member_session_with_renewal_request::Request {}),
};
let ss_get_member_session_res_msg = self
.connection_broker
.request(
bpr::ss::member_session::SUBJECT_GET_MEMBER_SESSION_WITH_RENEWAL,
ss_get_member_session_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_session_res =
bpr::ss::member_session::GetMemberSessionWithRenewalResponse::decode(
ss_get_member_session_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_session_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_session_res.result {
return Ok(r.member_session);
}
Ok(None)
}
async fn create_member_bank_deposit(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_deposit::SUBJECT_CREATE_MEMBER_BANK_DEPOSIT,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_deposit::CreateMemberBankDepositRequest::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_bank_deposit_req =
bpr::ss::member_bank_deposit::CreateMemberBankDepositRequest {
client: Some(client),
request: Some(
bpr::ss::member_bank_deposit::create_member_bank_deposit_request::Request {
member_id: member_session.member_id.clone(),
name: req.name,
amount: req.amount,
memo: req.memo,
},
),
};
let ss_create_member_bank_deposit_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_deposit::SUBJECT_CREATE_MEMBER_BANK_DEPOSIT,
ss_create_member_bank_deposit_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_bank_deposit_res =
bpr::ss::member_bank_deposit::CreateMemberBankDepositResponse::decode(
ss_create_member_bank_deposit_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_bank_deposit_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_bank_deposit_res.result {
message
.respond(
bpr::c2se::member_bank_deposit::CreateMemberBankDepositResponse {
error: None,
result: Some(
bpr::c2se::member_bank_deposit::create_member_bank_deposit_response::Result {
member_bank_deposit: r.member_bank_deposit,
},
),
}
.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_bank_deposit::CreateMemberBankDepositResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn list_member_bank_deposits(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_deposit::SUBJECT_LIST_MEMBER_BANK_DEPOSITS,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_deposit::ListMemberBankDepositsRequest::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_bank_deposits_req = bpr::ss::member_bank_deposit::ListMemberBankDepositsRequest {
client: Some(client),
request: Some(bpr::ss::member_bank_deposit::list_member_bank_deposits_request::Request {
pagination: req.pagination,
sorts: req.sorts,
search: match req.search {
Some(s) => Some(bpr::ss::member_bank_deposit::list_member_bank_deposits_request::request::Search {
member_id: Some(member_session.member_id.clone()),
name_like: s.name_like,
amount: s.amount,
state: s.state,
memo_like: s.memo_like,
}),
None => None,
}
})
};
let ss_list_member_bank_deposits_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_deposit::SUBJECT_LIST_MEMBER_BANK_DEPOSITS,
ss_list_member_bank_deposits_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_bank_deposits_res =
bpr::ss::member_bank_deposit::ListMemberBankDepositsResponse::decode(ss_list_member_bank_deposits_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_bank_deposits_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_bank_deposits_res.result {
message
.respond(
bpr::c2se::member_bank_deposit::ListMemberBankDepositsResponse {
error: None,
result: Some(bpr::c2se::member_bank_deposit::list_member_bank_deposits_response::Result {
total_count: r.total_count,
member_bank_deposits: r.member_bank_deposits,
}),
}
.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_bank_deposit::ListMemberBankDepositsResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn get_member_bank_deposit(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_deposit::SUBJECT_GET_MEMBER_BANK_DEPOSIT,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_deposit::GetMemberBankDepositRequest::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_bank_deposit_req =
bpr::ss::member_bank_deposit::GetMemberBankDepositRequest {
client: Some(client),
request: Some(
bpr::ss::member_bank_deposit::get_member_bank_deposit_request::Request { id: req.id },
),
};
let ss_get_member_bank_deposit_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_deposit::SUBJECT_GET_MEMBER_BANK_DEPOSIT,
ss_get_member_bank_deposit_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_bank_deposit_res =
bpr::ss::member_bank_deposit::GetMemberBankDepositResponse::decode(
ss_get_member_bank_deposit_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_bank_deposit_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_bank_deposit_res.result {
message
.respond(
bpr::c2se::member_bank_deposit::GetMemberBankDepositResponse {
error: None,
result: Some(
bpr::c2se::member_bank_deposit::get_member_bank_deposit_response::Result {
member_bank_deposit: r.member_bank_deposit,
},
),
}
.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_bank_deposit::GetMemberBankDepositResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn update_member_bank_deposit(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_deposit::SUBJECT_UPDATE_MEMBER_BANK_DEPOSIT,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_deposit::UpdateMemberBankDepositRequest::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_bank_deposit_req =
bpr::ss::member_bank_deposit::UpdateMemberBankDepositRequest {
client: Some(client),
request: Some(
bpr::ss::member_bank_deposit::update_member_bank_deposit_request::Request {
id: req.id,
name: req.name,
amount: req.amount,
memo: req.memo,
},
),
};
let ss_update_member_bank_deposit_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_deposit::SUBJECT_UPDATE_MEMBER_BANK_DEPOSIT,
ss_update_member_bank_deposit_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_bank_deposit_res =
bpr::ss::member_bank_deposit::UpdateMemberBankDepositResponse::decode(
ss_update_member_bank_deposit_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_bank_deposit_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_bank_deposit_res.result {
message
.respond(
bpr::c2se::member_bank_deposit::UpdateMemberBankDepositResponse {
error: None,
result: Some(
bpr::c2se::member_bank_deposit::update_member_bank_deposit_response::Result {
member_bank_deposit: r.member_bank_deposit,
},
),
}
.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_bank_deposit::UpdateMemberBankDepositResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn delete_member_bank_deposit(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_deposit::SUBJECT_DELETE_MEMBER_BANK_DEPOSIT,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_deposit::DeleteMemberBankDepositRequest::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_bank_deposit_req =
bpr::ss::member_bank_deposit::DeleteMemberBankDepositRequest {
client: Some(client),
request: Some(
bpr::ss::member_bank_deposit::delete_member_bank_deposit_request::Request {
id: req.id,
},
),
};
let ss_delete_member_bank_deposit_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_deposit::SUBJECT_DELETE_MEMBER_BANK_DEPOSIT,
ss_delete_member_bank_deposit_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_bank_deposit_res =
bpr::ss::member_bank_deposit::DeleteMemberBankDepositResponse::decode(
ss_delete_member_bank_deposit_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_bank_deposit_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_bank_deposit_res.result {
message
.respond(
bpr::c2se::member_bank_deposit::DeleteMemberBankDepositResponse {
error: None,
result: Some(
bpr::c2se::member_bank_deposit::delete_member_bank_deposit_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_bank_deposit::DeleteMemberBankDepositResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

View File

@ -0,0 +1 @@
pub mod service;

View File

@ -0,0 +1,706 @@
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-frontend-server-edge::member_bank_withdraw::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<dyn std::error::Error>> {
futures::try_join!(
self.create_member_bank_withdraw(),
self.list_member_bank_withdraws(),
self.get_member_bank_withdraw(),
self.update_member_bank_withdraw(),
self.delete_member_bank_withdraw(),
)
.map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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 get_member_session_with_renewal(
&self,
client: &bpr::models::core::network::Client,
) -> Result<Option<bpr::models::member_session::MemberSession>, bcr::error::rpc::Error> {
let ss_get_member_session_req = bpr::ss::member_session::GetMemberSessionWithRenewalRequest {
client: Some(client.clone()),
request: Some(bpr::ss::member_session::get_member_session_with_renewal_request::Request {}),
};
let ss_get_member_session_res_msg = self
.connection_broker
.request(
bpr::ss::member_session::SUBJECT_GET_MEMBER_SESSION_WITH_RENEWAL,
ss_get_member_session_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_session_res =
bpr::ss::member_session::GetMemberSessionWithRenewalResponse::decode(
ss_get_member_session_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_session_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_session_res.result {
return Ok(r.member_session);
}
Ok(None)
}
async fn create_member_bank_withdraw(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_withdraw::SUBJECT_CREATE_MEMBER_BANK_WITHDRAW,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_withdraw::CreateMemberBankWithdrawRequest::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_bank_withdraw_req =
bpr::ss::member_bank_withdraw::CreateMemberBankWithdrawRequest {
client: Some(client),
request: Some(
bpr::ss::member_bank_withdraw::create_member_bank_withdraw_request::Request {
member_id: member_session.member_id.clone(),
bank_name: req.bank_name,
account_number: req.account_number,
amount: req.amount,
password: req.password,
name: req.name,
memo: req.memo,
},
),
};
let ss_create_member_bank_withdraw_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_withdraw::SUBJECT_CREATE_MEMBER_BANK_WITHDRAW,
ss_create_member_bank_withdraw_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_bank_withdraw_res =
bpr::ss::member_bank_withdraw::CreateMemberBankWithdrawResponse::decode(
ss_create_member_bank_withdraw_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_bank_withdraw_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_bank_withdraw_res.result {
message
.respond(
bpr::c2se::member_bank_withdraw::CreateMemberBankWithdrawResponse {
error: None,
result: Some(
bpr::c2se::member_bank_withdraw::create_member_bank_withdraw_response::Result {
member_bank_withdraw: r.member_bank_withdraw,
},
),
}
.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_bank_withdraw::CreateMemberBankWithdrawResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn list_member_bank_withdraws(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_withdraw::SUBJECT_LIST_MEMBER_BANK_WITHDRAWS,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_withdraw::ListMemberBankWithdrawsRequest::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_bank_withdraws_req = bpr::ss::member_bank_withdraw::ListMemberBankWithdrawsRequest {
client: Some(client),
request: Some(bpr::ss::member_bank_withdraw::list_member_bank_withdraws_request::Request{
pagination: req.pagination,
sorts: req.sorts,
search: match req.search {
Some(s) => Some(bpr::ss::member_bank_withdraw::list_member_bank_withdraws_request::request::Search {
name_like: s.name_like,
member_id: Some(member_session.member_id.clone()),
bank_name_like: s.bank_name_like,
account_number_like: s.account_number_like,
amount: s.amount,
state: s.state,
memo_like: s.memo_like,
}),
None => None,
}
})
};
let ss_list_member_bank_withdraws_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_withdraw::SUBJECT_LIST_MEMBER_BANK_WITHDRAWS,
ss_list_member_bank_withdraws_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_bank_withdraws_res =
bpr::ss::member_bank_withdraw::ListMemberBankWithdrawsResponse::decode(ss_list_member_bank_withdraws_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_bank_withdraws_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_bank_withdraws_res.result {
message
.respond(
bpr::c2se::member_bank_withdraw::ListMemberBankWithdrawsResponse {
error: None,
result: Some(bpr::c2se::member_bank_withdraw::list_member_bank_withdraws_response::Result {
total_count: r.total_count,
member_bank_withdraws: r.member_bank_withdraws,
}),
}
.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_bank_withdraw::ListMemberBankWithdrawsResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn get_member_bank_withdraw(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_withdraw::SUBJECT_GET_MEMBER_BANK_WITHDRAW,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_withdraw::GetMemberBankWithdrawRequest::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_bank_withdraw_req = bpr::ss::member_bank_withdraw::GetMemberBankWithdrawRequest {
client: Some(client),
request: Some(bpr::ss::member_bank_withdraw::get_member_bank_withdraw_request::Request{
id: req.id,
})
};
let ss_get_member_bank_withdraw_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_withdraw::SUBJECT_GET_MEMBER_BANK_WITHDRAW,
ss_get_member_bank_withdraw_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_bank_withdraw_res =
bpr::ss::member_bank_withdraw::GetMemberBankWithdrawResponse::decode(ss_get_member_bank_withdraw_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_bank_withdraw_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_bank_withdraw_res.result {
message
.respond(
bpr::c2se::member_bank_withdraw::GetMemberBankWithdrawResponse {
error: None,
result: Some(bpr::c2se::member_bank_withdraw::get_member_bank_withdraw_response::Result { member_bank_withdraw: r.member_bank_withdraw }),
}
.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_bank_withdraw::GetMemberBankWithdrawResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn update_member_bank_withdraw(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_withdraw::SUBJECT_UPDATE_MEMBER_BANK_WITHDRAW,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_withdraw::UpdateMemberBankWithdrawRequest::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_bank_withdraw_req = bpr::ss::member_bank_withdraw::UpdateMemberBankWithdrawRequest {
client: Some(client),
request: Some(bpr::ss::member_bank_withdraw::update_member_bank_withdraw_request::Request{
id: req.id,
bank_name: req.bank_name,
account_number: req.account_number,
amount: req.amount,
password: req.password,
name: req.name,
memo: req.memo,
})
};
let ss_update_member_bank_withdraw_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_withdraw::SUBJECT_UPDATE_MEMBER_BANK_WITHDRAW,
ss_update_member_bank_withdraw_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_bank_withdraw_res = bpr::ss::member_bank_withdraw::UpdateMemberBankWithdrawResponse::decode(
ss_update_member_bank_withdraw_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_bank_withdraw_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_bank_withdraw_res.result {
message
.respond(
bpr::c2se::member_bank_withdraw::UpdateMemberBankWithdrawResponse {
error: None,
result: Some(bpr::c2se::member_bank_withdraw::update_member_bank_withdraw_response::Result {
member_bank_withdraw: r.member_bank_withdraw,
}),
}
.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_bank_withdraw::UpdateMemberBankWithdrawResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn delete_member_bank_withdraw(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_bank_withdraw::SUBJECT_DELETE_MEMBER_BANK_WITHDRAW,
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 member_session = match self.get_member_session_with_renewal(&client).await? {
Some(ms) => ms,
None => {
return Err(bcr::error::rpc::Error::InvalidRequest(
bcr::error::rpc::InvalidRequest {
message: "Unauthorized".to_string(),
},
));
}
};
let req = bpr::c2se::member_bank_withdraw::DeleteMemberBankWithdrawRequest::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_bank_withdraw_req = bpr::ss::member_bank_withdraw::DeleteMemberBankWithdrawRequest {
client: Some(client),
request: Some(bpr::ss::member_bank_withdraw::delete_member_bank_withdraw_request::Request{
id: req.id,
})
};
let ss_delete_member_bank_withdraw_res_msg = self
.connection_broker
.request(
bpr::ss::member_bank_withdraw::SUBJECT_DELETE_MEMBER_BANK_WITHDRAW,
ss_delete_member_bank_withdraw_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_bank_withdraw_res = bpr::ss::member_bank_withdraw::DeleteMemberBankWithdrawResponse::decode(
ss_delete_member_bank_withdraw_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_bank_withdraw_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_bank_withdraw_res.result {
message
.respond(
bpr::c2se::member_bank_withdraw::DeleteMemberBankWithdrawResponse {
error: None,
result: Some(bpr::c2se::member_bank_withdraw::delete_member_bank_withdraw_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_bank_withdraw::DeleteMemberBankWithdrawResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}

View File

@ -0,0 +1 @@
pub mod service;

View File

@ -0,0 +1,224 @@
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-frontend-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<dyn std::error::Error>> {
futures::try_join!(self.get_member_referrer_by_code(),).map(|_| ())
}
fn get_client_in_header(
&self,
message: &nats::asynk::Message,
) -> Result<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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 get_member_session_with_renewal(
&self,
client: &bpr::models::core::network::Client,
) -> Result<Option<bpr::models::member_session::MemberSession>, bcr::error::rpc::Error> {
let ss_get_member_session_req = bpr::ss::member_session::GetMemberSessionWithRenewalRequest {
client: Some(client.clone()),
request: Some(bpr::ss::member_session::get_member_session_with_renewal_request::Request {}),
};
let ss_get_member_session_res_msg = self
.connection_broker
.request(
bpr::ss::member_session::SUBJECT_GET_MEMBER_SESSION_WITH_RENEWAL,
ss_get_member_session_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_session_res =
bpr::ss::member_session::GetMemberSessionWithRenewalResponse::decode(
ss_get_member_session_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_session_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_session_res.result {
return Ok(r.member_session);
}
Ok(None)
}
async fn get_member_referrer_by_code(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::c2se::frontend::member_referrer::SUBJECT_GET_MEMBER_REFERRER_BY_CODE,
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::GetMemberReferrerByCodeRequest::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_by_code_req =
bpr::ss::member_referrer::GetMemberReferrerByCodeRequest {
client: Some(client),
request: Some(
bpr::ss::member_referrer::get_member_referrer_by_code_request::Request {
code: req.code,
},
),
};
let ss_get_member_referrer_by_code_res_msg = self
.connection_broker
.request(
bpr::ss::member_referrer::SUBJECT_GET_MEMBER_REFERRER_BY_CODE,
ss_get_member_referrer_by_code_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_by_code_res =
bpr::ss::member_referrer::GetMemberReferrerByCodeResponse::decode(
ss_get_member_referrer_by_code_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_by_code_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_by_code_res.result {
message
.respond(
bpr::c2se::member_referrer::GetMemberReferrerByCodeResponse {
error: None,
result: Some(
bpr::c2se::member_referrer::get_member_referrer_by_code_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::GetMemberReferrerByCodeResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}