Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: leaderboard api #3

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/controller/leaderboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { LeaderboardModel } = require("../schema/leaderboard");
const { WalletModel } = require("../schema/wallet");

exports.getRefferedCount = async function (req, res) {
try {
const address = req.params.address;

const result = await WalletModel.countDocuments({ referrer: address });
return res.status(200).json(result);
} catch (error) {
return res.status(500).json({ error: error.message });
}
};

exports.getVerifiedCount = async function (req, res) {
try {
const address = req.params.address;

const result = await LeaderboardModel.countDocuments({ referrer: address });
return res.status(200).json(result);
} catch (error) {
return res.status(500).json({ error: error.message });
}
};

exports.getAllLists = async function (req, res) {
try {
const wallets = await WalletModel.find({}).select("-_id -__v");

const results = await Promise.all(
wallets.map(async (wallet) => {
const leaderboardCount = await LeaderboardModel.countDocuments({ referrer: wallet.address });
return {
...wallet.toObject(),
leaderboardCount: leaderboardCount,
};
})
);

results.sort((a, b) => b.leaderboardCount - a.leaderboardCount);

return res.status(200).json(results);
} catch (error) {
return res.status(500).json({ error: error.message });
}
};
23 changes: 23 additions & 0 deletions src/controller/quest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { LeaderboardModel } = require("../schema/leaderboard");
const { QuestModel } = require("../schema/quest");
const { WalletModel } = require("../schema/wallet");

exports.getLists = async function (req, res) {
try {
Expand Down Expand Up @@ -28,6 +30,27 @@ exports.create = async function (req, res) {
{ upsert: true }
);

const count = await QuestModel.countDocuments({ address: requestBody.address });
if (count === 11) {
const wallet = await WalletModel.findOne(
{
address: requestBody.address,
},
"referrer"
);

if (wallet && wallet.referrer) {
await LeaderboardModel.findOneAndUpdate(
{ address: requestBody.address, referrer: wallet.referrer },
{
address: requestBody.address,
referrer: wallet.referrer,
},
{ upsert: true }
);
}
}

return res.status(200).json({
message: "Quest created.",
});
Expand Down
13 changes: 11 additions & 2 deletions src/controller/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports.getWallet = async function (req, res) {

exports.getAllWallets = async function (req, res) {
try {
const result = await WalletModel.find().select("-_id -__v").sort('-points');
const result = await WalletModel.find().select("-_id -__v").sort("-points");
return res.status(200).json(result);
} catch (error) {
return res.status(500).json({ error: error.message });
Expand Down Expand Up @@ -68,9 +68,18 @@ exports.create = async function (req, res) {
return res.status(400).json({ error: "Invalid request params" });
}
const refCode = generateReferralCode(requestBody.address);
const referral = requestBody.referral;

const referralWallet = await WalletModel.findOne(
{
refCode: referral,
},
"address"
);

await WalletModel.findOneAndUpdate(
{ address: requestBody.address },
{ $setOnInsert: { points: 0, refCode } },
{ $setOnInsert: { points: 0, refCode, referrer: referralWallet?.address ?? "" } },
{ upsert: true }
);

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const bodyParser = require("body-parser");
const tokensRouter = require("./router/TokenRoutes");
const walletRouter = require("./router/WalletRoutes");
const questRouter = require("./router/QuestRoutes");
const leaderboardRouter = require("./router/LeaderboardRoutes");

const port = parseInt(process.env.PORT || "1998");
const dbURI = process.env.MONGODB_URL || "mongodb://db:27017/__moniswap__";
Expand All @@ -24,6 +25,7 @@ app.use((_, res, next) => {
app.use("/tokens", tokensRouter);
app.use("/wallet", walletRouter);
app.use("/quest", questRouter);
app.use("/leaderboard", leaderboardRouter);

app.listen(port, () => {
console.info("Server running on %d", port);
Expand Down
9 changes: 9 additions & 0 deletions src/router/LeaderboardRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require("express");
const router = express.Router();
const LeaderboardController = require("../controller/leaderboard");

router.get("/getRefferedCount/:address", LeaderboardController.getRefferedCount);
router.get("/getVerifiedCount/:address", LeaderboardController.getVerifiedCount);
router.get("/getAllLists", LeaderboardController.getAllLists);

module.exports = router;
9 changes: 9 additions & 0 deletions src/schema/leaderboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mongoose = require("mongoose");

const leaderboardSchema = new mongoose.Schema({
address: String,
referrer: String,
});

const LeaderboardModel = mongoose.model("Leaderboard", leaderboardSchema);
module.exports = { LeaderboardModel };
1 change: 1 addition & 0 deletions src/schema/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const walletSchema = new mongoose.Schema({
address: { type: String, unique: true },
points: Number,
refCode: String,
referrer: String,
});

const WalletModel = mongoose.model("Wallet", walletSchema);
Expand Down
Loading