Skip to content
This repository has been archived by the owner on Nov 14, 2019. It is now read-only.

Commit

Permalink
Refactor doBlock to use the saveStandupReport helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mgwalker committed Feb 16, 2017
1 parent 9a931ac commit b72fe92
Showing 1 changed file with 48 additions and 90 deletions.
138 changes: 48 additions & 90 deletions lib/helpers/doBlock.js
Original file line number Diff line number Diff line change
@@ -1,116 +1,74 @@
'use strict';

var log = require('../../getLogger')('block recorder');
var moment = require('moment');
var timeHelper = require('./time');
var standupHelper = require('./getStandupReport');
var reportHelper = require('./doChannelReport');
var models = require('../../models');
const log = require('../../getLogger')('block recorder');
const moment = require('moment');
const timeHelper = require('./time');
const saveStandupReport = require('./saveStandupReport');
const standupHelper = require('../bot/message-builders').standupReport.getStandupReportAttachment;
const reportHelper = require('./doChannelReport');
const models = require('../../models');

module.exports = function doBlock(bot, message, blockChannel, blockUser) {
log.verbose('Got user standup info:\n' + message.match[0]);

var localReport = '';
var content = message.match[4];
var userRealName, thumbUrl;
const content = message.match[4];

models.Channel.findOne({
where: {
name: blockChannel
}
}).then(function (channel) {
if (channel) {
models.Standup.findOrCreate({
where: {
channel: blockChannel,
date: timeHelper.getReportFormat(),
user: message.user
}
}).then(function (standup) {
var notes = content.trim().split(/\n/);
var yesterday = standup.yesterday;
var today = standup.today;
var blockers = standup.blockers;
var goal = standup.goal;
const notes = content.trim().split(/\n/);

const newStandup = {
channel: blockChannel,
date: timeHelper.getReportFormat(),
user: message.user
};

for (var note in notes) {
localReport += '\n\n';
var item = notes[note].replace(/\n/g,'');
var firstChar = item[0].toLowerCase();
item = item.replace(/^[ytbg]:?\s*/i, '');
for (var note in notes) {
let item = notes[note].replace(/\n/g,'');
const firstChar = item[0].toLowerCase();
item = item.replace(/^[ytbg]:?\s*/i, '');

switch (firstChar) {
case 'y':
localReport += '*Yesterday*\n' + item;
yesterday = item;
switch (firstChar) {
case 'y':
newStandup.yesterday = item;
break;
case 't':
localReport += '*Today*\n' + item;
today = item;

case 't':
newStandup.today = item;
break;
case 'b':
localReport += '*Blockers*\n' + item;
blockers = item;

case 'b':
newStandup.blockers = item;
break;
case 'g':
localReport += '*Goal*\n' + item;
goal = item;

case 'g':
updatenewStandupdStandup.goal = item;
break;
default:

default:
log.info('no match for '+item);
}
}
}

bot.api.users.info({'user':message.user}, function(err, response) {
userRealName = response.user.real_name || response.user.name;
thumbUrl = response.user.profile.image_72;
models.Standup.update(
{
yesterday: yesterday,
today: today,
blockers: blockers,
goal: goal,
userRealName: userRealName,
thumbUrl: thumbUrl
},
{
where: {
channel: blockChannel,
date: timeHelper.getReportFormat(),
user: message.user
}
}
).then(function () {
models.Standup.findOne({
where: {
channel: blockChannel,
date: timeHelper.getReportFormat(),
user: message.user
}
}).then(function(standup) {
var now = timeHelper.getDisplayFormat();
var channelTime = timeHelper.getDisplayFormat(channel.time);
if (moment(now, 'hh:mm a Z').isBefore(moment(channelTime, 'hh:mm a Z'))) {
log.verbose('Standup info recorded for ' + userRealName);
bot.reply(message, {
text: 'Thanks! Your standup for <#'+blockChannel+
'> is recorded and will be reported at ' +
timeHelper.getDisplayFormat(channel.time) +
'. It will look like:',
attachments: [ standupHelper(standup) ]
});
} else {
log.verbose('Late report from '+userRealName+'; updating previous report');
bot.reply(message, {
text: 'Thanks! Your standup for <#'+blockChannel+
'> is recorded. It will look like:',
attachments: [ standupHelper(standup) ]
});
reportHelper(bot, blockChannel, true, userRealName);
}
});
saveStandupReport(bot, newStandup).then(out => {
if(out.updatedExistingReport) {
log.verbose(`Late report from ${out.standup.userRealName}; updating previous report`);
bot.reply(message, {
text: `Thanks! Your standup for <#${blockChannel}> is recorded. It will look like:`,
attachments: [ standupHelper(out.standup) ]
});
});
reportHelper(bot, blockChannel, true, out.standup.userRealName);
} else {
log.verbose(`Standup info recorded for ${out.standup.userRealName}`);
bot.reply(message, {
text: `Thanks! Your standup for <#${blockChannel}> is recorded and will be reported at ${timeHelper.getDisplayFormat(channel.time)}. It will look like:`,
attachments: [ standupHelper(out.standup) ]
});
}
});
}
});
Expand Down

0 comments on commit b72fe92

Please sign in to comment.