Skip to content

Commit

Permalink
feat: set tokens in cookies , feat: add 2fa routes
Browse files Browse the repository at this point in the history
  • Loading branch information
prajwalbhatia committed Apr 8, 2024
1 parent 1bd826c commit de3aa2f
Show file tree
Hide file tree
Showing 10 changed files with 2,865 additions and 224 deletions.
4 changes: 3 additions & 1 deletion controllers/recentActivities.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const getRecentActivities = asyncHandler(async (req, res , next) => {

const userId = req.userId;
const recentActivities = await RecentActivity.find({ userId });
res.status(200).json(recentActivities);

const sortedData = recentActivities.sort((a, b) => new Date(b.date) - new Date(a.date));
res.status(200).json(sortedData);
});


207 changes: 140 additions & 67 deletions controllers/streak.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Streak from '../models/streak.js';
import Reward from '../models/reward.js';
import RecentActivity from '../models/recentActivity.js';
import Streak from "../models/streak.js";
import Reward from "../models/reward.js";
import RecentActivity from "../models/recentActivity.js";
import StreakDetail from "../models/streakDetail.js";

import moment from 'moment';
import moment from "moment";
const ObjectId = mongoose.Types.ObjectId;

import mongoose from 'mongoose';
import asyncHandler from 'express-async-handler';
import mongoose from "mongoose";
import asyncHandler from "express-async-handler";

import { activityObj, throwError } from '../utils.js';
import { activityObj, throwError } from "../utils.js";

export const getStreaks = asyncHandler(async (req, res, next) => {
if (!req.userId) {
Expand All @@ -20,20 +20,20 @@ export const getStreaks = asyncHandler(async (req, res, next) => {
//Aggregating the reward with streak
const streaks = await Streak.aggregate([
{
$match: { userId: userId }
$match: { userId: userId },
},
{
$lookup: {
from: 'rewards',
localField: '_id',
foreignField: 'streakId',
as: 'rewards'
}
from: "rewards",
localField: "_id",
foreignField: "streakId",
as: "rewards",
},
},
]);

res.status(200).json(streaks);
})
});

export const getStreak = asyncHandler(async (req, res, next) => {
const streakId = req.params.id;
Expand All @@ -46,31 +46,36 @@ export const getStreak = asyncHandler(async (req, res, next) => {
//Aggregating the reward with streak
const streaks = await Streak.aggregate([
{
$match: { _id: ObjectId(streakId) }
$match: { _id: ObjectId(streakId) },
},
{
$lookup: {
from: 'rewards',
localField: '_id',
foreignField: 'streakId',
as: 'rewards'
}
from: "rewards",
localField: "_id",
foreignField: "streakId",
as: "rewards",
},
},
]);

res.status(200).json(streaks);
})
});

export const createStreak = asyncHandler(async (req, res, next) => {
if (!req.userId) {
throwError(next)
throwError(next);
}

const streak = req.body;
streak.userId = req.userId;

const newStreak = new Streak(streak);
const activity = activityObj(req.userId, 'create-streak', streak.title, moment().format());
const activity = activityObj(
req.userId,
"create-streak",
streak.title,
moment().format()
);
const newActivity = new RecentActivity(activity);
// try {
await newStreak.save();
Expand All @@ -79,14 +84,19 @@ export const createStreak = asyncHandler(async (req, res, next) => {
res.status(201).json(newStreak);
});

export const deleteStreak = async (req, res , next) => {
export const deleteStreak = async (req, res, next) => {
const streakId = req.params.id;
if (!req.userId) {
throwError(next);
}

const streak = await Streak.findByIdAndDelete(streakId);
const activity = activityObj(req.userId, 'delete-streak', streak.title, new Date());
const activity = activityObj(
req.userId,
"delete-streak",
streak.title,
new Date()
);
const newActivity = new RecentActivity(activity);
await newActivity.save();

Expand All @@ -97,60 +107,123 @@ export const deleteStreak = async (req, res , next) => {
//Deleting streak detail realated to streak
await StreakDetail.deleteMany({ streakId });
res.status(201).json({ message: "Streak deleted successfully" });
}

export const deleteStreakAndRewardUpdate = asyncHandler(async (req, res , next) => {
const streakId = req.params.id;
if (!req.userId) {
throwError(next);
};

export const deleteStreakAndRewardUpdate = asyncHandler(
async (req, res, next) => {
const streakId = req.params.id;
if (!req.userId) {
throwError(next);
}
//Getting the rewards associated with particular streak
//and removing the streak id and date field
let userId = "";
const rewards = await Reward.find({ streakId }).lean();

if (rewards.length > 0) {
userId = rewards[0].userId;
await Promise.all(
rewards.map(async (reward) => {
if (!reward.rewardEarned) {
let updatedReward = { ...reward };
delete updatedReward.streakId;
delete updatedReward.date;

await Reward.findByIdAndUpdate(reward._id, updatedReward, {
new: true,
overwrite: true,
});
}
})
);
}

//Finding the streak and deleting
const streak = await Streak.findByIdAndDelete(streakId);
const activity = activityObj(
req.userId,
"delete-streak",
streak.title,
new Date()
);
const newActivity = new RecentActivity(activity);
await newActivity.save();

if (!streak) {
throwError(404, `Streak not found with id ${streakId}`, next);
}

// //Deleting streak detail realated to streak
await StreakDetail.deleteMany({ streakId });
res.status(201).json({ message: "Streak deleted successfully" });
}
//Getting the rewards assosiated with particular streak
//and removing the streak id and date field
let userId = '';
const rewards = await Reward.find({ streakId }).lean();

if (rewards.length > 0) {
userId = rewards[0].userId;
await Promise.all(rewards.map(async (reward) => {
if (!reward.rewardEarned) {
let updatedReward = { ...reward }
delete updatedReward.streakId;
delete updatedReward.date

await Reward.findByIdAndUpdate(reward._id, updatedReward, { new: true, overwrite: true });
}
}));
);

export const deleteStreakAndRewardDelete = asyncHandler(
async (req, res, next) => {
const streakId = req.params.id;
if (!req.userId) {
throwError(next);
}
//Getting the rewards associated with particular streak
//and removing the streak id and date field
let userId = "";
const rewards = await Reward.find({ streakId }).lean();

if (rewards.length > 0) {
userId = rewards[0].userId;
await Promise.all(
rewards.map(async (reward) => {
if (!reward.rewardEarned) {
await Reward.findByIdAndDelete(reward._id);
}
})
);
}

//Finding the streak and deleting
const streak = await Streak.findByIdAndDelete(streakId);
const activity = activityObj(
req.userId,
"delete-streak",
streak.title,
new Date()
);
const newActivity = new RecentActivity(activity);
await newActivity.save();

if (!streak) {
throwError(404, `Streak not found with id ${streakId}`, next);
}

// //Deleting streak detail realated to streak
await StreakDetail.deleteMany({ streakId });
res.status(201).json({ message: "Streak deleted successfully" });
}
);

//Finding the streak and deleting
const streak = await Streak.findByIdAndDelete(streakId);
const activity = activityObj(req.userId, 'delete-streak', streak.title, new Date());
const newActivity = new RecentActivity(activity);
await newActivity.save();

if (!streak) {
throwError(404, `Streak not found with id ${streakId}`, next);
}

// //Deleting streak detail realated to streak
await StreakDetail.deleteMany({ streakId });
res.status(201).json({ message: "Streak deleted successfully" });
})

export const updateStreak = asyncHandler(async (req, res , next) => {
export const updateStreak = asyncHandler(async (req, res, next) => {
const { id: _id } = req.params;
if (!req.userId) {
throwError(next);
}

const streak = req.body;
if (!mongoose.Types.ObjectId.isValid(_id)) throwError(404, `${_id} is invalid`, next);
if (!mongoose.Types.ObjectId.isValid(_id))
throwError(404, `${_id} is invalid`, next);

const updatedStreak = await Streak.findByIdAndUpdate(_id, streak, { new: true });
const updatedStreak = await Streak.findByIdAndUpdate(_id, streak, {
new: true,
});
if (!updateStreak) throwError(404, `Streak not found with id ${_id}`, next);
const activity = activityObj(req.userId, 'update-streak', streak.title, moment().format());
const activity = activityObj(
req.userId,
"update-streak",
streak.title,
moment().format()
);
const newActivity = new RecentActivity(activity);
await newActivity.save();

res.json(updatedStreak);
})
});
Loading

0 comments on commit de3aa2f

Please sign in to comment.