Skip to content
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

delete member from team #897

Merged
merged 4 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/resources/team/team.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ const TeamSchema = new Schema(
{
_id: false,
memberid: { type: Schema.Types.ObjectId, ref: 'User', required: true },
roles: { type: [String], enum: ['reviewer', 'manager', 'metadata_editor'], required: true },
roles: {
type: [String],
enum: [
'reviewer',
'manager',
'metadata_editor',
'custodian.team.admin',
'custodian.metadata.manager',
'custodian.dar.manager'
],
required: true
},
dateCreated: Date,
dateUpdated: Date,
notifications: [
Expand Down
66 changes: 57 additions & 9 deletions src/resources/team/v3/team.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,74 @@ class TeamController extends TeamService {

async getTeamMembers(req, res) {
const teamId = req.params.teamid;
const user = req.user;
const users = req.user;
const currentUserId = req.user._id;

const team = await this.getMembersByTeamId(teamId);

let authorised = teamV3Util.checkTeamV3Permissions('', team.toObject(), currentUserId);
if (!authorised) {
authorised = teamV3Util.checkIfAdmin(user, [constants.roleTypes.ADMIN_DATASET]);
}
if (!authorised) {
return res.status(401).json({ success: false });
}
this.checkUserAuthorization(currentUserId, '', team, users);

let members = teamV3Util.formatTeamMembers(team);
console.log(members);

res.status(200).json({
members,
});
}

async deleteTeamMember(req, res) {
const teamId = req.params.teamid;
const deleteUserId = req.params.memberid;
const userObj = req.user;
const currentUserId = req.user._id;

const team = await this.getTeamByTeamId(teamId);

this.checkUserAuthorization(currentUserId, 'manager', team, userObj);

let { members = [], users = [] } = team;

this.checkIfLastManager(members, deleteUserId);

let updatedMembers = [...members].filter(mem => mem.memberid.toString() !== deleteUserId.toString());
if (members.length === updatedMembers.length) {
throw new Error(`The user requested for deletion is not a member of this team.`);
}

team.members = updatedMembers;
try {
team.save(function (err, result) {
if (err) {
throw new Error(err.message);
} else {
let removedUser = users.find(user => user._id.toString() === deleteUserId.toString());
teamV3Util.createTeamNotifications(constants.notificationTypes.MEMBERREMOVED, { removedUser }, team, userObj);
return res.status(204).json({
success: true,
});
}
});
} catch (e) {
throw new Error(e.message);
}
}

checkUserAuthorization(currUserId, permission, team, users) {
let authorised = teamV3Util.checkTeamV3Permissions(permission, team.toObject(), currUserId);
if (!authorised) {
authorised = teamV3Util.checkIfAdmin(users, [constants.roleTypes.ADMIN_DATASET]);
}
if (!authorised) {
throw new Error(`Not enough permissions. User is not authorized to perform this action.`);
}
}

checkIfLastManager(members, deleteUserId) {
let managerCount = members.filter(mem => mem.roles.includes('manager') && mem.memberid.toString() !== deleteUserId).length;
if (managerCount === 0) {
throw new Error(`You cannot delete the last manager in the team.`);
}
}

}

module.exports = new TeamController();
6 changes: 6 additions & 0 deletions src/resources/team/v3/team.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ const router = express.Router();
// @access Private
router.get('/:teamid/members', passport.authenticate('jwt'), (req, res) => TeamController.getTeamMembers(req, res));

// @route DELETE api/v3/teams/:teamid/members/:memberid
// @desc DELETE team member from the team
// @access Private
router.delete('/:teamid/members/:memberid', passport.authenticate('jwt'), (req, res) => TeamController.deleteTeamMember(req, res));


module.exports = router;
25 changes: 24 additions & 1 deletion src/resources/team/v3/team.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,27 @@ export default class TeamService {
throw new Error(e.message);
}
}
}

async getTeamByTeamId(teamId) {
try {
const team = await TeamModel
.findOne({ _id: teamId })
.populate([
{ path: 'users' },
{
path: 'publisher',
select: 'name'
}
]);

if (!team) {
throw new Error(`Team not Found`);
}

return team;
} catch (e) {
process.stdout.write(`TeamController.getTeamByTeamId : ${e.message}\n`);
throw new Error(e.message);
}
}
}
136 changes: 136 additions & 0 deletions src/resources/utilities/__mocks__/getTeamName.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const mockTeamWithPublisher = {
"active": true,
"_id": "63bbebf8ec565a91c474cd1b",
"members": [
{
"roles": [
"manager",
"custodian.team.admin",
"custodian.metadata.manager",
"custodian.dar.manager"
],
"memberid": "6308bfd1d2ff69e6c13427e7",
"notifications": []
}
],
"notifications": [],
"type": "publisher",
"createdAt": "2023-01-09T10:27:04.376Z",
"updatedAt": "2023-01-23T13:59:48.253Z",
"__v": 25,
"publisher": {
"_id": "63bbebf8ec565a91c474cd1b",
"name": "ALLIANCE > Test40"
},
"users": [
{
"feedback": false,
"news": false,
"isServiceAccount": false,
"advancedSearchRoles": [],
"_id": "6308bfd1d2ff69e6c13427e7",
"id": 6531262197049297,
"providerId": "102868775144293907483",
"provider": "google",
"firstname": "kymme",
"lastname": "hayley",
"email": "kymme@hdruk.dev",
"role": "Admin",
"createdAt": "2022-08-26T12:42:57.116Z",
"updatedAt": "2023-01-06T16:08:52.920Z",
"__v": 0,
"redirectURL": "/search?search=&tab=Datasets",
"discourseKey": "fa596dcd0486d6919c9ee98db5eb00429341d266e275c3c5d8b95b21ff27b89f",
"discourseUsername": "kymme.hayley"
},
{
"feedback": false,
"news": false,
"isServiceAccount": false,
"advancedSearchRoles": [],
"_id": "5e8c3823e63e5d83ac27c347",
"id": 5890232553870074,
"providerId": "102167422686846649659",
"provider": "google",
"firstname": "Ciara",
"lastname": "Ward",
"email": "ciara.ward@paconsulting.com",
"password": null,
"role": "Creator",
"__v": 0,
"discourseKey": "a23c62c2f9b06f0873a567522ac585a288af9fa8ec7b62eeec68baebef1cdf10",
"discourseUsername": "ciara.ward",
"updatedAt": "2021-05-12T09:51:49.573Z",
"createdAt": "2020-09-04T00:00:00.000Z"
}
]
};

const mockTeamWithoutPublisher = {
"active": true,
"_id": "63bbebf8ec565a91c474cd1b",
"members": [
{
"roles": [
"manager",
"custodian.team.admin",
"custodian.metadata.manager",
"custodian.dar.manager"
],
"memberid": "6308bfd1d2ff69e6c13427e7",
"notifications": []
}
],
"notifications": [],
"type": "publisher",
"createdAt": "2023-01-09T10:27:04.376Z",
"updatedAt": "2023-01-23T13:59:48.253Z",
"__v": 25,
"users": [
{
"feedback": false,
"news": false,
"isServiceAccount": false,
"advancedSearchRoles": [],
"_id": "6308bfd1d2ff69e6c13427e7",
"id": 6531262197049297,
"providerId": "102868775144293907483",
"provider": "google",
"firstname": "kymme",
"lastname": "hayley",
"email": "kymme@hdruk.dev",
"role": "Admin",
"createdAt": "2022-08-26T12:42:57.116Z",
"updatedAt": "2023-01-06T16:08:52.920Z",
"__v": 0,
"redirectURL": "/search?search=&tab=Datasets",
"discourseKey": "fa596dcd0486d6919c9ee98db5eb00429341d266e275c3c5d8b95b21ff27b89f",
"discourseUsername": "kymme.hayley"
},
{
"feedback": false,
"news": false,
"isServiceAccount": false,
"advancedSearchRoles": [],
"_id": "5e8c3823e63e5d83ac27c347",
"id": 5890232553870074,
"providerId": "102167422686846649659",
"provider": "google",
"firstname": "Ciara",
"lastname": "Ward",
"email": "ciara.ward@paconsulting.com",
"password": null,
"role": "Creator",
"__v": 0,
"discourseKey": "a23c62c2f9b06f0873a567522ac585a288af9fa8ec7b62eeec68baebef1cdf10",
"discourseUsername": "ciara.ward",
"updatedAt": "2021-05-12T09:51:49.573Z",
"createdAt": "2020-09-04T00:00:00.000Z"
}
]
};

export {
mockTeamWithPublisher,
mockTeamWithoutPublisher,
}
16 changes: 16 additions & 0 deletions src/resources/utilities/__tests__/getTeamName.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import teamV3Util from '../team.v3.util';
import { mockTeamWithPublisher, mockTeamWithoutPublisher } from '../__mocks__/getTeamName.mock';

describe('getTeamName test', () => {
it('should return a string who contain the publisher name', () => {
let response = teamV3Util.getTeamName(mockTeamWithPublisher);
expect(typeof response).toBe('string')
expect(response).toContain('ALLIANCE > Test40');
});

it('should return a string who contain a generic publisher name', () => {
let response = teamV3Util.getTeamName(mockTeamWithoutPublisher);
expect(typeof response).toBe('string')
expect(response).toContain('No team name');
});
});
Loading