Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend Logic Fixes #4

Merged
merged 4 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading