Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
feat: 🎸 now when searching apps you can get the complete list
Browse files Browse the repository at this point in the history
Passing -1 as page, the library automatically loops to retrieve the
whole list of applications
  • Loading branch information
ziccardi committed Dec 15, 2020
1 parent 3df2f65 commit 53ad8ac
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/commands/applications/DeleteApplicationsCommand.ts
Original file line number Diff line number Diff line change
@@ -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<PushApplication[]> {
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<PushApplication>(deletePromises);
Expand Down
3 changes: 3 additions & 0 deletions src/commands/applications/search/AbstractSearchCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 13 additions & 0 deletions src/commands/applications/search/SearchAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import {ApiClient} from '../../ApiClient';

export class SearchAll extends AbstractSearchCommand {
readonly execute = async (api: ApiClient, page: number, pageSize: number): Promise<SearchResult> => {
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: {
Expand Down
20 changes: 19 additions & 1 deletion src/commands/applications/search/SearchEngineFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>)[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();
}
Expand Down
9 changes: 4 additions & 5 deletions test/commands/applications/DeleteApplicationCommand.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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([]);
});
});
8 changes: 8 additions & 0 deletions test/commands/applications/SearchApplicationsCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 53ad8ac

Please sign in to comment.