@@ -4,12 +4,8 @@ import { Argument, Command } from "commander";
44import ora , { Ora } from "ora" ;
55
66import { getApp } from "../services/bootstrap.js" ;
7- import {
8- CompanyFeatureAccess ,
9- companyFeatureAccess ,
10- listCompanies ,
11- } from "../services/companies.js" ;
12- import { listFeatures } from "../services/features.js" ;
7+ import { listCompanies } from "../services/companies.js" ;
8+ import { listFeatures , updateFeatureAccess } from "../services/features.js" ;
139import { configStore } from "../stores/config.js" ;
1410import {
1511 handleError ,
@@ -19,9 +15,11 @@ import {
1915import {
2016 appIdOption ,
2117 companyFilterOption ,
22- companyIdArgument ,
18+ companyIdsOption ,
2319 disableFeatureOption ,
2420 enableFeatureOption ,
21+ segmentIdsOption ,
22+ userIdsOption ,
2523} from "../utils/options.js" ;
2624import { baseUrlSuffix } from "../utils/path.js" ;
2725
@@ -69,36 +67,53 @@ export const listCompaniesAction = async (options: { filter?: string }) => {
6967 }
7068} ;
7169
72- export const companyFeatureAccessAction = async (
73- companyId : string ,
70+ export const featureAccessAction = async (
7471 featureKey : string | undefined ,
75- options : { enable : boolean ; disable : boolean } ,
72+ options : {
73+ enable : boolean ;
74+ disable : boolean ;
75+ companies ?: string [ ] ;
76+ segments ?: string [ ] ;
77+ users ?: string [ ] ;
78+ } ,
7679) => {
7780 const { baseUrl, appId } = configStore . getConfig ( ) ;
7881 let spinner : Ora | undefined ;
7982
8083 if ( ! appId ) {
81- return handleError ( new MissingAppIdError ( ) , "Company Feature Access" ) ;
84+ return handleError ( new MissingAppIdError ( ) , "Feature Access" ) ;
8285 }
8386
8487 const app = getApp ( appId ) ;
8588 const production = app . environments . find ( ( e ) => e . isProduction ) ;
8689 if ( ! production ) {
87- return handleError ( new MissingEnvIdError ( ) , "Company Feature Access" ) ;
90+ return handleError ( new MissingEnvIdError ( ) , "Feature Access" ) ;
8891 }
8992
9093 // Validate conflicting options
9194 if ( options . enable && options . disable ) {
9295 return handleError (
9396 "Cannot both enable and disable a feature." ,
94- "Company Feature Access" ,
97+ "Feature Access" ,
9598 ) ;
9699 }
97100
98101 if ( ! options . enable && ! options . disable ) {
99102 return handleError (
100103 "Must specify either --enable or --disable." ,
101- "Company Feature Access" ,
104+ "Feature Access" ,
105+ ) ;
106+ }
107+
108+ // Validate at least one target is specified
109+ if (
110+ ! options . companies ?. length &&
111+ ! options . segments ?. length &&
112+ ! options . users ?. length
113+ ) {
114+ return handleError (
115+ "Must specify at least one target using --companies, --segments, or --users." ,
116+ "Feature Access" ,
102117 ) ;
103118 }
104119
@@ -116,10 +131,7 @@ export const companyFeatureAccessAction = async (
116131 } ) ;
117132
118133 if ( featuresResponse . data . length === 0 ) {
119- return handleError (
120- "No features found for this app." ,
121- "Company Feature Access" ,
122- ) ;
134+ return handleError ( "No features found for this app." , "Feature Access" ) ;
123135 }
124136
125137 spinner . succeed (
@@ -135,33 +147,38 @@ export const companyFeatureAccessAction = async (
135147 } ) ;
136148 } catch ( error ) {
137149 spinner ?. fail ( "Loading features failed." ) ;
138- return handleError ( error , "Company Feature Access" ) ;
150+ return handleError ( error , "Feature Access" ) ;
139151 }
140152 }
141153
142154 // Determine if enabling or disabling
143155 const isEnabled = options . enable ;
144156
145157 try {
158+ const targetTypes = [ ] ;
159+ if ( options . companies ?. length ) targetTypes . push ( "companies" ) ;
160+ if ( options . segments ?. length ) targetTypes . push ( "segments" ) ;
161+ if ( options . users ?. length ) targetTypes . push ( "users" ) ;
162+
146163 spinner = ora (
147- `${ isEnabled ? "Enabling" : "Disabling" } feature ${ chalk . cyan ( featureKey ) } for company ${ chalk . cyan ( companyId ) } ...` ,
164+ `${ isEnabled ? "Enabling" : "Disabling" } feature ${ chalk . cyan ( featureKey ) } for ${ targetTypes . join ( ", " ) } ...` ,
148165 ) . start ( ) ;
149166
150- const request : CompanyFeatureAccess = {
167+ await updateFeatureAccess ( appId , {
151168 envId : production . id ,
152- companyId,
153169 featureKey,
154170 isEnabled,
155- } ;
156-
157- await companyFeatureAccess ( appId , request ) ;
171+ companyIds : options . companies ,
172+ segmentIds : options . segments ,
173+ userIds : options . users ,
174+ } ) ;
158175
159176 spinner . succeed (
160- `${ isEnabled ? "Enabled" : "Disabled" } feature ${ chalk . cyan ( featureKey ) } for company ${ chalk . cyan ( companyId ) } .` ,
177+ `${ isEnabled ? "Enabled" : "Disabled" } feature ${ chalk . cyan ( featureKey ) } for ${ targetTypes . join ( ", " ) } .` ,
161178 ) ;
162179 } catch ( error ) {
163180 spinner ?. fail ( `Feature access update failed.` ) ;
164- void handleError ( error , "Company Feature Access" ) ;
181+ void handleError ( error , "Feature Access" ) ;
165182 }
166183} ;
167184
@@ -170,8 +187,8 @@ export function registerCompanyCommands(cli: Command) {
170187 "Manage companies." ,
171188 ) ;
172189
173- const companyFeaturesCommand = new Command ( "features" ) . description (
174- "Manage company features ." ,
190+ const featuresCommand = new Command ( "features" ) . description (
191+ "Manage feature access ." ,
175192 ) ;
176193
177194 companiesCommand
@@ -182,11 +199,12 @@ export function registerCompanyCommands(cli: Command) {
182199 . action ( listCompaniesAction ) ;
183200
184201 // Feature access command
185- companyFeaturesCommand
202+ featuresCommand
186203 . command ( "access" )
187- . description ( "Grant or revoke feature access for a specific company." )
204+ . description (
205+ "Grant or revoke feature access for companies, segments, and users." ,
206+ )
188207 . addOption ( appIdOption )
189- . addArgument ( companyIdArgument )
190208 . addArgument (
191209 new Argument (
192210 "[featureKey]" ,
@@ -195,9 +213,12 @@ export function registerCompanyCommands(cli: Command) {
195213 )
196214 . addOption ( enableFeatureOption )
197215 . addOption ( disableFeatureOption )
198- . action ( companyFeatureAccessAction ) ;
216+ . addOption ( companyIdsOption )
217+ . addOption ( segmentIdsOption )
218+ . addOption ( userIdsOption )
219+ . action ( featureAccessAction ) ;
199220
200- companiesCommand . addCommand ( companyFeaturesCommand ) ;
221+ companiesCommand . addCommand ( featuresCommand ) ;
201222
202223 // Update the config with the cli override values
203224 companiesCommand . hook ( "preAction" , ( _ , command ) => {
0 commit comments