Skip to content

Commit

Permalink
Merge pull request #316 from JaviFdez7/feature/backend/fix-work-exper…
Browse files Browse the repository at this point in the history
…ience-delete

Feature/backend/312 Fix work experience delete
  • Loading branch information
FJMonteroInformatica authored Apr 6, 2024
2 parents 30b8787 + 8952771 commit 58c0b6b
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ProfessionalExperience } from '../models/professional-experience';
import { Candidate } from '../../user/models/user';

export const getAllProfessionalExperience: any = async () => await ProfessionalExperience.find({});

Expand Down Expand Up @@ -28,6 +29,10 @@ export const updateProfessionalExperience: any = async (id: any, data: any) => {
export const deleteProfessionalExperience: any = async (id: any) => {
try {
await ProfessionalExperience.findByIdAndDelete(id);
await Candidate.updateMany(
{ profesionalExperiences: id },
{ $pull: { profesionalExperiences: id } }
);
return 'Professional experience deleted successfully.';
} catch (error) {
console.error('Error deleting professional experience', error)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,147 +1,182 @@
import { verifyJWT } from '../../user/helpers/handleJWT';
import { ProfessionalExperience } from '../models/professional-experience';
import { Candidate } from '../../user/models/user';
import e, { type Request, type Response, type NextFunction } from 'express';
import { ApiResponse } from '../../../utils/ApiResponse';
import { verifyJWT } from '../../user/helpers/handleJWT'
import { ProfessionalExperience } from '../models/professional-experience'
import { Candidate } from '../../user/models/user'
import e, { type Request, type Response, type NextFunction } from 'express'
import { ApiResponse } from '../../../utils/ApiResponse'

export const checkGetProfessionalExperienceById: any = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = req.params.id.toString();
const experience = await ProfessionalExperience.findById(id);
if (!experience) {
const message = 'Professional experience not found';
ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404);
return;
}
else{
next();
}
} catch (error:any) {
ApiResponse.sendError(res, [{
title: 'Error getting professional experience',
detail: error.message
}]);
}
export const checkGetProfessionalExperienceById: any = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const id = req.params.id.toString()
const experience = await ProfessionalExperience.findById(id)
if (!experience) {
const message = 'Professional experience not found'
ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404)
return
} else {
next()
}
} catch (error: any) {
ApiResponse.sendError(res, [
{
title: 'Error getting professional experience',
detail: error.message,
},
])
}
}

export const checkCreateProfessionalExperience: any = async (req: Request, res: Response, next: NextFunction) => {
try {
const data = req.body;

const token = req.headers.authorization ?? '';
export const checkCreateProfessionalExperience: any = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const data = req.body

if (!data) {
const message = 'No data to insert';
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 404);
return;
} else if (!data.startDate || !data.companyName || !data.userId || !data.professionalArea) {
const message = 'Missing required fields';
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 404);
return;
}
const token = req.headers.authorization ?? ''

const userId = data.userId
const existingCandidate = await Candidate.findById(userId);
if (!existingCandidate) {
const message = 'Invalid candidate';
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 404);
return;
}else if (token.length === 0) {
const message = 'No token provided';
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401);
return;
}
if (!data) {
const message = 'No data to insert'
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 404)
return
} else if (!data.startDate || !data.companyName || !data.userId || !data.professionalArea) {
const message = 'Missing required fields'
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 404)
return
}

const decodedToken = verifyJWT(token);

if (decodedToken.sub !== data.userId.toString()) {
const message = 'Incorrect token';
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401);
return;
}else{
next();
}
} catch (error:any) {
ApiResponse.sendError(res, [{
title: 'Error inserting professional experience',
detail: error.message
}]);
}
};
const userId = data.userId
const existingCandidate = await Candidate.findById(userId)
if (!existingCandidate) {
const message = 'Invalid candidate'
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 404)
return
} else if (token.length === 0) {
const message = 'No token provided'
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401)
return
}

export const checkUpdateProfessionalExperience: any = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = req.params.id.toString();
const token = req.headers.authorization ?? '';
const data = req.body;
const experience = await ProfessionalExperience.findById(id);
if (!experience) {
const message = 'Professional experience not found';
ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404);
return;
}
else if (!data) {
const message = 'No data to update';
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 400);
return;
}
else if (token.length === 0) {
const message = 'No token provided';
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401);
return;
}
const decodedToken = verifyJWT(token);
const candidate = await Candidate.findOne({ _id: req.body.userId, profesionalExperiences: experience._id });
if (decodedToken.sub !== candidate?._id.toString()) {
const message = 'Incorrect token';
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401);
return;
}else{
next();
}
} catch (error:any) {
ApiResponse.sendError(res, [{
title: 'Error updating professional experience',
detail: error.message
}]);
}
};
const decodedToken = verifyJWT(token)

export const checkDeleteProfessionalExperience: any = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = req.params.id.toString();
const token = req.headers.authorization ?? '';
const experience = await ProfessionalExperience.findById(id);
if (!experience) {
const message = 'Professional experience not found';
ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404);
return;
}
else if (token.length === 0) {
const message = 'No token provided';
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401);
return;
}
const decodedToken = verifyJWT(token);
const candidate = await Candidate.findOne({ _id: req.body.userId, profesionalExperiences: experience._id });
if (decodedToken.sub !== candidate?._id.toString()) {
const message = 'Incorrect token';
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401);
return;
}else{
next();
}
} catch (error:any) {
ApiResponse.sendError(res, [{
title: 'Error deleting professional experience',
detail: error.message
}]);
}
if (decodedToken.sub !== data.userId.toString()) {
const message = 'Incorrect token'
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401)
return
} else {
next()
}
} catch (error: any) {
ApiResponse.sendError(res, [
{
title: 'Error inserting professional experience',
detail: error.message,
},
])
}
}

export const checkUpdateProfessionalExperience: any = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const id = req.params.id.toString()
const token = req.headers.authorization ?? ''
const data = req.body
const experience = await ProfessionalExperience.findById(id)
if (!experience) {
const message = 'Professional experience not found'
ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404)
return
} else if (!data) {
const message = 'No data to update'
ApiResponse.sendError(res, [{ title: 'Bad Request', detail: message }], 400)
return
} else if (token.length === 0) {
const message = 'No token provided'
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401)
return
}
const decodedToken = verifyJWT(token)
const candidate = await Candidate.findOne({
_id: req.body.userId,
profesionalExperiences: experience._id,
})
if (decodedToken.sub !== candidate?._id.toString()) {
const message = 'Incorrect token'
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401)
return
} else {
next()
}
} catch (error: any) {
ApiResponse.sendError(res, [
{
title: 'Error updating professional experience',
detail: error.message,
},
])
}
}

export const checkDeleteProfessionalExperience: any = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const id = req.params.id.toString()
const token = req.headers.authorization ?? ''
const experience = await ProfessionalExperience.findById(id)
if (!experience) {
const message = 'Professional experience not found'
ApiResponse.sendError(res, [{ title: 'Not Found', detail: message }], 404)
return
} else if (token.length === 0) {
const message = 'No token provided'
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401)
return
}
const decodedToken = verifyJWT(token)
if (!decodedToken.sub) {
ApiResponse.sendError(
res,
[{ title: 'Unauthorized', detail: 'Invalid token payload' }],
401
)
return
}
const userId = decodedToken.sub.toString()
const candidate = await Candidate.findOne({
_id: userId,
profesionalExperiences: experience._id,
})
if (decodedToken.sub !== candidate?._id.toString()) {
const message = 'Incorrect token'
ApiResponse.sendError(res, [{ title: 'Unauthorized', detail: message }], 401)
return
} else {
next()
}
} catch (error: any) {
ApiResponse.sendError(res, [
{
title: 'Error deleting professional experience',
detail: error.message,
},
])
}
}

export default {
checkDeleteProfessionalExperience,
checkUpdateProfessionalExperience,
checkCreateProfessionalExperience,
checkGetProfessionalExperienceById
};
checkDeleteProfessionalExperience,
checkUpdateProfessionalExperience,
checkCreateProfessionalExperience,
checkGetProfessionalExperienceById,
}

0 comments on commit 58c0b6b

Please sign in to comment.