Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(teams): default team channel on team creation (#5063)

Will Hutchinson0a7850e74ea6
diff --git a/crates/teams/src/domain/team_service.rs b/crates/teams/src/domain/team_service.rs
index 69e496805b..4f2cb052b0 100644
--- a/crates/teams/src/domain/team_service.rs
+++ b/crates/teams/src/domain/team_service.rs
@@ -5,7 +5,10 @@ mod test;
use std::{collections::HashSet, sync::Arc};
-use channels::domain::ports::{ChannelMutationErr, ChannelService};
+use channels::domain::{
+ models::{ChannelType, CreateChannelRequest, Sender},
+ ports::{ChannelMutationErr, ChannelService},
+};
use entity_access::domain::models::{
AdminTeamRole, EntityAccessReceipt, MemberTeamRole, OwnerTeamRole,
};
@@ -557,6 +560,22 @@ where
.team_repository
.create_team(user_id, team_name, subscription_id)
.await?;
+ let owner_id = user_id.clone().into_owned();
+ self.channel_service
+ .create_channel(
+ Sender::new_from_user(owner_id.clone()),
+ None,
+ CreateChannelRequest {
+ name: Some(team.name().to_owned()),
+ channel_type: ChannelType::Team,
+ team_id: Some(*team.id()),
+ auto_join_team: true,
+ participants: HashSet::from([owner_id]),
+ },
+ )
+ .await
+ .map_err(|error| CreateTeamError::StorageLayerError(error.into()))?;
+
if let Some(subscription_id) = subscription_id {
self.customer_repository
.convert_subscription_to_team(subscription_id, team.id(), user_id)
diff --git a/crates/teams/src/domain/team_service/test.rs b/crates/teams/src/domain/team_service/test.rs
index d6d7d6260c..f74e3485ac 100644
--- a/crates/teams/src/domain/team_service/test.rs
+++ b/crates/teams/src/domain/team_service/test.rs
@@ -10,7 +10,8 @@ use std::{
use channels::domain::{
models::{
AttachmentEntityReference, ChannelAttachmentType, ChannelMessageFilters,
- ChannelParticipant, MessagePageDirection, ThreadReply,
+ ChannelParticipant, ChannelType, CreateChannelRequest, CreateChannelResponse,
+ MessagePageDirection, Sender, ThreadReply,
},
ports::{
ChannelAttachmentsPage, ChannelMessagesErr, ChannelMessagesQueryResult, ChannelMutationErr,
@@ -846,12 +847,21 @@ impl CustomerRepository for MockCustomerRepository {
// -- Recording ChannelService --
+#[derive(Clone)]
+struct RecordedCreateChannelCall {
+ actor: Sender,
+ actor_org_id: Option<i64>,
+ request: CreateChannelRequest,
+}
+
#[derive(Clone, Default)]
struct RecordingChannelService {
+ create_calls: Arc<Mutex<Vec<RecordedCreateChannelCall>>>,
auto_join_calls: Arc<Mutex<Vec<(uuid::Uuid, String)>>>,
leave_calls: Arc<Mutex<Vec<(uuid::Uuid, String)>>>,
restore_calls: Arc<Mutex<Vec<(String, Vec<uuid::Uuid>)>>>,
leave_channel_ids: Vec<uuid::Uuid>,
+ fail_create: bool,
fail_auto_join: bool,
fail_leave: bool,
fail_restore: bool,
@@ -914,6 +924,34 @@ impl ChannelService for RecordingChannelService {
async move { unimplemented!("not needed for team service tests") }
}
+ fn create_channel(
+ &self,
+ actor: Sender,
+ actor_org_id: Option<i64>,
+ request: CreateChannelRequest,
+ ) -> impl Future<Output = Result<CreateChannelResponse, ChannelMutationErr>> + Send {
+ self.create_calls
+ .lock()
+ .unwrap()
+ .push(RecordedCreateChannelCall {
+ actor,
+ actor_org_id,
+ request,
+ });
+ let fail = self.fail_create;
+ async move {
+ if fail {
+ Err(ChannelMutationErr::Repo(anyhow::anyhow!(
+ "create channel failed"
+ )))
+ } else {
+ Ok(CreateChannelResponse {
+ id: uuid::Uuid::from_u128(3000).to_string(),
+ })
+ }
+ }
+ }
+
fn auto_join_by_team_id(
&self,
team_id: &uuid::Uuid,
@@ -1909,6 +1947,102 @@ async fn team_payment_patch_payment_status_delegates_to_repository() {
);
}
+#[tokio::test]
+async fn create_team_creates_default_team_channel() {
+ let owner = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ let team_id = uuid::Uuid::from_u128(2000);
+ let team = Team::new(
+ team_id,
+ "Persisted Team Name".to_string(),
+ "PERSISTED_TEAM_NAME".to_string(),
+ owner.clone().into_owned(),
+ false,
+ false,
+ );
+ let mut team_repository = MockTeamRepository::new(
+ Vec::new(),
+ "Requested Team Name",
+ Arc::new(Mutex::new(Vec::new())),
+ );
+ team_repository.created_team = team;
+ let channel_service = RecordingChannelService::default();
+ let create_calls = channel_service.create_calls.clone();
+ let service = TeamServiceImpl::new(
+ team_repository,
+ MockCustomerRepository::default(),
+ channel_service,
+ MockUserRolesAndPermissionsService::default(),
+ Arc::new(MockNotificationIngress::new(HashSet::new())),
+ NoOpCrmEnqueuer,
+ NoOpTeamCrmSettingsRepository,
+ );
+
+ service
+ .create_team(&owner, "Requested Team Name", None)
+ .await
+ .unwrap();
+
+ let create_calls = create_calls.lock().unwrap();
+ assert_eq!(create_calls.len(), 1);
+ let call = &create_calls[0];
+ assert_eq!(
+ call.actor,
+ Sender::new_from_user(owner.clone().into_owned())
+ );
+ assert_eq!(call.actor_org_id, None);
+ assert_eq!(call.request.name.as_deref(), Some("Persisted Team Name"));
+ assert_eq!(call.request.channel_type, ChannelType::Team);
+ assert_eq!(call.request.team_id, Some(team_id));
+ assert!(call.request.auto_join_team);
+ assert_eq!(
+ call.request.participants,
+ HashSet::from([owner.into_owned()])
+ );
+}
+
+#[tokio::test]
+async fn create_team_fails_when_default_team_channel_creation_fails() {
+ let owner = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+ let team = Team::new(
+ uuid::Uuid::from_u128(2001),
+ "Failed Channel Team".to_string(),
+ "FAILED_CHANNEL_TEAM".to_string(),
+ owner.clone().into_owned(),
+ false,
+ false,
+ );
+ let mut team_repository = MockTeamRepository::new(
+ Vec::new(),
+ "Failed Channel Team",
+ Arc::new(Mutex::new(Vec::new())),
+ );
+ team_repository.created_team = team;
+ let analytics_events = Arc::new(Mutex::new(Vec::new()));
+ let event_broker = RecordingEventBroker::default();
+ let service = TeamServiceImpl::new_with_analytics(
+ team_repository,
+ MockCustomerRepository::default(),
+ RecordingChannelService {
+ fail_create: true,
+ ..Default::default()
+ },
+ MockUserRolesAndPermissionsService::default(),
+ Arc::new(MockNotificationIngress::new(HashSet::new())),
+ NoOpCrmEnqueuer,
+ NoOpTeamCrmSettingsRepository,
+ MockTeamAnalytics::new(analytics_events.clone()),
+ )
+ .with_event_broker(event_broker.clone());
+
+ let result = service
+ .create_team(&owner, "Failed Channel Team", None)
+ .await;
+
+ assert!(matches!(result, Err(CreateTeamError::StorageLayerError(_))));
+ assert!(analytics_events.lock().unwrap().is_empty());
+ assert!(event_broker.events().is_empty());
+}
+
#[tokio::test]
async fn test_create_team_moves_github_installation_to_created_team() {
let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();