Skip to content

Commit 23a96ab

Browse files
authored
Add identity type (#367)
Now when we call active identity endpoint we also return identity type.
1 parent 7f979ea commit 23a96ab

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/service/identity_service.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,38 @@ pub trait IdentityServiceApi: Send + Sync {
5858
) -> Result<Vec<u8>>;
5959
}
6060

61+
#[repr(u8)]
62+
#[derive(
63+
Debug,
64+
Clone,
65+
serde_repr::Serialize_repr,
66+
serde_repr::Deserialize_repr,
67+
PartialEq,
68+
Eq,
69+
ToSchema,
70+
BorshSerialize,
71+
BorshDeserialize,
72+
)]
73+
#[borsh(use_discriminant = true)]
74+
pub enum IdentityType {
75+
Person = 0,
76+
Company = 1,
77+
}
78+
79+
impl TryFrom<u64> for IdentityType {
80+
type Error = super::Error;
81+
82+
fn try_from(value: u64) -> std::result::Result<Self, Self::Error> {
83+
match value {
84+
0 => Ok(IdentityType::Person),
85+
1 => Ok(IdentityType::Company),
86+
_ => Err(super::Error::Validation(format!(
87+
"Invalid identity type found: {value}"
88+
))),
89+
}
90+
}
91+
}
92+
6193
/// The identity service is responsible for managing the local identity and syncing it
6294
/// with the dht data.
6395
#[derive(Clone)]

src/web/data.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::service::identity_service::IdentityType;
12
use crate::service::{
23
bill_service::LightBitcreditBillToReturn,
34
company_service::CompanyToReturn,
@@ -251,6 +252,8 @@ pub struct BillCombinedBitcoinKey {
251252

252253
#[derive(Debug, Serialize, Deserialize, ToSchema)]
253254
pub struct SwitchIdentity {
255+
#[serde(rename = "type")]
256+
pub t: IdentityType,
254257
pub node_id: String,
255258
}
256259

src/web/handlers/identity.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env;
22

33
use super::middleware::IdentityCheck;
44
use crate::external;
5+
use crate::service::identity_service::IdentityType;
56
use crate::service::Result;
67
use crate::util::date::{format_date_string, now};
78
use crate::util::file::{detect_content_type_for_bytes, UploadFileHandler};
@@ -167,11 +168,11 @@ pub async fn change_identity(
167168
#[get("/active")]
168169
pub async fn active(state: &State<ServiceContext>) -> Result<Json<SwitchIdentity>> {
169170
let current_identity_state = state.get_current_identity().await;
170-
let node_id = match current_identity_state.company {
171-
None => current_identity_state.personal,
172-
Some(company_node_id) => company_node_id,
171+
let (node_id, t) = match current_identity_state.company {
172+
None => (current_identity_state.personal, IdentityType::Person),
173+
Some(company_node_id) => (company_node_id, IdentityType::Company),
173174
};
174-
Ok(Json(SwitchIdentity { node_id }))
175+
Ok(Json(SwitchIdentity { t, node_id }))
175176
}
176177

177178
#[utoipa::path(

0 commit comments

Comments
 (0)