Skip to content

Commit

Permalink
Merge pull request #759 from HDRUK/release-preprod/v2.9.0
Browse files Browse the repository at this point in the history
Release preprod/v2.9.0
  • Loading branch information
cdjreekie authored May 26, 2022
2 parents a2f6c0e + ecca8d1 commit 5d89008
Show file tree
Hide file tree
Showing 13 changed files with 480 additions and 146 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"mongoose": "^5.12.7",
"morgan": "^1.10.0",
"multer": "^1.4.2",
"nodemailer": "^6.7.5",
"oidc-provider": "^6.29.3",
"passport": "^0.4.1",
"passport-azure-ad-oauth2": "0.0.4",
Expand Down
9 changes: 8 additions & 1 deletion src/resources/dataUseRegister/dataUseRegister.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,20 @@ export default class DataUseRegisterController extends Controller {

switch (type) {
case constants.dataUseRegisterNotifications.DATAUSEAPPROVED: {
let teamEmailNotification = [];
const adminTeam = await TeamModel.findOne({ type: 'admin' })
.populate({
path: 'users',
})
.lean();
const team = await TeamModel.findById(dataUseRegister.publisher.toString());
if (team.notifications.length > 0 && team.notifications[0].optIn) {
team.notifications[0].subscribedEmails.map(teamEmail => {
teamEmailNotification.push({email: teamEmail});
});
}
const dataUseTeamMembers = teamController.getTeamMembersByRole(adminTeam, constants.roleTypes.ADMIN_DATA_USE);
const emailRecipients = [...dataUseTeamMembers, uploader];
const emailRecipients = [...dataUseTeamMembers, uploader, ...teamEmailNotification];

const options = {
id,
Expand Down
159 changes: 133 additions & 26 deletions src/resources/datarequest/datarequest.controller.js

Large diffs are not rendered by default.

33 changes: 28 additions & 5 deletions src/resources/message/message.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module.exports = {

// 15. Prepare to send email if a new message has been created
if (messageType === 'message') {
let optIn, subscribedEmails;
let optIn, subscribedEmails, messageCreatorRecipient;
// 16. Find recipients who have opted in to email updates and exclude the requesting user
let messageRecipients = await UserModel.find({ _id: { $in: topicObj.recipients } });

Expand All @@ -125,10 +125,21 @@ module.exports = {
);
if (!_.isEmpty(subscribedMembersByType)) {
// build cleaner array of memberIds from subscribedMembersByType
const memberIds = [...subscribedMembersByType.map(m => m.memberid.toString()), topicObj.createdBy.toString()];
// returns array of objects [{email: '[email protected] '}] for members in subscribed emails users is list of full user object
const { memberEmails } = teamController.getMemberDetails([...memberIds], [...messageRecipients]);
messageRecipients = [...teamNotificationEmails, ...memberEmails];
if (topicObj.topicMessages !== undefined) {
const memberIds = [...subscribedMembersByType.map(m => m.memberid.toString()), ...topicObj.createdBy._id.toString()];
// returns array of objects [{email: '[email protected] '}] for members in subscribed emails users is list of full user object
const { memberEmails } = teamController.getMemberDetails([...memberIds], [...messageRecipients]);
messageRecipients = [...teamNotificationEmails, ...memberEmails];
} else {
const memberIds = [...subscribedMembersByType.map(m => m.memberid.toString())].filter(ele => ele !== topicObj.createdBy.toString());
const creatorObjectId = topicObj.createdBy.toString();
// returns array of objects [{email: '[email protected] '}] for members in subscribed emails users is list of full user object
const { memberEmails } = teamController.getMemberDetails([...memberIds], [...messageRecipients]);
const creatorEmail = await UserModel.findById(creatorObjectId);
messageCreatorRecipient = [{ email: creatorEmail.email}];
messageRecipients = [...teamNotificationEmails, ...memberEmails];
}

} else {
// only if not membersByType but has a team email setup
messageRecipients = [...messageRecipients, ...teamNotificationEmails];
Expand All @@ -155,6 +166,18 @@ module.exports = {
html,
false
);

if (messageCreatorRecipient) {
let htmlCreator = emailGenerator.generateMessageCreatorNotification(options);

emailGenerator.sendEmail(
messageCreatorRecipient,
constants.hdrukEmail,
`You have received a new message on the HDR UK Innovation Gateway`,
htmlCreator,
false
);
}
}
// 19. Return successful response with message data
const messageObj = message.toObject();
Expand Down
6 changes: 6 additions & 0 deletions src/resources/publisher/publisher.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const PublisherSchema = new Schema(
allowAccessRequestManagement: { type: Boolean, default: false },
uses5Safes: { type: Boolean, default: false },
wordTemplate: String,
federation: {
active: { type: Boolean },
auth: { type: Object, select: false },
endpoints: { type: Object, select: false },
notificationEmail: { type: Array, select: false },
},
},
{
toJSON: { virtuals: true },
Expand Down
21 changes: 17 additions & 4 deletions src/resources/team/team.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,11 +965,24 @@ const checkIfAdmin = (user, adminRoles) => {
};

const getTeamMembersByRole = (team, role) => {
// Destructure members array and populated users array (populate 'users' must be included in the original Mongo query)
let { members = [], users = [] } = team;
// Get all userIds for role within team
let userIds = members.filter(mem => mem.roles.includes(role) || role === 'All').map(mem => mem.memberid.toString());
// return all user records for role

let userIds = members.filter(mem => {
if (mem.roles.includes(role) || role === 'All') {
if(!_.has(mem, 'notifications')) {
return true;
}

if (_.has(mem, 'notifications') && !mem.notifications.length) {
return true;
}

if (_.has(mem, 'notifications') && mem.notifications.length && mem.notifications[0].optIn) {
return true;
}
}
}).map(mem => mem.memberid.toString());

return users.filter(user => userIds.includes(user._id.toString()));
};

Expand Down
35 changes: 0 additions & 35 deletions src/resources/user/__tests__/getCollaboratorsCohorts.test.js

This file was deleted.

44 changes: 44 additions & 0 deletions src/resources/utilities/__tests__/emailGenerator.util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import emailGenerator from '../emailGenerator.util';

describe('Email generator utility functions', () => {
describe('_generateMetadataOnboardingRejected', () => {
let isFederated;

it('SHOULD include federated warning if isFederated is true', async () => {
isFederated = true;

const emailBody = emailGenerator.generateMetadataOnboardingRejected({ isFederated });

// Federated warning should be present if dataset is from a federated publisher
expect(emailBody.includes('Do not apply these changes directly to the Gateway')).toBe(true);
});

it('SHOULD NOT include federated warning if isFederated is false', async () => {
isFederated = false;

const emailBody = emailGenerator.generateMetadataOnboardingRejected({ isFederated });

// Federated warning should not be present if dataset is not from a federated publisher
expect(emailBody.includes('Do not apply these changes directly to the Gateway')).toBe(false);
});
});
describe('_getRecipients', () => {
const mockRecipients = [
{ email: '[email protected]' },
{ email: '[email protected]' },
{ email: '[email protected]' },
{ email: '[email protected]' },
];
it('Should remove duplicaties for production', async () => {
const recipients = emailGenerator.getRecipients(mockRecipients, 'production', '[email protected]');
expect(recipients.length).toBe(3);
expect(recipients).toEqual([{ email: '[email protected]' }, { email: '[email protected]' }, { email: '[email protected]' }]);
});

it('Should replace recipients non production environtment to generic email', async () => {
const recipients = emailGenerator.getRecipients(mockRecipients, undefined, '[email protected]');
expect(recipients.length).toBe(1);
expect(recipients).toEqual([{ email: '[email protected]' }]);
});
});
});
2 changes: 2 additions & 0 deletions src/resources/utilities/constants.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ const _notificationTypes = {
MEMBERROLECHANGED: 'MemberRoleChanged',
WORKFLOWASSIGNED: 'WorkflowAssigned',
WORKFLOWCREATED: 'WorkflowCreated',
WORKFLOWUPDATED: 'WorkflowUpdated',
WORKFLOWDELETED: 'WorkflowDeleted',
INPROGRESS: 'InProgress',
APPLICATIONCLONED: 'ApplicationCloned',
APPLICATIONDELETED: 'ApplicationDeleted',
Expand Down
Loading

0 comments on commit 5d89008

Please sign in to comment.