generated from EyeSeeTea/dhis2-app-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from EyeSeeTea/feat/org-unit-selector
feat: Add Org Units filter
- Loading branch information
Showing
25 changed files
with
739 additions
and
927 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import _ from "lodash"; | ||
import { FutureData } from "../../domain/entities/Future"; | ||
import { OrgUnit } from "../../domain/entities/OrgUnit"; | ||
import { Id } from "../../domain/entities/Ref"; | ||
import { OrgUnitRepository } from "../../domain/repositories/OrgUnitRepository"; | ||
import { D2Api, MetadataPick } from "../../types/d2-api"; | ||
import { apiToFuture } from "../../utils/futures"; | ||
|
||
const orgUnitFields = { id: true, displayName: true, path: true, level: true } as const; | ||
|
||
type D2OrgUnit = MetadataPick<{ | ||
organisationUnits: { fields: typeof orgUnitFields }; | ||
}>["organisationUnits"][number]; | ||
|
||
export class OrgUnitD2Repository implements OrgUnitRepository { | ||
constructor(private api: D2Api) {} | ||
|
||
public getOrgUnitRoots(): FutureData<OrgUnit[]> { | ||
return apiToFuture( | ||
this.api.models.organisationUnits.get({ | ||
paging: false, | ||
filter: { level: { eq: "1" } }, | ||
fields: orgUnitFields, | ||
}) | ||
).map(orgUnits => { | ||
return orgUnits.objects.map(d2OrgUnit => this.convertToOrgUnit(d2OrgUnit)); | ||
}); | ||
} | ||
|
||
public getOrgUnitsByIds(ids: Id[]): FutureData<OrgUnit[]> { | ||
return apiToFuture( | ||
this.api.models.organisationUnits.get({ | ||
paging: false, | ||
fields: orgUnitFields, | ||
filter: { id: { in: ids } }, | ||
}) | ||
).map(orgUnits => { | ||
return orgUnits.objects.map(d2OrgUnit => this.convertToOrgUnit(d2OrgUnit)); | ||
}); | ||
} | ||
|
||
private convertToOrgUnit(orgUnitResponse: D2OrgUnit): OrgUnit { | ||
return { | ||
..._.omit(orgUnitResponse, ["displayName"]), | ||
name: orgUnitResponse.displayName, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import _ from "lodash"; | ||
import { Id } from "./Ref"; | ||
|
||
export type OrgUnitPath = string; | ||
|
||
export interface OrgUnit { | ||
id: Id; | ||
path: OrgUnitPath; | ||
name: string; | ||
level: number; | ||
} | ||
|
||
const pathSeparator = "/"; | ||
|
||
export function getIdFromPath(path: OrgUnitPath): Id { | ||
return _.last(path.split(pathSeparator)) as Id; | ||
} | ||
|
||
export function getOrgUnitParentPath(path: OrgUnitPath) { | ||
return _(path).split(pathSeparator).initial().join(pathSeparator); | ||
} |
Oops, something went wrong.