Skip to content

Commit

Permalink
Merge pull request #5 from SprintCV-S24/jg-generatename
Browse files Browse the repository at this point in the history
Unique name adjustments
  • Loading branch information
jackgarritano authored Mar 28, 2024
2 parents f72f5e2 + cc5b8bf commit 48be06e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/controllers/activities.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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");
}

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/education.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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");
}

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/experience.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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");
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/heading.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
42 changes: 32 additions & 10 deletions src/controllers/resume.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -47,7 +45,7 @@ export const getAllResumes = async (user: string) => {
{ cause: err },
);
}
}
};

export const getResumeById = async (user: string, resumeId: string) => {
try {
Expand All @@ -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 {
Expand All @@ -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");
}

Expand Down Expand Up @@ -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;
};
4 changes: 2 additions & 2 deletions src/controllers/sectionHeading.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
}

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/skills.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
}

Expand Down
4 changes: 2 additions & 2 deletions src/tests/controllers.tests/resume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ describe("Resume controller tests", () => {
expect(returnedResumes[0]).toMatchObject(resumeDummyData1);

//Can't add duplicate name
await expect(
expect(
createResume(resumeDummyData1 as resumeType),
).rejects.toThrowError();
).resolves;

const returnedResumes2 = await getAllResumes(resumeDummyData1.user);

Expand Down
8 changes: 4 additions & 4 deletions src/utils/checkDuplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
export const checkDuplicateItemName = async (user: string, value: string, excludedId: string | null = null): Promise<boolean> => {
const field = "itemName";
const models = [
ActivitiesModel,
Expand All @@ -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
Expand All @@ -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<boolean> => {
export const checkDuplicateResumeName = async (user: string, value: string, excludedId: string | null = null): Promise<boolean> => {
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
Expand Down

0 comments on commit 48be06e

Please sign in to comment.