From e73a00f4a284ec0b453e43c34cbc81cfb2979b4e Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 16 May 2018 12:00:40 -0400 Subject: [PATCH] multiple results response --- src/commands/get.command.ts | 6 +++--- src/models/response.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/commands/get.command.ts b/src/commands/get.command.ts index 7a7eb16abc..c10906ba77 100644 --- a/src/commands/get.command.ts +++ b/src/commands/get.command.ts @@ -77,7 +77,7 @@ export class GetCommand { let ciphers = await this.cipherService.getAllDecrypted(); ciphers = CliUtils.searchCiphers(ciphers, id); if (ciphers.length > 1) { - return Response.error('More than one result was found.'); + return Response.multipleResults(ciphers.map((c) => c.id)); } if (ciphers.length > 0) { decCipher = ciphers[0]; @@ -126,7 +126,7 @@ export class GetCommand { let folders = await this.folderService.getAllDecrypted(); folders = CliUtils.searchFolders(folders, id); if (folders.length > 1) { - return Response.error('More than one result was found.'); + return Response.multipleResults(folders.map((f) => f.id)); } if (folders.length > 0) { decFolder = folders[0]; @@ -151,7 +151,7 @@ export class GetCommand { let collections = await this.collectionService.getAllDecrypted(); collections = CliUtils.searchCollections(collections, id); if (collections.length > 1) { - return Response.error('More than one result was found.'); + return Response.multipleResults(collections.map((c) => c.id)); } if (collections.length > 0) { decCollection = collections[0]; diff --git a/src/models/response.ts b/src/models/response.ts index 167db8a2f2..d361c3a2b6 100644 --- a/src/models/response.ts +++ b/src/models/response.ts @@ -20,6 +20,15 @@ export class Response { return Response.error(message); } + static multipleResults(ids: string[]): Response { + let msg = 'More than one result was found. Try getting a specific object by `id` instead. ' + + 'The following objects were found:'; + ids.forEach((id) => { + msg += '\n' + id; + }); + return Response.error(msg); + } + static success(data?: BaseResponse): Response { const res = new Response(); res.success = true;