diff --git a/src/models/ApiResponse.ts b/src/models/ApiResponse.ts index de1e7d2..43936b5 100644 --- a/src/models/ApiResponse.ts +++ b/src/models/ApiResponse.ts @@ -9,10 +9,12 @@ import { FileInfo } from './FileInfo'; import { FormulaField } from './FormulaField'; import { GetPagedAppsResponse } from './GetPagedAppsResponse'; import { GetPagedFieldsResponse } from './GetPagedFieldsResponse'; +import { GetPagedReportsResponse } from './GetPagedReportsResponse'; import { ListField } from './ListField'; import { ListItemResponse } from './ListItemResponse'; import { ListValue } from './ListValue'; import { ReferenceField } from './ReferenceField'; +import { Report } from './Report'; /** * @class ApiResponse - A generic response object for API requests. @@ -275,6 +277,28 @@ export class ApiResponse { ); } + public asGetPagedReportsResponseType(): ApiResponse { + const apiResponse = this as ApiResponse; + + const reports = apiResponse.data.items.map((item: any) => { + return new Report(item.id, item.appId, item.name, item.description); + }); + + const getPagedReportsResponse = new GetPagedReportsResponse( + reports, + apiResponse.data.pageNumber, + apiResponse.data.pageSize, + apiResponse.data.totalPages, + apiResponse.data.totalRecords + ); + + return new ApiResponse( + apiResponse.statusCode, + apiResponse.message, + getPagedReportsResponse + ); + } + /** * @method asFileCollectionType - Converts the field item to the appropriate field object based upon the field item's type. * @param fieldItem - The field item to convert. diff --git a/src/models/EndpointFactory.ts b/src/models/EndpointFactory.ts index 2a4c631..71d08d7 100644 --- a/src/models/EndpointFactory.ts +++ b/src/models/EndpointFactory.ts @@ -219,9 +219,13 @@ export class EndpointFactory { /** * @method getReportsByAppIdEndpoint - Gets the get reports by app id endpoint. * @param {number} appId - The id of the app. + * @param {PagingRequest} pagingRequest - The paging information that will be used to make the request. * @returns {string} - The get reports by app id endpoint. */ - public static getReportsByAppIdEndpoint(appId: number): string { - return `/Reports/appId/${appId}`; + public static getReportsByAppIdEndpoint( + appId: number, + pagingRequest: PagingRequest + ): string { + return `/Reports/appId/${appId}?pageSize=${pagingRequest.pageSize}&pageNumber=${pagingRequest.pageNumber}`; } } diff --git a/src/models/GetPagedReportsResponse.ts b/src/models/GetPagedReportsResponse.ts new file mode 100644 index 0000000..aa023a5 --- /dev/null +++ b/src/models/GetPagedReportsResponse.ts @@ -0,0 +1,14 @@ +import { PagedResponse } from './PagedResponse'; +import { type Report } from './Report'; + +export class GetPagedReportsResponse extends PagedResponse { + constructor( + items: Report[], + pageNumber: number, + pageSize: number, + totalPages: number, + totalRecords: number + ) { + super(items, pageNumber, pageSize, totalPages, totalRecords); + } +} diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index 970398d..f40047b 100644 --- a/src/models/OnspringClient.ts +++ b/src/models/OnspringClient.ts @@ -306,6 +306,24 @@ export class OnspringClient { return apiResponse; } + public async getReportsByAppId( + appId: number, + pagingRequest: PagingRequest = new PagingRequest(1, 50) + ) { + const endpoint = EndpointFactory.getReportsByAppIdEndpoint( + appId, + pagingRequest + ); + + const apiResponse = await this.get(endpoint); + + if (apiResponse.isSuccessful === false) { + return apiResponse; + } + + return apiResponse.asGetPagedReportsResponseType(); + } + /** * @method get - Makes a GET request to the specified endpoint. * @param {string} endpoint - The endpoint that will be used to make the request. diff --git a/src/models/Report.ts b/src/models/Report.ts new file mode 100644 index 0000000..9078a76 --- /dev/null +++ b/src/models/Report.ts @@ -0,0 +1,34 @@ +/** + * @class Report - Represents a report. + */ +export class Report { + /** + * @property {number} appId - The id of the app. + */ + public appId: number; + + /** + * @property {number} id - The id of the report. + */ + public id: number; + + /** + * @property {string} name - The name of the report. + */ + public name: string; + + /** + * @property {string} description - The description of the report. + */ + public description: string; + + /** + * @constructor - Creates a new instance of the Report class. + */ + constructor(appId: number, id: number, name: string, description: string) { + this.appId = appId; + this.id = id; + this.name = name; + this.description = description; + } +} diff --git a/tests/ApiResponse.spec.ts b/tests/ApiResponse.spec.ts index 3f296d1..b011aad 100644 --- a/tests/ApiResponse.spec.ts +++ b/tests/ApiResponse.spec.ts @@ -942,4 +942,45 @@ describe('ApiResponse', function () { ); }); }); + + describe('asGetPagedReportsResponse', function () { + it('should be defined', function () { + expect(ApiResponse.prototype.asGetPagedReportsResponseType).to.not.be + .undefined; + }); + + it('should be a function', function () { + expect(ApiResponse.prototype.asGetPagedReportsResponseType).to.be.a( + 'function' + ); + }); + + it('should have no parameters', function () { + expect( + ApiResponse.prototype.asGetPagedReportsResponseType.length + ).to.equal(0); + }); + + it('should return an ApiResponse', function () { + const mockResponseData = [ + { + appId: 1, + id: 1, + name: 'string', + description: 'description', + }, + { + appId: 2, + id: 2, + name: 'string', + description: 'description' + } + ] + + const apiResponse = new ApiResponse(200, 'OK', mockResponseData); + const getPagedReportsResponse = apiResponse.asGetPagedReportsResponseType(); + + + }); + }); }); diff --git a/tests/EndpointFactory.spec.ts b/tests/EndpointFactory.spec.ts index aeebd22..4a81f66 100644 --- a/tests/EndpointFactory.spec.ts +++ b/tests/EndpointFactory.spec.ts @@ -162,8 +162,11 @@ describe('EndpointFactory', function () { describe('getReportsByAppIdEndpoint', function () { it('should return the correct reports by app id endpoint', function () { - const result = EndpointFactory.getReportsByAppIdEndpoint(1); - expect(result).to.equal('/Reports/appId/1'); + const result = EndpointFactory.getReportsByAppIdEndpoint( + 1, + new PagingRequest(2, 1000) + ); + expect(result).to.equal('/Reports/appId/1?pageSize=1000&pageNumber=2'); }); }); });