Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#9193): add functionality of getting places as pages or async iterables in cht-datasource #9368

Merged
merged 12 commits into from
Sep 2, 2024

Conversation

sugat009
Copy link
Member

@sugat009 sugat009 commented Aug 23, 2024

Description

Added functionality of getting places using pages or as AsyncGenerator using cht-datasource along with the addition of REST endpoint /api/v1/person which provides people as pages.

Issues: #9239 , #9240 , #9242

Usage:

  1. Pagination API
Place.v1.getPageByType(Qualifier.byContactType(placeType), cursor, limit); // limit and cursor are optional with defaults as 100 and "0" respectively
  1. Async Generator API
const getAllIterator = Place.v1.getByType(ctx)(Qualifier.byContactType('place'));
  1. REST API endpoint
HTTP GET api/v1/place

Query Params:

  • placeType - the contact_type of the place to fetch
  • limit - default is 100
  • cursor - default is 0

Code review checklist

  • Readable: Concise, well named, follows the style guide, documented if necessary.
  • Documented: Configuration and user documentation on cht-docs
  • Tested: Unit and/or e2e where appropriate

Compose URLs

If Build CI hasn't passed, these may 404:

License

The software is provided under AGPL-3.0. Contributions to this project are accepted under the same license.

@sugat009 sugat009 marked this pull request as ready for review August 26, 2024 13:11
@sugat009 sugat009 requested a review from jkuester August 26, 2024 13:11
@sugat009 sugat009 self-assigned this Aug 26, 2024
@sugat009 sugat009 added the Type: Feature Add something new label Aug 26, 2024
Copy link
Contributor

@jkuester jkuester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! I made it though all the implementation code and unit tests (just need to finish looking at the integration tests). But here are some minor comments I have so far!

shared-libs/cht-datasource/src/index.ts Outdated Show resolved Hide resolved
shared-libs/cht-datasource/src/index.ts Outdated Show resolved Hide resolved
shared-libs/cht-datasource/src/libs/core.ts Outdated Show resolved Hide resolved
shared-libs/cht-datasource/src/libs/core.ts Outdated Show resolved Hide resolved
Comment on lines 177 to 187
export const fetchAndFilter = (
getFunction: (limit: number, skip: number) => Promise<Nullable<Doc>[]>,
filterFunction: (settings: SettingsService, doc: Nullable<Doc>, uuid: string | undefined) => unknown,
settings: SettingsService,
limit: number,
): typeof recursionInner => {
const recursionInner = async (
currentLimit: number,
currentSkip: number,
currentDocs: Nullable<Doc>[] = [],
): Promise<Page<unknown>> => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to the wonders of generic types, we can do something like this:

Suggested change
export const fetchAndFilter = (
getFunction: (limit: number, skip: number) => Promise<Nullable<Doc>[]>,
filterFunction: (settings: SettingsService, doc: Nullable<Doc>, uuid: string | undefined) => unknown,
settings: SettingsService,
limit: number,
): typeof recursionInner => {
const recursionInner = async (
currentLimit: number,
currentSkip: number,
currentDocs: Nullable<Doc>[] = [],
): Promise<Page<unknown>> => {
export const fetchAndFilter = <T extends Doc> (
getFunction: (limit: number, skip: number) => Promise<Nullable<Doc>[]>,
filterFunction: (settings: SettingsService, doc: Nullable<Doc>, uuid: string | undefined) => doc is T,
settings: SettingsService,
limit: number,
): typeof recursionInner => {
const recursionInner = async (
currentLimit: number,
currentSkip: number,
currentDocs: T[] = [],
): Promise<Page<T>> => {

And then we do not have to have the as Page<Place.v1.Place> assertions in the person/place code! The only other required change is that we update the filter call below to be explicit about the assertion: const newDocs = docs.filter((doc): doc is T => filterFunction(settings, doc, doc?._id));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this would eliminate the use of unknown in the function, I couldn't get this to work without the as Page<Place.v1.Place> assertions because <T extends Doc> would mean they have properties from Doc but Place and Person have additional properties for which errors are thrown in other parts of the code.

shared-libs/cht-datasource/src/local/person.ts Outdated Show resolved Hide resolved
shared-libs/cht-datasource/test/local/place.spec.ts Outdated Show resolved Hide resolved
shared-libs/cht-datasource/test/local/place.spec.ts Outdated Show resolved Hide resolved
api/src/routing.js Show resolved Hide resolved
api/src/controllers/place.js Outdated Show resolved Hide resolved
@sugat009 sugat009 requested a review from jkuester August 29, 2024 15:10
@jkuester jkuester requested a review from tatilepizs August 29, 2024 21:50
Copy link
Contributor

@jkuester jkuester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! This should be good to go! 🚀

shared-libs/cht-datasource/.eslintrc.js Outdated Show resolved Hide resolved
tests/integration/api/controllers/place.spec.js Outdated Show resolved Hide resolved
@sugat009 sugat009 merged commit 09dc817 into master Sep 2, 2024
44 checks passed
@sugat009 sugat009 deleted the 9193-api-endpoints-for-getting-contacts-by-type branch September 2, 2024 12:07
@sugat009 sugat009 modified the milestone: 4.12.0 Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature Add something new
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants