Skip to content

Commit

Permalink
fix: removing winston wrapper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zjorge96 committed Nov 1, 2023
1 parent 505ff33 commit 1ba3659
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 128 deletions.
37 changes: 0 additions & 37 deletions service/apiwrappers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { SlackError } = require("./errors");
const winston = require("../winston");

async function userInfo(client, userId) {
const response = await client.users.info({ user: userId });
Expand All @@ -14,42 +13,6 @@ async function userInfo(client, userId) {
);
}

function winstonInfo(info, func, message) {
if (message.user.id) {
message.user = message.user.id;
}
winston.info(info, {
func: func,
callingUser: message.user,
slackMessage: message.text,
reactionEmoji: message.reaction,
});
}

function winstonDebug(info, func, message) {
if (message.reactions) {
message.text = message.reactions;
}
winston.debug(info, {
func: func,
callingUser: message.user,
slackMessage: message.text,
reactionEmoji: message.reaction,
});
}

function winstonError(info, func, message, userInfo) {
winston.error(info, {
func: func,
callingUser: message.user,
slackMessage: message.text,
error: userInfo.error,
});
}

module.exports = {
userInfo,
winstonInfo,
winstonDebug,
winstonError,
};
23 changes: 11 additions & 12 deletions service/balance.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const config = require("../config");
const winston = require("../winston");
const moment = require("moment-timezone");
const { winstonInfo, winstonError } = require("./apiwrappers");

const recognitionCollection = require("../database/recognitionCollection");
const goldenRecognitionCollection = require("../database/goldenRecognitionCollection");
Expand Down Expand Up @@ -63,20 +62,20 @@ async function dailyGratitudeRemaining(user, timezone) {
}

async function respondToBalance({ message, client }) {
winstonInfo(
"@gratibot balance Called",
"service.balance.respondToBalance",
message
);
winston.info("@gratibot balance Called", {
func: "service.balance.respondToBalance",
callingUser: message.user,
slackMessage: message.text,
});

const userInfo = await client.users.info({ user: message.user });
if (!userInfo.ok) {
winstonError(
"Slack API returned error from users.info",
"service.balance.respondToBalance",
message,
userInfo
);
winston.error("Slack API returned error from users.info", {
func: "service.balance.respondToBalance",
callingUser: message.user,
slackMessage: message.text,
error: userInfo.error,
});
await client.chat.postEphemeral({
channel: message.channel,
user: message.user,
Expand Down
32 changes: 15 additions & 17 deletions service/deduction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const deductionCollection = require("../database/deductionCollection");
const { userRegex } = require("../regex");
const { redemptionAdmins } = require("../config");
const monk = require("monk");
const { winstonInfo, winstonError } = require("./apiwrappers");

async function createDeduction(user, value, message = "") {
let timestamp = new Date();
Expand Down Expand Up @@ -59,11 +58,10 @@ async function isBalanceSufficent(user, deductionValue) {
}

async function respondToRefund({ message, client, admins = redemptionAdmins }) {
winstonInfo(
"@gratibot refund Called",
"service.deduction.respondToRefund",
message
);
winston.info("@gratibot refund Called", {
callingUser: message.user,
slackMessage: message.text,
});

if (!admins.includes(message.user)) {
await sendMessage(
Expand Down Expand Up @@ -105,20 +103,20 @@ async function respondToRefund({ message, client, admins = redemptionAdmins }) {
}

async function respondToDeduction({ message, client }) {
winstonInfo(
"@gratibot deduction Called",
"service.deduction.respondToDeduction",
message
);
winston.info("@gratibot deduction Called", {
func: "service.deduction.respondToDeduction",
callingUser: message.user,
slackMessage: message.text,
});

const userInfo = await client.users.info({ user: message.user });
if (!userInfo.ok) {
winstonError(
"Slack API returned error from users.info",
"service.deduction.respondToDeduction",
message,
userInfo
);
winston.error("Slack API returned error from users.info", {
func: "service.deduction.respondToDeduction",
callingUser: message.user,
slackMessage: message.text,
error: userInfo.error,
});
await sendEphemeralMessage(
client,
message.channel,
Expand Down
13 changes: 7 additions & 6 deletions service/golden-recognition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const winston = require("../winston");
const config = require("../config");
const goldenRecognitionCollection = require("../database/goldenRecognitionCollection");
const { userInfo, winstonInfo } = require("./apiwrappers");
const { userInfo } = require("./apiwrappers");
const {
handleAllErrors,
sendNotificationToReceivers,
Expand Down Expand Up @@ -49,11 +50,11 @@ async function giverGoldenSlackNotification(gratitude) {
}

async function respondToGoldenRecognitionMessage({ message, client }) {
winstonInfo(
`Heard reference to ${goldenRecognizeEmoji}`,
"service.recognition.respondToGoldenRecognitionMessage",
message
);
winston.info(`Heard reference to ${goldenRecognizeEmoji}`, {
func: "service.recognition.respondToGoldenRecognitionMessage",
callingUser: message.user,
slackMessage: message.text,
});
let allUsers = [];
let gratitude;
try {
Expand Down
18 changes: 10 additions & 8 deletions service/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
goldenRecognizeEmoji,
} = require("../config");
const winston = require("../winston");
const { winstonInfo } = require("./apiwrappers");

const helpMarkdown = `
:wave: Hi there! Let's take a look at what I can do!
Expand Down Expand Up @@ -95,8 +94,11 @@ Upon receiving the golden fistbump, the user will receive 20 fistbumps and will
`;

async function respondToHelp({ message, client }) {
winstonInfo("@gratibot help Called", "feature.help.respondToHelp", message);

winston.info(`@gratibot help Called`, {
func: "feature.help.respondToHelp",
callingUser: message.user,
slackMessage: message.text,
});
await client.chat.postEphemeral({
channel: message.channel,
user: message.user,
Expand All @@ -118,11 +120,11 @@ const thunderfuryResponse = [
].join(" ");

async function respondToEasterEgg({ message, say }) {
winstonInfo(
"heard reference to thunderfury",
"feature.help.respondToEasterEgg",
message
);
winston.info(`Heard reference to thunderfury`, {
func: "feature.help.respondToEasterEgg",
callingUser: message.user,
slackMessage: message.text,
});

await say(thunderfuryResponse);

Expand Down
20 changes: 9 additions & 11 deletions service/leaderboard.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const winston = require("../winston");
const recognition = require("./recognition");
const { goldenFistbumpHolder } = require("./golden-recognition");
const { winstonInfo } = require("./apiwrappers");

const rank = [
"1st",
Expand Down Expand Up @@ -276,11 +275,11 @@ function convertToScores(leaderboardData) {
* this call.
*/
async function respondToLeaderboard({ message, client }) {
winstonInfo(
"@gratibot leaderboard Called",
"service.leaderboard.respondToLeaderboard",
message
);
winston.info("@gratibot leaderboard Called", {
func: "service.leaderboard.respondToLeaderboard",
callingUser: message.user,
slackMessage: message.text,
});
await client.chat.postEphemeral({
channel: message.channel,
user: message.user,
Expand All @@ -302,11 +301,10 @@ async function respondToLeaderboard({ message, client }) {
*/
async function updateLeaderboardResponse({ ack, body, action, respond }) {
await ack();
winstonInfo(
"Gratibot interactive leaderboard button clicked",
"service.leaderboard.updateLeaderboardResponse",
body
);
winston.info("Gratibot interactive leaderboard button clicked", {
func: "service.leaderboard.updateLeaderboardResponse",
callingUser: body.user.id,
});

await respond({
blocks: await createLeaderboardBlocks(action.value),
Expand Down
20 changes: 9 additions & 11 deletions service/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const winston = require("../winston");
const moment = require("moment-timezone");

const recognition = require("./recognition");
const { winstonInfo } = require("./apiwrappers");

async function createMetricsBlocks(timeRange) {
let blocks = [];
Expand Down Expand Up @@ -182,11 +181,11 @@ function aggregateData(response, timeRange) {
}

async function respondToMetrics({ message, client }) {
winstonInfo(
"@gratibot metrics Called",
"service.metrics.respondToMetrics",
message
);
winston.info("@gratibot metrics Called", {
func: "service.metrics.respondToMetrics",
callingUser: message.user,
slackMessage: message.text,
});
await client.chat.postEphemeral({
channel: message.channel,
user: message.user,
Expand All @@ -202,11 +201,10 @@ async function respondToMetrics({ message, client }) {

async function updateMetricsResponse({ ack, body, action, respond }) {
await ack();
winstonInfo(
"Gratibot interactive metrics button clicked",
"service.metrics.updateMetricsResponse",
body
);
winston.info("Gratibot interactive metrics button clicked", {
func: "service.metrics.updateMetricsResponse",
callingUser: body.user.id,
});

await respond({
blocks: await createMetricsBlocks(action.value),
Expand Down
42 changes: 24 additions & 18 deletions service/recognition.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const goldenRecognitionCollection = require("../database/goldenRecognitionCollec
const balance = require("./balance");
const { SlackError, GratitudeError } = require("./errors");
const winston = require("../winston");
const { userInfo, winstonInfo, winstonDebug } = require("./apiwrappers");
const { userInfo } = require("./apiwrappers");
const {
handleAllErrors,
sendNotificationToReceivers,
Expand Down Expand Up @@ -330,18 +330,21 @@ async function giverSlackNotification(gratitude) {
}

async function respondToRecognitionMessage({ message, client }) {
winstonInfo(
`Heard reference to ${recognizeEmoji}`,
"service.recognition.respondToRecognitionMessage",
message
);
winston.info(`Heard reference to ${recognizeEmoji}`, {
func: "service.recognition.respondToRecognitionMessage",
callingUser: message.user,
slackMessage: message.text,
});
try {
const gratitude = await buildGratitudeObject({ message, client });
await validateAndSendGratitude(gratitude);
winstonDebug(
winston.debug(
`validated and stored message recognitions from ${gratitude.giver}`,
"service.recognition.respondToRecognitionMessage",
message
{
func: "service.recognition.respondToRecognitionMessage",
callingUser: message.user,
slackMessage: message.text,
}
);

return Promise.all([
Expand All @@ -355,11 +358,11 @@ async function respondToRecognitionMessage({ message, client }) {
}

async function respondToRecognitionReaction({ event, client }) {
winstonInfo(
`Saw a reaction containing ${reactionEmoji}`,
"service.recognition.respondToRecognitionReaction",
event
);
winston.info(`Saw a reaction containing ${reactionEmoji}`, {
func: "service.recognition.respondToRecognitionReaction",
callingUser: event.user,
reactionEmoji: event.reaction,
});
try {
event.channel = event.item.channel;
const { gratitude, originalMessage } = await buildGratitudeFromReaction({
Expand All @@ -372,10 +375,13 @@ async function respondToRecognitionReaction({ event, client }) {
}

await validateAndSendGratitude(gratitude);
winstonDebug(
`validated and stored message recognitions from ${gratitude.giver}`,
"service.recognition.respondToRecognitionReaction",
event
winston.debug(
`validated and stored reaction recognitions from ${gratitude.giver}`,
{
func: "service.recognition.respondToRecognitionReaction",
callingUser: event.user,
slackMessage: event.reactions,
}
);

return Promise.all([
Expand Down
13 changes: 5 additions & 8 deletions service/redeem.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const config = require("../config");
const fs = require("fs");

const winston = require("../winston");
const balance = require("./balance");
const deduction = require("./deduction");

const { winstonInfo } = require("./apiwrappers");

const path = require("path");
// TODO: Long term this should be sourced from DB
let rawdata = fs.readFileSync(path.resolve(__dirname, "../rewards.json"));
Expand Down Expand Up @@ -132,12 +131,10 @@ function getSelectedItemDetails(selectedItem) {
}

async function respondToRedeem({ message, client }) {
winstonInfo(
"@gratibot redeem Called",
"service.redeem.respondToRedeem",
message
);

winston.info("@gratibot redeem Called", {
callingUser: message.user,
slackMessage: message.text,
});
const currentBalance = await balance.currentBalance(message.user);
await client.chat.postEphemeral({
channel: message.channel,
Expand Down

0 comments on commit 1ba3659

Please sign in to comment.