Skip to content

Commit

Permalink
change limit to number and stringified number in cht-datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
sugat009 committed Dec 6, 2024
1 parent e6aea4f commit c21e52a
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 23 deletions.
4 changes: 1 addition & 3 deletions api/src/controllers/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ module.exports = {
Object.assign(qualifier, Qualifier.byContactType(req.query.type));
}

const limit = req.query.limit ? Number(req.query.limit) : req.query.limit;

const docs = await getContactIds()(qualifier, req.query.cursor, limit);
const docs = await getContactIds()(qualifier, req.query.cursor, req.query.limit);

return res.json(docs);
}),
Expand Down
3 changes: 1 addition & 2 deletions api/src/controllers/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ module.exports = {
await checkUserPermissions(req);

const personType = Qualifier.byContactType(req.query.type);
const limit = req.query.limit ? Number(req.query.limit) : req.query.limit;

const docs = await getPageByType()( personType, req.query.cursor, limit );
const docs = await getPageByType()( personType, req.query.cursor, req.query.limit );

return res.json(docs);
}),
Expand Down
3 changes: 1 addition & 2 deletions api/src/controllers/place.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ module.exports = {
await checkUserPermissions(req);

const placeType = Qualifier.byContactType(req.query.type);
const limit = req.query.limit ? Number(req.query.limit) : req.query.limit;

const docs = await getPageByType()( placeType, req.query.cursor, limit );
const docs = await getPageByType()( placeType, req.query.cursor, req.query.limit );

return res.json(docs);
})
Expand Down
3 changes: 1 addition & 2 deletions api/src/controllers/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ module.exports = {
await checkUserPermissions(req);

const qualifier = Qualifier.byFreetext(req.query.freetext);
const limit = req.query.limit ? Number(req.query.limit) : req.query.limit;

const docs = await getReportIds()(qualifier, req.query.cursor, limit);
const docs = await getReportIds()(qualifier, req.query.cursor, req.query.limit);

return res.json(docs);
})
Expand Down
4 changes: 2 additions & 2 deletions shared-libs/cht-datasource/src/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export namespace v1 {
const curriedFn = async (
qualifier: ContactTypeQualifier | FreetextQualifier,
cursor: Nullable<string> = null,
limit = DEFAULT_CONTACT_PAGE_LIMIT
limit: number | `${number}` = DEFAULT_CONTACT_PAGE_LIMIT
): Promise<Page<string>> => {
assertCursor(cursor);
assertLimit(limit);
Expand All @@ -115,7 +115,7 @@ export namespace v1 {
assertFreetextQualifier(qualifier);
}

return fn(qualifier, cursor, limit);
return fn(qualifier, cursor, Number(limit));
};
return curriedFn;
};
Expand Down
8 changes: 4 additions & 4 deletions shared-libs/cht-datasource/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const getDatasource = (ctx: DataContext) => {
freetext: Nullable<string> = null,
type: Nullable<string> = null,
cursor: Nullable<string> = null,
limit = DEFAULT_CONTACT_PAGE_LIMIT
limit: number | `${number}` = DEFAULT_CONTACT_PAGE_LIMIT
) => ctx.bind(Contact.v1.getIdsPage)(
Contact.v1.createQualifier(freetext, type), cursor, limit
),
Expand Down Expand Up @@ -149,7 +149,7 @@ export const getDatasource = (ctx: DataContext) => {
getPageByType: (
placeType: string,
cursor: Nullable<string> = null,
limit = DEFAULT_PLACE_PAGE_LIMIT
limit: number | `${number}` = DEFAULT_PLACE_PAGE_LIMIT
) => ctx.bind(Place.v1.getPage)(
Qualifier.byContactType(placeType), cursor, limit
),
Expand Down Expand Up @@ -194,7 +194,7 @@ export const getDatasource = (ctx: DataContext) => {
getPageByType: (
personType: string,
cursor: Nullable<string> = null,
limit = DEFAULT_PEOPLE_PAGE_LIMIT
limit: number | `${number}` = DEFAULT_PEOPLE_PAGE_LIMIT
) => ctx.bind(Person.v1.getPage)(
Qualifier.byContactType(personType), cursor, limit
),
Expand Down Expand Up @@ -231,7 +231,7 @@ export const getDatasource = (ctx: DataContext) => {
getIdsPage: (
qualifier: string,
cursor: Nullable<string> = null,
limit = DEFAULT_REPORT_PAGE_LIMIT
limit: number | `${number}` = DEFAULT_REPORT_PAGE_LIMIT
// eslint-disable-next-line compat/compat
) => ctx.bind(Report.v1.getIdsPage)(
Qualifier.byFreetext(qualifier), cursor, limit
Expand Down
5 changes: 3 additions & 2 deletions shared-libs/cht-datasource/src/libs/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ export const assertTypeQualifier: (qualifier: unknown) => asserts qualifier is C
};

/** @internal */
export const assertLimit: (limit: unknown) => asserts limit is number = (limit: unknown) => {
if (typeof limit !== 'number' || !Number.isInteger(limit) || limit <= 0) {
export const assertLimit: (limit: unknown) => asserts limit is number | `${number}` = (limit: unknown) => {
const numberLimit = Number(limit);
if (!Number.isInteger(numberLimit) || numberLimit <= 0) {
throw new InvalidArgumentError(`The limit must be a positive number: [${String(limit)}].`);
}
};
Expand Down
4 changes: 2 additions & 2 deletions shared-libs/cht-datasource/src/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ export namespace v1 {
const curriedFn = async (
personType: ContactTypeQualifier,
cursor: Nullable<string> = null,
limit = DEFAULT_PEOPLE_PAGE_LIMIT
limit: number | `${number}` = DEFAULT_PEOPLE_PAGE_LIMIT
): Promise<Page<Person>> => {
assertTypeQualifier(personType);
assertCursor(cursor);
assertLimit(limit);

return fn(personType, cursor, limit);
return fn(personType, cursor, Number(limit));
};
return curriedFn;
};
Expand Down
4 changes: 2 additions & 2 deletions shared-libs/cht-datasource/src/place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ export namespace v1 {
const curriedFn = async (
placeType: ContactTypeQualifier,
cursor: Nullable<string> = null,
limit = DEFAULT_PLACE_PAGE_LIMIT
limit: number | `${number}` = DEFAULT_PLACE_PAGE_LIMIT
): Promise<Page<Place>> => {
assertTypeQualifier(placeType);
assertCursor(cursor);
assertLimit(limit);

return fn(placeType, cursor, limit);
return fn(placeType, cursor, Number(limit));
};
return curriedFn;
};
Expand Down
4 changes: 2 additions & 2 deletions shared-libs/cht-datasource/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ export namespace v1 {
const curriedFn = async (
qualifier: FreetextQualifier,
cursor: Nullable<string> = null,
limit = DEFAULT_REPORT_PAGE_LIMIT
limit: number | `${number}` = DEFAULT_REPORT_PAGE_LIMIT
): Promise<Page<string>> => {
assertFreetextQualifier(qualifier);
assertCursor(cursor);
assertLimit(limit);

return fn(qualifier, cursor, limit);
return fn(qualifier, cursor, Number(limit));
};
return curriedFn;
};
Expand Down
19 changes: 19 additions & 0 deletions shared-libs/cht-datasource/test/contact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ describe('contact', () => {
const cursor = '1';
const pageData = { data: contactIds, cursor };
const limit = 3;
const stringifiedLimit = '3';
const contactTypeQualifier = { contactType: 'person' } as const;
const freetextQualifier = { freetext: 'freetext'} as const;
const qualifier = {
Expand Down Expand Up @@ -209,6 +210,24 @@ describe('contact', () => {
expect(isFreetextQualifier.calledOnceWithExactly(qualifier)).to.be.true;
});

it('retrieves contact id page from the data context when cursor is not null and ' +
'limit is stringified number', async () => {
isContactTypeQualifier.returns(true);
isFreetextQualifier.returns(true);
getIdsPage.resolves(pageData);

const result = await Contact.v1.getIdsPage(dataContext)(qualifier, cursor, stringifiedLimit);

expect(result).to.equal(pageData);
expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(
adapt.calledOnceWithExactly(dataContext, Local.Contact.v1.getPage, Remote.Contact.v1.getPage)
).to.be.true;
expect(getIdsPage.calledOnceWithExactly(qualifier, cursor, limit)).to.be.true;
expect(isContactTypeQualifier.calledOnceWithExactly(qualifier)).to.be.true;
expect(isFreetextQualifier.calledOnceWithExactly(qualifier)).to.be.true;
});

it('throws an error if the data context is invalid', () => {
isContactTypeQualifier.returns(true);
isFreetextQualifier.returns(true);
Expand Down
15 changes: 15 additions & 0 deletions shared-libs/cht-datasource/test/person.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe('person', () => {
const cursor = '1';
const pageData = { data: people, cursor };
const limit = 3;
const stringifiedLimit = '3';
const personTypeQualifier = {contactType: 'person'} as const;
const invalidQualifier = { contactType: 'invalid' } as const;
let getPage: SinonStub;
Expand Down Expand Up @@ -167,6 +168,20 @@ describe('person', () => {
expect(isContactTypeQualifier.calledOnceWithExactly((personTypeQualifier))).to.be.true;
});

it('retrieves people from the data context when cursor is not null and ' +
'limit is stringified number', async () => {
isContactTypeQualifier.returns(true);
getPage.resolves(pageData);

const result = await Person.v1.getPage(dataContext)(personTypeQualifier, cursor, stringifiedLimit);

expect(result).to.equal(pageData);
expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Person.v1.getPage, Remote.Person.v1.getPage)).to.be.true;
expect(getPage.calledOnceWithExactly(personTypeQualifier, cursor, limit)).to.be.true;
expect(isContactTypeQualifier.calledOnceWithExactly((personTypeQualifier))).to.be.true;
});

it('throws an error if the data context is invalid', () => {
isContactTypeQualifier.returns(true);
assertDataContext.throws(new Error(`Invalid data context [null].`));
Expand Down
15 changes: 15 additions & 0 deletions shared-libs/cht-datasource/test/place.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe('place', () => {
const cursor = '1';
const pageData = { data: places, cursor };
const limit = 3;
const stringifiedLimit = '3';
const placeTypeQualifier = {contactType: 'place'} as const;
const invalidQualifier = { contactType: 'invalid' } as const;
let getPage: SinonStub;
Expand Down Expand Up @@ -167,6 +168,20 @@ describe('place', () => {
expect(isContactTypeQualifier.calledOnceWithExactly((placeTypeQualifier))).to.be.true;
});

it('retrieves places from the data context when cursor is not null and ' +
'limit is stringified number', async () => {
isContactTypeQualifier.returns(true);
getPage.resolves(pageData);

const result = await Place.v1.getPage(dataContext)(placeTypeQualifier, cursor, stringifiedLimit);

expect(result).to.equal(pageData);
expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Place.v1.getPage, Remote.Place.v1.getPage)).to.be.true;
expect(getPage.calledOnceWithExactly(placeTypeQualifier, cursor, limit)).to.be.true;
expect(isContactTypeQualifier.calledOnceWithExactly((placeTypeQualifier))).to.be.true;
});

it('throws an error if the data context is invalid', () => {
isContactTypeQualifier.returns(true);
assertDataContext.throws(new Error(`Invalid data context [null].`));
Expand Down
16 changes: 16 additions & 0 deletions shared-libs/cht-datasource/test/report.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('report', () => {
const cursor = '1';
const pageData = { data: ids, cursor };
const limit = 3;
const stringifiedLimit = '3';
const freetextQualifier = { freetext: 'freetext'} as const;
const invalidFreetextQualifier = { freetext: 'invalid_freetext'} as const;
let getIdsPage: SinonStub;
Expand Down Expand Up @@ -117,6 +118,21 @@ describe('report', () => {
expect(isFreetextQualifier.calledOnceWithExactly(freetextQualifier)).to.be.true;
});

it('retrieves report ids from the data context when cursor is not null and ' +
'limit is stringified number', async () => {
isFreetextQualifier.returns(true);
getIdsPage.resolves(pageData);

// eslint-disable-next-line compat/compat
const result = await Report.v1.getIdsPage(dataContext)(freetextQualifier, cursor, stringifiedLimit);

expect(result).to.equal(pageData);
expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Report.v1.getPage, Remote.Report.v1.getPage)).to.be.true;
expect(getIdsPage.calledOnceWithExactly(freetextQualifier, cursor, limit)).to.be.true;
expect(isFreetextQualifier.calledOnceWithExactly(freetextQualifier)).to.be.true;
});

it('throws an error if the data context is invalid', () => {
isFreetextQualifier.returns(true);
assertDataContext.throws(new Error(`Invalid data context [null].`));
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/shared-libs/cht-datasource/contact.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ describe('Contact', () => {
const getIdsPage = Contact.v1.getIdsPage(dataContext);
const fourLimit = 4;
const twoLimit = 2;
const stringifiedLimit = '7';
const cursor = null;
const invalidContactType = 'invalidPerson';
const freetext = 'contact';
Expand Down Expand Up @@ -212,6 +213,16 @@ describe('Contact', () => {
expect(responseCursor).to.be.equal(null);
});

it('returns a page of people type contact ids ' +
'when stringified limit and null cursor is passed', async () => {
const responsePage = await getIdsPage(Qualifier.byContactType(personType), null, stringifiedLimit);
const responsePeople = responsePage.data;
const responseCursor = responsePage.cursor;

expect(responsePeople).excludingEvery([ '_rev', 'reported_date' ]).to.deep.equalInAnyOrder(expectedPeopleIds);
expect(responseCursor).to.be.equal('7');
});

it('returns a page of people type contact ids' +
' when limit and cursor is passed and cursor can be reused', async () => {
const firstPage = await getIdsPage(Qualifier.byContactType(personType), cursor, fourLimit);
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/shared-libs/cht-datasource/person.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ describe('Person', () => {
describe('getPage', async () => {
const getPage = Person.v1.getPage(dataContext);
const limit = 4;
const stringifiedLimit = '7';
const cursor = null;
const invalidContactType = 'invalidPerson';

Expand All @@ -158,6 +159,15 @@ describe('Person', () => {
expect(responseCursor).to.be.equal(null);
});

it('returns a page of people for stringified limit and null cursor passed', async () => {
const responsePage = await getPage(Qualifier.byContactType(personType), null, stringifiedLimit);
const responsePeople = responsePage.data;
const responseCursor = responsePage.cursor;

expect(responsePeople).excludingEvery([ '_rev', 'reported_date' ]).to.deep.equalInAnyOrder(expectedPeople);
expect(responseCursor).to.be.equal('7');
});

it('returns a page of people when limit and cursor is passed and cursor can be reused', async () => {
const firstPage = await getPage(Qualifier.byContactType(personType), cursor, limit);
const secondPage = await getPage(Qualifier.byContactType(personType), firstPage.cursor, limit);
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/shared-libs/cht-datasource/place.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe('Place', () => {
describe('getPage', async () => {
const getPage = Place.v1.getPage(dataContext);
const limit = 2;
const stringifiedLimit = '3';
const cursor = null;
const invalidContactType = 'invalidPlace';

Expand All @@ -136,6 +137,16 @@ describe('Place', () => {
expect(responseCursor).to.be.equal(null);
});

it('returns a page of places for stringified limit and null cursor passed', async () => {
const responsePage = await getPage(Qualifier.byContactType(placeType), null, stringifiedLimit);
const responsePlaces = responsePage.data;
const responseCursor = responsePage.cursor;

expect(responsePlaces).excludingEvery([ '_rev', 'reported_date' ])
.to.deep.equalInAnyOrder(expectedPlaces);
expect(responseCursor).to.be.equal('3');
});

it('returns a page of places when limit and cursor is passed and cursor can be reused', async () => {
const firstPage = await getPage(Qualifier.byContactType(placeType), cursor, limit);
const secondPage = await getPage(Qualifier.byContactType(placeType), firstPage.cursor, limit);
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/shared-libs/cht-datasource/report.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('Report', () => {
const getReport = Report.v1.getIdsPage(dataContext);
const freetext = 'report';
const limit = 4;
const stringifiedLimit = '6';
const cursor = null;

it('returns a page of report ids for no limit and cursor passed', async () => {
Expand All @@ -129,6 +130,15 @@ describe('Report', () => {
expect(responseCursor).to.be.equal(null);
});

it('returns a page of report ids for stringified limit and cursor passed', async () => {
const responsePage = await getReport(Qualifier.byFreetext(freetext), null, stringifiedLimit);
const responsePeople = responsePage.data;
const responseCursor = responsePage.cursor;

expect(responsePeople).excludingEvery([ '_rev', 'reported_date' ]).to.deep.equalInAnyOrder(allReportsIds);
expect(responseCursor).to.be.equal('6');
});

it('returns a page of report ids when limit and cursor is passed and cursor can be reused', async () => {
const firstPage = await getReport(Qualifier.byFreetext(freetext), cursor, limit);
const secondPage = await getReport(Qualifier.byFreetext(freetext), firstPage.cursor, limit);
Expand Down

0 comments on commit c21e52a

Please sign in to comment.