From c72bba55e92f5281dd5c9423cdf7ad32e8ee3b76 Mon Sep 17 00:00:00 2001 From: Lazaro Alvarenga Date: Tue, 6 Nov 2018 18:54:24 -0200 Subject: [PATCH 1/3] insert new members into config/coreteam.yml --- config/coreteam.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/coreteam.yml b/config/coreteam.yml index 563bb6fb..c4acaba2 100644 --- a/config/coreteam.yml +++ b/config/coreteam.yml @@ -13,3 +13,6 @@ members: - UASHT1H7U - UBV9QPYP9 - UCS9EMKG9 + - U90418USW + - UATDAFP9T + - UCV6RPQRL From c5902d63b0ec7f5db3b3da7f953c0891d964c21d Mon Sep 17 00:00:00 2001 From: Lazaro Alvarenga Date: Wed, 7 Nov 2018 17:06:09 -0200 Subject: [PATCH 2/3] Send manual points to impulsersC --- config/coreteam.yml | 3 ++ controllers/interaction.js | 27 +++++++++++++++++- controllers/user.js | 1 + mocks/index.js | 9 +++++- routes/bot.js | 58 ++++++++++++++++++++------------------ utils/index.js | 37 ++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 30 deletions(-) diff --git a/config/coreteam.yml b/config/coreteam.yml index 563bb6fb..558ebac9 100644 --- a/config/coreteam.yml +++ b/config/coreteam.yml @@ -13,3 +13,6 @@ members: - UASHT1H7U - UBV9QPYP9 - UCS9EMKG9 +admins: + - UB348CP6Z + - UCJA2A8Q5 diff --git a/controllers/interaction.js b/controllers/interaction.js index 84bb8cd7..037389e5 100644 --- a/controllers/interaction.js +++ b/controllers/interaction.js @@ -32,6 +32,15 @@ const normalize = data => { type: "thread", user: data.user }; + } else if (data.type === "manual") { + return { + type: data.type, + user: data.user, + value: data.value, + thread: false, + description: data.text, + channel: "mundão" + }; } else { return { channel: data.channel, @@ -144,10 +153,26 @@ export const lastMessage = async user => { return result || _throw("Error finding last interaction by user"); }; +const manualInteractions = async data => { + const InteractionModel = mongoose.model("Interaction"); + const interaction = normalize(data); + const instance = new InteractionModel(interaction); + const score = await todayScore(interaction.user); + const todayLimitStatus = config.xprules.limits.daily - score; + + if (todayLimitStatus > 0) { + const response = await instance.save(); + userController.update(interaction); + + return response || _throw("Error adding new manual interaction"); + } +}; + export default { find, remove, save, todayScore, - lastMessage + lastMessage, + manualInteractions }; diff --git a/controllers/user.js b/controllers/user.js index 431e8b11..5cff8aff 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -102,6 +102,7 @@ export const find = async (userId, isCoreTeam = false) => { slackId: userId, isCoreTeam: isCoreTeam }).exec(); + result.score = parseInt(result.score); return result || _throw("Error finding a specific user"); }; diff --git a/mocks/index.js b/mocks/index.js index e2d99e01..4ab923c0 100644 --- a/mocks/index.js +++ b/mocks/index.js @@ -47,9 +47,16 @@ const reactioRemoved = { event_ts: "1537559720.000100" }; +const manualPoints = { + type: "manual", + user: "UCX1DSFEV", + value: 20 +}; + export default { message, reactionAdded, reactioRemoved, - thread + thread, + manualPoints }; diff --git a/routes/bot.js b/routes/bot.js index ede7bb23..1f412659 100644 --- a/routes/bot.js +++ b/routes/bot.js @@ -2,9 +2,10 @@ import config from "config-yml"; import express from "express"; import request from "make-requests"; import bodyParser from "body-parser"; -import { analyticsSendBotCollect } from "../utils"; +import { analyticsSendBotCollect, getRanking } from "../utils"; import userController from "../controllers/user"; +import interactionController from "../controllers/interaction"; import { isCoreTeam } from "../utils"; const router = express.Router(); @@ -95,38 +96,39 @@ router.post("/feedback", urlencodedParser, async (req, res) => { return response; }); -const getRanking = async (req, isCoreTeamMember) => { - let users = []; - let myPosition = 0; +router.post("/sendpoints", urlencodedParser, async (req, res) => { + console.log("====================", req.body); let response = { - text: "Veja as primeiras pessoas do ranking:", - attachments: [] + text: "você tá tentando dar pontos prum coleguinha, né?!" }; + const value = +req.body.text.split("> ")[1]; + const userId = req.body.text + .split("|")[0] + .substring(2, req.body.text.split("|")[0].length); - try { - users = await userController.findAll(isCoreTeamMember, 5); - myPosition = await userController.rankingPosition( - req.body.user_id, - isCoreTeamMember - ); + if (config.coreteam.admins.some(user => user === req.body.user_id)) { + try { + await interactionController.manualInteractions({ + type: "manual", + user: userId, + text: `você recebeu esses ${value || 0} pontos de ${req.body + .user_name || "ninguém"}`, + value: value + }); + + response.text = `você tá dando ${value || 0} pontos para ${userId || + "ninguém"}`; + } catch (e) { + response.text = + "Ocorreu um erro nessa sua tentativa legal de dar pontos para outro coleguinha"; + console.log(e); + } + } else { response.text = - users.length === 0 ? "Ops! Ainda ninguém pontuou. =/" : response.text; - response.attachments = users.map((user, index) => ({ - text: `${index + 1}º lugar está ${ - user.slackId === req.body.user_id ? "você" : user.name - } com ${user.score} XP, no nível ${user.level}` - })); - - response.attachments.push({ - text: `Ah, e você está na posição ${myPosition} do ranking` - }); - - analyticsSendBotCollect(req.body); - } catch (e) { - console.log(e); + "Nobre cavaleiro(a) da casa de bronze, infelizmente sua armadura não dá permissão para tal façanha =/"; } - return response; -}; + res.json(response); +}); export default router; diff --git a/utils/index.js b/utils/index.js index 7f1e316f..15a1cb0a 100644 --- a/utils/index.js +++ b/utils/index.js @@ -2,6 +2,7 @@ import config from "config-yml"; import dotenv from "dotenv"; import request from "make-requests"; +import userController from '../controllers/user'; import { sendCollect, sendBotCollect } from "./analytics"; if (process.env.NODE_ENV !== "production") { dotenv.config(); @@ -61,6 +62,8 @@ export const calculateScore = interaction => { interaction.parentUser !== interaction.user ) { score = config.xprules.threads.send; + } else if (interaction.type === "manual") { + score = interaction.value; } return score; }; @@ -137,3 +140,37 @@ export const isCoreTeam = userId => { return !!allCoreTeam.find(member => member === userId); }; + +export const getRanking = async (req, isCoreTeamMember) => { + let users = []; + let myPosition = 0; + let response = { + text: "Veja as primeiras pessoas do ranking:", + attachments: [] + }; + + try { + users = await userController.findAll(isCoreTeamMember, 5); + myPosition = await userController.rankingPosition( + req.body.user_id, + isCoreTeamMember + ); + response.text = + users.length === 0 ? "Ops! Ainda ninguém pontuou. =/" : response.text; + response.attachments = users.map((user, index) => ({ + text: `${index + 1}º lugar está ${ + user.slackId === req.body.user_id ? "você" : user.name + } com ${user.score} XP, no nível ${user.level}` + })); + + response.attachments.push({ + text: `Ah, e você está na posição ${myPosition} do ranking` + }); + + analyticsSendBotCollect(req.body); + } catch (e) { + console.log(e); + } + + return response; +}; From dfb359738146c948b7426aad1cb6d7f5c591ab73 Mon Sep 17 00:00:00 2001 From: Lazaro Alvarenga Date: Wed, 7 Nov 2018 17:10:13 -0200 Subject: [PATCH 3/3] removing console.log --- routes/bot.js | 1 - utils/index.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/routes/bot.js b/routes/bot.js index 1f412659..1b297212 100644 --- a/routes/bot.js +++ b/routes/bot.js @@ -97,7 +97,6 @@ router.post("/feedback", urlencodedParser, async (req, res) => { }); router.post("/sendpoints", urlencodedParser, async (req, res) => { - console.log("====================", req.body); let response = { text: "você tá tentando dar pontos prum coleguinha, né?!" }; diff --git a/utils/index.js b/utils/index.js index 15a1cb0a..0264a56f 100644 --- a/utils/index.js +++ b/utils/index.js @@ -2,7 +2,7 @@ import config from "config-yml"; import dotenv from "dotenv"; import request from "make-requests"; -import userController from '../controllers/user'; +import userController from "../controllers/user"; import { sendCollect, sendBotCollect } from "./analytics"; if (process.env.NODE_ENV !== "production") { dotenv.config();