From 827ad9c5a1eb859b06935ee0e6fabc757fb76aed Mon Sep 17 00:00:00 2001 From: StevanFreeborn <=> Date: Sun, 12 Feb 2023 00:32:41 -0600 Subject: [PATCH] feat: add filter operator enum, add models for getting records by ids and querying records --- src/enums/FilterOperators.ts | 12 ++++++ src/models/GetRecordsRequest.ts | 45 ++++++++++++++++++++++ src/models/OnspringClient.ts | 7 ++++ src/models/QueryRecordsRequest.ts | 55 +++++++++++++++++++++++++++ tests/FilterOperators.spec.ts | 48 ++++++++++++++++++++++++ tests/GetRecordsRequest.spec.ts | 51 +++++++++++++++++++++++++ tests/QueryRecordsRequest.spec.ts | 62 +++++++++++++++++++++++++++++++ 7 files changed, 280 insertions(+) create mode 100644 src/enums/FilterOperators.ts create mode 100644 src/models/GetRecordsRequest.ts create mode 100644 src/models/QueryRecordsRequest.ts create mode 100644 tests/FilterOperators.spec.ts create mode 100644 tests/GetRecordsRequest.spec.ts create mode 100644 tests/QueryRecordsRequest.spec.ts diff --git a/src/enums/FilterOperators.ts b/src/enums/FilterOperators.ts new file mode 100644 index 0000000..6ca881c --- /dev/null +++ b/src/enums/FilterOperators.ts @@ -0,0 +1,12 @@ +export enum FilterOperators { + Equal = 'eq', + NotEqual = 'ne', + Contains = 'contains', + IsNull = 'isnull', + NotNull = 'notnull', + GreaterThan = 'gt', + LessThan = 'lt', + And = 'and', + Or = 'or', + Not = 'not', +} diff --git a/src/models/GetRecordsRequest.ts b/src/models/GetRecordsRequest.ts new file mode 100644 index 0000000..b1a0f8e --- /dev/null +++ b/src/models/GetRecordsRequest.ts @@ -0,0 +1,45 @@ +import { DataFormat } from '../enums/DataFormat'; + +/** + * @class GetRecordsRequest - Request to get records by their ids + */ +export class GetRecordsRequest { + /** + * @property {number} appId - The id of the app the records belong to. + */ + appId: number; + + /** + * @property {number[]} recordIds - The ids of the records to get. + */ + recordIds: number[]; + + /** + * @property {number[]} fieldIds - The ids of the fields to include in the response. + */ + fieldIds: number[]; + + /** + * @property {DataFormat} dataFormat - The format of the data in the response. + */ + dataFormat: DataFormat; + + /** + * @constructor - Creates a new instance of GetRecordsRequest + * @param {number} appId - The id of the app the records belong to. + * @param {number[]} recordIds - The ids of the records to get. + * @param {number[]} fieldIds - The ids of the fields to include in the response. + * @param {DataFormat} dataFormat - The format of the data in the response. + */ + constructor( + appId: number, + recordIds: number[], + fieldIds: number[] = [], + dataFormat: DataFormat = DataFormat.Raw + ) { + this.appId = appId; + this.recordIds = recordIds; + this.fieldIds = fieldIds; + this.dataFormat = dataFormat; + } +} diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index 843ff6a..5292f23 100644 --- a/src/models/OnspringClient.ts +++ b/src/models/OnspringClient.ts @@ -24,6 +24,7 @@ import { type Record } from './Record'; import { type GetRecordRequest } from './GetRecordRequest'; import { type GetRecordsByAppIdRequest } from './GetRecordsByAppIdRequest'; import { type GetPagedRecordsResponse } from './GetPagedRecordsResponse'; +import { type GetRecordsRequest } from './GetRecordsRequest'; /** * @class OnspringClient - A client that can communicate with the Onspring API. @@ -356,6 +357,12 @@ export class OnspringClient { return apiResponse.asRecordType(); } + public async getRecordsByIds( + request: GetRecordsRequest + ): Promise>> { + throw new Error('Not implemented'); + } + /** * @method getReportsByAppId - Gets a paged list of reports by the app id. * @param {number} appId - The id of the app to get the reports for. diff --git a/src/models/QueryRecordsRequest.ts b/src/models/QueryRecordsRequest.ts new file mode 100644 index 0000000..361ad0a --- /dev/null +++ b/src/models/QueryRecordsRequest.ts @@ -0,0 +1,55 @@ +import { DataFormat } from '../enums/DataFormat'; +import { PagingRequest } from './PagingRequest'; + +/** + * @class QueryRecordsRequest - Request to query for records. + */ +export class QueryRecordsRequest { + /** + * @property {number} appId - The id of the app the records belong to. + */ + appId: number; + + /** + * @property {string} filter - The filter to use to query for records. + */ + filter: string; + + /** + * @property {number[]} fieldIds - The ids of the fields to include in the response. + */ + fieldIds: number[]; + + /** + * @property {DataFormat} dataFormat - The format of the data in the response. + */ + dataFormat: DataFormat; + + /** + * @property {PagingRequest} pagingRequest - The paging request to use to query for records. + */ + pagingRequest: PagingRequest; + + /** + * @constructor - Creates a new instance of QueryRecordsRequest + * @param {number} appId - The id of the app the records belong to. + * @param {string} filter - The filter to use to query for records. + * @param {number[]} fieldIds - The ids of the fields to include in the response. + * @param {DataFormat} dataFormat - The format of the data in the response. + * @param {PagingRequest} pagingRequest - The paging request to use to query for records. + * @returns {QueryRecordsRequest} - A new instance of QueryRecordsRequest + */ + constructor( + appId: number, + filter: string, + fieldIds: number[] = [], + dataFormat: DataFormat = DataFormat.Raw, + pagingRequest: PagingRequest = new PagingRequest(1, 50) + ) { + this.appId = appId; + this.filter = filter; + this.fieldIds = fieldIds; + this.dataFormat = dataFormat; + this.pagingRequest = pagingRequest; + } +} diff --git a/tests/FilterOperators.spec.ts b/tests/FilterOperators.spec.ts new file mode 100644 index 0000000..cc18c5f --- /dev/null +++ b/tests/FilterOperators.spec.ts @@ -0,0 +1,48 @@ +import { FilterOperators } from '../src/enums/FilterOperators'; +import { expect } from 'chai'; + +describe('FilterOperators', function () { + it('should be defined', function () { + expect(FilterOperators).to.not.be.undefined; + }); + + it('should have a Equal property with value "eq"', function () { + expect(FilterOperators).to.have.property('Equal', 'eq'); + }); + + it('should have a NotEqual property with value "ne"', function () { + expect(FilterOperators).to.have.property('NotEqual', 'ne'); + }); + + it('should have a Contains property with value "contains"', function () { + expect(FilterOperators).to.have.property('Contains', 'contains'); + }); + + it('should have a IsNull property with value "isnull"', function () { + expect(FilterOperators).to.have.property('IsNull', 'isnull'); + }); + + it('should have a NotNull property with value "notnull"', function () { + expect(FilterOperators).to.have.property('NotNull', 'notnull'); + }); + + it('should have a GreaterThan property with value "gt"', function () { + expect(FilterOperators).to.have.property('GreaterThan', 'gt'); + }); + + it('should have a LessThan property with value "lt"', function () { + expect(FilterOperators).to.have.property('LessThan', 'lt'); + }); + + it('should have a And property with value "and"', function () { + expect(FilterOperators).to.have.property('And', 'and'); + }); + + it('should have a Or property with value "or"', function () { + expect(FilterOperators).to.have.property('Or', 'or'); + }); + + it('should have a Not property with value "not"', function () { + expect(FilterOperators).to.have.property('Not', 'not'); + }); +}); diff --git a/tests/GetRecordsRequest.spec.ts b/tests/GetRecordsRequest.spec.ts new file mode 100644 index 0000000..bf80609 --- /dev/null +++ b/tests/GetRecordsRequest.spec.ts @@ -0,0 +1,51 @@ +import { GetRecordsRequest } from '../src/models/GetRecordsRequest'; +import { expect } from 'chai'; +import { DataFormat } from '../src/enums/DataFormat'; + +describe('GetRecordsRequest', function () { + it('should be defined', function () { + expect(GetRecordsRequest).to.not.be.undefined; + }); + + it('should have a constructor', function () { + expect(GetRecordsRequest).to.have.property('constructor'); + }); + + it('should have a constructor with 2 parameters', function () { + expect(GetRecordsRequest).to.have.lengthOf(2); + }); + + it('should have an appId property', function () { + expect(new GetRecordsRequest(1, [1, 2])).to.have.property('appId'); + }); + + it('should have a recordIds property', function () { + expect(new GetRecordsRequest(1, [1, 2])).to.have.property('recordIds'); + }); + + it('should have a fieldIds property', function () { + expect(new GetRecordsRequest(1, [1, 2])).to.have.property('fieldIds'); + }); + + it('should have a dataFormat property', function () { + expect(new GetRecordsRequest(1, [1, 2])).to.have.property('dataFormat'); + }); + + it('should have a constructor that sets its properties correctly', function () { + const appId = 1; + const recordIds = [1, 2]; + const fieldIds = [1, 2]; + const dataFormat = DataFormat.Raw; + + const request = new GetRecordsRequest( + appId, + recordIds, + fieldIds, + dataFormat + ); + expect(request).to.have.property('appId', appId); + expect(request).to.have.property('recordIds', recordIds); + expect(request).to.have.property('fieldIds', fieldIds); + expect(request).to.have.property('dataFormat', dataFormat); + }); +}); diff --git a/tests/QueryRecordsRequest.spec.ts b/tests/QueryRecordsRequest.spec.ts new file mode 100644 index 0000000..fa32918 --- /dev/null +++ b/tests/QueryRecordsRequest.spec.ts @@ -0,0 +1,62 @@ +import { QueryRecordsRequest } from '../src/models/QueryRecordsRequest'; +import { expect } from 'chai'; +import { DataFormat } from '../src/enums/DataFormat'; +import { PagingRequest } from '../src/models/PagingRequest'; + +describe('QueryRecordsRequest', function () { + it('should be defined', function () { + expect(QueryRecordsRequest).to.not.be.undefined; + }); + + it('should have a constructor', function () { + expect(QueryRecordsRequest).to.have.property('constructor'); + }); + + it('should have a constructor that takes 2 parameters', function () { + expect(QueryRecordsRequest).to.have.lengthOf(2); + }); + + it('should have an appId property', function () { + expect(new QueryRecordsRequest(1, 'filter')).to.have.property('appId'); + }); + + it('should have a filter property', function () { + expect(new QueryRecordsRequest(1, 'filter')).to.have.property('filter'); + }); + + it('should have a fieldIds property', function () { + expect(new QueryRecordsRequest(1, 'filter')).to.have.property('fieldIds'); + }); + + it('should have a dataFormat property', function () { + expect(new QueryRecordsRequest(1, 'filter')).to.have.property('dataFormat'); + }); + + it('should have a pagingRequest property', function () { + expect(new QueryRecordsRequest(1, 'filter')).to.have.property( + 'pagingRequest' + ); + }); + + it('should have a constructor that sets its properties correctly', function () { + const appId = 1; + const filter = 'filter'; + const fieldIds = [1, 2, 3]; + const dataFormat = DataFormat.Raw; + const pagingRequest = new PagingRequest(1, 50); + + const request = new QueryRecordsRequest( + appId, + filter, + fieldIds, + dataFormat, + pagingRequest + ); + + expect(request).to.have.property('appId', appId); + expect(request).to.have.property('filter', filter); + expect(request).to.have.property('fieldIds', fieldIds); + expect(request).to.have.property('dataFormat', dataFormat); + expect(request).to.have.property('pagingRequest', pagingRequest); + }); +});