Skip to content

Commit

Permalink
add async API Contact.v1.getIdsAll
Browse files Browse the repository at this point in the history
  • Loading branch information
sugat009 committed Nov 15, 2024
1 parent 6f5c326 commit 192af16
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
70 changes: 58 additions & 12 deletions shared-libs/cht-datasource/src/contact.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { Doc } from './libs/doc';
import {
assertCursor, assertFreetextQualifier,
assertLimit, assertTypeQualifier,
assertCursor,
assertFreetextQualifier,
assertLimit,
assertTypeQualifier,
DataObject,
getPagedGenerator,
Identifiable,
isDataObject,
isIdentifiable,
Nullable,
Page,
} from './libs/core';
import { ContactTypeQualifier, FreetextQualifier, isUuidQualifier, UuidQualifier } from './qualifier';
import {
byContactType,
byFreetext,
ContactTypeQualifier,
FreetextQualifier,
isUuidQualifier,
UuidQualifier
} from './qualifier';
import { adapt, assertDataContext, DataContext } from './libs/data-context';
import { LocalDataContext } from './local/libs/data-context';
import { RemoteDataContext } from './remote/libs/data-context';
Expand Down Expand Up @@ -61,6 +71,27 @@ export namespace v1 {
}
};

/** @internal */
export const createQualifier = (
freetext: Nullable<string> = null,
type: Nullable<string> = null
): ContactTypeQualifier | FreetextQualifier => {
if (!freetext && !type) {
throw new InvalidArgumentError('Either "freetext" or "type" is required');
}

const qualifier = {};
if (freetext) {
Object.assign(qualifier, byFreetext(freetext));
}

if (type) {
Object.assign(qualifier, byContactType(type));
}

return qualifier as ContactTypeQualifier | FreetextQualifier;
};

const getContact =
<T>(
localFn: (c: LocalDataContext) => (qualifier: UuidQualifier) => Promise<T>,
Expand Down Expand Up @@ -151,13 +182,28 @@ export namespace v1 {
* @returns a function for getting a generator that fetches contact identifiers
* @throws Error if a data context is not provided
*/
/**
* Returns a generator for fetching all contact identifiers that match the given qualifier
* @param qualifier the limiter defining which identifiers to return
* @returns a generator for fetching all contact identifiers that match the given qualifier
* @throws Error if no qualifier is provided or if the qualifier is invalid
*/
// const getIdsAll = (context: DataContext) => (
// qualifier: ContactTypeQualifier | FreetextQualifier
// ) => AsyncGenerator<string, null>;
export const getIdsAll = (context: DataContext): typeof curriedGen => {
assertDataContext(context);
const getPage = context.bind(v1.getIdsPage);

/**
* Returns a generator for fetching all contact identifiers that match the given qualifier
* @param qualifier the limiter defining which identifiers to return
* @returns a generator for fetching all contact identifiers that match the given qualifier
* @throws Error if no qualifier is provided or if the qualifier is invalid
*/
const curriedGen = (
qualifier: ContactTypeQualifier | FreetextQualifier
): AsyncGenerator<string, null> => {
if (isContactType(qualifier)) {
assertTypeQualifier(qualifier);
}

if (isFreetextType(qualifier)) {
assertFreetextQualifier(qualifier);
}
return getPagedGenerator(getPage, qualifier);
};
return curriedGen;
};
}
15 changes: 11 additions & 4 deletions shared-libs/cht-datasource/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ export const getDatasource = (ctx: DataContext) => {
getByUuidWithLineage: (uuid: string) => ctx.bind(Contact.v1.getWithLineage)(Qualifier.byUuid(uuid)),

/** TODO */
getPageByType: (
qualifier: string,
getIdsPage: (
freetext: Nullable<string> = null,
type: Nullable<string> = null,
cursor: Nullable<string> = null,
limit = 100
) => ctx.bind(Contact.v1.getIdsPage)(
Qualifier.byFreetext(qualifier), cursor, limit
Contact.v1.createQualifier(freetext, type), cursor, limit
),

/** TODO */
getIds: (
freetext: Nullable<string> = null,
type: Nullable<string> = null
) => ctx.bind(Contact.v1.getIdsAll)(Contact.v1.createQualifier(freetext, type)),
},
place: {
/**
Expand Down Expand Up @@ -169,7 +176,7 @@ export const getDatasource = (ctx: DataContext) => {
getByUuid: (uuid: string) => ctx.bind(Report.v1.get)(Qualifier.byUuid(uuid)),

/** TODO */
getPageByType: (
getIdsPage: (
qualifier: string,
cursor: Nullable<string> = null,
limit = 100
Expand Down

0 comments on commit 192af16

Please sign in to comment.