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 branch 'development' into feat/create-text-editor-field
- Loading branch information
Showing
38 changed files
with
1,440 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { D2Api } from "../../types/d2-api"; | ||
import { ResourceRepository } from "../../domain/repositories/ResourceRepository"; | ||
import { DataStoreClient } from "../DataStoreClient"; | ||
import { isExistingResource, Resource } from "../../domain/entities/resources/Resource"; | ||
import { FutureData } from "../api-futures"; | ||
import { Future } from "../../domain/entities/generic/Future"; | ||
import { Id } from "../../domain/entities/Ref"; | ||
|
||
const RESOURCES_KEY = "resources"; | ||
|
||
export class ResourceD2Repository implements ResourceRepository { | ||
private dataStoreClient: DataStoreClient; | ||
|
||
constructor(private api: D2Api) { | ||
this.dataStoreClient = new DataStoreClient(api); | ||
} | ||
|
||
getAllResources(): FutureData<Resource[]> { | ||
return this.dataStoreClient | ||
.getObject<Resource[]>(RESOURCES_KEY) | ||
.flatMap(resources => Future.success(resources ?? [])); | ||
} | ||
|
||
saveResource(resource: Resource): FutureData<void> { | ||
if (!resource) return Future.error(new Error("No resource form data found")); | ||
|
||
return this.getAllResources().flatMap(resourcesInDataStore => { | ||
const updatedResources = this.getResourcesToSave(resourcesInDataStore, resource); | ||
|
||
return this.dataStoreClient.saveObject<Resource[]>(RESOURCES_KEY, updatedResources); | ||
}); | ||
} | ||
|
||
private getResourcesToSave(resourcesInDataStore: Resource[], resource: Resource) { | ||
const isResourceExisting = isExistingResource(resourcesInDataStore, resource); | ||
|
||
const updatedResources = isResourceExisting | ||
? resourcesInDataStore.map(resourceInDataStore => | ||
isResourceExisting ? resource : resourceInDataStore | ||
) | ||
: [...resourcesInDataStore, resource]; | ||
return updatedResources; | ||
} | ||
|
||
deleteResource(fileId: Id): FutureData<void> { | ||
return this.getAllResources().flatMap(resources => { | ||
const updatedResources = resources.filter( | ||
resource => resource.resourceFileId !== fileId | ||
); | ||
|
||
return this.dataStoreClient.saveObject<Resource[]>(RESOURCES_KEY, updatedResources); | ||
}); | ||
} | ||
} |
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,41 @@ | ||
import { D2Api } from "../../types/d2-api"; | ||
import { ResourceFile } from "../../domain/entities/resources/ResourceFile"; | ||
import { apiToFuture, FutureData } from "../api-futures"; | ||
import { Future } from "../../domain/entities/generic/Future"; | ||
import { Id } from "../../domain/entities/Ref"; | ||
import { ResourceFileRepository } from "../../domain/repositories/ResourceFileRepository"; | ||
|
||
export class ResourceFileD2Repository implements ResourceFileRepository { | ||
constructor(private api: D2Api) {} | ||
|
||
uploadFile(file: File): FutureData<Id> { | ||
return apiToFuture( | ||
this.api.files.upload({ | ||
name: file.name, | ||
data: file, | ||
}) | ||
).flatMap(fileResource => Future.success(fileResource.id)); | ||
} | ||
|
||
downloadFile(fileId: Id): FutureData<ResourceFile> { | ||
if (!fileId) return Future.error(new Error("No file id found")); | ||
|
||
return apiToFuture(this.api.files.get(fileId)) | ||
.map(blob => { | ||
return new File([blob], "file", { type: blob.type }); | ||
}) | ||
.flatMap(file => | ||
Future.success({ | ||
fileId: fileId, | ||
file: file, | ||
}) | ||
); | ||
} | ||
|
||
deleteResourceFile(fileId: Id): FutureData<void> { | ||
return apiToFuture(this.api.files.delete(fileId)).flatMap(response => { | ||
if (response.httpStatus === "OK") return Future.success(undefined); | ||
else return Future.error(new Error("Error while deleting resource file")); | ||
}); | ||
} | ||
} |
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,22 @@ | ||
import { Id } from "@eyeseetea/d2-api"; | ||
import { Future } from "../../../domain/entities/generic/Future"; | ||
import { FutureData } from "../../api-futures"; | ||
import { ResourceFileRepository } from "../../../domain/repositories/ResourceFileRepository"; | ||
import { ResourceFile } from "../../../domain/entities/resources/ResourceFile"; | ||
|
||
export class ResourceFileTestRepository implements ResourceFileRepository { | ||
uploadFile(_file: File): FutureData<Id> { | ||
return Future.success("test-file-id"); | ||
} | ||
|
||
downloadFile(fileId: Id): FutureData<ResourceFile> { | ||
return Future.success({ | ||
fileId: fileId, | ||
file: new File(["test"], "test.txt", { type: "text/plain" }), | ||
}); | ||
} | ||
|
||
deleteResourceFile(_fileId: Id): FutureData<void> { | ||
return Future.success(undefined); | ||
} | ||
} |
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,33 @@ | ||
import { Id } from "@eyeseetea/d2-api"; | ||
import { Future } from "../../../domain/entities/generic/Future"; | ||
import { Resource, ResourceType } from "../../../domain/entities/resources/Resource"; | ||
import { ResourceRepository } from "../../../domain/repositories/ResourceRepository"; | ||
import { FutureData } from "../../api-futures"; | ||
|
||
export class ResourceTestRepository implements ResourceRepository { | ||
getAllResources(): FutureData<Resource[]> { | ||
const resources: Resource[] = [ | ||
{ | ||
resourceLabel: "Incident Action Plan", | ||
resourceType: ResourceType.TEMPLATE, | ||
resourceFileId: "123", | ||
}, | ||
{ | ||
resourceLabel: "Excel line list", | ||
resourceType: ResourceType.RESPONSE_DOCUMENT, | ||
resourceFolder: "Case line lists", | ||
resourceFileId: "456", | ||
}, | ||
]; | ||
|
||
return Future.success(resources); | ||
} | ||
|
||
saveResource(_resource: Resource): FutureData<void> { | ||
return Future.success(undefined); | ||
} | ||
|
||
deleteResource(_fileId: Id): FutureData<void> { | ||
return Future.success(undefined); | ||
} | ||
} |
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
Oops, something went wrong.