Skip to content

Commit

Permalink
fix: Add fields inference to new Tracker entities
Browse files Browse the repository at this point in the history
  • Loading branch information
tokland committed Sep 16, 2024
1 parent 021237a commit fd64b70
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 44 deletions.
14 changes: 8 additions & 6 deletions src/api/trackerEnrollments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { D2ApiGeneric } from "./d2Api";
import { Id, Selector, D2ApiResponse, SelectedPick } from "./base";
import { Preset, FieldPresets } from "../schemas";
import { getFieldsAsString } from "./common";
import { D2TrackerEvent } from "./trackerEvents";
import { D2TrackerEvent, D2TrackerEventSchema } from "./trackerEvents";
import _ from "lodash";

export class TrackerEnrollments {
constructor(public api: D2ApiGeneric) {}

get<Fields extends D2TrackerEnrollmentFields>(
params: TrackerEnrollmentsParams<Fields>
): D2ApiResponse<TrackerEnrollmentsResponse> {
return this.api.get<TrackerEnrollmentsResponse>("/tracker/enrollments", {
): D2ApiResponse<TrackerEnrollmentsResponse<Fields>> {
return this.api.get<TrackerEnrollmentsResponse<Fields>>("/tracker/enrollments", {
..._.omit(params, ["fields"]),
fields: getFieldsAsString(params.fields),
});
Expand Down Expand Up @@ -88,17 +88,19 @@ type TrackerEnrollmentsParamsBase = {
type SemiColonDelimitedListOfUid = string;
type CommaDelimitedListOfUid = string;

export interface TrackerEnrollmentsResponse {
export interface TrackerEnrollmentsResponse<Fields> {
page: number;
pageSize: number;
instances: D2TrackerEnrollment[];
instances: SelectedPick<D2TrackerEnrollmentSchema, Fields>[];
total?: number; // Only if requested with totalPages=true
}

export interface D2TrackerEnrollmentSchema {
name: "D2TrackerEnrollment";
model: D2TrackerEnrollment;
fields: D2TrackerEnrollment;
fields: D2TrackerEnrollment & {
events: D2TrackerEventSchema[];
};
fieldPresets: {
$all: Preset<D2TrackerEnrollment, keyof D2TrackerEnrollment>;
$identifiable: Preset<D2TrackerEnrollment, FieldPresets["identifiable"]>;
Expand Down
38 changes: 19 additions & 19 deletions src/api/trackerEvents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { D2ApiGeneric } from "./d2Api";
import { Id, Selector, D2ApiResponse } from "./base";
import { Id, Selector, D2ApiResponse, SelectedPick } from "./base";
import { Preset, FieldPresets, D2Geometry } from "../schemas";
import { getFieldsAsString } from "./common";
import _ from "lodash";
Expand All @@ -9,8 +9,8 @@ export class TrackerEvents {

get<Fields extends D2TrackerEventFields>(
params: EventsParams<Fields>
): D2ApiResponse<TrackerEventsResponse> {
return this.api.get<TrackerEventsResponse>("/tracker/events", {
): D2ApiResponse<TrackerEventsResponse<Fields>> {
return this.api.get<TrackerEventsResponse<Fields>>("/tracker/events", {
..._.omit(params, ["fields"]),
fields: getFieldsAsString(params.fields),
});
Expand Down Expand Up @@ -50,21 +50,21 @@ interface D2TrackerEventBase {
enrollment?: Id;
enrollmentStatus?: "ACTIVE" | "COMPLETED" | "CANCELLED";
orgUnit: Id;
orgUnitName?: string;
relationships?: [];
orgUnitName: string;
relationships: [];
occurredAt: IsoDate;
scheduledAt?: IsoDate;
storedBy?: Username;
followup?: boolean;
deleted?: boolean;
createdAt?: IsoDate;
updatedAt?: IsoDate;
createdBy?: UserInfo;
attributeOptionCombo?: Id;
attributeCategoryOptions?: Id;
updatedBy?: UserInfo;
scheduledAt: IsoDate;
storedBy: Username;
followup: boolean;
deleted: boolean;
createdAt: IsoDate;
updatedAt: IsoDate;
createdBy: UserInfo;
attributeOptionCombo: Id;
attributeCategoryOptions: Id;
updatedBy: UserInfo;
dataValues: DataValue[];
notes?: [];
notes: [];
trackedEntity?: Id;
}

Expand Down Expand Up @@ -151,14 +151,14 @@ export interface DataValue {
providedElsewhere?: boolean;
}

export interface TrackerEventsResponse {
export interface TrackerEventsResponse<Fields> {
page: number;
pageSize: number;
instances: D2TrackerEvent[];
instances: SelectedPick<D2TrackerEventSchema, Fields>[];
total?: number; // Only if requested with totalPages=true
}

interface D2TrackerEventSchema {
export interface D2TrackerEventSchema {
name: "D2TrackerEvent";
model: D2TrackerEvent;
fields: D2TrackerEvent & {
Expand Down
40 changes: 21 additions & 19 deletions src/api/trackerTrackedEntities.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import _ from "lodash";
import { D2Geometry, FieldPresets, Preset } from "../schemas";
import { Id, Selector } from "./base";
import { Id, Selector, SelectedPick } from "./base";
import { D2ApiResponse, getFieldsAsString } from "./common";
import { D2ApiGeneric } from "./d2Api";
import { D2TrackerEnrollment } from "./trackerEnrollments";
import { D2TrackerEnrollment, D2TrackerEnrollmentSchema } from "./trackerEnrollments";

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

get<Fields extends D2TrackerTrackedEntityFields>(
params: TrackerTrackedEntitiesParams<Fields>
): D2ApiResponse<TrackedEntitiesGetResponse> {
return this.d2Api.get<TrackedEntitiesGetResponse>("/tracker/trackedEntities", {
): D2ApiResponse<TrackedEntitiesGetResponse<Fields>> {
return this.d2Api.get<TrackedEntitiesGetResponse<Fields>>("/tracker/trackedEntities", {
..._.omit(params, ["fields"]),
fields: getFieldsAsString(params.fields),
});
Expand All @@ -25,18 +25,18 @@ type CommaDelimitedListOfUid = string;
type CommaDelimitedListOfAttributeFilter = string;

interface D2TrackerTrackedEntityBase {
trackedEntity?: Id;
trackedEntityType?: Id;
createdAt?: IsoDate;
createdAtClient?: IsoDate;
updatedAt?: IsoDate;
orgUnit?: SemiColonDelimitedListOfUid;
inactive?: boolean;
deleted?: boolean;
relationships?: Relationship[];
attributes?: Attribute[];
enrollments?: D2TrackerEnrollment[];
programOwners?: ProgramOwner[];
trackedEntity: Id;
trackedEntityType: Id;
createdAt: IsoDate;
createdAtClient: IsoDate;
updatedAt: IsoDate;
orgUnit: SemiColonDelimitedListOfUid;
inactive: boolean;
deleted: boolean;
relationships: Relationship[];
attributes: Attribute[];
enrollments: D2TrackerEnrollment[];
programOwners: ProgramOwner[];
}

export type D2TrackerTrackedEntity = TrackedEntityGeometryPoint | TrackedEntityGeometryPolygon;
Expand Down Expand Up @@ -131,17 +131,19 @@ export type TrackedEntitiesParamsBase = {
potentialDuplicate: boolean;
};

export interface TrackedEntitiesGetResponse {
export interface TrackedEntitiesGetResponse<Fields> {
page: number;
pageSize: number;
instances: D2TrackerTrackedEntity[];
instances: SelectedPick<D2TrackerTrackedEntitySchema, Fields>[];
total?: number; // Only if requested with totalPages=true
}

export interface D2TrackerTrackedEntitySchema {
name: "D2TrackerTrackedEntity";
model: D2TrackerTrackedEntity;
fields: D2TrackerTrackedEntity;
fields: D2TrackerTrackedEntity & {
enrollments: D2TrackerEnrollmentSchema[];
};
fieldPresets: {
$all: Preset<D2TrackerTrackedEntity, keyof D2TrackerTrackedEntity>;
$identifiable: Preset<D2TrackerTrackedEntity, FieldPresets["identifiable"]>;
Expand Down

0 comments on commit fd64b70

Please sign in to comment.