wekan_cli/commands/admin/
organizations.rs1use super::features::{FeatureChanges, UpdateAllArgs, confirm_bulk, identifier};
2use crate::{
3 client::WekanClientFactory,
4 command_result::*,
5 commands::{authenticated::AuthenticatedContext, client_error::map_client_error},
6 credentials::CredentialStore,
7 error::AppError,
8 input::ConfirmationProvider,
9 redaction::Redactor,
10};
11use clap::{Args, Subcommand};
12
13#[derive(Debug, Args)]
14pub struct OrganizationArgs {
15 #[command(subcommand)]
16 pub command: OrganizationCommand,
17}
18#[derive(Debug, Subcommand)]
19pub enum OrganizationCommand {
20 List,
22 Features(FeaturesArgs),
24}
25#[derive(Debug, Args)]
26pub struct FeaturesArgs {
27 #[command(subcommand)]
28 pub command: FeaturesCommand,
29}
30#[derive(Debug, Subcommand)]
31pub enum FeaturesCommand {
32 Update(UpdateArgs),
34 UpdateAll(UpdateAllArgs),
36}
37#[derive(Debug, Args)]
38pub struct UpdateArgs {
39 #[arg(long = "org", value_parser = identifier)]
41 id: String,
42 #[command(flatten)]
43 changes: FeatureChanges,
44}
45
46pub(super) async fn execute(
47 command: OrganizationCommand,
48 factory: &WekanClientFactory,
49 store: &dyn CredentialStore,
50 confirmation: &dyn ConfirmationProvider,
51) -> Result<CommandSuccess, AppError> {
52 let context = AuthenticatedContext::load(factory, store)?;
53 let redactor = Redactor::with_secret(context.record().token());
54 match command {
55 OrganizationCommand::List => {
56 let organizations = context
57 .client()
58 .admin_organizations(context.record().token())
59 .await
60 .map_err(|e| map_client_error(e, &redactor, "admin org list", false))?;
61 Ok(CommandSuccess::AdminOrganizations(
62 AdminOrganizationCollectionSuccess { organizations },
63 ))
64 }
65 OrganizationCommand::Features(args) => match args.command {
66 FeaturesCommand::Update(args) => {
67 let organization = context
68 .client()
69 .update_organization_features(
70 &args.id,
71 &args.changes.organization_request(),
72 context.record().token(),
73 )
74 .await
75 .map_err(|e| {
76 map_client_error(e, &redactor, "admin org features update", true)
77 })?;
78 Ok(CommandSuccess::AdminOrganizationUpdated(Box::new(
79 AdminOrganizationUpdateSuccess {
80 organization,
81 submitted_fields: args.changes.submitted_fields(),
82 },
83 )))
84 }
85 FeaturesCommand::UpdateAll(args) => {
86 let operation = DestructiveOperation::AdminOrganizationFeaturesUpdateAll;
87 let Some(guard) = confirm_bulk(&args, confirmation, &context, store, operation)?
88 else {
89 return Ok(CommandSuccess::Cancelled(CancellationSuccess::new(
90 operation,
91 )));
92 };
93 let field = args.field.into();
94 let result = context
95 .client()
96 .update_all_organization_features(field, args.value, context.record().token())
97 .await;
98 drop(guard);
99 let response = result.map_err(|e| {
100 map_client_error(e, &redactor, "admin org features update-all", true)
101 })?;
102 Ok(CommandSuccess::AdminOrganizationFeaturesUpdatedAll(
103 AdminFeaturesUpdateAllSuccess {
104 field,
105 value: args.value,
106 updated: response.updated,
107 },
108 ))
109 }
110 },
111 }
112}