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

[fix] [new tracker] Add fields inference to new Tracker entities #151

Open
wants to merge 19 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18.14.2
21 changes: 19 additions & 2 deletions src/api/d2Api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { AxiosHttpClientRepository } from "../data/AxiosHttpClientRepository";
import { FetchHttpClientRepository } from "../data/FetchHttpClientRepository";
import { HttpClientRepository, HttpRequest } from "../repositories/HttpClientRepository";
import {
HttpClientRepository,
HttpRequest,
HttpClientResponse,
} from "../repositories/HttpClientRepository";
import { D2SchemaProperties } from "../schemas";
import { cache, defineLazyCachedProperty } from "../utils/cache";
import { joinPath } from "../utils/connection";
import { Analytics } from "./analytics";
import { AppHub } from "./appHub";
import { Audit } from "./audit";
import { D2ApiDefinitionBase, D2ApiResponse, Params } from "./common";
import { D2ApiDefinitionBase, D2ApiResponse, Params, HttpResponse } from "./common";
import { CurrentUser } from "./currentUser";
import { DataStore } from "./dataStore";
import { DataValues } from "./dataValues";
Expand Down Expand Up @@ -221,4 +225,17 @@ export abstract class D2ApiVersioned<
}
}

/* Starting at 2.38, some POST/PUT endpoints return a wrapped HTTP response.
* See https://github.com/dhis2/dhis2-releases/blob/master/releases/2.38/README.md#api
*
* Instead of checking the API version, inspect the structure of the response.
**/
export function unwrap<T>(res: HttpClientResponse<T | HttpResponse<T>>): T {
const { data } = res;

return typeof data === "object" && data && "httpStatus" in data && "response" in data
? (data as { response: T }).response
: (data as T);
}

export { D2ApiOptions };
2 changes: 1 addition & 1 deletion src/api/dataStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { D2ApiResponse, HttpResponse, validate2xx, validate404 } from "./common";
import { HttpResponse as HttpClientResponse } from "../repositories/HttpClientRepository";
import { HttpClientResponse } from "../repositories/HttpClientRepository";
import { D2ApiGeneric } from "./d2Api";

type UpdateResponse = HttpResponse<undefined>;
Expand Down
20 changes: 12 additions & 8 deletions src/api/dataValues.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import { Id } from "../schemas";
import { AsyncPostResponse, D2ApiResponse, HttpResponse } from "./common";
import { D2ApiGeneric } from "./d2Api";
import { D2ApiGeneric, unwrap } from "./d2Api";

export interface DataValueSetsPostRequest {
dataSet?: Id;
Expand Down Expand Up @@ -171,21 +171,25 @@ export class DataValues {
params: DataValueSetsPostParams,
request: DataValueSetsPostRequest
): D2ApiResponse<DataValueSetsPostResponse> {
return this.d2Api.post<DataValueSetsPostResponse>(
"/dataValueSets",
{ ...params, async: false },
request
);
return this.d2Api
.post<DataValueSetsPostResponse | HttpResponse<DataValueSetsPostResponse>>(
"/dataValueSets",
{ ...params, async: false },
request
)
.map(unwrap);
}

postSetAsync(
params: DataValueSetsPostParams,
request: DataValueSetsPostRequest
): D2ApiResponse<AsyncPostResponse<"DATAVALUE_IMPORT">> {
return this.d2Api.post<AsyncPostResponse<"DATAVALUE_IMPORT">>(
): D2ApiResponse<DataValueImport> {
return this.d2Api.post<DataValueImport>(
"/dataValueSets",
{ ...params, async: true },
request
);
}
}

type DataValueImport = AsyncPostResponse<"DATAVALUE_IMPORT">;
24 changes: 14 additions & 10 deletions src/api/events.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
AsyncPostResponse,
D2ApiResponse,
HttpResponse,
getFieldsAsString,
validate2xx,
HttpResponse,
} from "./common";
import { D2ApiGeneric } from "./d2Api";
import { D2ApiGeneric, unwrap } from "./d2Api";
import { Pager } from "./model";
import { Selector, SelectedPick } from ".";
import { Preset, FieldPresets, Id } from "../schemas";
Expand Down Expand Up @@ -36,14 +36,18 @@ export class Events {
});
}

post(
params: EventsPostParams,
request: EventsPostRequest
): D2ApiResponse<HttpResponse<EventsPostResponse>> {
return this.d2Api.post<HttpResponse<EventsPostResponse>>("/events", params, request, {
// The API returns 409 when there are warning but the POST was performed, so consider it valid
validateStatus: status => validate2xx(status) || status == 409,
});
post(params: EventsPostParams, request: EventsPostRequest): D2ApiResponse<EventsPostResponse> {
return this.d2Api
.post<EventsPostResponse | HttpResponse<EventsPostResponse>>(
"/events",
params,
request,
{
// The API returns 409 when there are warning but the POST was performed, so consider it valid
validateStatus: status => validate2xx(status) || status == 409,
}
)
.map(unwrap);
}

postAsync(
Expand Down
11 changes: 8 additions & 3 deletions src/api/files.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import FormData from "form-data";
import { generateUid } from "../utils/uid";
import { Id, MetadataResponse } from "./base";
import { D2ApiResponse, ErrorReport } from "./common";
import { D2ApiGeneric } from "./d2Api";
import { D2ApiResponse, ErrorReport, HttpResponse } from "./common";
import { D2ApiGeneric, unwrap } from "./d2Api";

interface FileHttpResponse<Response> {
httpStatus: "OK" | "Conflict" | "Not Found";
Expand Down Expand Up @@ -100,7 +100,12 @@ export class Files {
const document = { id, name, url: fileResourceId };

return this.d2Api
.post<MetadataResponse>("/metadata", {}, { documents: [document] })
.post<MetadataResponse | HttpResponse<MetadataResponse>>(
"/metadata",
{},
{ documents: [document] }
)
.map(unwrap)
.map(({ data }) => ({ id, fileResourceId, response: data }));
});
}
Expand Down
19 changes: 11 additions & 8 deletions src/api/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
MetadataPayloadBase,
Params,
processFieldsFilterParams,
HttpResponse,
} from "./common";
import { D2ApiGeneric } from "./d2Api";
import { D2ApiGeneric, unwrap } from "./d2Api";
import { GetFields, SelectedPick } from "./inference";

export interface PostOptions {
Expand Down Expand Up @@ -103,13 +104,15 @@ export class Metadata<D2ApiDefinition extends D2ApiDefinitionBase> {
data: Partial<MetadataPayloadBase<D2ApiDefinition["schemas"]>>,
options: Partial<PostOptions> = {}
): D2ApiResponse<MetadataResponse> {
return this.d2Api.request({
method: "post",
url: "/metadata",
params: { ...options, async: false } as Params,
data,
validateStatus: (status: number) => status >= 200 && status < 300,
});
return this.d2Api
.request<MetadataResponse | HttpResponse<MetadataResponse>>({
method: "post",
url: "/metadata",
params: { ...options, async: false } as Params,
data,
validateStatus: (status: number) => status >= 200 && status < 300,
})
.map(unwrap);
}

postAsync(
Expand Down
30 changes: 20 additions & 10 deletions src/api/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {
PartialPersistedModel,
processFieldsFilterParams,
} from "./common";
import { D2ApiGeneric } from "./d2Api";
import { D2ApiGeneric, unwrap } from "./d2Api";
import { D2ModelSchemaBase, GetFields, SelectedPick } from "./inference";

type ModelResponse = HttpResponse<{
type ModelResponse = {
responseType: "ObjectReport";
uid: string;
klass: string;
errorReports?: ErrorReport[];
}>;
};

export interface Pager {
page: number;
Expand Down Expand Up @@ -112,21 +112,31 @@ export class Model<
payload: PartialModel<D2ModelSchema["model"]>,
options?: Partial<UpdateOptions>
): D2ApiResponse<ModelResponse> {
return this.d2Api.post(this.modelName, (options || {}) as Params, payload as object);
return this.d2Api
.post<ModelResponse | HttpResponse<ModelResponse>>(
this.modelName,
(options || {}) as Params,
payload as object
)
.map(unwrap);
}

put(
payload: PartialPersistedModel<D2ModelSchema["model"]>,
options?: Partial<UpdateOptions>
): D2ApiResponse<ModelResponse> {
return this.d2Api.put(
[this.modelName, payload.id].join("/"),
(options || {}) as Params,
payload
);
return this.d2Api
.put<ModelResponse | HttpResponse<ModelResponse>>(
[this.modelName, payload.id].join("/"),
(options || {}) as Params,
payload
)
.map(unwrap);
}

delete(payload: PartialPersistedModel<D2ModelSchema["model"]>): D2ApiResponse<ModelResponse> {
return this.d2Api.delete(`/${this.modelName}/${payload.id}`);
return this.d2Api
.delete<ModelResponse | HttpResponse<ModelResponse>>(`/${this.modelName}/${payload.id}`)
.map(unwrap);
}
}
8 changes: 2 additions & 6 deletions src/api/sharing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { D2ApiGeneric } from "./d2Api";
import { D2ApiResponse } from "./common";
import { PartialBy } from "../utils/types";
import { HttpResponse } from "../repositories/HttpClientRepository";

/* https://docs.dhis2.org/master/en/developer/html/webapi_sharing.html */

Expand All @@ -16,11 +15,8 @@ export class Sharing {
return this.d2Api.get<SharingGetResponse>("/sharing", params);
}

post(
params: SharingParams,
object: SharingObjectUpdate
): D2ApiResponse<HttpResponse<undefined>> {
return this.d2Api.post<HttpResponse<undefined>>("/sharing", params, { object });
post(params: SharingParams, object: SharingObjectUpdate): D2ApiResponse<void> {
return this.d2Api.post<void>("/sharing", params, { object });
}
}

Expand Down
19 changes: 9 additions & 10 deletions src/api/trackedEntityInstances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { D2Geometry, D2ProgramOwner } from "../schemas";
import { PartialBy } from "../utils/types";
import { Id, Pager } from "./base";
import { AsyncPostResponse, D2ApiResponse, HttpResponse } from "./common";
import { D2ApiGeneric } from "./d2Api";
import { D2ApiGeneric, unwrap } from "./d2Api";
import { D2Event, D2EventToPost } from "./events";

export class TrackedEntityInstances {
Expand All @@ -26,15 +26,14 @@ export class TrackedEntityInstances {
});
}

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

postAsync(
Expand Down
13 changes: 6 additions & 7 deletions src/api/tracker.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { cache } from "../utils/cache";
import { D2TrackerEnrollment, TrackerEnrollments } from "./trackerEnrollments";
import { TrackerEnrollments, D2TrackerEnrollmentToPost } from "./trackerEnrollments";
import { D2ApiResponse } from "./base";
import { AsyncPostResponse } from "./common";
import { D2ApiGeneric } from "./d2Api";
import { D2TrackerEvent, TrackerEvents } from "./trackerEvents";
import { TrackedEntities } from "./trackerTrackedEntities";
import { D2TrackerTrackedEntity } from "./trackerTrackedEntities";
import { TrackerEvents, D2TrackerEventToPost } from "./trackerEvents";
import { TrackedEntities, D2TrackedEntityInstanceToPost } from "./trackerTrackedEntities";

export class Tracker {
constructor(public d2Api: D2ApiGeneric) {}
Expand Down Expand Up @@ -47,9 +46,9 @@ export class Tracker {
}

export interface TrackerPostRequest {
trackedEntities?: D2TrackerTrackedEntity[];
enrollments?: D2TrackerEnrollment[];
events?: D2TrackerEvent[];
trackedEntities?: D2TrackedEntityInstanceToPost[];
enrollments?: D2TrackerEnrollmentToPost[];
events?: D2TrackerEventToPost[];
}

type SchemeOptions = "UID" | "CODE" | "NAME" | "ATTRIBUTE";
Expand Down
Loading