Skip to content

Commit

Permalink
feat(#9237): Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sugat009 committed Aug 12, 2024
1 parent 182e9e7 commit 24db6ff
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion shared-libs/cht-datasource/src/local/place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export namespace v1 {
const getLineageDocs = getLineageDocsById(medicDb);
const getMedicDocsById = getDocsByIds(medicDb);
return async (identifier: UuidQualifier): Promise<Nullable<Place.v1.PlaceWithLineage>> => {
const [place, ...lineagePlaces] = await getLineageDocs(identifier.uuid, identifier.uuid);
const [place, ...lineagePlaces] = await getLineageDocs(identifier.uuid);
if (!isPlace(settings, identifier.uuid, place)) {
return null;
}
Expand Down
13 changes: 7 additions & 6 deletions shared-libs/cht-datasource/src/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export namespace v1 {
}
};

const assertTypeQualifier: (qualifier: unknown) => asserts qualifier is ContactTypeQualifier = (
const assertTypeQualifier: (
qualifier: unknown
) => {
) => asserts qualifier is ContactTypeQualifier = (qualifier: unknown) => {
if (!isContactTypeQualifier(qualifier)) {
throw new Error(`Invalid type [${JSON.stringify(qualifier)}].`);
}
Expand Down Expand Up @@ -71,14 +71,15 @@ export namespace v1 {
};

const getPeople = <T>(
localFn: (c: LocalDataContext) => (limit: number, skip: number) => Promise<T>,
remoteFn: (c: RemoteDataContext) => (limit: number, skip: number) => Promise<T>
localFn: (c: LocalDataContext) => (personType: ContactTypeQualifier, limit: number, skip: number) => Promise<T>,
remoteFn: (c: RemoteDataContext) => (personType: ContactTypeQualifier, limit: number, skip: number) => Promise<T>
) => (context: DataContext) => {
assertDataContext(context);
const fn = adapt(context, localFn, remoteFn);

return async (limit = 100, skip = 0): Promise<T> => {
return fn(limit, skip);
return async (personType: ContactTypeQualifier, limit = 100, skip = 0): Promise<T> => {
assertTypeQualifier(personType);
return fn(personType, limit, skip);
};
};

Expand Down
2 changes: 1 addition & 1 deletion shared-libs/cht-datasource/src/qualifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const isUuidQualifier = (identifier: unknown): identifier is UuidQualifie
};

/**
A qualifier that identifies contacts based on type.
* A qualifier that identifies an entity based on type
*/
export type ContactTypeQualifier = Readonly<{ contactType: string }>;

Expand Down
4 changes: 4 additions & 0 deletions shared-libs/cht-datasource/src/remote/person.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<<<<<<< HEAD
import { Nullable, Page } from '../libs/core';
=======
import { Nullable } from '../libs/core';
>>>>>>> de63197bb (feat(#9237): Address PR comments)
import { ContactTypeQualifier, UuidQualifier } from '../qualifier';
import * as Person from '../person';
import { getResource, getResources, RemoteDataContext } from './libs/data-context';
Expand Down
4 changes: 0 additions & 4 deletions shared-libs/cht-datasource/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import * as Qualifier from '../src/qualifier';
import sinon, { SinonStub } from 'sinon';
import * as Context from '../src/libs/data-context';
import { DataContext } from '../src';
<<<<<<< HEAD
import { Page } from '../src/libs/core';
=======
import { Doc } from '../src/libs/doc';
>>>>>>> 08e2b2711 (feat(#9237): Add functionality of getting people with pagination in cht-datasource)

describe('CHT Script API - getDatasource', () => {
let dataContext: DataContext;
Expand Down
5 changes: 4 additions & 1 deletion shared-libs/cht-datasource/test/local/libs/doc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ describe('local doc lib', () => {

expect(result).to.deep.equal([]);
expect(dbQuery.calledOnceWithExactly('medic-client/contacts_by_type', {
include_docs: true, key: contactType, limit, skip
include_docs: true,
key: contactType,
limit,
skip
})).to.be.true;
expect(isDoc.args).to.deep.equal([]);
});
Expand Down
12 changes: 7 additions & 5 deletions shared-libs/cht-datasource/test/local/person.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('local person', () => {
const result = await Person.v1.getWithLineage(localContext)(identifier);

expect(result).to.equal(copiedPerson);
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid, identifier.uuid)).to.be.true;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid)).to.be.true;
expect(isPerson.calledOnceWithExactly(settings, person)).to.be.true;
expect(warn.notCalled).to.be.true;
expect(debug.notCalled).to.be.true;
Expand All @@ -167,7 +167,7 @@ describe('local person', () => {
const result = await Person.v1.getWithLineage(localContext)(identifier);

expect(result).to.be.null;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid, identifier.uuid)).to.be.true;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid)).to.be.true;
expect(isPerson.notCalled).to.be.true;
expect(warn.calledOnceWithExactly(`No person found for identifier [${identifier.uuid}].`)).to.be.true;
expect(debug.notCalled).to.be.true;
Expand All @@ -191,7 +191,7 @@ describe('local person', () => {
const result = await Person.v1.getWithLineage(localContext)(identifier);

expect(result).to.be.null;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid, identifier.uuid)).to.be.true;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid)).to.be.true;
expect(isPerson.calledOnceWithExactly(settings, person)).to.be.true;
expect(warn.calledOnceWithExactly(`Document [${identifier.uuid}] is not a valid person.`)).to.be.true;
expect(debug.notCalled).to.be.true;
Expand All @@ -212,7 +212,7 @@ describe('local person', () => {
const result = await Person.v1.getWithLineage(localContext)(identifier);

expect(result).to.equal(person);
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid, identifier.uuid)).to.be.true;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid)).to.be.true;
expect(isPerson.calledOnceWithExactly(settings, person)).to.be.true;
expect(warn.notCalled).to.be.true;
expect(debug.calledOnceWithExactly(`No lineage places found for person [${identifier.uuid}].`)).to.be.true;
Expand Down Expand Up @@ -275,7 +275,9 @@ describe('local person', () => {

expect(settingsGetAll.calledOnce).to.be.true;
expect(getPersonTypes.calledOnceWithExactly(settings)).to.be.true;
expect(queryDocsByKeyOuter.notCalled).to.be.true;
expect(queryDocsByKeyOuter.calledOnceWithExactly(
localContext.medicDb, 'medic-client/contacts_by_type'
)).to.be.true;
expect(queryDocsByKeyInner.notCalled).to.be.true;
expect(isPerson.notCalled).to.be.true;
});
Expand Down
8 changes: 4 additions & 4 deletions shared-libs/cht-datasource/test/local/place.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('local place', () => {
const result = await Place.v1.getWithLineage(localContext)(identifier);

expect(result).to.equal(copiedPlace);
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid, identifier.uuid)).to.be.true;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid)).to.be.true;
expect(isPlace.calledOnceWithExactly(settings, place0)).to.be.true;
expect(warn.notCalled).to.be.true;
expect(debug.notCalled).to.be.true;
Expand All @@ -166,7 +166,7 @@ describe('local place', () => {
const result = await Place.v1.getWithLineage(localContext)(identifier);

expect(result).to.be.null;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid, identifier.uuid)).to.be.true;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid)).to.be.true;
expect(isPlace.notCalled).to.be.true;
expect(warn.calledOnceWithExactly(`No place found for identifier [${identifier.uuid}].`)).to.be.true;
expect(debug.notCalled).to.be.true;
Expand All @@ -189,7 +189,7 @@ describe('local place', () => {
const result = await Place.v1.getWithLineage(localContext)(identifier);

expect(result).to.be.null;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid, identifier.uuid)).to.be.true;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid)).to.be.true;
expect(isPlace.calledOnceWithExactly(settings, place0)).to.be.true;
expect(warn.calledOnceWithExactly(`Document [${identifier.uuid}] is not a valid place.`)).to.be.true;
expect(debug.notCalled).to.be.true;
Expand All @@ -210,7 +210,7 @@ describe('local place', () => {
const result = await Place.v1.getWithLineage(localContext)(identifier);

expect(result).to.equal(place);
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid, identifier.uuid)).to.be.true;
expect(getLineageDocsByIdInner.calledOnceWithExactly(identifier.uuid)).to.be.true;
expect(isPlace.calledOnceWithExactly(settings, place)).to.be.true;
expect(warn.notCalled).to.be.true;
expect(debug.calledOnceWithExactly(`No lineage places found for place [${identifier.uuid}].`)).to.be.true;
Expand Down
14 changes: 13 additions & 1 deletion shared-libs/cht-datasource/test/person.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('person', () => {

it('retrieves people from the data context', async () => {
isContactTypeQualifier.returns(true);
getPage.resolves(pageData);
getPage.resolves(people);

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

Expand Down Expand Up @@ -204,6 +204,18 @@ describe('person', () => {
expect(isContactTypeQualifier.calledOnceWithExactly((personTypeQualifier))).to.be.true;
expect(getPage.notCalled).to.be.true;
});

it('throws an error if the qualifier is invalid', async () => {
isContactTypeQualifier.returns(false);

await expect(Person.v1.getPage(dataContext)(invalidQualifier))
.to.be.rejectedWith(`Invalid type [${JSON.stringify(invalidQualifier)}].`);

expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Person.v1.getPage, Remote.Person.v1.getPage)).to.be.true;
expect(isContactTypeQualifier.calledOnceWithExactly(invalidQualifier)).to.be.true;
expect(getPage.notCalled).to.be.true;
});
});

describe('getAll', () => {
Expand Down

0 comments on commit 24db6ff

Please sign in to comment.