shell bypass 403
const express = require('express');
const multer = require('multer');
const ffmpeg = require('fluent-ffmpeg');
const ffmpegStatic = require('ffmpeg-static');
const path = require('path');
const fs = require('fs');
const router = express.Router();
const User = require('../../schema/User')
const Project = require('../../schema/Project')
const jwt = require('jsonwebtoken');
const JWT_SECRET = "E4d4U$er";
// Set the ffmpeg path
ffmpeg.setFfmpegPath(ffmpegStatic);
// Configure storage for video uploads
const storageVideo = multer.diskStorage({
destination: function (req, file, cb) {
const dir = './views/assets/uploads/video';
const fullPath = path.resolve(dir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
cb(null, fullPath);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "_" + Date.now() + path.extname(file.originalname));
}
});
// Configure storage for video uploads
const storageResume = multer.diskStorage({
destination: function (req, file, cb) {
const dir = './views/assets/uploads/resume';
const fullPath = path.resolve(dir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
cb(null, fullPath);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "_" + Date.now() + path.extname(file.originalname));
}
});
const storageDocument = multer.diskStorage({
destination: function (req, file, cb) {
const dir = './views/assets/uploads/document';
const fullPath = path.resolve(dir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
cb(null, fullPath);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "_" + Date.now() + path.extname(file.originalname));
}
});
// Configure storage for video uploads
const storageCoverLetter = multer.diskStorage({
destination: function (req, file, cb) {
const dir = './views/assets/uploads/cover-letter';
const fullPath = path.resolve(dir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
cb(null, fullPath);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "_" + Date.now() + path.extname(file.originalname));
}
});
// Configure storage for image uploads
const storageImage = multer.diskStorage({
destination: function (req, file, cb) {
const dir = './views/assets/uploads/projects';
const fullPath = path.resolve(dir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
cb(null, fullPath);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "_" + Date.now() + path.extname(file.originalname));
}
});
// Configure storage for Profile image uploads
const storageProfilePic = multer.diskStorage({
destination: function (req, file, cb) {
const dir = './views/assets/uploads/profilePic';
const fullPath = path.resolve(dir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
cb(null, fullPath);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "_" + Date.now() + path.extname(file.originalname));
}
});
// Configure storage for Cover Photo uploads
const storageCoverPhoto = multer.diskStorage({
destination: function (req, file, cb) {
const dir = './views/assets/uploads/CoverPhoto';
const fullPath = path.resolve(dir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
cb(null, fullPath);
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "_" + Date.now() + path.extname(file.originalname));
}
});
// File filter to accept only video files
const fileFilter = (req, file, cb) => {
const allowedMimeTypes = [
"video/mp4",
"video/avi",
"video/mkv",
"video/mov",
"video/webm"
];
if (allowedMimeTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(null, false);
}
};
// File filter to accept only video files
const docFilter = (req, file, cb) => {
const allowedMimeTypes = [
"application/pdf"
];
if (allowedMimeTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(null, false);
}
};
const imageFilter = (req, file, cb) => {
console.log("file", file)
// only image files
if (file.mimetype === "image/jpeg" || file.mimetype === "image/png") {
cb(null, true);
} else {
cb(null, false);
}
};
const mediaFilter = (req, file, cb) => {
console.log("file", file);
if (
file.mimetype === "image/jpeg" ||
file.mimetype === "image/png" ||
file.mimetype === "video/mp4"
) {
cb(null, true);
} else {
cb(new Error("Only JPEG, PNG, and MP4 files are allowed!"), false);
}
};
// Set up multer for video uploads
const uploadProfileVideo = multer({ storage: storageVideo, fileFilter: fileFilter });
const uploadImage = multer({ storage: storageImage, fileFilter: imageFilter });
const uploadMedia = multer({ storage: storageImage, fileFilter: mediaFilter });
const uploadProfileImage = multer({ storage: storageProfilePic, fileFilter: imageFilter });
const uploadCoverPhoto = multer({ storage: storageCoverPhoto, fileFilter: imageFilter });
const uploadResume = multer({ storage: storageResume, fileFilter: docFilter });
const uploadDocument = multer({ storage: storageDocument, fileFilter: docFilter });
const uploadCoverLetter = multer({ storage: storageCoverLetter, fileFilter: docFilter });
// Video upload route
router.post('/profileVideo', uploadProfileVideo.single("profileVideo"), async (req, res, next) => {
try {
const video = req.file;
const userId = req.body.user
const user = await User.findById(userId)
if (!video) {
const error = new Error("Please upload a video file");
error.httpStatusCode = 400;
return next(error);
}
if (!user) {
const error = new Error("Something went Wrong");
error.httpStatusCode = 400;
return next(error);
}
const videoUrl = `/assets/uploads/video/${video.filename}`;
user.profileVideo = {
url: videoUrl
}
await user.save()
res.status(200).json({ url: videoUrl, file: video });
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
// Video upload route
router.post('/remove-profile-video', async (req, res) => {
try {
const { id } = req.body
const user = await User.findById(id)
if (!user) {
return res.status(409).redirect('/edit/profile?error=Something Went Wrong')
}
user.profileVideo = null
await user.save()
return res.redirect('/edit/profile?message=Profile Video Deleted')
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
// Edit User Data
router.post('/user-info', async (req, res) => {
try {
const { id, fullName, subHeading, jobTitle, about, phone, portfolio, experience, skills, location } = req.body
console.log('body', req.body)
const user = await User.findById(id)
console.log('user', user)
// const formData = {}
if (!user) {
return res.redirect('/edit/profile?error=User Not Found')
}
if (fullName) {
user.fullName = fullName
}
if (subHeading) {
user.subHeading = subHeading
}
if (jobTitle) {
user.jobTitle = jobTitle
}
if (about) {
user.about = about
}
if (phone) {
user.phone = phone
}
if (portfolio) {
user.portfolio = portfolio
}
if (experience) {
user.experience = experience
}
if (skills) {
user.skills = JSON.parse(skills)
}
if (location) {
user.location = location
}
console.log('user', user)
await user.save()
return res.redirect('/edit/profile?message=User Info Updated')
} catch (error) {
console.log(error.message);
return res.status(500).json({ message: error.message });
}
});
router.post('/social-info', async (req, res) => {
try {
const { id, facebook, instagram, twitter, linkedin, behance, pinterest, dribbble, linktree } = req.body
console.log('id: ', id, 'facebook: ', facebook, 'instagram: ', instagram, 'twitter: ', twitter, 'linkedin: ', linkedin, 'behance: ', behance, 'pinterest: ', pinterest, 'dribbble: ', dribbble, 'linktree: ', linktree)
console.log('body', req.body)
const user = await User.findById(id)
// const formData = {}
if (!user) {
return res.json({
success: false,
error: 'User Not Found'
})
}
const socialLinks = user.socialLinks?.length > 0 ? user.socialLinks : []
// Helper function to add or update a social link
const addOrUpdateLink = (platform, username) => {
let existingLink = socialLinks.find(link => link.platform === platform);
const index = socialLinks.indexOf(existingLink)
if (existingLink) {
if (username !== '') {
existingLink.username = username; // Update the username if the platform already exists
} else {
socialLinks.splice(index, 1);
}
} else {
if(username){
socialLinks.push({ platform, username }); // Add new link if platform doesn't exist
}
}
};
// Add or update social links based on the provided data
if (facebook !== undefined) { addOrUpdateLink('facebook', facebook) };
if (instagram !== undefined) { addOrUpdateLink('instagram', instagram) };
if (twitter !== undefined) { addOrUpdateLink('twitter', twitter) };
if (linkedin !== undefined) { addOrUpdateLink('linkedin', linkedin) };
if (behance !== undefined) { addOrUpdateLink('behance', behance) };
if (pinterest !== undefined) { addOrUpdateLink('pinterest', pinterest) };
if (dribbble !== undefined) { addOrUpdateLink('dribbble', dribbble) };
if (linktree !== undefined) { addOrUpdateLink('linktree', linktree) };
// Update user's socialLinks and save
user.socialLinks = socialLinks;
console.log('user', user)
await user.save()
// return res.redirect('/edit/profile?message=User Info Updated')
return res.redirect('/edit/profile?message=Social Links Updated')
} catch (error) {
console.log(error.message);
// return res.status(500).json({ message: error.message });
return res.redirect('/edit/profile?message=Something Went Wrong')
}
});
// project upload route
router.post('/project', uploadMedia.single("image"), async (req, res, next) => {
try {
const image = req.file;
const { title, id } = req.body
const user = await User.findById(id)
if (!image) {
// return res.json({ error: 'Project Image Not Found' });
return res.redirect('/edit/profile?error=Error Updating Project Image')
}
if (!user) {
// return res.json({ error: 'User Not Found' });
return res.redirect('/edit/profile?error=Something Went Wrong')
}
let newProject = await Project.create({
user: id,
title,
image: `/assets/uploads/projects/` + image.filename
})
let projects = user.projects ? user.projects : []
projects.push(newProject._id)
user.projects = projects
await user.save()
return res.redirect('/edit/profile?message=Project Added Successfully')
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
router.post('/education/add', uploadDocument.single("file"), async (req, res) => {
try {
const file = req.file;
const { userId, institute, degree, startingDate_month, startingDate_year, endingDate_month, endingDate_year } = req.body
const user = await User.findById(userId)
if (!user) {
return res.redirect('/edit/profile?error=Something Went Wrong')
}
let startingDate = new Date()
startingDate.setMonth(startingDate_month)
startingDate.setYear(startingDate_year)
let endingDate = new Date()
endingDate.setMonth(endingDate_month)
endingDate.setYear(endingDate_year)
console.log('startingDate', startingDate)
console.log('endingDate', endingDate)
if (endingDate < startingDate) {
return res.redirect('/edit/profile?error=Starting Date should be before Ending Date')
}
const education = { institute, degree, startingDate, endingDate, fileUrl: file ? `/assets/uploads/document/` + file.filename : '' }
let educations = user.educations ? user.educations : []
educations.push(education)
user.educations = educations
await user.save()
return res.redirect('/edit/profile?message=New Education Added Successfully')
} catch (error) {
console.log(error.message);
return res.redirect(`/edit/profile?error=${error.message}`)
}
})
router.post('/education/edit', uploadDocument.single("file"), async (req, res) => {
try {
const file = req.file;
const { userId, eduId, institute, degree, startingDate_month, startingDate_year, endingDate_month, endingDate_year } = req.body
let startingDate = new Date()
startingDate.setMonth(startingDate_month)
startingDate.setYear(startingDate_year)
let endingDate = new Date()
endingDate.setMonth(endingDate_month)
endingDate.setYear(endingDate_year)
const updatedEducationData = {
institute, degree, startingDate, endingDate, fileUrl: file ? `/assets/uploads/document/` + file.filename : ''
}
const updatedUser = await User.findOneAndUpdate(
{ _id: userId, "educations._id": eduId },
{
$set: {
"educations.$": updatedEducationData
}
},
{ new: true }
);
if (!updatedUser) {
return res.redirect('/edit/profile?error=Something Went Wrong')
}
return res.redirect('/edit/profile?message=Education Updated Successfully')
} catch (error) {
console.log(error.message);
return res.redirect(`/edit/profile?error=${error.message}`)
}
});
router.post('/education/delete', async (req, res) => {
try {
const { userId, educationId } = req.body
const updatedUser = await User.findOneAndUpdate(
{ _id: userId },
{ $pull: { educations: { _id: educationId } } },
{ new: true } // Return the updated document
);
if (!updatedUser) {
return res.json({
success: false,
error: 'Something Went Wrong'
})
}
return res.json({
success: true,
message: 'Education Deleted!'
})
} catch (error) {
console.log(error.message);
res.status(500).json({
success: false,
error: error.message
});
}
});
router.post('/education/get', async (req, res) => {
try {
const { userId, educationId } = req.body
const updatedUser = await User.findById(userId);
let education;
updatedUser?.educations.forEach((item) => {
if (item._id == educationId) {
education = item
}
})
if (!education) {
return res.json({
success: false,
error: 'Something Went Wrong'
})
}
return res.json({
success: true,
education
})
} catch (error) {
console.log(error.message);
res.status(500).json({
success: false,
error: error.message
});
}
});
router.post('/experience/add', uploadDocument.single("file"), async (req, res) => {
try {
const file = req.file;
const { id, title, employementType, companyName, location, locationType, currentlyWorking, startingDate_month, startingDate_year, endingDate_month, endingDate_year, description } = req.body
let startingDate = new Date()
startingDate.setMonth(startingDate_month)
startingDate.setYear(startingDate_year)
let endingDate = new Date()
endingDate.setMonth(endingDate_month)
endingDate.setYear(endingDate_year)
if (endingDate < startingDate) {
return res.redirect('/edit/profile?error=Starting Date should be before Ending Date')
}
const textDescription = description.replaceAll('<p>', '').replaceAll('</p>', '').replaceAll('<ul>', '').replaceAll('</ul>', '').replaceAll('<ol>', '').replaceAll('</ol>', '').replaceAll('<li>', '').replaceAll('</li>', '').replaceAll('<strong>', '').replaceAll('</strong>', '')
console.log('textDescription', textDescription.length)
if (textDescription.length > 1150) {
console.log('textDescription', textDescription)
return res.redirect('/edit/profile?error=Experience Description Should Not Exceed 1000 characters')
}
const user = await User.findById(id)
if (!user) {
// return res.json({ error: 'User Not Found' });
return res.redirect('/edit/profile?error=Something Went Wrong')
}
let experience = {
title, employementType, companyName, location, locationType, currentlyWorking, startingDate, endingDate, description, fileUrl: file ? `/assets/uploads/document/` + file.filename : ''
}
let experiences = user.experiences ? user.experiences : []
experiences.push(experience)
user.experiences = experiences
await user.save()
return res.redirect('/edit/profile?message=New Experience Added Successfully')
} catch (error) {
console.log(error.message);
return res.redirect(`/edit/profile?error=${error.message}`)
}
});
router.post('/experience/edit', uploadDocument.single("file"), async (req, res) => {
try {
const file = req.file;
const { userId, expId, title, employementType, companyName, location, locationType, currentlyWorking, startingDate_month, startingDate_year, endingDate_month, endingDate_year, description } = req.body
let startingDate = new Date()
startingDate.setMonth(startingDate_month)
startingDate.setYear(startingDate_year)
let endingDate = new Date()
endingDate.setMonth(endingDate_month)
endingDate.setYear(endingDate_year)
const textDescription = description.replaceAll('<p>', '').replaceAll('</p>', '').replaceAll('<ul>', '').replaceAll('</ul>', '').replaceAll('<ol>', '').replaceAll('</ol>', '').replaceAll('<li>', '').replaceAll('</li>', '').replaceAll('<strong>', '').replaceAll('</strong>', '')
console.log('textDescription', textDescription.length)
if (textDescription.length > 1150) {
console.log('textDescription', textDescription)
return res.redirect('/edit/profile?error=Experience Description Should Not Exceed 1000 characters')
}
// const updatedUser = await User.findOneAndUpdate(
// { _id: userId },
// { $pull: { experiences: { _id: expId } } }
// );
const updatedExperienceData = {
title, employementType, companyName, location, locationType, currentlyWorking, startingDate, endingDate, description, fileUrl: file ? `/assets/uploads/document/` + file.filename : ''
}
const updatedUser = await User.findOneAndUpdate(
{ _id: userId, "experiences._id": expId }, // Find the user with the specific experience
{
$set: {
"experiences.$": updatedExperienceData // Update the specific experience in the array
}
},
{ new: true } // Return the updated document
);
// const user = await User.findById(id)
if (!updatedUser) {
// return res.json({ error: 'User Not Found' });
return res.redirect('/edit/profile?error=Something Went Wrong')
}
// let experience = {
// title, employementType, companyName, location, locationType, currentlyWorking, startingDate, endingDate, description
// }
// let experiences = user.experiences ? user.experiences : []
// experiences.push(experience)
// user.experiences = experiences
// await user.save()
return res.redirect('/edit/profile?message=Experience Updated Successfully')
} catch (error) {
console.log(error.message);
return res.redirect(`/edit/profile?error=${error.message}`)
}
});
router.post('/experience/delete', async (req, res) => {
try {
const { userId, experienceId } = req.body
const updatedUser = await User.findOneAndUpdate(
{ _id: userId },
{ $pull: { experiences: { _id: experienceId } } },
{ new: true } // Return the updated document
);
if (!updatedUser) {
return res.json({
success: false,
error: 'Something Went Wrong'
})
}
return res.json({
success: true,
message: 'Experience Deleted!'
})
} catch (error) {
console.log(error.message);
res.status(500).json({
success: false,
error: error.message
});
}
});
router.post('/experience/get', async (req, res) => {
try {
const { userId, experienceId } = req.body
const updatedUser = await User.findById(userId);
let experience;
updatedUser?.experiences.forEach((item) => {
if (item._id == experienceId) {
experience = item
}
})
if (!experience) {
return res.json({
success: false,
error: 'Something Went Wrong'
})
}
return res.json({
success: true,
experience
})
} catch (error) {
console.log(error.message);
res.status(500).json({
success: false,
error: error.message
});
}
});
router.post('/volunteer-experience/add', uploadDocument.single("file"), async (req, res) => {
try {
const file = req.file;
const { id, title, companyName, cause, currentlyWorking, startingDate_month, startingDate_year, endingDate_month, endingDate_year } = req.body
let startingDate = new Date()
startingDate.setMonth(startingDate_month)
startingDate.setYear(startingDate_year)
let endingDate = new Date()
endingDate.setMonth(endingDate_month)
endingDate.setYear(endingDate_year)
if (endingDate < startingDate) {
return res.redirect('/edit/profile?error=Starting Date should be before Ending Date')
}
const user = await User.findById(id)
if (!user) {
// return res.json({ error: 'User Not Found' });
return res.redirect('/edit/profile?error=Something Went Wrong')
}
let volunteerExperience = {
title, companyName, cause, currentlyWorking, startingDate, endingDate, fileUrl: file ? `/assets/uploads/document/` + file.filename : ''
}
let volunteerExperiences = user.volunteerExperiences ? user.volunteerExperiences : []
volunteerExperiences.push(volunteerExperience)
user.volunteerExperiences = volunteerExperiences
await user.save()
return res.redirect('/edit/profile?message=New Volunteer Experience Added Successfully')
} catch (error) {
console.log(error.message);
return res.redirect(`/edit/profile?error=${error.message}`)
}
});
router.post('/volunteer-experience/edit', uploadDocument.single("file"), async (req, res) => {
try {
const file = req.file;
console.log('file', file)
const { userId, expId, title, companyName, cause, currentlyWorking, startingDate_month, startingDate_year, endingDate_month, endingDate_year } = req.body
let startingDate = new Date()
startingDate.setMonth(startingDate_month)
startingDate.setYear(startingDate_year)
let endingDate = new Date()
endingDate.setMonth(endingDate_month)
endingDate.setYear(endingDate_year)
if (endingDate < startingDate) {
return res.redirect('/edit/profile?error=Starting Date should be before Ending Date')
}
const updatedExperienceData = {
title, companyName, cause, currentlyWorking, startingDate, endingDate, fileUrl: file ? `/assets/uploads/document/` + file.filename : ''
}
const updatedUser = await User.findOneAndUpdate(
{ _id: userId, "volunteerExperiences._id": expId }, // Find the user with the specific experience
{
$set: {
"volunteerExperiences.$": updatedExperienceData // Update the specific experience in the array
}
},
{ new: true } // Return the updated document
);
// const user = await User.findById(id)
if (!updatedUser) {
// return res.json({ error: 'User Not Found' });
return res.redirect('/edit/profile?error=Something Went Wrong')
}
// let experience = {
// title, employementType, companyName, location, locationType, currentlyWorking, startingDate, endingDate, description
// }
// let experiences = user.experiences ? user.experiences : []
// experiences.push(experience)
// user.experiences = experiences
// await user.save()
return res.redirect('/edit/profile?message=Volunteer Experience Updated Successfully')
} catch (error) {
console.log(error.message);
return res.redirect(`/edit/profile?error=${error.message}`)
}
});
router.post('/volunteer-experience/delete', async (req, res) => {
try {
const { userId, experienceId } = req.body
const updatedUser = await User.findOneAndUpdate(
{ _id: userId },
{ $pull: { volunteerExperiences: { _id: experienceId } } },
{ new: true } // Return the updated document
);
if (!updatedUser) {
return res.json({
success: false,
error: 'Something Went Wrong'
})
}
return res.json({
success: true,
message: 'Volunteer Experience Deleted!'
})
} catch (error) {
console.log(error.message);
res.status(500).json({
success: false,
error: error.message
});
}
});
router.post('/volunteer-experience/get', async (req, res) => {
try {
const { userId, experienceId } = req.body
const updatedUser = await User.findById(userId);
let experience;
updatedUser?.volunteerExperiences.forEach((item) => {
if (item._id == experienceId) {
experience = item
}
})
if (!experience) {
return res.json({
success: false,
error: 'Something Went Wrong'
})
}
return res.json({
success: true,
experience
})
} catch (error) {
console.log(error.message);
res.status(500).json({
success: false,
error: error.message
});
}
});
router.post('/license-certificate/add', uploadDocument.single("file"), async (req, res, next) => {
try {
const file = req.file;
const { id, title, organization, startingDate_month, startingDate_year } = req.body
const user = await User.findById(id)
if (!user) {
// return res.json({ error: 'User Not Found' });
return res.redirect('/edit/profile?error=Something Went Wrong')
}
console.log('file', file)
let issueDate = new Date()
issueDate.setMonth(startingDate_month)
issueDate.setYear(startingDate_year)
let newProject = {
user: id,
title,
organization,
issueDate,
fileUrl: file ? `/assets/uploads/document/` + file.filename : ''
}
let licenseCertification = user.licenseCertification ? user.licenseCertification : []
licenseCertification.push(newProject)
user.licenseCertification = licenseCertification
await user.save()
return res.redirect('/edit/profile?message=License or Certificate Added Successfully')
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
router.post('/license-certificate/edit', uploadDocument.single("file"), async (req, res) => {
try {
const { userId, certId, title, organization, startingDate_month, startingDate_year } = req.body
const file = req.file
let issueDate = new Date()
issueDate.setMonth(startingDate_month)
issueDate.setYear(startingDate_year)
let newProject = {
user: userId,
title,
organization,
issueDate
}
if (file?.filename) {
newProject.fileUrl = `/assets/uploads/document/` + file.filename
}
const updatedUser = await User.findOneAndUpdate(
{ _id: userId, "licenseCertification._id": certId },
{
$set: {
"licenseCertification.$": newProject
}
},
{ new: true }
);
if (!updatedUser) {
return res.redirect('/edit/profile?error=Something Went Wrong')
}
return res.redirect('/edit/profile?message=License or Certificate Updated Successfully')
} catch (error) {
console.log(error.message);
return res.redirect(`/edit/profile?error=${error.message}`)
}
});
router.post('/license-certificate/delete', async (req, res) => {
try {
const { userId, certId } = req.body
const updatedUser = await User.findOneAndUpdate(
{ _id: userId },
{ $pull: { licenseCertification: { _id: certId } } },
{ new: true } // Return the updated document
);
if (!updatedUser) {
return res.json({
success: false,
error: 'Something Went Wrong'
})
}
return res.json({
success: true,
message: 'License or Certificate Deleted!'
})
} catch (error) {
console.log(error.message);
res.status(500).json({
success: false,
error: error.message
});
}
});
router.post('/license-certificate/get', async (req, res) => {
try {
const { userId, certId } = req.body
const updatedUser = await User.findById(userId);
let cert;
updatedUser?.licenseCertification.forEach((item) => {
if (item._id == certId) {
cert = item
}
})
if (!cert) {
return res.json({
success: false,
error: 'Something Went Wrong'
})
}
return res.json({
success: true,
cert
})
} catch (error) {
console.log(error.message);
res.status(500).json({
success: false,
error: error.message
});
}
});
router.post('/remove-project-item', async (req, res) => {
try {
const { user, project } = req.body
const userObj = await User.findById(user)
if (!userObj) {
// return res.json({ error: 'User Not Found' });
return res.redirect('/edit/profile?error=Something Went Wrong')
}
await Project.findByIdAndDelete(project)
const projectIndex = userObj.projects.indexOf(project)
userObj.projects.splice(projectIndex, 1)
await userObj.save()
return res.redirect('/edit/profile?message=Project Removed Successfully')
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
router.post('/profile-pic', uploadProfileImage.single("profilePic"), async (req, res, next) => {
try {
const image = req.file;
const { id } = req.body
const user = await User.findById(id)
if (!image) {
// return res.json({ error: 'Project Image Not Found' });
return res.status(409).redirect('/edit/profile?error=Error Updating Profile Image')
}
if (!user) {
// return res.json({ error: 'User Not Found' });
return res.status(409).redirect('/edit/profile?error=Something Went Wrong')
}
console.log('image', image)
user.profilePic = `/assets/uploads/profilePic/` + image.filename
await user.save()
return res.redirect('/edit/profile?message=Profile Pic Updated Successfully')
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
router.post('/cover-photo', uploadCoverPhoto.single("coverPhoto"), async (req, res, next) => {
try {
const image = req.file;
const { id } = req.body
const user = await User.findById(id)
if (!image) {
// return res.json({ error: 'Project Image Not Found' });
return res.status(409).redirect('/edit/profile?error=Error Updating Cover Photo')
}
if (!user) {
// return res.json({ error: 'User Not Found' });
return res.status(409).redirect('/edit/profile?error=Something Went Wrong')
}
console.log('image', image)
user.coverPhoto = `/assets/uploads/CoverPhoto/` + image.filename
await user.save()
return res.redirect('/edit/profile?message=Cover Photo Updated Successfully')
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
router.post('/personal-document/add', uploadDocument.single("file"), async (req, res, next) => {
try {
const file = req.file;
const { id, title } = req.body
const user = await User.findById(id)
if (!file) {
return res.redirect('/edit/profile?error=File Required in Personal Document')
}
user.personalDocuments = user.personalDocuments ? user.personalDocuments : []
user.personalDocuments.push({
title, fileUrl: `/assets/uploads/document/` + file.filename
})
await user.save()
// return res.redirect('/edit/profile?message=Personal Document Uploaded Successfully')
const addedDocument = user.personalDocuments[user.personalDocuments.length - 1];
const token = jwt.sign({
key: 'personalDocuments',
title: 'Personal Document',
data: addedDocument
}, JWT_SECRET);
return res.redirect(`/share-with-connections/${token}?message=Personal Document Uploaded Successfully`)
// return res.redirect('/edit/profile?message=Personal Document Uploaded Successfully')
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
router.post('/personal-document/remove', async (req, res, next) => {
try {
const { userId, perDocId } = req.body
const updatedUser = await User.findOneAndUpdate(
{ _id: userId },
{ $pull: { personalDocuments: { _id: perDocId } } },
{ new: true } // Return the updated document
);
if (!updatedUser) {
// return res.json({
// success: false,
// error: 'Something Went Wrong'
// })
return res.redirect('/edit/profile?error=Something Went Wrong')
}
// return res.json({
// success: true,
// message: 'Personal Document Deleted!'
// })
return res.redirect('/edit/profile?success=Personal Document Deleted!')
} catch (error) {
console.log(error.message);
// res.status(500).json({
// success: false,
// error: error.message
// });
return res.redirect(`/edit/profile?error=${error.message}`)
}
});
module.exports = router;