Skip to content

Commit

Permalink
Merge pull request #189 from EyeSeeTea/development
Browse files Browse the repository at this point in the history
Release 0.5.0
  • Loading branch information
adrianq authored Jan 29, 2021
2 parents f7afd23 + 5b1ff76 commit 7a0dc8e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d2-user-extended-app",
"version": "0.4.0",
"version": "0.5.0",
"description": "DHIS2 Extended User app",
"main": "src/index.html",
"license": "GPL-3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default class CopyInUserBatchModelsMultiSelectComponent extends React.Com
updateStrategy: this.props.parents.length > 1 ? "merge" : "replace",
copyUserGroups: false,
copyUserRoles: false,
copyOrgUnitOutput: false,
copyOrgUnits: false,
};
}

Expand Down Expand Up @@ -58,14 +60,29 @@ export default class CopyInUserBatchModelsMultiSelectComponent extends React.Com
cancelButton: {
marginRight: 16,
},
toggle: {
userGroupsToggle: {
width: 145,
marginTop: 10,
marginRight: 5,
float: "right",
},
userRolesToggle: {
width: 135,
marginTop: 10,
marginRight: 60,
float: "right",
},
orgUnitToggle: {
width: 125,
marginRight: 65,
float: "right",
marginTop: 20,
marginRight: 50,
},
copyOrgUnitOutputToggle: {
width: 140,
float: "right",
marginRight: 5,
},
};

componentDidMount() {
const { parents, model } = this.props;
return Promise.all([model.getAllChildren(), model.getParents(parents)])
Expand Down Expand Up @@ -110,10 +127,23 @@ export default class CopyInUserBatchModelsMultiSelectComponent extends React.Com
}

async copyInUserSave() {
const { parents, selectedIds, copyUserGroups, copyUserRoles } = this.state;
const {
parents,
selectedIds,
copyUserGroups,
copyUserRoles,
copyOrgUnitOutput,
copyOrgUnits,
} = this.state;
this.setState({ state: "loading" });
const copyAccessElements = {
userGroups: copyUserGroups,
userRoles: copyUserRoles,
orgUnitOutput: copyOrgUnitOutput,
orgUnits: copyOrgUnits,
};
await this.props.model
.copyInUserSave(parents, selectedIds, copyUserGroups, copyUserRoles)
.copyInUserSave(parents, selectedIds, copyAccessElements)
.then(() => this.close(this.props.onSuccess))
.catch(err => this.close(this.props.onError))
.finally(() => this.setState({ state: "ready" }));
Expand Down Expand Up @@ -145,9 +175,15 @@ export default class CopyInUserBatchModelsMultiSelectComponent extends React.Com
}

copy() {
const { copyUserGroups, copyUserRoles, selectedIds } = this.state;
const {
copyUserGroups,
copyUserRoles,
copyOrgUnitOutput,
copyOrgUnits,
selectedIds,
} = this.state;

if (!copyUserGroups && !copyUserRoles) {
if (!copyUserGroups && !copyUserRoles && !copyOrgUnitOutput && !copyOrgUnits) {
snackActions.show({ message: this.getTranslation("select_one_toggle") });
} else if (_.isEmpty(selectedIds)) {
snackActions.show({ message: this.getTranslation("select_at_least_one_user") });
Expand Down Expand Up @@ -206,16 +242,30 @@ export default class CopyInUserBatchModelsMultiSelectComponent extends React.Com

<Toggle
label={"User Groups"}
style={this.styles.toggle}
style={this.styles.userGroupsToggle}
checked={this.state.copyUserGroups == true}
onToggle={(ev, newValue) => this.setState({ copyUserGroups: newValue })}
/>
<Toggle
label={"User Roles"}
style={this.styles.toggle}
style={this.styles.userRolesToggle}
checked={this.state.copyUserRoles === true}
onToggle={(ev, newValue) => this.setState({ copyUserRoles: newValue })}
/>
<div>
<Toggle
label={"OU Outputs"}
style={this.styles.copyOrgUnitOutputToggle}
checked={this.state.copyOrgUnitOutput === true}
onToggle={(ev, newValue) => this.setState({ copyOrgUnitOutput: newValue })}
/>
<Toggle
label={"Org Units"}
style={this.styles.orgUnitToggle}
checked={this.state.copyOrgUnits == true}
onToggle={(ev, newValue) => this.setState({ copyOrgUnits: newValue })}
/>
</div>
<div style={this.styles.contents}>
<MultiSelect
isLoading={isLoading}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ export default class CopyInUserBatchModelsMultiSelectModel {
});
return users;
}
async copyInUserSave(parents, selectedIds, copyUserGroups, copyUserRoles) {
async copyInUserSave(parents, selectedIds, copyAccessElements) {
const parentWithRoles = await this.getUserInfo([getOwnedPropertyJSON(parents[0]).id]);
const childrenUsers = await this.getUserInfo(selectedIds);

const payload = await this.getPayload(
...parentWithRoles,
childrenUsers,
copyUserGroups,
copyUserRoles
copyAccessElements
);
return payload;
}
Expand Down
78 changes: 53 additions & 25 deletions src/models/userHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,14 +527,15 @@ async function saveUsers(d2, users) {
return postMetadata(api, payload);
}

async function saveCopyInUsers(d2, users, copyUserGroups, copyUserRoles) {
async function saveCopyInUsers(d2, users, copyUserGroups) {
const api = d2.Api.getApi();
const userGroupsToSave = await getUserGroupsToSave(api, users, []);
const payload = { users: users, userGroups: userGroupsToSave };

if (copyUserRoles && !copyUserGroups) {
if (copyUserGroups) {
const userGroupsToSave = await getUserGroupsToSave(api, users, []);
const payload = { users: users, userGroups: userGroupsToSave };
return postMetadata(api, payload);
} else {
return postMetadata(api, { users: users });
} else return postMetadata(api, payload);
}
}

/* Return an array of users from DHIS2 API.
Expand Down Expand Up @@ -637,28 +638,55 @@ async function getExistingUsers(d2, options = {}) {
});
return users;
}
function addItems(items1, items2, shouldAdd) {
if (!shouldAdd) {
return items1;
} else {
return _(items1)
.unionBy(items2, element => element.id)
.value();
}
}

function getPayload(parentUser, destUsers, copyUserGroups, copyUserRoles) {
function getPayload(parentUser, destUsers, fields) {
const users = destUsers.map(childUser => {
let childUserRoles = childUser.userCredentials.userRoles;
let childUserGroups = childUser.userGroups;
if (copyUserRoles) {
parentUser.userCredentials.userRoles.forEach(role => {
if (childUserRoles.find(element => element.id === role.id) === undefined) {
childUserRoles.push(role);
}
});
}
if (copyUserGroups) {
parentUser.userGroups.forEach(group => {
if (childUserGroups.find(element => element.id === group.id) === undefined) {
childUserGroups.push(group);
}
});
}
return childUser;
const newChildUserCredentials = {
...childUser.userCredentials,
userRoles: addItems(
childUser.userCredentials.userRoles,
parentUser.userCredentials.userRoles,
fields.userRoles
),
};

const newChildUserGroups = addItems(
childUser.userGroups,
parentUser.userGroups,
fields.userGroups
);

const newChildOrgUnitsOutput = addItems(
childUser.dataViewOrganisationUnits,
parentUser.dataViewOrganisationUnits,
fields.orgUnitOutput
);

const newChildOrgUnits = addItems(
childUser.organisationUnits,
parentUser.organisationUnits,
fields.orgUnits
);

return {
...childUser,
userCredentials: newChildUserCredentials,
userGroups: newChildUserGroups,
dataViewOrganisationUnits: newChildOrgUnitsOutput,
organisationUnits: newChildOrgUnits,
};
});
return saveCopyInUsers(d2, users, copyUserGroups, copyUserRoles);

return saveCopyInUsers(d2, users, fields.userGroups);
}

export {
Expand Down

0 comments on commit 7a0dc8e

Please sign in to comment.