Skip to content

Commit

Permalink
userId rename to id from auth in token and ad controlller and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m-a-y-a-n-k committed Aug 16, 2024
1 parent 6ccda3c commit c179101
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions backend/src/controllers/ad/checkAdEligibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ const User = require("../../models/User");

async function checkAdEligibility(req, res) {
try {
const { userId } = req.user; // Extract userId from authenticated user
const { id } = req.user; // Extract id from authenticated user

// Check the cache first
let cachedData = cache.get(`adEligibility:${userId}`);
let cachedData = cache.get(`adEligibility:${id}`);
if (!cachedData) {
// If not cached, fetch from the database
const user = await User.findById(userId);
const user = await User.findById(id);
if (!user) {
return res.status(404).json({ error: "User not found" });
}

// Cache the eligibility status
cachedData = user.adEligible;
cache.set(`adEligibility:${userId}`, cachedData);
cache.set(`adEligibility:${id}`, cachedData);
}

// Respond with the cached or retrieved eligibility status
Expand Down
26 changes: 13 additions & 13 deletions backend/src/controllers/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const User = require("../models/User");
const tokenController = {
async earnTokens(req, res) {
try {
const { userId } = req.user; // Extract userId from authenticated user
const { id } = req.user; // Extract id from authenticated user
const { adId } = req.body;

// Validate adId
Expand All @@ -13,10 +13,10 @@ const tokenController = {
}

// Check the cache first
let cachedData = cache.get(`user:${userId}`);
let cachedData = cache.get(`user:${id}`);
if (!cachedData) {
// If not cached, fetch from database
const user = await User.findById(userId);
const user = await User.findById(id);
if (!user) {
return res.status(404).send({ error: "User not found" });
}
Expand All @@ -25,7 +25,7 @@ const tokenController = {
adEligible: user.adEligible,
};
// Cache the relevant data
cache.set(`user:${userId}`, cachedData);
cache.set(`user:${id}`, cachedData);
}

if (!cachedData.adEligible) {
Expand All @@ -39,10 +39,10 @@ const tokenController = {
cachedData.tokens += tokensEarned;

// Update the user's token balance in the database
await User.findByIdAndUpdate(userId, { tokens: cachedData.tokens });
await User.findByIdAndUpdate(id, { tokens: cachedData.tokens });

// Update the cache
cache.set(`user:${userId}`, cachedData);
cache.set(`user:${id}`, cachedData);

// Send back the updated token balance
res.status(200).send({
Expand All @@ -57,7 +57,7 @@ const tokenController = {

async spendTokens(req, res) {
try {
const { userId } = req.user; // Extract userId from authenticated user
const { id } = req.user; // Extract id from authenticated user
const { tokens } = req.body;

// Validate token amount
Expand All @@ -68,10 +68,10 @@ const tokenController = {
}

// Check the cache first
let cachedData = cache.get(`user:${userId}`);
let cachedData = cache.get(`user:${id}`);
if (!cachedData) {
// If not cached, fetch from database
const user = await User.findById(userId);
const user = await User.findById(id);
if (!user) {
return res.status(404).send({ error: "User not found" });
}
Expand All @@ -80,7 +80,7 @@ const tokenController = {
adEligible: user.adEligible,
};
// Cache the relevant data
cache.set(`user:${userId}`, cachedData);
cache.set(`user:${id}`, cachedData);
}

if (!cachedData.adEligible) {
Expand All @@ -99,14 +99,14 @@ const tokenController = {
cachedData.adEligible = false;

// Update the user's token balance and ad eligibility in the database
await User.findByIdAndUpdate(userId, {
await User.findByIdAndUpdate(id, {
tokens: cachedData.tokens,
adEligible: false,
});

// Update the cache
cache.set(`user:${userId}`, cachedData);
cache.set(`adEligibility:${userId}`, false);
cache.set(`user:${id}`, cachedData);
cache.set(`adEligibility:${id}`, false);

// Send back the updated token balance
res.status(200).send({
Expand Down
8 changes: 4 additions & 4 deletions backend/tests/unit/controllers/adController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("Ad Controller", () => {

beforeEach(() => {
// Mock request and response objects
req = { user: { userId: "user123" } };
req = { user: { id: "user123" } };
res = {
status: sinon.stub().returnsThis(),
json: sinon.stub(),
Expand Down Expand Up @@ -50,7 +50,7 @@ describe("Ad Controller", () => {
// Stub the User.findById method to return a user with adEligible: false
sinon.stub(User, "findById").resolves({ adEligible: false });

const req = { user: { userId: "user456" } };
const req = { user: { id: "user456" } };

await adController.checkAdEligibility(req, res);

Expand All @@ -69,7 +69,7 @@ describe("Ad Controller", () => {
// Stub the User.findById method to return null
sinon.stub(User, "findById").resolves(null);

const req = { user: { userId: "user789" } };
const req = { user: { id: "user789" } };

await adController.checkAdEligibility(req, res);

Expand All @@ -88,7 +88,7 @@ describe("Ad Controller", () => {
// Stub the User.findById method to throw an error
sinon.stub(User, "findById").throws(new Error("User lookup failed"));

const req = { user: { userId: "user000" } };
const req = { user: { id: "user000" } };

await adController.checkAdEligibility(req, res);

Expand Down
14 changes: 7 additions & 7 deletions backend/tests/unit/controllers/tokenController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("Token Controller", () => {
beforeEach(() => {
// Mock request and response objects
req = {
user: { userId: new mongoose.Types.ObjectId() }, // Mock authenticated user
user: { id: new mongoose.Types.ObjectId() }, // Mock authenticated user
body: {},
};
res = {
Expand All @@ -22,7 +22,7 @@ describe("Token Controller", () => {

// Mock a user object
user = {
_id: req.user.userId,
_id: req.user.id,
tokens: 100,
adEligible: true,
save: sinon.stub().resolves(),
Expand All @@ -31,7 +31,7 @@ describe("Token Controller", () => {
sinon.stub(User, "findById").resolves(user); // Mock the User.findById method
sinon.stub(User, "findByIdAndUpdate").resolves();
sinon.stub(cache, "get").callsFake((key) => {
if (key === `user:${req.user.userId}`) {
if (key === `user:${req.user.id}`) {
return {
tokens: user.tokens,
adEligible: user.adEligible,
Expand Down Expand Up @@ -91,7 +91,7 @@ describe("Token Controller", () => {
})
).to.be.true;
expect(
cache.set.calledWith(`user:${req.user.userId}`, {
cache.set.calledWith(`user:${req.user.id}`, {
tokens: 150,
adEligible: true,
})
Expand Down Expand Up @@ -158,13 +158,13 @@ describe("Token Controller", () => {
})
).to.be.true;
expect(
cache.set.calledWith(`user:${req.user.userId}`, {
cache.set.calledWith(`user:${req.user.id}`, {
tokens: 20,
adEligible: false,
})
).to.be.true; // Check cache update
expect(cache.set.calledWith(`adEligibility:${req.user.userId}`, false)).to
.be.true; // Check cache update
expect(cache.set.calledWith(`adEligibility:${req.user.id}`, false)).to.be
.true; // Check cache update
});
});
});

0 comments on commit c179101

Please sign in to comment.