Skip to content

Commit

Permalink
Merge pull request #72 from impulsonetwork/dev
Browse files Browse the repository at this point in the history
Dev into Master
  • Loading branch information
lazaroalvarenga authored Nov 8, 2018
2 parents 77ba839 + 8e729a5 commit e439371
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 30 deletions.
6 changes: 6 additions & 0 deletions config/coreteam.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ members:
- UASHT1H7U
- UBV9QPYP9
- UCS9EMKG9
- U90418USW
- UATDAFP9T
- UCV6RPQRL
admins:
- UB348CP6Z
- UCJA2A8Q5
27 changes: 26 additions & 1 deletion controllers/interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
};
1 change: 1 addition & 0 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
};
Expand Down
9 changes: 8 additions & 1 deletion mocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
57 changes: 29 additions & 28 deletions routes/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -95,38 +96,38 @@ 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) => {
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;
37 changes: 37 additions & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};

0 comments on commit e439371

Please sign in to comment.