Skip to content

General Processor changes #111

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

Merged
merged 2 commits into from
May 7, 2019
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
53 changes: 50 additions & 3 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,56 @@ module.exports = {

KAFKA_CONSUMER_RULESETS: {
// key is Kafka topic name, value is array of ruleset which have key as handler function name defined in src/processors/index.js
'challenge.notification.events': [{ handleChallenge: { type: 'UPDATE_DRAFT_CHALLENGE', roles: ["Submitter" /** Competitor */, "Copilot", "Reviewer"] } }],
'notifications.autopilot.events': [{ handleAutoPilot: { phaseTypeName: 'Checkpoint Screening', state: 'START', roles: ["Copilot", "Reviewer"] } }],
'submission.notification.create': [{ handleSubmission: { resource: 'submission', roles: ["Copilot", "Reviewer"], selfOnly: true /** Submitter only */ } }],
'challenge.notification.events': [
{
handleChallenge: /** topic handler name */
{
type: 'UPDATE_DRAFT_CHALLENGE',
roles: ["Submitter" /** Competitor */, "Copilot", "Reviewer"],
notification:
{
id: 0, /** challengeid or projectid */
name: '', /** challenge name */
group: 'Challenge',
title: 'Challenge specification is modified.'
}
}
}
],
'notifications.autopilot.events': [
{
handleAutoPilot:
{
phaseTypeName: 'Checkpoint Screening',
state: 'START',
roles: ["Copilot", "Reviewer"],
notification:
{
id: 0, /** challengeid or projectid */
name: '', /** challenge name */
group: 'Challenge',
title: 'Challenge checkpoint review.'
}
}
}
],
'submission.notification.create': [
{
handleSubmission:
{
resource: 'submission',
roles: ["Copilot", "Reviewer"],
selfOnly: true /** Submitter only */,
notification:
{
id: 0, /** challengeid or projectid */
name: '', /** challenge name */
group: 'Submission',
title: 'A new submission is uploaded.'
}
}
}
],
//'notifications.community.challenge.created': ['handleChallengeCreated'],
//'notifications.community.challenge.phasewarning': ['handleChallengePhaseWarning'],
},
Expand Down
2 changes: 1 addition & 1 deletion consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function startKafkaConsumer() {
yield models.Notification.bulkCreate(_.map(notifications, (n) => ({
userId: n.userId,
type: n.type || topic,
contents: n.contents || messageJSON.payload || {},
contents: n.contents || n.notification || messageJSON.payload || {},
read: false,
seen: false,
version: n.version || null,
Expand Down
39 changes: 35 additions & 4 deletions src/common/tcApiHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ function* notifyUserViaEmail(user, message) {
*/
function* getChallenge(challengeId) {
// this is public API, M2M token is not needed
const res = yield request.get(`${config.TC_API_V4_BASE_URL}/challenges/${challengeId}`);
const url = `${config.TC_API_V4_BASE_URL}/challenges/${challengeId}`
logger.info(`calling public challenge api ${url}`)
const res = yield request.get(url);
if (!_.get(res, 'body.result.success')) {
throw new Error(`Failed to get challenge by id ${challengeId}`);
}
Expand All @@ -198,10 +200,10 @@ function* getChallenge(challengeId) {
/**
* Notify users of message.
* @param {Array} users the users
* @param {Object} message the Kafka message
* @param {Object} notification notifcation node
* @returns {Array} the notifications
*/
function* notifyUsersOfMessage(users, message) {
function* notifyUsersOfMessage(users, notification) {
if (!users || users.length === 0) {
logger.info('No users to notify message.');
return [];
Expand All @@ -212,7 +214,7 @@ function* notifyUsersOfMessage(users, message) {
for (let i = 0; i < users.length; i += 1) {
const user = users[i];
// construct notification, rest fields are set in consumer.js
notifications.push({ userId: user.userId });
notifications.push({ userId: user.userId, notification: notification });

/* TODO Sachin disabled this code
if (config.ENABLE_EMAILS) {
Expand Down Expand Up @@ -287,6 +289,34 @@ function filterChallengeUsers(usersInfo, filterOnRoles = [], filterOnUsers = [])
return users
}

/**
* modify notification template
* @param {Object} ruleSet rule
* @param {Object} data values to be filled
*
* @returns {Object} notification node
*/
function* modifyNotificationNode(ruleSet, data) {
const notification = _.get(ruleSet, "notification")
const id = data.id || data.challengeId || 0
const name = _.get(data, "name")

notification.id = id

if (name) {
notification.name = name
} else {
try {
const challenge = yield getChallenge(id)
notification.name = _.get(challenge, "challengeTitle")
} catch (error) {
notification.name = ''
logger.error(`Error in fetching challenge detail : ${error}`)
}
}
return notification
}

module.exports = {
getM2MToken,
getUsersBySkills,
Expand All @@ -297,4 +327,5 @@ module.exports = {
notifyUsersOfMessage,
getUsersInfoFromChallenge,
filterChallengeUsers,
modifyNotificationNode,
};
7 changes: 5 additions & 2 deletions src/services/AutoPilotService.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ function* handle(message, ruleSets) {
if ((message.payload.phaseTypeName === _.get(ruleSets, "phaseTypeName"))
&& (message.payload.state === _.get(ruleSets, "state"))) {
const challengeId = message.payload.projectId
const usersInfo = yield tcApiHelper.getUsersInfoFromChallenge(challengeId)
const filerOnRoles = _.get(ruleSets, "roles")

const notification = yield tcApiHelper.modifyNotificationNode(ruleSets, { id: challengeId})
const usersInfo = yield tcApiHelper.getUsersInfoFromChallenge(challengeId)
const users = tcApiHelper.filterChallengeUsers(usersInfo, filerOnRoles)

logger.info(`Successfully filetered ${users.length} users on rulesets ${JSON.stringify(filerOnRoles)} `)
// notify users of message
return yield tcApiHelper.notifyUsersOfMessage(users, message);
return yield tcApiHelper.notifyUsersOfMessage(users, notification);
}
return {}
}
Expand Down
7 changes: 5 additions & 2 deletions src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ function* handle(message, ruleSets) {

if (message.payload.type === _.get(ruleSets, "type")) {
const challengeId = message.payload.data.id
const usersInfo = yield tcApiHelper.getUsersInfoFromChallenge(challengeId)
const filterOnRoles = _.get(ruleSets, "roles")
const challengeTitle = _.get(message.payload, "data.name")

const notification = yield tcApiHelper.modifyNotificationNode(ruleSets, { id: challengeId, name: challengeTitle })
const usersInfo = yield tcApiHelper.getUsersInfoFromChallenge(challengeId)
const users = tcApiHelper.filterChallengeUsers(usersInfo, filterOnRoles)
logger.info(`Successfully filetered ${users.length} users on rulesets ${JSON.stringify(filterOnRoles)} `)
// notify users of message
return yield tcApiHelper.notifyUsersOfMessage(users, message);
return yield tcApiHelper.notifyUsersOfMessage(users, notification);
}
return {}
}
Expand Down
10 changes: 6 additions & 4 deletions src/services/SubmissionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ function* handle(message, ruleSets) {

if (message.payload.resource === _.get(ruleSets, "resource")) {
const challengeId = message.payload.challengeId
const usersInfo = yield tcApiHelper.getUsersInfoFromChallenge(challengeId)
const filterOnRoles = _.get(ruleSets, "roles")

const filterOnUsers = []
if (_.get(ruleSets, 'selfOnly')) {
const memberId = _.get(message.payload, "memberId")
filterOnUsers.push(memberId)
}
const filterOnRoles = _.get(ruleSets, "roles")

const usersInfo = yield tcApiHelper.getUsersInfoFromChallenge(challengeId)
const users = tcApiHelper.filterChallengeUsers(usersInfo, filterOnRoles, filterOnUsers)
const notification = yield tcApiHelper.modifyNotificationNode(ruleSets, { id: challengeId})
logger.info(`Successfully filetered ${users.length} users on rulesets ${JSON.stringify(filterOnRoles)} `)
// notify users of message
return yield tcApiHelper.notifyUsersOfMessage(users, message);
return yield tcApiHelper.notifyUsersOfMessage(users, notification);
}
return {}
}
Expand All @@ -42,7 +44,7 @@ handle.schema = {
timestamp: joi.date().required(),
'mime-type': joi.string().required(),
payload: joi.object().keys({
resource: joi.string().required()
resource: joi.string().required()
}).unknown(true).required(),
}).required(),
ruleSets: joi.object()
Expand Down