systemprompt_users/services/user/
mod.rs1mod provider;
12
13use std::collections::HashMap;
14use std::sync::Arc;
15use systemprompt_identifiers::{SessionId, UserId};
16
17use crate::error::{Result, UserError};
18use crate::models::{
19 User, UserActivity, UserCountBreakdown, UserRole, UserSession, UserStats, UserStatus,
20 UserWithSessions,
21};
22use crate::repository::{MergeResult, UpdateUserParams, UserRepository};
23
24#[derive(Debug, Clone)]
25pub struct UserService {
26 repository: Arc<UserRepository>,
27}
28
29impl UserService {
30 pub const fn new(repository: Arc<UserRepository>) -> Self {
31 Self { repository }
32 }
33
34 pub async fn find_by_id(&self, id: &UserId) -> Result<Option<User>> {
35 self.repository.find_by_id(id).await
36 }
37
38 pub async fn find_by_email(&self, email: &str) -> Result<Option<User>> {
39 self.repository.find_by_email(email).await
40 }
41
42 pub async fn find_by_name(&self, name: &str) -> Result<Option<User>> {
43 self.repository.find_by_name(name).await
44 }
45
46 pub async fn find_by_role(&self, role: UserRole) -> Result<Vec<User>> {
47 self.repository.find_by_role(role).await
48 }
49
50 pub async fn find_first_user(&self) -> Result<Option<User>> {
51 self.repository.find_first_user().await
52 }
53
54 pub async fn find_first_admin(&self) -> Result<Option<User>> {
55 self.repository.find_first_admin().await
56 }
57
58 pub async fn find_authenticated_user(&self, user_id: &UserId) -> Result<Option<User>> {
59 self.repository.find_authenticated_user(user_id).await
60 }
61
62 pub async fn find_with_sessions(&self, user_id: &UserId) -> Result<Option<UserWithSessions>> {
63 self.repository.find_with_sessions(user_id).await
64 }
65
66 pub async fn get_activity(&self, user_id: &UserId) -> Result<UserActivity> {
67 self.repository.get_activity(user_id).await
68 }
69
70 pub async fn list(&self, limit: i64, offset: i64) -> Result<Vec<User>> {
71 self.repository.list(limit, offset).await
72 }
73
74 pub async fn list_all(&self) -> Result<Vec<User>> {
75 self.repository.list_all().await
76 }
77
78 pub async fn search(&self, query: &str, limit: i64) -> Result<Vec<User>> {
79 self.repository.search(query, limit).await
80 }
81
82 pub async fn count(&self) -> Result<i64> {
83 self.repository.count().await
84 }
85
86 pub async fn is_temporary_anonymous(&self, id: &UserId) -> Result<bool> {
87 self.repository.is_temporary_anonymous(id).await
88 }
89
90 pub async fn list_non_anonymous_with_sessions(
91 &self,
92 limit: i64,
93 ) -> Result<Vec<UserWithSessions>> {
94 self.repository
95 .list_non_anonymous_with_sessions(limit)
96 .await
97 }
98
99 pub async fn list_sessions(&self, user_id: &UserId) -> Result<Vec<UserSession>> {
100 self.repository.list_sessions(user_id).await
101 }
102
103 pub async fn list_active_sessions(&self, user_id: &UserId) -> Result<Vec<UserSession>> {
104 self.repository.list_active_sessions(user_id).await
105 }
106
107 pub async fn list_recent_sessions(
108 &self,
109 user_id: &UserId,
110 limit: i64,
111 ) -> Result<Vec<UserSession>> {
112 self.repository.list_recent_sessions(user_id, limit).await
113 }
114
115 pub async fn session_exists(&self, session_id: &SessionId) -> Result<bool> {
116 self.repository.session_exists(session_id).await
117 }
118
119 pub async fn end_session(&self, session_id: &SessionId) -> Result<bool> {
120 self.repository.end_session(session_id).await
121 }
122
123 pub async fn end_all_sessions(&self, user_id: &UserId) -> Result<u64> {
124 self.repository.end_all_sessions(user_id).await
125 }
126
127 pub async fn create(
128 &self,
129 name: &str,
130 email: &str,
131 full_name: Option<&str>,
132 display_name: Option<&str>,
133 ) -> Result<User> {
134 self.repository
135 .create(name, email, full_name, display_name)
136 .await
137 }
138
139 pub async fn create_if_absent(
140 &self,
141 name: &str,
142 email: &str,
143 full_name: Option<&str>,
144 display_name: Option<&str>,
145 ) -> Result<Option<User>> {
146 self.repository
147 .create_if_absent(name, email, full_name, display_name)
148 .await
149 }
150
151 pub async fn create_anonymous(&self, fingerprint: &str) -> Result<User> {
152 self.repository.create_anonymous(fingerprint).await
153 }
154
155 pub async fn find_or_create_federated(
156 &self,
157 issuer: &str,
158 external_sub: &str,
159 claims: &systemprompt_traits::FederatedIdentityClaims,
160 ) -> Result<User> {
161 self.repository
162 .find_or_create_federated(issuer, external_sub, claims)
163 .await
164 }
165
166 pub async fn update_email(&self, id: &UserId, email: &str) -> Result<User> {
167 self.repository.update_email(id, email).await
168 }
169
170 pub async fn update_full_name(&self, id: &UserId, full_name: &str) -> Result<User> {
171 self.repository.update_full_name(id, full_name).await
172 }
173
174 pub async fn update_status(&self, id: &UserId, status: UserStatus) -> Result<User> {
175 self.repository.update_status(id, status).await
176 }
177
178 pub async fn update_email_verified(&self, id: &UserId, verified: bool) -> Result<User> {
179 self.repository.update_email_verified(id, verified).await
180 }
181
182 pub async fn update_display_name(&self, id: &UserId, display_name: &str) -> Result<User> {
183 self.repository.update_display_name(id, display_name).await
184 }
185
186 pub async fn update_all_fields(
187 &self,
188 id: &UserId,
189 params: UpdateUserParams<'_>,
190 ) -> Result<User> {
191 self.repository.update_all_fields(id, params).await
192 }
193
194 pub async fn assign_roles(&self, id: &UserId, roles: &[String]) -> Result<User> {
195 self.repository.assign_roles(id, roles).await
196 }
197
198 pub async fn delete(&self, id: &UserId) -> Result<()> {
199 self.repository.delete(id).await
200 }
201
202 pub async fn cleanup_old_anonymous(&self, days: i32) -> Result<u64> {
203 self.repository.cleanup_old_anonymous(days).await
204 }
205
206 pub async fn count_old_anonymous(&self, days: i32) -> Result<i64> {
207 self.repository.count_old_anonymous(days).await
208 }
209
210 pub async fn count_with_breakdown(&self) -> Result<UserCountBreakdown> {
211 let total = self.repository.count().await?;
212 let by_status_vec = self.repository.count_by_status().await?;
213 let by_role_vec = self.repository.count_by_role().await?;
214
215 let by_status: HashMap<String, i64> = by_status_vec.into_iter().collect();
216 let by_role: HashMap<String, i64> = by_role_vec.into_iter().collect();
217
218 Ok(UserCountBreakdown {
219 total,
220 by_status,
221 by_role,
222 })
223 }
224
225 pub async fn get_stats(&self) -> Result<UserStats> {
226 self.repository.get_stats().await
227 }
228
229 pub async fn list_by_filter(
230 &self,
231 status: Option<&str>,
232 role: Option<&str>,
233 older_than_days: Option<i64>,
234 limit: i64,
235 ) -> Result<Vec<User>> {
236 self.repository
237 .list_by_filter(status, role, older_than_days, limit)
238 .await
239 }
240
241 pub async fn bulk_update_status(&self, user_ids: &[UserId], new_status: &str) -> Result<u64> {
242 self.repository
243 .bulk_update_status(user_ids, new_status)
244 .await
245 }
246
247 pub async fn bulk_delete(&self, user_ids: &[UserId]) -> Result<u64> {
248 self.repository.bulk_delete(user_ids).await
249 }
250
251 pub async fn merge_users(&self, source_id: &UserId, target_id: &UserId) -> Result<MergeResult> {
252 self.repository.merge_users(source_id, target_id).await
253 }
254
255 pub async fn promote_anonymous(
259 &self,
260 source_id: &UserId,
261 target_id: &UserId,
262 ) -> Result<MergeResult> {
263 if source_id == target_id {
264 return Err(UserError::Validation(
265 "cannot promote a user onto itself".to_owned(),
266 ));
267 }
268 let source = self
269 .repository
270 .find_by_id(source_id)
271 .await?
272 .ok_or_else(|| UserError::NotFound(source_id.clone()))?;
273 if !source.has_role(UserRole::Anonymous) {
274 return Err(UserError::Validation(format!(
275 "user {} is not anonymous; use an explicit admin merge instead",
276 source_id
277 )));
278 }
279 self.repository.merge_users(source_id, target_id).await
280 }
281}