1use crate::{AuthorId, AuthorKind, ProjectId, TenantId, UserRole, VersionId, scopes::Scopes};
2use haste_fhir_model::r4::generated::terminology::IssueType;
3use haste_fhir_operation_error::OperationOutcomeError;
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
7pub enum SubscriptionTier {
8 #[serde(rename = "free")]
9 Free,
10 #[serde(rename = "professional")]
11 Professional,
12 #[serde(rename = "team")]
13 Team,
14 #[serde(rename = "unlimited")]
15 Unlimited,
16}
17
18impl From<SubscriptionTier> for String {
19 fn from(tier: SubscriptionTier) -> Self {
20 match tier {
21 SubscriptionTier::Free => "free".to_string(),
22 SubscriptionTier::Professional => "professional".to_string(),
23 SubscriptionTier::Team => "team".to_string(),
24 SubscriptionTier::Unlimited => "unlimited".to_string(),
25 }
26 }
27}
28
29impl TryFrom<String> for SubscriptionTier {
30 type Error = OperationOutcomeError;
31 fn try_from(value: String) -> Result<Self, Self::Error> {
32 match value.as_str() {
33 "free" => Ok(SubscriptionTier::Free),
34 "professional" => Ok(SubscriptionTier::Professional),
35 "team" => Ok(SubscriptionTier::Team),
36 "unlimited" => Ok(SubscriptionTier::Unlimited),
37 _ => Err(OperationOutcomeError::error(
38 IssueType::Invalid(None),
39 format!("Invalid subscription tier: '{}'", value),
40 )),
41 }
42 }
43}
44
45#[derive(Serialize, Deserialize, Debug, Clone)]
46pub struct UserTokenClaims {
47 pub sub: AuthorId,
48 pub exp: usize,
49 pub aud: String,
50 pub scope: Scopes,
51
52 #[serde(rename = "https://haste.health/tenant")]
53 pub tenant: TenantId,
54 #[serde(rename = "https://haste.health/subscription_tier")]
55 pub subscription_tier: SubscriptionTier,
56 #[serde(rename = "https://haste.health/project")]
57 pub project: Option<ProjectId>,
58 #[serde(rename = "https://haste.health/user_role")]
59 pub user_role: UserRole,
60 #[serde(rename = "https://haste.health/user_id")]
61 pub user_id: AuthorId,
62 #[serde(rename = "https://haste.health/resource_type")]
63 pub resource_type: AuthorKind,
64 #[serde(rename = "https://haste.health/access_policies")]
65 pub access_policy_version_ids: Vec<VersionId>,
66 #[serde(rename = "https://haste.health/membership")]
67 pub membership: Option<String>,
68}