Skip to content

Commit

Permalink
Merge pull request #93 from impulsonetwork/dev
Browse files Browse the repository at this point in the history
Dev to Master
  • Loading branch information
lazaroalvarenga authored Dec 6, 2018
2 parents a584ee6 + 77dab95 commit 3ab4050
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 99 deletions.
84 changes: 45 additions & 39 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,46 +52,9 @@ const update = async interaction => {
const user = await UserModel.findOne({ slackId: interaction.user }).exec();

if (user) {
return UserModel.findOne({ slackId: interaction.user }, (err, doc) => {
if (err) {
throw new Error("Error updating user");
}
const newScore = doc.score + score;
doc.level = calculateLevel(newScore);
doc.score = newScore < 0 ? 0 : newScore;
doc.isCoreTeam = isCoreTeam(interaction.user);
doc.messages =
interaction.type === "message" ? doc.messages + 1 : doc.messages;
doc.replies =
interaction.type === "thread" ? doc.replies + 1 : doc.replies;
doc.reactions =
interaction.type === "reaction_added"
? doc.reactions + 1
: doc.reactions;
doc.reactions =
interaction.type === "reaction_removed"
? doc.reactions - 1
: doc.reactions;
doc.lastUpdate = Date.now();
doc.save();
return doc;
});
return updateUserData(UserModel, interaction, score);
} else {
const obj = {
avatar: userInfo.profile.image_72,
name: userInfo.profile.real_name,
level: 1,
score: score,
slackId: interaction.user,
messages: interaction.type === "message" ? 1 : 0,
replies: interaction.type === "thread" ? 1 : 0,
reactions: interaction.type === "reaction_added" ? 1 : 0,
lastUpdate: new Date(),
isCoreTeam: isCoreTeam(interaction.user)
};
const instance = new UserModel(obj);
sendHelloOnSlack(obj.slackId);
return instance.save();
return createUserData(userInfo, score, interaction, UserModel);
}
} else {
throw new Error(`Error: ${userInfo.error}`);
Expand Down Expand Up @@ -173,6 +136,49 @@ const findInactivities = async () => {
return result || _throw("Error finding inactivity users");
};

const createUserData = (userInfo, score, interaction, UserModel) => {
const obj = {
avatar: userInfo.profile.image_72,
name: userInfo.profile.real_name,
email: userInfo.profile.email,
level: 1,
score: score,
slackId: interaction.user,
messages: interaction.type === "message" ? 1 : 0,
replies: interaction.type === "thread" ? 1 : 0,
reactions: interaction.type === "reaction_added" ? 1 : 0,
lastUpdate: new Date(),
isCoreTeam: isCoreTeam(interaction.user)
};
const instance = new UserModel(obj);
sendHelloOnSlack(obj.slackId);
return instance.save();
};

const updateUserData = (UserModel, interaction, score) => {
return UserModel.findOne({ slackId: interaction.user }, (err, doc) => {
if (err) {
throw new Error("Error updating user");
}
const newScore = doc.score + score;
doc.level = calculateLevel(newScore);
doc.score = newScore < 0 ? 0 : newScore;
doc.isCoreTeam = isCoreTeam(interaction.user);
doc.messages =
interaction.type === "message" ? doc.messages + 1 : doc.messages;
doc.replies = interaction.type === "thread" ? doc.replies + 1 : doc.replies;
doc.reactions =
interaction.type === "reaction_added" ? doc.reactions + 1 : doc.reactions;
doc.reactions =
interaction.type === "reaction_removed"
? doc.reactions - 1
: doc.reactions;
doc.lastUpdate = Date.now();
doc.save();
return doc;
});
};

export default {
find,
findAll,
Expand Down
4 changes: 4 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const userSchema = new mongoose.Schema({
type: String,
required: true
},
email: {
type: String,
required: false
},
messages: {
type: Number,
required: false
Expand Down
32 changes: 9 additions & 23 deletions routes/ranking.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@ router.get("/", async (req, res) => {
console.log(e);
}

if (req.query.format === "json") {
res.send({
title: "Veja o Ranking do nosso game | Impulso Network",
users
});
} else {
res.render("ranking", {
title: "Veja o Ranking do nosso game | Impulso Network",
users
});
}
res.render("ranking", {
title: "Veja o Ranking do nosso game | Impulso Network",
users
});
});

router.get("/user/:id", async (req, res) => {
Expand All @@ -33,23 +26,16 @@ router.get("/user/:id", async (req, res) => {

try {
user = await userController.find(id);

user.next_level = config.levelrules.levels_range[user.level - 1];
} catch (e) {
console.log(e);
}

if (req.query.format === "json") {
res.send({
user
});
} else {
res.render("profile", {
title:
"Perfil da pessoa jogadora, pra saber tudo de legal que fez pra ter 9.990 XP",
user
});
}
res.render("profile", {
title:
"Perfil da pessoa jogadora, pra saber tudo de legal que fez pra ter 9.990 XP",
user
});
});

export default router;
7 changes: 0 additions & 7 deletions routes/resources.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import config from "config-yml";
import express from "express";

import userController from "../controllers/user";
const router = express.Router();

router.get("/normalize", async (req, res) => {
await userController.checkCoreTeam();
res.send({ x: config.coreteam.members });
});

export default router;
29 changes: 0 additions & 29 deletions routes/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,4 @@ slackEvents.on("reaction_removed", e => handleEvent(e));

slackEvents.on("error", console.error);

router.get("/user/:id", async (req, res) => {
let user = {};

try {
user = await await getUserInfo(req.params.id);
} catch (e) {
console.log(getStyleLog("red"), `\n-- ${e}`);
}

res.send({
user: user && user.profile
});
});

router.get("/channel/:id", async (req, res) => {
let channel = req.params.id;
if (isValidChannel(channel)) {
channel = await getChannelInfo(channel);
res.send({
channel: channel && channel.channel
});
} else {
res.send({
ok: false,
message: "Não estamos computando esse canal."
});
}
});

export default router;
2 changes: 1 addition & 1 deletion views/profile.pug
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ block yield
.col.col__small
h4 Reações dadas
p: strong= user.reactions
p.gamer__card-footer= `Faltam ${user.next_level - user.score} XP para chegar no Nível ${user.level + 1}`
p.gamer__card-footer= `Faltam ${parseInt(user.next_level - user.score)} XP para chegar no Nível ${user.level + 1}`
include includes/footer

0 comments on commit 3ab4050

Please sign in to comment.