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 24db6ff commit 0ba0b73
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
8 changes: 8 additions & 0 deletions shared-libs/cht-datasource/src/local/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ export namespace v1 {

/** @internal */
export const getPage = ({ medicDb, settings }: LocalDataContext) => {
<<<<<<< HEAD
return async (
personType: ContactTypeQualifier,
cursor: string,
limit: number,
): Promise<Page<Person.v1.Person>> => {
const personTypes = contactTypeUtils.getPersonTypes(settings.getAll());
const personTypesIds = personTypes.map((item) => item.id);
=======
return async (personType: ContactTypeQualifier, limit: number, skip: number): Promise<Person.v1.Person[]> => {
const personTypes = contactTypeUtils.getPersonTypes(settings.getAll());
const personTypesIds = personTypes.map(item => item.id);

const getDocsByPage = queryDocsByKey(medicDb, 'medic-client/contacts_by_type');
>>>>>>> 8fbc245fe (feat(#9237): Address PR comments)

if (!personTypesIds.includes(personType.contactType)) {
throw new Error(`Invalid person type: ${personType.contactType}`);
Expand Down
17 changes: 2 additions & 15 deletions shared-libs/cht-datasource/src/person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,12 @@ export namespace v1 {
) => (context: DataContext) => {
assertDataContext(context);
const fn = adapt(context, localFn, remoteFn);
return async (qualifier: UuidQualifier): Promise<T> => {
assertPersonQualifier(qualifier);
return fn(qualifier);
};
};

return async (qualifier: UuidQualifier): Promise<T> => {shared-libs/cht-datasource/src/person.ts
const getPeople = <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 (personType: ContactTypeQualifier, limit = 100, skip = 0): Promise<T> => {
assertTypeQualifier(personType);
return fn(personType, limit, skip);
};
};

assertDataContext(context);shared-libs/cht-datasource/src/person.ts
/**
* Returns a person for the given qualifier.
* @param context the current data context
Expand Down
21 changes: 20 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 @@ -255,13 +255,32 @@ describe('local doc lib', () => {
const result = await queryDocsByKey(db, 'medic-client/contacts_by_type')(contactType, limit, skip);

expect(result).to.deep.equal([]);
expect(dbQuery.calledOnceWithExactly('medic-client/contacts_by_type', {
include_docs: true, key: contactType, limit, skip
})).to.be.true;
expect(isDoc.args).to.deep.equal([]);
});

it('returns null valued array if rows from database are not docs', async () => {
const doc0 = { _id: 'doc0' };

dbQuery.resolves({
rows: [
{ doc: doc0 },
]
});
isDoc.returns(false);

const result = await queryDocsByKey(db, 'medic-client/contacts_by_type')(contactType, limit, skip);

expect(result).to.deep.equal([null]);
expect(dbQuery.calledOnceWithExactly('medic-client/contacts_by_type', {
include_docs: true,
key: contactType,
limit,
skip
})).to.be.true;
expect(isDoc.args).to.deep.equal([]);
expect(isDoc.args).to.deep.equal([[doc0]]);
});

it('returns null valued array if rows from database are not docs', async () => {
Expand Down
26 changes: 26 additions & 0 deletions shared-libs/cht-datasource/test/person.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,32 @@ describe('person', () => {
expect(isContactTypeQualifier.calledOnceWithExactly(invalidQualifier)).to.be.true;
expect(getPage.notCalled).to.be.true;
});

it('throws an error if limit is invalid', async () => {
isContactTypeQualifier.returns(true);
getPage.resolves(people);

await expect(Person.v1.getPage(dataContext)(personTypeQualifier, invalidLimit, skip))
.to.be.rejectedWith(`limit must be a positive number`);

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((personTypeQualifier))).to.be.true;
expect(getPage.notCalled).to.be.true;
});

it('throws an error if skip is invalid', async () => {
isContactTypeQualifier.returns(true);
getPage.resolves(people);

await expect(Person.v1.getPage(dataContext)(personTypeQualifier, limit, invalidSkip))
.to.be.rejectedWith(`skip must be a non-negative number`);

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((personTypeQualifier))).to.be.true;
expect(getPage.notCalled).to.be.true;
});
});

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

0 comments on commit 0ba0b73

Please sign in to comment.