1- import { OrganizationResponse , SchemaApp , SchemaAppRequest , SmartThingsURLProvider , ViperAppLinks } from '@smartthings/core-sdk'
1+ import {
2+ OrganizationResponse ,
3+ SchemaApp ,
4+ SchemaAppRequest ,
5+ SmartThingsURLProvider ,
6+ ViperAppLinks ,
7+ } from '@smartthings/core-sdk'
28
39import {
410 APICommand ,
@@ -19,12 +25,16 @@ import {
1925 selectFromList ,
2026 SelectFromListConfig ,
2127 staticDef ,
28+ stdinIsTTY ,
29+ stdoutIsTTY ,
2230 stringDef ,
2331 stringTranslateToId ,
2432 undefinedDef ,
2533 updateFromUserInput ,
2634} from '@smartthings/cli-lib'
2735import { awsHelpText } from '../aws-utils'
36+ import { chooseOrganization } from './organization-util'
37+ import { CLIError } from '@oclif/core/lib/errors'
2838
2939
3040export const SCHEMA_AWS_PRINCIPAL = '148790070172'
@@ -189,3 +199,42 @@ export const chooseSchemaApp = async (command: APICommand<typeof APICommand.flag
189199 : schemaAppFromArg
190200 return selectFromList ( command , config , { preselectedId, listItems, autoChoose : true } )
191201}
202+
203+ // The endpoint to get a schema app automatically assigns the users org to an app if it
204+ // doesn't have one already. This causes a problem if the app is certified because the user
205+ // organization is almost certainly the wrong one and the user can't change it after it's been
206+ // set. So, here we check to see if the app has an organization before we query it and
207+ // prompt the user for the correct organization.
208+ export const getSchemaAppEnsuringOrganization = async (
209+ command : APICommand < typeof APICommand . flags > ,
210+ schemaAppId : string ,
211+ flags : {
212+ json : boolean
213+ yaml : boolean
214+ input ?: string
215+ output ?: string
216+ } ,
217+ ) : Promise < SchemaApp > => {
218+ const apps = await command . client . schema . list ( )
219+ const appFromList = apps . find ( app => app . endpointAppId === schemaAppId )
220+ if ( appFromList && ! appFromList . organizationId ) {
221+ if ( flags . json || flags . yaml || flags . output || flags . input || ! stdinIsTTY ( ) || ! stdoutIsTTY ( ) ) {
222+ throw new CLIError (
223+ 'Schema app does not have an organization associated with it.\n' +
224+ `Please run "smartthings schema ${ schemaAppId } " and choose an organization when prompted.` ,
225+ )
226+ }
227+ // If we found an app but it didn't have an organization, ask the user to choose one.
228+ // (If we didn't find an app at all, it's safe to use the single get because that means
229+ // either it doesn't exist (bad app id) or it already has an organization.)
230+ console . log (
231+ `The schema "${ appFromList . appName } " (${ schemaAppId } ) does not have an organization\n` +
232+ 'You must choose one now.' ,
233+ )
234+ const organizationId = await chooseOrganization ( command )
235+ // eslint-disable-next-line @typescript-eslint/naming-convention
236+ const orgClient = command . client . clone ( { 'X-ST-Organization' : organizationId } )
237+ return orgClient . schema . get ( schemaAppId )
238+ }
239+ return command . client . schema . get ( schemaAppId )
240+ }
0 commit comments