1use crate::db::UserId;
 2use chrono::NaiveDateTime;
 3use sea_orm::entity::prelude::*;
 4use serde::Serialize;
 5
 6/// A user model.
 7#[derive(Clone, Debug, Default, PartialEq, Eq, DeriveEntityModel, Serialize)]
 8#[sea_orm(table_name = "users")]
 9pub struct Model {
10    #[sea_orm(primary_key)]
11    pub id: UserId,
12    pub github_login: String,
13    pub github_user_id: i32,
14    pub github_user_created_at: Option<NaiveDateTime>,
15    pub email_address: Option<String>,
16    pub name: Option<String>,
17    pub admin: bool,
18    pub invite_code: Option<String>,
19    pub invite_count: i32,
20    pub inviter_id: Option<UserId>,
21    pub connected_once: bool,
22    pub metrics_id: Uuid,
23    pub created_at: NaiveDateTime,
24    pub accepted_tos_at: Option<NaiveDateTime>,
25    pub custom_llm_monthly_allowance_in_cents: Option<i32>,
26}
27
28#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
29pub enum Relation {
30    #[sea_orm(has_many = "super::access_token::Entity")]
31    AccessToken,
32    #[sea_orm(has_one = "super::room_participant::Entity")]
33    RoomParticipant,
34    #[sea_orm(has_many = "super::project::Entity")]
35    HostedProjects,
36    #[sea_orm(has_many = "super::channel_member::Entity")]
37    ChannelMemberships,
38    #[sea_orm(has_one = "super::contributor::Entity")]
39    Contributor,
40}
41
42impl Model {
43    /// Returns the timestamp of when the user's account was created.
44    ///
45    /// This will be the earlier of the `created_at` and `github_user_created_at` timestamps.
46    pub fn account_created_at(&self) -> NaiveDateTime {
47        let mut account_created_at = self.created_at;
48        if let Some(github_created_at) = self.github_user_created_at {
49            account_created_at = account_created_at.min(github_created_at);
50        }
51
52        account_created_at
53    }
54
55    /// Returns the age of the user's account.
56    pub fn account_age(&self) -> chrono::Duration {
57        chrono::Utc::now().naive_utc() - self.account_created_at()
58    }
59}
60
61impl Related<super::access_token::Entity> for Entity {
62    fn to() -> RelationDef {
63        Relation::AccessToken.def()
64    }
65}
66
67impl Related<super::room_participant::Entity> for Entity {
68    fn to() -> RelationDef {
69        Relation::RoomParticipant.def()
70    }
71}
72
73impl Related<super::project::Entity> for Entity {
74    fn to() -> RelationDef {
75        Relation::HostedProjects.def()
76    }
77}
78
79impl Related<super::channel_member::Entity> for Entity {
80    fn to() -> RelationDef {
81        Relation::ChannelMemberships.def()
82    }
83}
84
85impl ActiveModelBehavior for ActiveModel {}