Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
.env.development.local
.env.test.local
.env.production.local
config.env

npm-debug.log*
yarn-debug.log*
Expand Down
297 changes: 198 additions & 99 deletions controllers/v1/joinChapter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ const JoinChapter = require("../../../model/v1/joinChapter");
const Locations = require("../../../model/v1/Locations");
const Notifications = require("../../../model/v1/notifications");

import sendEmail from '../../../utils/email/sendEmail';
import emailTemplate from '../../../utils/email/chapter/joinChapterEmail';
import emailTemplate2 from '../../../utils/email/chapter/joinedChapterEmail';

import sendEmail from "../../../utils/email/sendEmail";
import emailTemplate from "../../../utils/email/chapter/joinChapterEmail";
import emailTemplate2 from "../../../utils/email/chapter/joinedChapterEmail";

// Validation
const validateJoinChapter = require("../../../validators/joinChapter");
Expand All @@ -24,86 +23,151 @@ const AppError = require("../../../utils/appError");
exports.createJoinChapter = async (req, res, next) => {
const { errors, isValid } = validateJoinChapter(req.body);
try {
const findLocation = await Locations.findById(req.body.chapterLocation_id).populate(
"_id"
);
const findLocation = await Locations.findById(
req.body.chapterLocation_id
).populate("_id");
if (!findLocation) {
console.log('no Chapter found')
errors.msg = "no Chapter found";
return AppError.validationError(res, UNAUTHORIZED, errors);
}

else if((findLocation.added_by).toString() === (req.user.id).toString()){
console.log('You can not join a chapter you created')
errors.msg = "You can not join a chapter you created";
return AppError.validationError(res, BAD_REQUEST, errors);
}

else{

let joinChapter = {
...req.body,
chapterLocation_id: req.body.chapterLocation_id,
user_id : req.user.id,
};


const JoinChapters = await JoinChapter.find({}).sort("-createdAt");

JoinChapters.map((joined) => {
if ((joined.chapterLocation_id).toString() === (req.body.chapterLocation_id).toString() && (joined.user_id).toString() === (req.user.id).toString()){
errors.msg = "Already joined this Chapter";
console.log("no Chapter found");
errors.msg = "no Chapter found";
return AppError.validationError(res, UNAUTHORIZED, errors);
}
})
} else if (findLocation.added_by.toString() === req.user.id.toString()) {
console.log("You can not join a chapter you created");
errors.msg = "You can not join a chapter you created";
return AppError.validationError(res, BAD_REQUEST, errors);
} else {
const JoinChapters = await JoinChapter.find({}).sort("-createdAt");
let err = false;
JoinChapters.some((joined) => {
if (
joined.chapterLocation_id.toString() ===
req.body.chapterLocation_id.toString() &&
joined.user_id.toString() === req.user.id.toString()
) {
console.log("you've already joined this chapter");
errors.msg = "you've already joined this chapter";
err = true;
return AppError.validationError(res, BAD_REQUEST, errors);
}
});
if (!err) {
let joinChapter = {
...req.body,
chapterLocation_id: req.body.chapterLocation_id,
user_id: req.user.id,
};
const newJoinChapter = await JoinChapter.create(joinChapter);

const newJoinChapter= await JoinChapter.create(joinChapter);
const subject = `Successfully Requested to join ${findLocation.LocationName} Chapter`;
sendEmail(
emailTemplate(
req.user.firstName,
findLocation.LocationName,
findLocation.location
),
subject,
req.user.email
);
const newNotification = await Notifications.create({
receiverId: req.user.id,
title: subject,
chapter_id: req.body.chapterLocation_id,
type: "Chapter",
authorId: null,
});

const subject = `Successfully Requested to join ${findLocation.LocationName} Chapter`;
sendEmail(emailTemplate(req.user.firstName, findLocation.LocationName, findLocation.location), subject, req.user.email);
const newNotification = await Notifications.create({receiverId: req.user.id, title: subject, chapter_id: req.body.chapterLocation_id, type: 'Chapter', authorId: null});
return successWithData(
res,
CREATED,
"request to join chapter sent successfully",
newJoinChapter
);
}
}
} catch (err) {
console.log(err);
return AppError.tryCatchError(res, err);
}
};


exports.getAllJoinedChapter = async (req, res, next) => {
try {
const JoinedChapters = await JoinChapter.find({})
.populate("user_id")
.populate("chapterLocation_id")
.sort("-createdAt");
return successWithData(
res,
CREATED,
"request to join chapter sent successfully",
newJoinChapter
OK,
"Joined Chapters fetched successfully",
JoinedChapters
);
}
} catch (err) {
console.log(err);
return AppError.tryCatchError(res, err);
}
};

exports.getAllJoinedChapter= async (req, res, next) => {
exports.getUserJoinRequests = async (req, res, next) => {
try {
const JoinedChapters = await JoinChapter.find({}).populate("user_id").populate("chapterLocation_id").sort("-createdAt");
return successWithData(res, OK, "Joined Chapters fetched successfully", JoinedChapters);
const userJoinRequests = await JoinChapter.find({ user_id: req.user.id })
.populate("user_id")
.populate("event_id")
.sort("-createdAt");
if (!userJoinRequests) {
let error = { message: "you have not any join Request" };
return AppError.tryCatchError(res, error);
}
return successWithData(
res,
OK,
"user Join Requests fetched successfully",
userJoinRequests
);
} catch (err) {
console.log(err);
return AppError.tryCatchError(res, err);
}
};

exports.getUserJoinRequests= async (req, res, next) => {
exports.getChapterUsers = async (req, res, next) => {
try {
const userJoinRequests = await JoinChapter.find({user_id : req.user.id}).populate("user_id").populate("event_id").sort("-createdAt");
if (!userJoinRequests) { let error = {message: "you have not any join Request"}; return AppError.tryCatchError(res, error);}
return successWithData(res, OK, "user Join Requests fetched successfully", userJoinRequests);
const userJoinRequests = await JoinChapter.find({
chapterLocation_id: req.params.id,
})
.populate("user_id")
.populate("chapterLocation_id")
.sort("-createdAt");
if (!userJoinRequests) {
let error = { message: "no members found" };
return AppError.tryCatchError(res, error);
}
return successWithData(
res,
OK,
"chapter's users fetched successfully",
userJoinRequests
);
} catch (err) {
console.log(err);
return AppError.tryCatchError(res, err);
}
};


exports.getJoinedChapter = async (req, res, next) => {
try {
const JoinedChapters = await JoinChapter.findById(req.params.id).populate("user_id").populate("chapterLocation_id").sort("-createdAt");
if (!JoinedChapters) { let error = {message: "undefined joined chapter"}; return AppError.tryCatchError(res, error);}
return successWithData(res, OK, "Joined Chapter fetched successfully", JoinedChapters);
const JoinedChapters = await JoinChapter.findById(req.params.id)
.populate("user_id")
.populate("chapterLocation_id")
.sort("-createdAt");
if (!JoinedChapters) {
let error = { message: "undefined joined chapter" };
return AppError.tryCatchError(res, error);
}
return successWithData(
res,
OK,
"Joined Chapter fetched successfully",
JoinedChapters
);
} catch (err) {
console.log(err);
return AppError.tryCatchError(res, err);
Expand All @@ -113,79 +177,114 @@ exports.getJoinedChapter = async (req, res, next) => {
exports.updateJoinedChapter = async (req, res, next) => {
try {
const JoinedChapterUpdate = await JoinChapter.findById(req.params.id);
if (!JoinedChapterUpdate) { let error = {message: "undefined join chapter"}; return AppError.tryCatchError(res, error);}
if (!JoinedChapterUpdate) {
let error = { message: "undefined join chapter" };
return AppError.tryCatchError(res, error);
}

const findLocation = await Locations.findById(req.body.chapterLocation_id).populate(
"_id"
);
if (!findLocation) {
console.log('no Chapter found')
errors.msg = "no Chapter found";
return AppError.validationError(res, UNAUTHORIZED, errors);
const findLocation = await Locations.findById(
req.body.chapterLocation_id
).populate("_id");
if (!findLocation) {
console.log("no Chapter found");
errors.msg = "no Chapter found";
return AppError.validationError(res, UNAUTHORIZED, errors);
}

if((findLocation.added_by._id).toString() !== (req.user.id).toString()){
console.log('You can not update this Chapter join request')
errors.msg = "You can not update this Chapter join request";
return AppError.validationError(res, BAD_REQUEST, errors);
if (findLocation.added_by._id.toString() !== req.user.id.toString()) {
console.log("You can not update this Chapter join request");
errors.msg = "You can not update this Chapter join request";
return AppError.validationError(res, BAD_REQUEST, errors);
}

let JoinedChapter = {
...req.body,
};
...req.body,
};

const modifiedJoinedChapter = await JoinChapter.findOneAndUpdate(
{ _id: req.params.id },
{ ...JoinedChapter },
{ new: true }
);
return successWithData(res, OK, "Joined Chapter modified", modifiedJoinedChapter);
return successWithData(
res,
OK,
"Joined Chapter modified",
modifiedJoinedChapter
);
} catch (err) {
console.log(err);
return AppError.tryCatchError(res, err);
}
};

exports.acceptJoinRequest = async (req, res, next) => {
try {
const findLocation = await Locations.findById(req.params.location).populate(
"_id"
);
if (!findLocation) {
console.log('no Chapter found')
let error = {message : "no Chapter found"};
return AppError.tryCatchError(res, error);
try {
const findLocation = await Locations.findById(req.body.location).populate(
"_id"
);
if (!findLocation) {
console.log("no Chapter found");
let error = { message: "no Chapter found" };
return AppError.tryCatchError(res, error);
}

if((findLocation.added_by._id).toString() !== (req.user.id).toString()){
console.log('You can not update this Chapter join request')
let error = {message : "You can not update this Chapter join request"};
return AppError.tryCatchError(res, error);
if (findLocation.added_by._id.toString() !== req.user.id.toString()) {
console.log("You can not update this Chapter join request");
let error = { message: "You can not update this Chapter join request" };
return AppError.tryCatchError(res, error);
}

const requestId = await JoinChapter.findById(req.params.id);
if (!requestId) { let error = {message: "undefined requestId"}; return AppError.tryCatchError(res, error);}

const acceptRequest = await JoinChapter.findOneAndUpdate(
{ _id: req.params.id },
{ approved: true }
);
if (!requestId) {
let error = { message: "undefined requestId" };
return AppError.tryCatchError(res, error);
}

const acceptRequest = await JoinChapter.findOneAndUpdate(
{ _id: req.params.id },
{ approved: true }
);
const location = await Locations.findOneAndUpdate(
{ _id: req.body.location },
{ member: findLocation.member + 1 }
);
const subject = `Successfully accepted to join ${findLocation.LocationName} Chapter`;
sendEmail(emailTemplate2(req.user.firstName, findLocation.LocationName, findLocation.location), subject, req.user.email);
const newNotification = await Notifications.create({receiverId: req.user.id, title: subject, chapter_id: findLocation._id, type: 'Chapter', authorId: null});
sendEmail(
emailTemplate2(
req.user.firstName,
findLocation.LocationName,
findLocation.location
),
subject,
req.user.email
);
const newNotification = await Notifications.create({
receiverId: req.user.id,
title: subject,
chapter_id: findLocation._id,
type: "Chapter",
authorId: null,
});

return successWithData(res, OK, "Request to join chapter approved", acceptRequest);
} catch (err) {
console.log(err);
return AppError.tryCatchError(res, err);
}
};
return successWithData(
res,
OK,
"Request to join chapter approved",
acceptRequest
);
} catch (err) {
console.log(err);
return AppError.tryCatchError(res, err);
}
};

exports.deleteJoinedChapter = async (req, res, file) => {
try {
const deleted = await JoinChapter.findOneAndDelete({ _id: req.params.id });
if (!deleted) { let error = {message: "undefined join chapter"}; return AppError.tryCatchError(res, error);}
if (!deleted) {
let error = { message: "undefined join chapter" };
return AppError.tryCatchError(res, error);
}
return successNoData(res, OK, "Joined Chapter deleted");
} catch (err) {
console.log(err);
Expand Down
Loading