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.
fix: refactor, abstract patterns, avoid as usage, coding stds
- Loading branch information
Showing
18 changed files
with
351 additions
and
230 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
import { D2Api } from "@eyeseetea/d2-api/2.36"; | ||
import { Code, NamedRef } from "../../domain/entities/Ref"; | ||
import { D2Api } from "../../types/d2-api"; | ||
import { Code, Option } from "../../domain/entities/Ref"; | ||
import { apiToFuture, FutureData } from "../api-futures"; | ||
import { OptionsRepository } from "../../domain/repositories/OptionsRepository"; | ||
import { Future } from "../../domain/entities/generic/Future"; | ||
import { assertOrError } from "./utils/AssertOrError"; | ||
|
||
export class OptionsD2Repository implements OptionsRepository { | ||
constructor(private api: D2Api) {} | ||
|
||
get(code: Code): FutureData<NamedRef> { | ||
if (!code) return Future.success({ id: "", name: "" }); | ||
get(code: Code): FutureData<Option> { | ||
return apiToFuture( | ||
this.api.metadata.get({ | ||
options: { fields: { code: true, name: true }, filter: { code: { eq: code } } }, | ||
}) | ||
).map(response => { | ||
if (!response.options[0]) throw new Error("Option not found"); | ||
const option: NamedRef = { | ||
id: response.options[0].code, | ||
name: response.options[0].name, | ||
}; | ||
return option; | ||
}); | ||
) | ||
.flatMap(response => assertOrError(response.options[0], "Option")) | ||
.map(d2Option => { | ||
const option: Option = { | ||
id: d2Option.code, | ||
name: d2Option.name, | ||
}; | ||
return option; | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { Future } from "../../../domain/entities/generic/Future"; | ||
import { Id, NamedRef } from "../../../domain/entities/Ref"; | ||
import { Id, Option } from "../../../domain/entities/Ref"; | ||
import { OptionsRepository } from "../../../domain/repositories/OptionsRepository"; | ||
import { FutureData } from "../../api-futures"; | ||
|
||
export class OptionsTestRepository implements OptionsRepository { | ||
get(id: Id): FutureData<NamedRef> { | ||
get(id: Id): FutureData<Option> { | ||
return Future.success({ id: id, name: "Test Option" }); | ||
} | ||
} |
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,7 @@ | ||
import { Future } from "../../../domain/entities/generic/Future"; | ||
import { FutureData } from "../../api-futures"; | ||
|
||
export function assertOrError<T>(objects: T, name: string): FutureData<NonNullable<T>> { | ||
if (!objects) return Future.error(new Error(`${name} not found`)); | ||
else return Future.success(objects); | ||
} |
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,3 @@ | ||
export function getCurrentTimeString(): string { | ||
return new Date().getTime().toString(); | ||
} |
Oops, something went wrong.