From d0b5fd6a499d3ece4b99d784cf4cc7c4091d9eb4 Mon Sep 17 00:00:00 2001 From: Jack Garritano <114269804+jackgarritano@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:54:23 -0500 Subject: [PATCH] fixes unique name check to be per user and changes resume implementation to generate a unique name by appending a number --- src/controllers/activities.controller.ts | 4 +- src/controllers/education.controller.ts | 4 +- src/controllers/experience.controller.ts | 4 +- src/controllers/heading.controller.ts | 4 +- src/controllers/project.controller.ts | 2 +- src/controllers/resume.controller.ts | 42 +++++++++++++++----- src/controllers/sectionHeading.controller.ts | 4 +- src/controllers/skills.controller.ts | 4 +- src/utils/checkDuplicates.ts | 8 ++-- 9 files changed, 49 insertions(+), 27 deletions(-) diff --git a/src/controllers/activities.controller.ts b/src/controllers/activities.controller.ts index aedac03..419489b 100644 --- a/src/controllers/activities.controller.ts +++ b/src/controllers/activities.controller.ts @@ -9,7 +9,7 @@ import { checkDuplicateItemName } from "../utils/checkDuplicates"; export const createActivity = async (activitiesFields: ActivitiesType) => { try { - if (await checkDuplicateItemName(activitiesFields.itemName)) { + if (await checkDuplicateItemName(activitiesFields.user, activitiesFields.itemName)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } @@ -87,7 +87,7 @@ export const updateActivity = async ( ); } - if (activitiesFields.itemName != null && await checkDuplicateItemName(activitiesFields.itemName, activityId)) { + if (activitiesFields.itemName != null && await checkDuplicateItemName(activitiesFields.user, 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 3d3f3b9..cf10117 100644 --- a/src/controllers/education.controller.ts +++ b/src/controllers/education.controller.ts @@ -6,7 +6,7 @@ import { checkDuplicateItemName } from "../utils/checkDuplicates"; export const createEducation = async (educationFields: EducationType) => { try { - if (await checkDuplicateItemName(educationFields.itemName)) { + if (await checkDuplicateItemName(educationFields.user, educationFields.itemName)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } @@ -84,7 +84,7 @@ export const updateEducation = async ( ); } - if (educationFields.itemName != null && await checkDuplicateItemName(educationFields.itemName, educationId)) { + if (educationFields.itemName != null && await checkDuplicateItemName(educationFields.user, 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 af11488..cfdced0 100644 --- a/src/controllers/experience.controller.ts +++ b/src/controllers/experience.controller.ts @@ -9,7 +9,7 @@ import { checkDuplicateItemName } from "../utils/checkDuplicates"; export const createExperience = async (experienceFields: ExperienceType) => { try { - if (await checkDuplicateItemName(experienceFields.itemName)) { + if (await checkDuplicateItemName(experienceFields.user, experienceFields.itemName)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } @@ -89,7 +89,7 @@ export const updateExperience = async ( if ( experienceFields.itemName != null && - (await checkDuplicateItemName(experienceFields.itemName, experienceId)) + (await checkDuplicateItemName(experienceFields.user, experienceFields.itemName, experienceId)) ) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } diff --git a/src/controllers/heading.controller.ts b/src/controllers/heading.controller.ts index 11d1b94..9885d3e 100644 --- a/src/controllers/heading.controller.ts +++ b/src/controllers/heading.controller.ts @@ -6,7 +6,7 @@ import { checkDuplicateItemName } from "../utils/checkDuplicates"; export const createHeading = async (headingFields: HeadingType) => { try { - if (await checkDuplicateItemName(headingFields.itemName)) { + if (await checkDuplicateItemName(headingFields.user, headingFields.itemName)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } @@ -86,7 +86,7 @@ export const updateHeading = async ( if ( headingFields.itemName != null && - (await checkDuplicateItemName(headingFields.itemName, headingId)) + (await checkDuplicateItemName(headingFields.user, headingFields.itemName, headingId)) ) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } diff --git a/src/controllers/project.controller.ts b/src/controllers/project.controller.ts index 39383f4..2227895 100644 --- a/src/controllers/project.controller.ts +++ b/src/controllers/project.controller.ts @@ -6,7 +6,7 @@ import { checkDuplicateItemName } from "../utils/checkDuplicates"; export const createProject = async (projectFields: ProjectType) => { try { - if (await checkDuplicateItemName(projectFields.itemName)) { + if (await checkDuplicateItemName(projectFields.user, projectFields.itemName)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } diff --git a/src/controllers/resume.controller.ts b/src/controllers/resume.controller.ts index afa9865..10674f4 100644 --- a/src/controllers/resume.controller.ts +++ b/src/controllers/resume.controller.ts @@ -4,12 +4,10 @@ import { checkDuplicateResumeName } from "../utils/checkDuplicates"; export const createResume = async (resumesFields: resumeType) => { try { - if(await checkDuplicateResumeName(resumesFields.itemName)){ - throw new HttpError( - HttpStatus.BAD_REQUEST, - "Duplicate resume name", - ) - } + resumesFields.itemName = await generateUniqueResumeName( + resumesFields.user, + resumesFields.itemName, + ); const newResumes = new ResumeModel(resumesFields); await newResumes.save(); @@ -30,7 +28,7 @@ export const createResume = async (resumesFields: resumeType) => { }; export const getAllResumes = async (user: string) => { - try { + try { const resumes = await ResumeModel.find({ user: user }); return resumes; } catch (err: unknown) { @@ -47,7 +45,7 @@ export const getAllResumes = async (user: string) => { { cause: err }, ); } -} +}; export const getResumeById = async (user: string, resumeId: string) => { try { @@ -74,7 +72,7 @@ export const getResumeById = async (user: string, resumeId: string) => { export const updateResume = async ( user: string, - resumeId: string, + resumeId: string, resumesFields: resumeType, ) => { try { @@ -85,7 +83,14 @@ export const updateResume = async ( ); } - if (resumesFields.itemName != null && await checkDuplicateResumeName(resumesFields.itemName, resumeId)) { + if ( + resumesFields.itemName != null && + (await checkDuplicateResumeName( + resumesFields.user, + resumesFields.itemName, + resumeId, + )) + ) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate resume name"); } @@ -139,3 +144,20 @@ export const deleteResume = async (user: string, resumeId: string) => { ); } }; + +export const generateUniqueResumeName = async ( + user: string, + origName: string, +) => { + if (!(await checkDuplicateResumeName(user, origName))) { + return origName; + } + + let counter = 1; + let newName = `${origName} (${counter})`; + while (await checkDuplicateResumeName(user, newName)) { + counter++; + newName = `${origName} (${counter})`; + } + return newName; +}; diff --git a/src/controllers/sectionHeading.controller.ts b/src/controllers/sectionHeading.controller.ts index f824fd3..fda9ebf 100644 --- a/src/controllers/sectionHeading.controller.ts +++ b/src/controllers/sectionHeading.controller.ts @@ -6,7 +6,7 @@ import { checkDuplicateItemName } from "../utils/checkDuplicates"; export const createSectionHeading = async (sectionHeadingsFields: SectionHeadingType) => { try { - if(await checkDuplicateItemName(sectionHeadingsFields.itemName)){ + if(await checkDuplicateItemName(sectionHeadingsFields.user, sectionHeadingsFields.itemName)){ throw new HttpError( HttpStatus.BAD_REQUEST, "Duplicate item name", @@ -87,7 +87,7 @@ export const updateSectionHeading = async ( ); } - if (sectionHeadingsFields.itemName != null &&await checkDuplicateItemName(sectionHeadingsFields.itemName, sectionHeadingId)) { + if (sectionHeadingsFields.itemName != null &&await checkDuplicateItemName(sectionHeadingsFields.user, 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 81b9092..373233f 100644 --- a/src/controllers/skills.controller.ts +++ b/src/controllers/skills.controller.ts @@ -6,7 +6,7 @@ import { checkDuplicateItemName } from "../utils/checkDuplicates"; export const createSkill = async (skillsFields: SkillsType) => { try { - if(await checkDuplicateItemName(skillsFields.itemName)){ + if(await checkDuplicateItemName(skillsFields.user, skillsFields.itemName)){ throw new HttpError( HttpStatus.BAD_REQUEST, "Duplicate item name", @@ -87,7 +87,7 @@ export const updateSkill = async ( ); } - if (skillsFields.itemName != null && await checkDuplicateItemName(skillsFields.itemName, skillId)) { + if (skillsFields.itemName != null && await checkDuplicateItemName(skillsFields.user, skillsFields.itemName, skillId)) { throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name"); } diff --git a/src/utils/checkDuplicates.ts b/src/utils/checkDuplicates.ts index 6cf50bc..35fab05 100644 --- a/src/utils/checkDuplicates.ts +++ b/src/utils/checkDuplicates.ts @@ -8,7 +8,7 @@ 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 => { +export const checkDuplicateItemName = async (user: string, value: string, excludedId: string | null = null): Promise => { const field = "itemName"; const models = [ ActivitiesModel, @@ -22,7 +22,7 @@ export const checkDuplicateItemName = async (value: string, excludedId: string | // 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(), + model.countDocuments({ user, [field]: value, '_id': { $ne: excludedId } }).exec(), ); // Await all checks to resolve @@ -35,13 +35,13 @@ export const checkDuplicateItemName = async (value: string, excludedId: string | return totalDuplicates > 0; }; -export const checkDuplicateResumeName = async (value: string, excludedId: string | null = null): Promise => { +export const checkDuplicateResumeName = async (user: string, 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(), + model.countDocuments({ user, [field]: value, '_id': { $ne: excludedId } }).exec(), ); // Await all checks to resolve