darkwing/server/services/
user_services.rsuse mockall::automock;
use std::sync::Arc;
use tracing::info;
use async_trait::async_trait;
use crate::{
database::{
team::{DynTeamsRepository, Plan},
user::DynUsersRepository,
},
server::{
dtos::{team_dto::ResponseTeamDto, user_dto::ResponseUserDto},
error::AppResult,
},
};
pub type DynUsersService = Arc<dyn UsersServiceTrait + Send + Sync>;
#[automock]
#[async_trait]
pub trait UsersServiceTrait {
async fn get_user_by_id(&self, user_id: u64) -> AppResult<ResponseUserDto>;
async fn get_team_by_id(&self, team_id: i64) -> AppResult<ResponseTeamDto>;
fn is_fully_free_plan(&self, team: &ResponseTeamDto) -> bool;
fn does_plan_allows_scenarios(&self, plan: Plan) -> bool;
}
#[derive(Clone)]
pub struct UsersService {
user_repository: DynUsersRepository,
team_repository: DynTeamsRepository,
}
impl UsersService {
pub fn new(
user_repository: DynUsersRepository,
team_repository: DynTeamsRepository,
) -> Self {
Self {
user_repository,
team_repository,
}
}
}
#[async_trait]
impl UsersServiceTrait for UsersService {
async fn get_user_by_id(&self, user_id: u64) -> AppResult<ResponseUserDto> {
info!("retrieving user {:?}", user_id);
let user = self.user_repository.get_user_by_id(user_id).await?;
info!("user found with username {:?}", user.username);
Ok(user.into_dto())
}
async fn get_team_by_id(&self, team_id: i64) -> AppResult<ResponseTeamDto> {
info!("retrieving team id: {:?}", team_id);
let team = self.team_repository.get_team_by_id(team_id).await?;
Ok(team.into())
}
fn is_fully_free_plan(&self, team: &ResponseTeamDto) -> bool {
team.plan == Plan::Free && team.browser_profiles_limit == 10
}
fn does_plan_allows_scenarios(&self, plan: Plan) -> bool {
plan == Plan::Paid
}
}