Skip to content

Commit

Permalink
fixed duplicate resume name issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgarritano committed Mar 24, 2024
1 parent 8b81d1c commit a0eca58
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/controllers/resume.controller.ts
Original file line number Diff line number Diff line change
@@ -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",
)
}

Expand Down Expand Up @@ -85,8 +85,8 @@ export const updateResume = async (
);
}

if (resumesFields.itemName != null && 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(
Expand Down
2 changes: 1 addition & 1 deletion src/models/resume.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface resumeType extends mongoose.Document {

const Resume = new Schema<resumeType>({
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: false, default: null },
});
Expand Down
20 changes: 20 additions & 0 deletions src/utils/checkDuplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
const field = "itemName";
Expand Down Expand Up @@ -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<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(),
);

// 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;
};

0 comments on commit a0eca58

Please sign in to comment.