From 49799a3dd2f040d7b68a715b7676ae44c57eecf2 Mon Sep 17 00:00:00 2001 From: Matt O'Keefe Date: Tue, 5 Oct 2021 10:23:53 -0500 Subject: [PATCH] Bug fix (#100) * Log new user log * Fixing bug with user creation * bumping version and fixing minor bugs --- package-lock.json | 2 +- package.json | 2 +- src/cronEvents.js | 2 +- src/lib/regexpCreator.js | 10 ------ src/migrations.js | 72 ++++++++++++++++++++++++++-------------- src/scoreboard.js | 32 +++++++++--------- src/wallet.js | 10 +++++- test/scoreboard.test.js | 35 ------------------- 8 files changed, 75 insertions(+), 90 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0a40c71..7884cab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "hubot-plusplus-expanded", - "version": "3.3.2", + "version": "3.3.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 02e4f9e..fd30125 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hubot-plusplus-expanded", - "version": "3.3.2", + "version": "3.3.3", "description": "A hubot script for micro praise", "main": "index.js", "engines": { diff --git a/src/cronEvents.js b/src/cronEvents.js index ab50fdb..e2eaeca 100644 --- a/src/cronEvents.js +++ b/src/cronEvents.js @@ -43,7 +43,7 @@ module.exports = (robot) => { if (topRecipient.length > 0) { for (let i = 0, end = topRecipient.length - 1, asc = end >= 0; asc ? i <= end : i >= end; asc ? i++ : i--) { const person = `<@${topRecipient[i]._id}>`; - const pointStr = topRecipient[i].scoreChange > 1 ? 'points given' : 'point given'; + const pointStr = topRecipient[i].scoreChange > 1 ? 'points received' : 'point received'; message.push(`${i + 1}. ${person} (${topRecipient[i].scoreChange} ${pointStr})`); } } else { diff --git a/src/lib/regexpCreator.js b/src/lib/regexpCreator.js index f87369a..8b0d4f3 100644 --- a/src/lib/regexpCreator.js +++ b/src/lib/regexpCreator.js @@ -34,16 +34,6 @@ class RegExpCreator { ); } - /** - * - */ - createBotDayRegExp(botName) { - return new RegExp( - `(what\\sday|when|which\\sday|which)\\sis\\s(my|@?\\w+\\.\\w+)(\\s)?('s)?\\s${botName}(\\s)?day(\\?)?`, - 'i' - ); - } - /** * { user1, user2 }++ * { user1, user2 }-- diff --git a/src/migrations.js b/src/migrations.js index 937be53..b5684c9 100644 --- a/src/migrations.js +++ b/src/migrations.js @@ -10,74 +10,73 @@ module.exports = (robot) => { robot.respond(/try to map more data to all slack users to db users/, mapMoreUserFieldsBySlackId); robot.respond(/try to map @.* to db users/, mapSingleUserToDb); robot.respond(/unmap all users/, unmapUsersToDb); + robot.respond(/map all slackIds to slackEmail/, mapSlackIdToEmail); - async function mapUsersToDb(msg, props) { + async function mapUsersToDb(msg) { if (msg.message.user.id !== 'UD46NSKSM' && msg.message.user.id !== 'U0231VDAB1B') { msg.reply('Sorry, can\'t do that https://i.imgur.com/Gp6wNZr.gif'); return; } - const databaseService = new DatabaseService({ robot, ...props }); + const databaseService = new DatabaseService({ robot, ...procVars }); await databaseService.init(); const db = await databaseService.getDb(); - const web = new WebClient(msg.robot.adapter.options.token); + const web = new WebClient(robot.adapter.options.token); const { members } = await web.users.list(); const mappings = []; for (const member of members) { try { - msg.robot.logger.debug('Map this member', JSON.stringify(member)); + robot.logger.debug('Map this member', JSON.stringify(member)); const localMember = await databaseService.getUser(member.name); localMember.slackId = member.id; if (localMember._id) { await db.collection(scoresDocumentName).replaceOne({ name: localMember.name }, localMember); mappings.push(`\`{ name: ${localMember.name}, slackId: ${localMember.slackId}, id: ${localMember._id} }\``); } - msg.robot.logger.debug(`Save the new member ${JSON.stringify(localMember)}`); + robot.logger.debug(`Save the new member ${JSON.stringify(localMember)}`); } catch (er) { - msg.robot.logger.error('failed to find', member, er); + robot.logger.error('failed to find', member, er); } } msg.reply(`Ding fries are done. We mapped ${mappings.length} of ${members.length} users. \n${mappings.join('\n')}`); } - async function mapMoreUserFieldsBySlackId(msg, props) { + async function mapMoreUserFieldsBySlackId(msg) { if (msg.message.user.id !== 'UD46NSKSM' && msg.message.user.id !== 'U0231VDAB1B') { msg.reply('Sorry, can\'t do that https://i.imgur.com/Gp6wNZr.gif'); return; } - const { robot } = msg; - const databaseService = new DatabaseService({ robot, ...props }); + const databaseService = new DatabaseService({ robot, ...procVars }); await databaseService.init(); const db = await databaseService.getDb(); - const web = new WebClient(msg.robot.adapter.options.token); + const web = new WebClient(robot.adapter.options.token); const { members } = await web.users.list(); for (const member of members) { if (member.profile.email) { try { - msg.robot.logger.debug('Map this member', JSON.stringify(member)); + robot.logger.debug('Map this member', JSON.stringify(member)); const localMember = await databaseService.getUser(member); localMember.slackId = member.id; localMember.slackEmail = member.profile.email; if (localMember._id) { await db.collection(scoresDocumentName).replaceOne({ slackId: localMember.slackId }, localMember); } - msg.robot.logger.debug(`Save the new member ${JSON.stringify(localMember)}`); + robot.logger.debug(`Save the new member ${JSON.stringify(localMember)}`); } catch (er) { - msg.robot.logger.error('failed to find', member, er); + robot.logger.error('failed to find', member, er); } } } msg.reply('Ding fries are done.'); } - async function mapSingleUserToDb(msg, props) { + async function mapSingleUserToDb(msg) { if (msg.message.user.id !== 'UD46NSKSM' && msg.message.user.id !== 'U0231VDAB1B') { msg.reply('Sorry, can\'t do that https://i.imgur.com/Gp6wNZr.gif'); return; } - const { robot } = msg; const { mentions } = msg.message; if (!mentions) { msg.reply('You need to @ someone to map.'); @@ -88,15 +87,15 @@ module.exports = (robot) => { userMentions.shift(); // shift off @hubot } const to = userMentions.shift(); - const databaseService = new DatabaseService({ robot, ...props }); + const databaseService = new DatabaseService({ robot, ...procVars }); await databaseService.init(); const db = await databaseService.getDb(); - const web = new WebClient(msg.robot.adapter.options.token); + const web = new WebClient(robot.adapter.options.token); const { user } = await web.users.info({ user: to.id }); try { - msg.robot.logger.debug('Map this member', JSON.stringify(user)); + robot.logger.debug('Map this member', JSON.stringify(user)); const localMember = await databaseService.getUser(user.name); localMember.slackId = user.id; // eslint-disable-next-line no-underscore-dangle @@ -105,27 +104,52 @@ module.exports = (robot) => { msg.reply(`Mapping completed for ${to.name}: { name: ${localMember.name}, slackId: ${localMember.slackId}, id: ${localMember._id} }`); return; } - msg.robot.logger.debug(`Save the new member ${JSON.stringify(localMember)}`); + robot.logger.debug(`Save the new member ${JSON.stringify(localMember)}`); } catch (er) { - msg.robot.logger.error('failed to find', user, er); + robot.logger.error('failed to find', user, er); } } - async function unmapUsersToDb(msg, props) { + async function unmapUsersToDb(msg) { if (msg.message.user.id !== 'UD46NSKSM' && msg.message.user.id !== 'U0231VDAB1B') { msg.reply('Sorry, can\'t do that https://i.imgur.com/Gp6wNZr.gif'); return; } - const { robot } = msg; - const databaseService = new DatabaseService({ robot, ...props }); + const databaseService = new DatabaseService({ robot, ...procVars }); await databaseService.init(); try { const db = await databaseService.getDb(); await db.collection(scoresDocumentName).updateMany({}, { $unset: { slackId: 1 } }); } catch (er) { - msg.robot.logger.error('failed to unset all slack ids', er); + robot.logger.error('failed to unset all slack ids', er); } msg.reply('Ding fries are done. We unmapped all users'); } + + async function mapSlackIdToEmail(msg) { + if (msg.message.user.id !== 'UD46NSKSM' && msg.message.user.id !== 'U0231VDAB1B') { + msg.reply('Sorry, can\'t do that https://i.imgur.com/Gp6wNZr.gif'); + return; + } + + const databaseService = new DatabaseService({ robot, ...procVars }); + await databaseService.init(); + const db = await databaseService.getDb(); + + try { + const missingEmailUsers = await db.collection(scoresDocumentName).find({ slackId: { $exists: true }, slackEmail: { $exists: false } }); + const web = new WebClient(robot.adapter.options.token); + + for (const user of missingEmailUsers) { + robot.logger.debug('Map this member', user.slackId, user.name); + const { slackUser } = await web.users.info({ user: user.slackId }); + user.slackEmail = slackUser.profile.email; + await db.collection(scoresDocumentName).replaceOne({ slackId: user.slackId }, user); + msg.send(`Mapping completed for ${user.name}: { name: ${user.name}, slackId: <@${user.slackId}>, email: ${user.slackEmail} }`); + } + } catch (er) { + robot.logger.error('Error processing users', er); + } + } }; diff --git a/src/scoreboard.js b/src/scoreboard.js index d609d04..fe2c10d 100644 --- a/src/scoreboard.js +++ b/src/scoreboard.js @@ -1,3 +1,18 @@ +// Description: +// Hubot scoreboard for hubot-plusplus-expanded. +// +// Commands: +// @hubot score for @user - displays a snap shot of the user requested +// @hubot top scores 10 - displays top 10 (or any number) scores of all time +// @hubot bottom scores 5 - displays bottom 5 (or any number) scores of all time +// @hubot top tokens 7 - displays top 7 (or any number) tokens of all time +// @hubot bottom tokens 2 - displays top 2 (or any number) tokens of all time +// @hubot top scores 10 - displays top 10 scores of all time +// @hubot top scores 10 - displays top 10 scores of all time +// +// Author: +// O'Mutt (Matt@OKeefe.dev) + const moment = require('moment'); const clark = require('clark'); const _ = require('lodash'); @@ -14,7 +29,6 @@ module.exports = function plusPlus(robot) { robot.respond(regExpCreator.createTopBottomRegExp(), respondWithLeaderLoserBoard); robot.respond(regExpCreator.createTopBottomTokenRegExp(), respondWithLeaderLoserTokenBoard); robot.respond(regExpCreator.createTopPointGiversRegExp(), getTopPointSenders); - robot.respond(regExpCreator.createBotDayRegExp(robot.name), respondWithUsersBotDay); async function respondWithScore(msg) { const { mentions } = msg.message; @@ -139,20 +153,4 @@ module.exports = function plusPlus(robot) { return msg.send(message.join('\n')); } - - async function respondWithUsersBotDay(msg) { - let userToLookup = msg.message.user.name; - const isMy = msg.match[2].toLowerCase() !== 'my'; - let messageName = 'Your'; - robot.logger.debug(`respond with users bot day ${msg.match}`); - if (isMy) { - userToLookup = helpers.cleanName(msg.match[2]); - } - const user = await databaseService.getUser({ name: userToLookup }); - if (isMy) { - messageName = user.slackId ? `<@${user.slackId}>'s` : `${user.name}'s`; - } - const dateObj = new Date(user[`${robot.name}Day`]); - msg.send(`${messageName} ${robot.name}day is ${moment(dateObj).format('MM-DD-yyyy')}`); - } }; diff --git a/src/wallet.js b/src/wallet.js index a4e1f25..4256948 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -1,8 +1,16 @@ +// Description: +// Hubot crypto wallet. This is used to manage the users wallet within the hubot-plusplus-expanded module +// +// Commands: +// @hubot hot wallet - displays info for hubot's 'hot wallet' +// @hubot level up my account - moves the user's account up 1 level (e.g. 1->2) to allow them to start receiving crypto +// +// Author: +// O'Mutt (Matt@OKeefe.dev) const _ = require('lodash'); const Conversation = require('hubot-conversation'); const tokenBuddy = require('token-buddy'); - const helpers = require('./lib/helpers'); const regExpCreator = require('./lib/regexpCreator'); const DatabaseService = require('./lib/services/database'); diff --git a/test/scoreboard.test.js b/test/scoreboard.test.js index 35a6789..6fbf0a7 100644 --- a/test/scoreboard.test.js +++ b/test/scoreboard.test.js @@ -100,39 +100,4 @@ describe('Scoreboard', function () { ); }); }); - - describe('respondWithUsersBotDay', function respondWithUsersBotDay() { - it('should respond with the hubot day when asked', async function () { - room.user.say('matt.erickson', 'hubot when is my hubotday?'); - await new Promise((resolve) => setTimeout(resolve, 45)); - expect(room.messages[1][1]).to.equal('Your hubotday is 07-09-2020'); - }); - - it('should respond with the hubot day when asked about a different persons hubot day', async function () { - room.user.say('phil.bob', 'hubot what day is matt.erickson hubot day?'); - await new Promise((resolve) => setTimeout(resolve, 45)); - expect(room.messages[1][1]).to.equal( - '<@matt.erickson>\'s hubotday is 07-09-2020', - ); - }); - - it('should respond with the hubot day when asked about a different persons (with \') hubot day', async function () { - room.user.say('phil.bob', 'hubot what day is matt.erickson\'s hubot day?'); - await new Promise((resolve) => setTimeout(resolve, 45)); - expect(room.messages[1][1]).to.equal( - '<@matt.erickson>\'s hubotday is 07-09-2020', - ); - }); - - it('should respond with the hubot day when asked about a different persons (with space \') hubot day', async function () { - room.user.say( - 'phil.bob', - 'hubot what day is matt.erickson \'s hubot day?', - ); - await new Promise((resolve) => setTimeout(resolve, 45)); - expect(room.messages[1][1]).to.equal( - '<@matt.erickson>\'s hubotday is 07-09-2020', - ); - }); - }); });