Skip to main content

systemprompt_users/services/user/
bulk.rs

1//! Counts, statistics and filtered bulk operations over user accounts.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use std::collections::HashMap;
7
8use systemprompt_identifiers::UserId;
9
10use super::UserService;
11use crate::error::Result;
12use crate::models::{User, UserCountBreakdown, UserStats, UserStatus};
13
14impl UserService {
15    pub async fn count_with_breakdown(&self) -> Result<UserCountBreakdown> {
16        let total = self.repository.count().await?;
17        let by_status_vec = self.repository.count_by_status().await?;
18        let by_role_vec = self.repository.count_by_role().await?;
19
20        let by_status: HashMap<String, i64> = by_status_vec.into_iter().collect();
21        let by_role: HashMap<String, i64> = by_role_vec.into_iter().collect();
22
23        Ok(UserCountBreakdown {
24            total,
25            by_status,
26            by_role,
27        })
28    }
29
30    pub async fn get_stats(&self) -> Result<UserStats> {
31        self.repository.get_stats().await
32    }
33
34    pub async fn list_by_filter(
35        &self,
36        status: Option<UserStatus>,
37        role: Option<&str>,
38        older_than_days: Option<i64>,
39        limit: i64,
40    ) -> Result<Vec<User>> {
41        self.repository
42            .list_by_filter(status, role, older_than_days, limit)
43            .await
44    }
45
46    pub async fn bulk_update_status(
47        &self,
48        user_ids: &[UserId],
49        new_status: UserStatus,
50    ) -> Result<u64> {
51        self.repository
52            .bulk_update_status(user_ids, new_status)
53            .await
54    }
55
56    pub async fn bulk_delete(&self, user_ids: &[UserId]) -> Result<u64> {
57        self.repository.bulk_delete(user_ids).await
58    }
59}