diff --git a/src/commands/applications/DeleteApplicationsCommand.ts b/src/commands/applications/DeleteApplicationsCommand.ts index e0ee338..b653ea5 100644 --- a/src/commands/applications/DeleteApplicationsCommand.ts +++ b/src/commands/applications/DeleteApplicationsCommand.ts @@ -1,21 +1,21 @@ import {AbstractFilteredApplicationsCommand} from './AbstractFilteredApplicationsCommand'; import {SearchApplicationsCommand} from './SearchApplicationsCommand'; import {PushApplication} from './PushApplication'; -import {NotFoundError} from '../../errors/NotFoundError'; -import {UpsErrorDetails} from '../../errors/UpsError'; export class DeleteApplicationsCommand extends AbstractFilteredApplicationsCommand< PushApplication[], DeleteApplicationsCommand > { protected async exec(): Promise { - const deletePromises = (await new SearchApplicationsCommand(this.api).withFilter(this.filter).execute()).list.map( + const deletePromises = ( + await new SearchApplicationsCommand(this.api).withFilter(this.filter).page(-1).execute() + ).list.map( async application => await this.api.delete(`/applications/${application.pushApplicationID}`).then(() => application) ); if (deletePromises.length < 1) { - throw new NotFoundError("Can't find the requested application", this.filter as UpsErrorDetails); + return []; } return Promise.all(deletePromises); diff --git a/src/commands/applications/search/AbstractSearchCommand.ts b/src/commands/applications/search/AbstractSearchCommand.ts index a15cd9c..96404cb 100644 --- a/src/commands/applications/search/AbstractSearchCommand.ts +++ b/src/commands/applications/search/AbstractSearchCommand.ts @@ -26,6 +26,9 @@ export abstract class AbstractSearchCommand { }; protected readonly getPage = (appList: PushApplication[], page: number, pageSize: number): PushApplication[] => { + if (page === -1) { + return appList; + } const firstIndex = pageSize * page; const endIndex = firstIndex + pageSize; diff --git a/src/commands/applications/search/SearchAll.ts b/src/commands/applications/search/SearchAll.ts index 7f7ecac..ac52444 100644 --- a/src/commands/applications/search/SearchAll.ts +++ b/src/commands/applications/search/SearchAll.ts @@ -4,6 +4,19 @@ import {ApiClient} from '../../ApiClient'; export class SearchAll extends AbstractSearchCommand { readonly execute = async (api: ApiClient, page: number, pageSize: number): Promise => { + if (page === -1) { + // all the pages should be returned + const result: SearchResult = {total: 0, list: []}; + for (let i = 0; ; i++) { + const res = await this.execute(api, i, 200); + result.total = res.total > result.total ? res.total : result.total; + if (res.list.length === 0) { + break; + } + res.list.forEach(app => result.list.push(app)); + } + return result; + } const url = '/applications'; const response = await api.get(url, { params: { diff --git a/src/commands/applications/search/SearchEngineFactory.ts b/src/commands/applications/search/SearchEngineFactory.ts index b8e5fcc..302cee6 100644 --- a/src/commands/applications/search/SearchEngineFactory.ts +++ b/src/commands/applications/search/SearchEngineFactory.ts @@ -6,11 +6,29 @@ import {AbstractSearchCommand} from './AbstractSearchCommand'; export class SearchEngineFactory { static produce = (filter?: PushApplicationFilter): AbstractSearchCommand => { + const noFilter = (): boolean => { + if (!filter || Object.keys(filter).length === 0 || (Object.keys(filter).length === 1 && 'page' in filter)) { + // no filters + return true; + } + + let res = true; + Object.keys(filter).forEach(key => { + if (key === 'page') { + return; + } + if (((filter as unknown) as Record)[key]) { + res = false; + } + }); + return res; + }; + if (filter?.pushApplicationID) { return new SearchByID(filter); } - if (!filter || Object.keys(filter).length === 0 || (Object.keys(filter).length === 1 && 'page' in filter)) { + if (noFilter()) { // no filters return new SearchAll(); } diff --git a/test/commands/applications/DeleteApplicationCommand.test.ts b/test/commands/applications/DeleteApplicationCommand.test.ts index 7683411..a4019f9 100644 --- a/test/commands/applications/DeleteApplicationCommand.test.ts +++ b/test/commands/applications/DeleteApplicationCommand.test.ts @@ -1,7 +1,6 @@ import {UpsAdminClient} from '../../../src'; import {createApplications, deleteApplication, getAllApplications, initMockEngine} from '../mocks/UPSMock'; import {UPS_URL} from '../mocks/constants'; -import {NotFoundError} from '../../../src/errors/NotFoundError'; beforeEach(() => { initMockEngine(UPS_URL); @@ -30,14 +29,14 @@ describe('DeleteApplicationCommand', () => { ).toHaveLength(0); }); - it('Should fail deleting non existent application', async () => { + it('Should silently return an empty list', async () => { createApplications({appCount: 1}); const app = getAllApplications()[0]; // delete the application so that it doesn't exists anymore deleteApplication(app.pushApplicationID); - await expect( - async () => await upsAdminClient.applications.delete().withApplicationID(app.pushApplicationID).execute() - ).rejects.toThrow(NotFoundError); + const deletedApps = await upsAdminClient.applications.delete().withApplicationID(app.pushApplicationID).execute(); + + expect(deletedApps).toEqual([]); }); }); diff --git a/test/commands/applications/SearchApplicationsCommand.test.ts b/test/commands/applications/SearchApplicationsCommand.test.ts index 467784c..5f0be65 100644 --- a/test/commands/applications/SearchApplicationsCommand.test.ts +++ b/test/commands/applications/SearchApplicationsCommand.test.ts @@ -36,6 +36,14 @@ describe('SearchApplicationCommand', () => { expect(apps.total).toEqual(45); }); + it('Should return all apps (total)', async () => { + createApplications({appCount: 45}); + + const apps = await upsAdminClient.applications.search().page(-1).execute(); + expect(apps.list).toHaveLength(45); + expect(apps.total).toEqual(45); + }); + it('Should return a given app', async () => { createApplications({appCount: 10}); // get one app