Skip to content

Commit

Permalink
Merge pull request #4 from SprintCV-S24/jg-backend-fixes
Browse files Browse the repository at this point in the history
Backend Logic Fixes
  • Loading branch information
jackgarritano authored Mar 24, 2024
2 parents 413283d + a0eca58 commit f72f5e2
Show file tree
Hide file tree
Showing 11 changed files with 443 additions and 425 deletions.
2 changes: 1 addition & 1 deletion src/controllers/activities.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

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

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

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 },
);
}
};
Loading

0 comments on commit f72f5e2

Please sign in to comment.