diff --git a/src/controllers/activities.controller.ts b/src/controllers/activities.controller.ts index 8423bbd..aedac03 100644 --- a/src/controllers/activities.controller.ts +++ b/src/controllers/activities.controller.ts @@ -87,7 +87,7 @@ export const updateActivity = async ( ); } - if (await checkDuplicateItemName(activitiesFields.itemName, activityId)) { + if (activitiesFields.itemName != null && await checkDuplicateItemName(activitiesFields.itemName, activityId)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } diff --git a/src/controllers/education.controller.ts b/src/controllers/education.controller.ts index 93653f5..3d3f3b9 100644 --- a/src/controllers/education.controller.ts +++ b/src/controllers/education.controller.ts @@ -84,7 +84,7 @@ export const updateEducation = async ( ); } - if (await checkDuplicateItemName(educationFields.itemName, educationId)) { + if (educationFields.itemName != null && await checkDuplicateItemName(educationFields.itemName, educationId)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } diff --git a/src/controllers/experience.controller.ts b/src/controllers/experience.controller.ts index dd9c8bb..af11488 100644 --- a/src/controllers/experience.controller.ts +++ b/src/controllers/experience.controller.ts @@ -1,149 +1,151 @@ import { - ExperienceModel, - type ExperienceType, - } from "../models/experience.model"; - import { ResumeModel } from "../models/resume.model"; + ExperienceModel, + type ExperienceType, +} from "../models/experience.model"; +import { ResumeModel } from "../models/resume.model"; import mongoose from "mongoose"; - import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; - import { checkDuplicateItemName } from "../utils/checkDuplicates"; - - export const createExperience = async (experienceFields: ExperienceType) => { - try { - if (await checkDuplicateItemName(experienceFields.itemName)) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); - } - - const newExperience = new ExperienceModel(experienceFields); - await newExperience.save(); - return newExperience; - } catch (err: unknown) { - if (err instanceof HttpError) { - throw err; - } - - checkMongooseErrors(err); - - throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Experience creation failed", - { cause: err }, - ); +import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; +import { checkDuplicateItemName } from "../utils/checkDuplicates"; + +export const createExperience = async (experienceFields: ExperienceType) => { + try { + if (await checkDuplicateItemName(experienceFields.itemName)) { + throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } - }; - - export const getAllExperiences = async (user: string) => { - try { - const experience = await ExperienceModel.find({ user: user }); - return experience; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - - throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Experience retrieval failed", - { cause: err }, - ); + + const newExperience = new ExperienceModel(experienceFields); + await newExperience.save(); + return newExperience; + } catch (err: unknown) { + if (err instanceof HttpError) { + throw err; } - }; - - export const getExperienceById = async (user: string, experienceId: string) => { - try { - const experience = await ExperienceModel.findOne({ - user: user, - _id: experienceId, - }); - return experience; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - - throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Experience retrieval failed", - { cause: err }, - ); + + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Experience creation failed", + { cause: err }, + ); + } +}; + +export const getAllExperiences = async (user: string) => { + try { + const experience = await ExperienceModel.find({ user: user }); + return experience; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; } - }; - - export const updateExperience = async ( - user: string, - experienceId: string, - experienceFields: ExperienceType, - ) => { - try { - if (!experienceId) { - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Missing experience ID for update", - ); - } - - if (await checkDuplicateItemName(experienceFields.itemName, experienceId)) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); - } - - const updatedExperience = await ExperienceModel.findOneAndUpdate( - { _id: experienceId, user: user }, // Query to match the document by _id and user - { $set: experienceFields }, // Update operation - { new: true, runValidators: true }, // Options: return the updated document and run schema validators - ); - return updatedExperience; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Experience retrieval failed", + { cause: err }, + ); + } +}; + +export const getExperienceById = async (user: string, experienceId: string) => { + try { + const experience = await ExperienceModel.findOne({ + user: user, + _id: experienceId, + }); + return experience; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; + } + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Experience retrieval failed", + { cause: err }, + ); + } +}; + +export const updateExperience = async ( + user: string, + experienceId: string, + experienceFields: ExperienceType, +) => { + try { + if (!experienceId) { throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Experience update failed", - { cause: err }, + HttpStatus.BAD_REQUEST, + "Missing experience ID for update", ); } - }; - - export const deleteExperience = async (user: string, experienceId: string) => { - try { - await ResumeModel.updateMany( - { itemIds: new mongoose.Types.ObjectId(experienceId) }, - { $pull: { itemIds: new mongoose.Types.ObjectId(experienceId) } } - ); - const deletedExperience = await ExperienceModel.findOneAndDelete({ - _id: experienceId, - user: user, - }); - if (!deletedExperience) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Experience not found or already deleted", - ); - } - return { message: "Experience deleted successfully" }; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - + if ( + experienceFields.itemName != null && + (await checkDuplicateItemName(experienceFields.itemName, experienceId)) + ) { + throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); + } + + const updatedExperience = await ExperienceModel.findOneAndUpdate( + { _id: experienceId, user: user }, // Query to match the document by _id and user + { $set: experienceFields }, // Update operation + { new: true, runValidators: true }, // Options: return the updated document and run schema validators + ); + return updatedExperience; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; + } + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Experience update failed", + { cause: err }, + ); + } +}; + +export const deleteExperience = async (user: string, experienceId: string) => { + try { + await ResumeModel.updateMany( + { itemIds: new mongoose.Types.ObjectId(experienceId) }, + { $pull: { itemIds: new mongoose.Types.ObjectId(experienceId) } }, + ); + + const deletedExperience = await ExperienceModel.findOneAndDelete({ + _id: experienceId, + user: user, + }); + if (!deletedExperience) { throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Experience deletion failed", - { cause: err }, + HttpStatus.NOT_FOUND, + "Experience not found or already deleted", ); } - }; - \ No newline at end of file + return { message: "Experience deleted successfully" }; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; + } + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Experience deletion failed", + { cause: err }, + ); + } +}; diff --git a/src/controllers/heading.controller.ts b/src/controllers/heading.controller.ts index da5d4ce..11d1b94 100644 --- a/src/controllers/heading.controller.ts +++ b/src/controllers/heading.controller.ts @@ -1,149 +1,148 @@ -import { - HeadingModel, - type HeadingType, - } from "../models/heading.model"; - import { ResumeModel } from "../models/resume.model"; +import { HeadingModel, type HeadingType } from "../models/heading.model"; +import { ResumeModel } from "../models/resume.model"; import mongoose from "mongoose"; - import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; - import { checkDuplicateItemName } from "../utils/checkDuplicates"; - - export const createHeading = async (headingFields: HeadingType) => { - try { - if (await checkDuplicateItemName(headingFields.itemName)) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); - } - - const newHeading = new HeadingModel(headingFields); - await newHeading.save(); - return newHeading; - } catch (err: unknown) { - if (err instanceof HttpError) { - throw err; - } - - checkMongooseErrors(err); - - throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Heading creation failed", - { cause: err }, - ); +import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; +import { checkDuplicateItemName } from "../utils/checkDuplicates"; + +export const createHeading = async (headingFields: HeadingType) => { + try { + if (await checkDuplicateItemName(headingFields.itemName)) { + throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } - }; - - export const getAllHeadings = async (user: string) => { - try { - const heading = await HeadingModel.find({ user: user }); - return heading; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - - throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Heading retrieval failed", - { cause: err }, - ); + + const newHeading = new HeadingModel(headingFields); + await newHeading.save(); + return newHeading; + } catch (err: unknown) { + if (err instanceof HttpError) { + throw err; } - }; - - export const getHeadingById = async (user: string, headingId: string) => { - try { - const heading = await HeadingModel.findOne({ - user: user, - _id: headingId, - }); - return heading; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - - throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Heading retrieval failed", - { cause: err }, - ); + + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Heading creation failed", + { cause: err }, + ); + } +}; + +export const getAllHeadings = async (user: string) => { + try { + const heading = await HeadingModel.find({ user: user }); + return heading; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; } - }; - - export const updateHeading = async ( - user: string, - headingId: string, - headingFields: HeadingType, - ) => { - try { - if (!headingId) { - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Missing heading ID for update", - ); - } - - if (await checkDuplicateItemName(headingFields.itemName, headingId)) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); - } - - const updatedHeading = await HeadingModel.findOneAndUpdate( - { _id: headingId, user: user }, // Query to match the document by _id and user - { $set: headingFields }, // Update operation - { new: true, runValidators: true }, // Options: return the updated document and run schema validators - ); - return updatedHeading; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Heading retrieval failed", + { cause: err }, + ); + } +}; + +export const getHeadingById = async (user: string, headingId: string) => { + try { + const heading = await HeadingModel.findOne({ + user: user, + _id: headingId, + }); + return heading; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; + } + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Heading retrieval failed", + { cause: err }, + ); + } +}; + +export const updateHeading = async ( + user: string, + headingId: string, + headingFields: HeadingType, +) => { + try { + if (!headingId) { throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Heading update failed", - { cause: err }, + HttpStatus.BAD_REQUEST, + "Missing heading ID for update", ); } - }; - - export const deleteHeading = async (user: string, headingId: string) => { - try { - await ResumeModel.updateMany( - { itemIds: new mongoose.Types.ObjectId(headingId) }, - { $pull: { itemIds: new mongoose.Types.ObjectId(headingId) } } - ); - const deletedHeading = await HeadingModel.findOneAndDelete({ - _id: headingId, - user: user, - }); - if (!deletedHeading) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Heading not found or already deleted", - ); - } - return { message: "Heading deleted successfully" }; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - + if ( + headingFields.itemName != null && + (await checkDuplicateItemName(headingFields.itemName, headingId)) + ) { + throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); + } + + const updatedHeading = await HeadingModel.findOneAndUpdate( + { _id: headingId, user: user }, // Query to match the document by _id and user + { $set: headingFields }, // Update operation + { new: true, runValidators: true }, // Options: return the updated document and run schema validators + ); + return updatedHeading; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; + } + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Heading update failed", + { cause: err }, + ); + } +}; + +export const deleteHeading = async (user: string, headingId: string) => { + try { + await ResumeModel.updateMany( + { itemIds: new mongoose.Types.ObjectId(headingId) }, + { $pull: { itemIds: new mongoose.Types.ObjectId(headingId) } }, + ); + + const deletedHeading = await HeadingModel.findOneAndDelete({ + _id: headingId, + user: user, + }); + if (!deletedHeading) { throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Heading deletion failed", - { cause: err }, + HttpStatus.NOT_FOUND, + "Heading not found or already deleted", ); } - }; - \ No newline at end of file + return { message: "Heading deleted successfully" }; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; + } + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Heading deletion failed", + { cause: err }, + ); + } +}; diff --git a/src/controllers/project.controller.ts b/src/controllers/project.controller.ts index de67f97..39383f4 100644 --- a/src/controllers/project.controller.ts +++ b/src/controllers/project.controller.ts @@ -1,149 +1,145 @@ -import { - ProjectModel, - type ProjectType, - } from "../models/project.model"; - import { ResumeModel } from "../models/resume.model"; +import { ProjectModel, type ProjectType } from "../models/project.model"; +import { ResumeModel } from "../models/resume.model"; import mongoose from "mongoose"; - import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; - import { checkDuplicateItemName } from "../utils/checkDuplicates"; - - export const createProject = async (projectFields: ProjectType) => { - try { - if (await checkDuplicateItemName(projectFields.itemName)) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); - } - - const newProject = new ProjectModel(projectFields); - await newProject.save(); - return newProject; - } catch (err: unknown) { - if (err instanceof HttpError) { - throw err; - } - - checkMongooseErrors(err); - - throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Project creation failed", - { cause: err }, - ); +import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; +import { checkDuplicateItemName } from "../utils/checkDuplicates"; + +export const createProject = async (projectFields: ProjectType) => { + try { + if (await checkDuplicateItemName(projectFields.itemName)) { + throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } - }; - - export const getAllProjects = async (user: string) => { - try { - const project = await ProjectModel.find({ user: user }); - return project; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - - throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Project retrieval failed", - { cause: err }, - ); + + const newProject = new ProjectModel(projectFields); + await newProject.save(); + return newProject; + } catch (err: unknown) { + if (err instanceof HttpError) { + throw err; } - }; - - export const getProjectById = async (user: string, projectId: string) => { - try { - const project = await ProjectModel.findOne({ - user: user, - _id: projectId, - }); - return project; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - - throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Project retrieval failed", - { cause: err }, - ); + + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Project creation failed", + { cause: err }, + ); + } +}; + +export const getAllProjects = async (user: string) => { + try { + const project = await ProjectModel.find({ user: user }); + return project; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; } - }; - - export const updateProject = async ( - user: string, - projectId: string, - projectFields: ProjectType, - ) => { - try { - if (!projectId) { - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Missing project ID for update", - ); - } - - if (await checkDuplicateItemName(projectFields.itemName, projectId)) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); - } - - const updatedProject = await ProjectModel.findOneAndUpdate( - { _id: projectId, user: user }, // Query to match the document by _id and user - { $set: projectFields }, // Update operation - { new: true, runValidators: true }, // Options: return the updated document and run schema validators - ); - return updatedProject; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Project retrieval failed", + { cause: err }, + ); + } +}; + +export const getProjectById = async (user: string, projectId: string) => { + try { + const project = await ProjectModel.findOne({ + user: user, + _id: projectId, + }); + return project; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; + } + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Project retrieval failed", + { cause: err }, + ); + } +}; + +export const updateProject = async ( + user: string, + projectId: string, + projectFields: ProjectType, +) => { + try { + if (!projectId) { throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Project update failed", - { cause: err }, + HttpStatus.BAD_REQUEST, + "Missing project ID for update", ); } - }; - - export const deleteProject = async (user: string, projectId: string) => { - try { - await ResumeModel.updateMany( - { itemIds: new mongoose.Types.ObjectId(projectId) }, - { $pull: { itemIds: new mongoose.Types.ObjectId(projectId) } } - ); - const deletedProject = await ProjectModel.findOneAndDelete({ - _id: projectId, - user: user, - }); - if (!deletedProject) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Project not found or already deleted", - ); - } - return { message: "Project deleted successfully" }; - } catch (err: unknown) { - //rethrow any errors as HttpErrors - if (err instanceof HttpError) { - throw err; - } - //checks if mongoose threw and will rethrow with appropriate status code and message - checkMongooseErrors(err); - + if (projectFields.itemName != null && await checkDuplicateItemName(projectFields.itemName, projectId)) { + throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); + } + + const updatedProject = await ProjectModel.findOneAndUpdate( + { _id: projectId, user: user }, // Query to match the document by _id and user + { $set: projectFields }, // Update operation + { new: true, runValidators: true }, // Options: return the updated document and run schema validators + ); + return updatedProject; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; + } + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Project update failed", + { cause: err }, + ); + } +}; + +export const deleteProject = async (user: string, projectId: string) => { + try { + await ResumeModel.updateMany( + { itemIds: new mongoose.Types.ObjectId(projectId) }, + { $pull: { itemIds: new mongoose.Types.ObjectId(projectId) } }, + ); + + const deletedProject = await ProjectModel.findOneAndDelete({ + _id: projectId, + user: user, + }); + if (!deletedProject) { throw new HttpError( - HttpStatus.INTERNAL_SERVER_ERROR, - "Project deletion failed", - { cause: err }, + HttpStatus.NOT_FOUND, + "Project not found or already deleted", ); } - }; - \ No newline at end of file + return { message: "Project deleted successfully" }; + } catch (err: unknown) { + //rethrow any errors as HttpErrors + if (err instanceof HttpError) { + throw err; + } + //checks if mongoose threw and will rethrow with appropriate status code and message + checkMongooseErrors(err); + + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Project deletion failed", + { cause: err }, + ); + } +}; diff --git a/src/controllers/resume.controller.ts b/src/controllers/resume.controller.ts index 6ee7f04..afa9865 100644 --- a/src/controllers/resume.controller.ts +++ b/src/controllers/resume.controller.ts @@ -1,13 +1,13 @@ import { ResumeModel, type resumeType } from "../models/resume.model"; import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors"; -import { checkDuplicateItemName } from "../utils/checkDuplicates"; +import { checkDuplicateResumeName } from "../utils/checkDuplicates"; export const createResume = async (resumesFields: resumeType) => { try { - if(await checkDuplicateItemName(resumesFields.itemName)){ + if(await checkDuplicateResumeName(resumesFields.itemName)){ throw new HttpError( HttpStatus.BAD_REQUEST, - "Duplicate item name", + "Duplicate resume name", ) } @@ -85,8 +85,8 @@ export const updateResume = async ( ); } - if (await checkDuplicateItemName(resumesFields.itemName, resumeId)) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); + if (resumesFields.itemName != null && await checkDuplicateResumeName(resumesFields.itemName, resumeId)) { + throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate resume name"); } const updatedResume = await ResumeModel.findOneAndUpdate( diff --git a/src/controllers/sectionHeading.controller.ts b/src/controllers/sectionHeading.controller.ts index e2dc68e..f824fd3 100644 --- a/src/controllers/sectionHeading.controller.ts +++ b/src/controllers/sectionHeading.controller.ts @@ -87,7 +87,7 @@ export const updateSectionHeading = async ( ); } - if (await checkDuplicateItemName(sectionHeadingsFields.itemName, sectionHeadingId)) { + if (sectionHeadingsFields.itemName != null &&await checkDuplicateItemName(sectionHeadingsFields.itemName, sectionHeadingId)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } diff --git a/src/controllers/skills.controller.ts b/src/controllers/skills.controller.ts index 9a80cd5..81b9092 100644 --- a/src/controllers/skills.controller.ts +++ b/src/controllers/skills.controller.ts @@ -87,7 +87,7 @@ export const updateSkill = async ( ); } - if (await checkDuplicateItemName(skillsFields.itemName, skillId)) { + if (skillsFields.itemName != null && await checkDuplicateItemName(skillsFields.itemName, skillId)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } diff --git a/src/middlewares/verifyToken.ts b/src/middlewares/verifyToken.ts index 4ddba5a..3fbe512 100644 --- a/src/middlewares/verifyToken.ts +++ b/src/middlewares/verifyToken.ts @@ -25,7 +25,8 @@ export const verifyToken = async ( } // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - req.body.user = decodeValue; + req.body.userData = decodeValue; + req.body.user = decodeValue.uid; next(); } catch (e) { next(e); // Pass the error to Express error-handling middleware diff --git a/src/models/resume.model.ts b/src/models/resume.model.ts index 6e9e1df..fd476ed 100644 --- a/src/models/resume.model.ts +++ b/src/models/resume.model.ts @@ -13,9 +13,9 @@ export interface resumeType extends mongoose.Document { const Resume = new Schema({ user: { type: String, required: true }, - itemName: { type: String, required: true, unique: true }, + itemName: { type: String, required: true }, itemIds: { type: [Schema.Types.ObjectId], required: true }, - templateId: { type: Schema.Types.ObjectId, required: true }, + templateId: { type: Schema.Types.ObjectId, required: false, default: null }, }); export const ResumeModel = mongoose.model("Resume", Resume); \ No newline at end of file diff --git a/src/utils/checkDuplicates.ts b/src/utils/checkDuplicates.ts index 57085b6..6cf50bc 100644 --- a/src/utils/checkDuplicates.ts +++ b/src/utils/checkDuplicates.ts @@ -6,6 +6,7 @@ import { HeadingModel } from "../models/heading.model"; import { ProjectModel } from "../models/project.model"; import { SectionHeadingModel } from "../models/sectionHeading.model"; import { SkillsModel } from "../models/skills.model"; +import { ResumeModel } from "../models/resume.model"; export const checkDuplicateItemName = async (value: string, excludedId: string | null = null): Promise => { const field = "itemName"; @@ -33,3 +34,22 @@ export const checkDuplicateItemName = async (value: string, excludedId: string | // If totalDuplicates is greater than 0, a duplicate exists return totalDuplicates > 0; }; + +export const checkDuplicateResumeName = async (value: string, excludedId: string | null = null): Promise => { + const field = "itemName"; + const models = [ResumeModel]; + + // Check each model for the count of documents with the specified itemName value + const checks = models.map((model) => + model.countDocuments({ [field]: value, '_id': { $ne: excludedId } }).exec(), + ); + + // Await all checks to resolve + const results = await Promise.all(checks); + + // Sum the counts from all models + const totalDuplicates = results.reduce((acc, count) => acc + count, 0); + + // If totalDuplicates is greater than 0, a duplicate exists + return totalDuplicates > 0; +};