Skip to content

Commit

Permalink
fixes checkDuplicates to work with update controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgarritano committed Feb 28, 2024
1 parent 1e2adf2 commit e6565b8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/controllers/activities.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const updateActivity = async (
);
}

if (await checkDuplicateItemName(activitiesFields.itemName)) {
if (await checkDuplicateItemName(activitiesFields.itemName, activityId)) {
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name");
}

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/education.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const updateEducation = async (
);
}

if (await checkDuplicateItemName(educationFields.itemName)) {
if (await checkDuplicateItemName(educationFields.itemName, educationId)) {
throw new HttpError(HttpStatus.BAD_REQUEST, "Duplicate item name");
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils/checkDuplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ProjectModel } from "../models/project.model";
import { SectionHeadingModel } from "../models/sectionHeading.model";
import { SkillsModel } from "../models/skills.model";

export const checkDuplicateItemName = async (value: string): Promise<boolean> => {
export const checkDuplicateItemName = async (value: string, excludedId: string | null = null): Promise<boolean> => {
const field = "itemName";
const models = [
ActivitiesModel,
Expand All @@ -21,7 +21,7 @@ export const checkDuplicateItemName = async (value: string): Promise<boolean> =>

// Check each model for the count of documents with the specified itemName value
const checks = models.map((model) =>
model.countDocuments({ [field]: value }).exec(),
model.countDocuments({ [field]: value, '_id': { $ne: excludedId } }).exec(),
);

// Await all checks to resolve
Expand Down

0 comments on commit e6565b8

Please sign in to comment.