Skip to content

Commit

Permalink
Merge pull request #114 from EyeSeeTea/development
Browse files Browse the repository at this point in the history
  • Loading branch information
SferaDev authored Nov 3, 2021
2 parents d18235b + 48edae1 commit a61063f
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@eyeseetea/d2-api",
"description": "Typed wrapper over DHIS2 API",
"version": "1.9.2",
"version": "1.9.3",
"license": "GPL-3.0",
"author": "EyeSeeTea team",
"repository": {
Expand Down
8 changes: 7 additions & 1 deletion src/api/d2Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import { MessageConversations } from "./messageConversations";
import { Metadata } from "./metadata";
import { Model } from "./model";
import { Sharing } from "./sharing";
import { SqlViews } from "./SqlViews";
import { System } from "./system";
import { TrackedEntityInstances } from "./trackedEntityInstances";
import { D2ApiOptions, D2ApiRequest, IndexedModels } from "./types";
import { SqlViews } from "./SqlViews";

export class D2ApiGeneric {
public baseUrl: string;
Expand Down Expand Up @@ -141,6 +142,11 @@ export abstract class D2ApiVersioned<
return new Events(this);
}

@cache()
get trackedEntityInstances() {
return new TrackedEntityInstances(this);
}

@cache()
get system() {
return new System(this);
Expand Down
2 changes: 2 additions & 0 deletions src/api/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { D2ApiGeneric } from "./d2Api";
import { DataValueSetsPostResponse } from "./dataValues";
import { EventsPostResponse } from "./events";
import { MetadataResponse } from "./metadata";
import { TeiPostResponse } from "./trackedEntityInstances";

export class System {
constructor(public d2Api: D2ApiGeneric) {}
Expand Down Expand Up @@ -209,4 +210,5 @@ export type WaitForResponse = {
DATAVALUE_IMPORT: DataValueSetsPostResponse;
EVENT_IMPORT: EventsPostResponse;
METADATA_IMPORT: MetadataResponse;
TEI_IMPORT: TeiPostResponse;
};
197 changes: 197 additions & 0 deletions src/api/trackedEntityInstances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import { Id, Pager } from "./base";
import { AsyncPostResponse, D2ApiResponse, HttpResponse } from "./common";
import { D2ApiGeneric } from "./d2Api";

export class TrackedEntityInstances {
constructor(public d2Api: D2ApiGeneric) {}

get(params: TeiGetRequest): D2ApiResponse<PaginatedTeiGetResponse> {
return this.d2Api.get<PaginatedTeiGetResponse>("/trackedEntityInstances", {
...params,
ou: params.ou ? params.ou.join(";") : undefined,
paging: true,
});
}

getAll(params: TeiGetRequest): D2ApiResponse<TeiGetResponse> {
return this.d2Api.get<TeiGetResponse>("/trackedEntityInstances", {
...params,
ou: params.ou ? params.ou.join(";") : undefined,
paging: false,
page: undefined,
pageSize: undefined,
});
}

post(
params: TeiPostParams,
request: TeiPostRequest
): D2ApiResponse<HttpResponse<TeiPostResponse>> {
return this.d2Api.post<HttpResponse<TeiPostResponse>>(
"/trackedEntityInstances",
{ ...params, async: false },
request
);
}

postAsync(
params: TeiPostParams,
request: TeiPostRequest
): D2ApiResponse<AsyncPostResponse<"TEI_IMPORT">> {
return this.d2Api.post<AsyncPostResponse<"TEI_IMPORT">>(
"/trackedEntityInstances",
{ ...params, async: true },
request
);
}
}

export interface TrackedEntityInstance {
trackedEntityInstance: Id;
trackedEntityType: Id;
inactive: boolean;
orgUnit: Id;
attributes: Attribute[];
enrollments: Enrollment[];
relationships: Relationship[];
}

export interface Relationship {
relationship: Id;
relationshipType: Id;
relationshipName: string;
from: RelationshipItem;
to: RelationshipItem;
}

export interface RelationshipItem {
trackedEntityInstance?: {
trackedEntityInstance: Id;
};
event?: { event: Id };
}

export interface Enrollment {
enrollment: Id;
program: Id;
orgUnit: Id;
enrollmentDate: string;
incidentDate: string;
events?: Event[];
}

export interface AttributeValue {
attribute: Attribute;
value: string;
optionId?: Id;
}

export interface Attribute {
attribute: Id;
valueType?: string;
value: string;
}

export type TeiOuRequest =
| { ouMode?: "ACCESSIBLE" | "CAPTURE" | "ALL"; ou?: never[] }
| { ouMode?: "SELECTED" | "CHILDREN" | "DESCENDANTS"; ou: Id[] };

export type TeiGetRequest = TeiOuRequest & {
// Program and tracked entity type cannot be specified simultaneously
program?: Id;
trackedEntityType?: Id;
programStatus?: "ACTIVE" | "COMPLETED" | "CANCELLED";
followUp?: boolean;
order?: string;
pageSize?: number;
page?: number;
totalPages: true;
fields?: string; // TODO: Add inference
programStartDate?: string;
programEndDate?: string;
lastUpdatedStartDate?: string;
lastUpdatedEndDate?: string;
lastUpdatedDuration?: string;
assignedUserMode?: "CURRENT" | "PROVIDED" | "NONE" | "ANY";
trackedEntityInstance?: string;
includeDeleted?: boolean;
};

export interface TeiGetResponse {
trackedEntityInstances: TrackedEntityInstance[];
}

export interface PaginatedTeiGetResponse extends TeiGetResponse {
pager: Pager;
}

export interface TeiPostRequest {
trackedEntityInstances: Array<{
trackedEntityInstance: Id;
trackedEntityType: Id;
inactive?: boolean;
orgUnit: Id;
attributes: Attribute[];
enrollments: Enrollment[];
relationships: Relationship[];
}>;
}

export type TeiPostParams = Partial<{
idScheme: string;
dataElementIdScheme: string;
orgUnitIdScheme: string;
skipNotifications: boolean;
skipFirst: boolean;
strategy: "CREATE" | "UPDATE" | "CREATE_AND_UPDATE" | "DELETE";
importReportMode: "FULL" | "ERRORS" | "DEBUG";
async: boolean;
dryRun: boolean;
}>;

export interface TeiPostResponse {
responseType: "ImportSummaries";
status: "ERROR" | "SUCCESS";
imported: number;
updated: number;
deleted: number;
ignored: number;
total: number;
importSummaries?: Array<
| {
responseType: "ImportSummary";
status: "ERROR";
reference?: string;
conflicts: Array<{
object: string;
value: string;
}>;
importCount: {
imported: number;
updated: number;
ignored: number;
deleted: number;
};
}
| {
responseType: "ImportSummary";
status: "SUCCESS";
reference: string;
importCount: {
imported: number;
updated: number;
ignored: number;
deleted: number;
};
enrollments: {
responseType: "ImportSummaries";
status: "ERROR" | "SUCCESS";
imported: number;
updated: number;
deleted: number;
ignored: number;
total: number;
};
}
>;
}

0 comments on commit a61063f

Please sign in to comment.