From 7e613d0d3bd924f94801df88a909947d2a76d8cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3jcik?= Date: Tue, 18 Jul 2023 03:24:30 +0200 Subject: [PATCH] Adds additional command --- .../externalconnection-remove.spec.ts | 37 +++++++++++++++++++ .../externalconnection-remove.ts | 3 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/m365/search/commands/externalconnection/externalconnection-remove.spec.ts b/src/m365/search/commands/externalconnection/externalconnection-remove.spec.ts index bbe6c5c7edd..64b3dd8e9d7 100644 --- a/src/m365/search/commands/externalconnection/externalconnection-remove.spec.ts +++ b/src/m365/search/commands/externalconnection/externalconnection-remove.spec.ts @@ -218,4 +218,41 @@ describe(commands.EXTERNALCONNECTION_REMOVE, () => { } } as any), new CommandError("Multiple external connections with name My HR found. Please disambiguate (IDs): fabrikamhr, contosohr")); }); + + it('handles selecting single result when external connections with the specified name found and cli is set to prompt', async () => { + let removeRequestIssued = false; + + sinonUtil.restore(request.get); + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://graph.microsoft.com/v1.0/external/connections?$filter=name eq 'My%20HR'&$select=id`) { + return { + value: [ + { + "id": "fabrikamhr" + }, + { + "id": "contosohr" + } + ] + }; + } + + throw "Invalid request"; + }); + + sinon.stub(Cli, 'handleMultipleResultsFound').resolves({ + "id": "contosohr" + }); + + sinon.stub(request, 'delete').callsFake(async (opts: any) => { + if (opts.url === 'https://graph.microsoft.com/v1.0/external/connections/contosohr') { + removeRequestIssued = true; + return; + } + throw ''; + }); + + await command.action(logger, { options: { name: "My HR", confirm: true } }); + assert(removeRequestIssued); + }); }); diff --git a/src/m365/search/commands/externalconnection/externalconnection-remove.ts b/src/m365/search/commands/externalconnection/externalconnection-remove.ts index ec39baebffc..54b5aa50664 100644 --- a/src/m365/search/commands/externalconnection/externalconnection-remove.ts +++ b/src/m365/search/commands/externalconnection/externalconnection-remove.ts @@ -78,7 +78,8 @@ class SearchExternalConnectionRemoveCommand extends GraphCommand { throw `The specified connection does not exist in Microsoft Search`; } - throw `Multiple external connections with name ${args.options.name} found. Please disambiguate (IDs): ${res.value.map(x => x.id).join(', ')}`; + const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', res.value); + return ((await Cli.handleMultipleResultsFound(`Multiple external connections with name ${args.options.name} found. Choose the correct ID:`, `Multiple external connections with name ${args.options.name} found. Please disambiguate (IDs): ${res.value.map(x => x.id).join(', ')}`, resultAsKeyValuePair)) as { id: string }).id; } public async commandAction(logger: Logger, args: CommandArgs): Promise {