Skip to content

Commit

Permalink
#11 - Work on assign next; @todo: manage handoff message
Browse files Browse the repository at this point in the history
  • Loading branch information
kmaida committed May 2, 2020
1 parent 3ab0085 commit c2d3838
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bot-response/message-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const msgText = {
assignHandoffConfirm: (usermention, rotation) => {
return `Your handoff message for the "${rotation}" rotation has been sent to ${usermention} via DM.`;
},
assignNextError: (rotation) => {
return ':shrug: I couldn\'t assign the next person in the "' + rotation + '" rotation because there is nobody in the rotation staff list.\nTo set staff, use `@rota "' + rotation + '" staff [@user1 @user2 @user3]`.\nThen you can use `@rota "' + rotation + '" assign next [optional handoff message]` to rotate to the next user on staff.\nIf you don\'t want to use a staff list, assign someone directly by username: `@rota "' + rotation +'" assign [@user] [optional handoff message]`.';
},
assignError: (rotation) => {
return ':shrug: I couldn\'t complete this assignment because the "' + rotation + '" rotation does not exist. To create it, use `@rota "' + rotation + '" create [description]`.';
},
Expand Down
70 changes: 70 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ app.event('app_mention', async({ event, context }) => {
const isStaff = utils.isCmd('staff', text);
const isResetStaff = utils.isCmd('reset staff', text);
const isAssign = utils.isCmd('assign', text);
const isAssignNext = utils.isCmd('assign next', text);
const isWho = utils.isCmd('who', text);
const isAbout = utils.isCmd('about', text);
const isClear = utils.isCmd('clear', text);
Expand All @@ -49,6 +50,7 @@ app.event('app_mention', async({ event, context }) => {
!isStaff && !text.includes('" staff <@') // catch malformed staff commands and don't put them through as messages
!isResetStaff &&
!isAssign &&
!isAssignNext &&
!isWho &&
!isAbout &&
!isClear &&
Expand All @@ -58,6 +60,7 @@ app.event('app_mention', async({ event, context }) => {
!isStaff &&
!isResetStaff &&
!isAssign &&
!isAssignNext &&
!isWho &&
!isAbout &&
!isClear &&
Expand Down Expand Up @@ -310,6 +313,73 @@ app.event('app_mention', async({ event, context }) => {
}
}

/*--
ASSIGN NEXT
@rota "[rotation]" assign next [handoff message]
Assigns next user in staff list to rotation
--*/
else if (isAssignNext) {
try {
const pCmd = utils.parseCmd('assign next', event, context);
const rotation = pCmd.rotation;
const handoffMsg = pCmd.handoff;

if (rotation in store.getStoreList()) {
// Rotation exists
const staffList = store.getStaffList(rotation);
if (staffList && staffList.length) {
// Staff list exists and is not an empty array
const lastAssigned = store.getAssignment(rotation);
const lastAssignedIndex = staffList.indexOf(lastAssigned);
const lastIndex = staffList.length - 1; // last available position in staff list
const firstUser = staffList[0];
let usermention;
if (lastAssigned) {
// There's a user currently assigned
if (lastAssignedIndex > -1 && lastAssignedIndex < lastIndex) {
// The last assigned user is in the staff list and are NOT last in the list
// Set assignment to next user in staff list
usermention = staffList[lastAssignedIndex + 1];
} else {
// Either last user isn't in staff list or we're at the end of the list
// Assign first
usermention = firstUser;
}
} else {
// No previous assignment; start at beginning of staff list
usermention = firstUser;
}
store.saveAssignment(rotation, usermention);
const result = await app.client.chat.postMessage({
token: botToken,
channel: channelID,
text: msgText.assignConfirm(usermention, rotation)
});
} else {
// No staff list; cannot use "next"
const result = await app.client.chat.postMessage({
token: botToken,
channel: channelID,
text: msgText.assignNextError(rotation)
});
}
} else {
// If rotation doesn't exist, send message saying so
const result = await app.client.chat.postMessage({
token: botToken,
channel: channelID,
text: msgText.assignError(rotation)
});
}
}
catch (err) {
console.error(err);
const errResult = await app.client.chat.postMessage(
utils.errorMsgObj(botToken, channelID, msgText.error(err))
);
}
}

/*--
WHO
@rota "[rotation]" who
Expand Down
9 changes: 9 additions & 0 deletions utils/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ const store = {
const list = this.getStoreList();
return list[rotation].assigned;
},
/*----
Get staff list for a rotation
@Params: rotation
@Returns: staff list array (or undefined)
----*/
getStaffList(rotation) {
const list = this.getStoreList();
return list[rotation].staff;
},
/*----
Clears assigned user for a rotation (rotation value)
@Params: rotation
Expand Down
8 changes: 8 additions & 0 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ const utils = {
handoff: res[5].trim()
}
}
// Rotation, command, freeform text
else if (cmd === 'assign next') {
return {
rotation: res[2],
command: res[3],
handoff: res[4].trim()
}
}
// Rotation, command, list of space-separated usermentions
// Proofed to accommodate use of comma+space separation and minor whitespace typos
else if (cmd === 'staff') {
Expand Down

0 comments on commit c2d3838

Please sign in to comment.