-
Notifications
You must be signed in to change notification settings - Fork 1
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
add sharing step #472
add sharing step #472
Changes from 7 commits
330badf
85d8b17
1a1bc02
f9a8c05
d85d170
8ee1c9c
61c7841
1f048b7
e39eb53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { apiToFuture } from "$/data/api-futures"; | ||
import { metadataCodes } from "$/data/repositories/D2ApiMetadata"; | ||
import { Config, UserGroup } from "$/domain/entities/Config"; | ||
import { Region } from "$/domain/entities/Region"; | ||
import { Future, FutureData } from "$/domain/entities/generic/Future"; | ||
import { ConfigRepository } from "$/domain/repositories/ConfigRepository"; | ||
import { D2Api } from "$/types/d2-api"; | ||
|
||
export class ConfigD2Repository implements ConfigRepository { | ||
constructor(private api: D2Api) {} | ||
|
||
get(): FutureData<Config> { | ||
return this.getOrgUnitLevelGroup().flatMap(orgUnitLevel => { | ||
return Future.joinObj({ | ||
regions: this.getRegions(orgUnitLevel), | ||
userGroups: this.getUserGroups(), | ||
}); | ||
}); | ||
} | ||
|
||
private getOrgUnitLevelGroup(): FutureData<number> { | ||
return apiToFuture( | ||
this.api.models.organisationUnitLevels.get({ | ||
fields: { id: true, level: true }, | ||
filter: { name: { eq: metadataCodes.orgUnitLevels.country } }, | ||
}) | ||
).flatMap(d2Response => { | ||
const orgUnitLevel = d2Response.objects[0]; | ||
return orgUnitLevel | ||
? Future.success(orgUnitLevel.level) | ||
: Future.error(new Error("Country level not found")); | ||
}); | ||
} | ||
|
||
private getRegions(level: number): FutureData<Region[]> { | ||
return apiToFuture( | ||
this.api.models.organisationUnits.get({ | ||
fields: { id: true, code: true, name: true }, | ||
filter: { level: { eq: String(level) }, children: { gt: "0" } }, | ||
paging: false, | ||
}) | ||
).map(d2Response => { | ||
return d2Response.objects.map(region => ({ | ||
id: region.id, | ||
name: region.name, | ||
code: this.extractRegionCode(region.code), | ||
})); | ||
}); | ||
} | ||
|
||
private getUserGroups(): FutureData<UserGroup[]> { | ||
return apiToFuture( | ||
this.api.models.userGroups.get({ | ||
fields: { id: true, name: true }, | ||
paging: false, | ||
}) | ||
).map(d2Response => { | ||
return d2Response.objects.map(region => ({ | ||
id: region.id, | ||
name: region.name, | ||
code: this.extractCode(region.name), | ||
})); | ||
}); | ||
} | ||
|
||
private extractRegionCode(code: string): string { | ||
return (code.slice(0, 2) || "").toUpperCase(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment with an example, so the reader knows why are we processing it. |
||
} | ||
|
||
private extractCode(code: string): string { | ||
return (code.split("_")[0] || "").toUpperCase(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import { D2AttributeValue, MetadataPick } from "@eyeseetea/d2-api/2.36"; | |
import { D2Api, MetadataResponse } from "$/types/d2-api"; | ||
|
||
import { apiToFuture } from "$/data/api-futures"; | ||
import { DataSet, DataSetList } from "$/domain/entities/DataSet"; | ||
import { AccessData, DataSet, DataSetList } from "$/domain/entities/DataSet"; | ||
import { Paginated } from "$/domain/entities/Paginated"; | ||
import { | ||
DataSetName, | ||
|
@@ -12,7 +12,11 @@ import { | |
import { Future, FutureData } from "$/domain/entities/generic/Future"; | ||
import { getUid } from "$/utils/uid"; | ||
import _ from "$/domain/entities/generic/Collection"; | ||
import { DataSetD2Api, dataSetFieldsWithOrgUnits } from "$/data/repositories/DataSetD2Api"; | ||
import { | ||
DataSetD2Api, | ||
OctalNotationPermission, | ||
dataSetFieldsWithOrgUnits, | ||
} from "$/data/repositories/DataSetD2Api"; | ||
import { Maybe } from "$/utils/ts-utils"; | ||
import { chunkRequest, runMetadata } from "$/data/utils"; | ||
import { D2Config } from "$/data/repositories/D2ApiMetadata"; | ||
|
@@ -309,8 +313,8 @@ export class DataSetD2Repository implements DataSetRepository { | |
}; | ||
}), | ||
userGroupAccesses: _(dataSet.access) | ||
.compactMap(access => { | ||
if (access.type !== "groups") return undefined; | ||
.filter(access => access.type === "groups") | ||
.map(access => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. declarative (nit-picking): after the filter, it's not just an "access", it's a "groupAccess" |
||
return { | ||
access: this.d2DataSetApi.generateFullPermission(access.permissions), | ||
id: access.id, | ||
|
@@ -326,6 +330,20 @@ export class DataSetD2Repository implements DataSetRepository { | |
}; | ||
} | ||
|
||
private convertSharingGroupsToAccessData(d2UserGroups: Maybe<SharingUserGroup>): AccessData[] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this method used? |
||
if (!d2UserGroups || Object.keys(d2UserGroups).length === 0) return []; | ||
|
||
return Object.values(d2UserGroups).map(({ id, access }) => ({ | ||
id, | ||
permissions: { | ||
data: this.d2DataSetApi.buildPermission(access, "data"), | ||
metadata: this.d2DataSetApi.buildPermission(access, "metadata"), | ||
}, | ||
name: "", | ||
type: "groups", | ||
})); | ||
} | ||
|
||
private buildDataSetElements(dataSet: DataSetToSave) { | ||
const relatedDataElements = dataSet.indicators | ||
.filter(indicator => indicator.type === "outcomes") | ||
|
@@ -398,6 +416,7 @@ type D2DataSetSection = { | |
indicators: Ref[]; | ||
}; | ||
|
||
type SharingUserGroup = Record<Id, { id: Id; access: OctalNotationPermission }>; | ||
const indicatorTypeLabel: Record<IndicatorAttrs["type"], string> = { | ||
outcomes: "Outcomes", | ||
outputs: "Outputs", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Future, FutureData } from "$/domain/entities/generic/Future"; | ||
import { Region } from "$/domain/entities/Region"; | ||
import { RegionRepository } from "$/domain/repositories/RegionRepository"; | ||
import { D2Api } from "$/types/d2-api"; | ||
|
||
export class RegionD2Repository implements RegionRepository { | ||
constructor(private _api: D2Api) {} | ||
|
||
get(): FutureData<Region[]> { | ||
return Future.success([]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { NamedCodeRef } from "$/domain/entities/Ref"; | ||
import { Region } from "$/domain/entities/Region"; | ||
|
||
export type UserGroup = NamedCodeRef; | ||
|
||
export type Config = { regions: Region[]; userGroups: UserGroup[] }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we want "userGroup" here instead of "region".