Skip to content

Commit

Permalink
implements resume controller, router, test, and correct itemId list u…
Browse files Browse the repository at this point in the history
…pdating functionality
  • Loading branch information
jackgarritano committed Mar 3, 2024
1 parent bc25cf1 commit b73a348
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/controllers/activities.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
ActivitiesModel,
type ActivitiesType,
} from "../models/activities.model";
import { ResumeModel } from "../models/resume.model";
import mongoose from "mongoose";
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors";
import { checkDuplicateItemName } from "../utils/checkDuplicates";

Expand Down Expand Up @@ -113,6 +115,11 @@ export const updateActivity = async (

export const deleteActivity = async (user: string, activityId: string) => {
try {
await ResumeModel.updateMany(
{ itemIds: new mongoose.Types.ObjectId(activityId) },
{ $pull: { itemIds: new mongoose.Types.ObjectId(activityId) } }
);

const deletedActivity = await ActivitiesModel.findOneAndDelete({
_id: activityId,
user: user,
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/education.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { EducationModel, type EducationType } from "../models/education.model";
import { ResumeModel } from "../models/resume.model";
import mongoose from "mongoose";
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors";
import { checkDuplicateItemName } from "../utils/checkDuplicates";

Expand Down Expand Up @@ -110,6 +112,11 @@ export const updateEducation = async (

export const deleteEducation = async (user: string, educationId: string) => {
try {
await ResumeModel.updateMany(
{ itemIds: new mongoose.Types.ObjectId(educationId) },
{ $pull: { itemIds: new mongoose.Types.ObjectId(educationId) } }
);

const deletedEducation = await EducationModel.findOneAndDelete({
_id: educationId,
user: user,
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/experience.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
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";

Expand Down Expand Up @@ -113,6 +115,11 @@ import {

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,
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/heading.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
HeadingModel,
type HeadingType,
} from "../models/heading.model";
import { ResumeModel } from "../models/resume.model";
import mongoose from "mongoose";
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors";
import { checkDuplicateItemName } from "../utils/checkDuplicates";

Expand Down Expand Up @@ -113,6 +115,11 @@ import {

export const deleteHeading = async (user: string, headingId: string) => {
try {
await ResumeModel.updateMany(
{ itemIds: new mongoose.Types.ObjectId(headingId) },
{ $pull: { itemIds: new mongoose.Types.ObjectId(headingId) } }
);

const deletedHeading = await HeadingModel.findOneAndDelete({
_id: headingId,
user: user,
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
ProjectModel,
type ProjectType,
} from "../models/project.model";
import { ResumeModel } from "../models/resume.model";
import mongoose from "mongoose";
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors";
import { checkDuplicateItemName } from "../utils/checkDuplicates";

Expand Down Expand Up @@ -113,6 +115,11 @@ import {

export const deleteProject = async (user: string, projectId: string) => {
try {
await ResumeModel.updateMany(
{ itemIds: new mongoose.Types.ObjectId(projectId) },
{ $pull: { itemIds: new mongoose.Types.ObjectId(projectId) } }
);

const deletedProject = await ProjectModel.findOneAndDelete({
_id: projectId,
user: user,
Expand Down
141 changes: 141 additions & 0 deletions src/controllers/resume.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { ResumeModel, type resumeType } from "../models/resume.model";
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors";
import { checkDuplicateItemName } from "../utils/checkDuplicates";

export const createResume = async (resumesFields: resumeType) => {
try {
if(await checkDuplicateItemName(resumesFields.itemName)){
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Duplicate item name",
)
}

const newResumes = new ResumeModel(resumesFields);
await newResumes.save();
return newResumes;
} catch (err: unknown) {
if (err instanceof HttpError) {
throw err;
}

checkMongooseErrors(err);

throw new HttpError(
HttpStatus.INTERNAL_SERVER_ERROR,
"Resume creation failed",
{ cause: err },
);
}
};

export const getAllResumes = async (user: string) => {
try {
const resumes = await ResumeModel.find({ user: user });
return resumes;
} 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,
"Resumes retrieval failed",
{ cause: err },
);
}
}

export const getResumeById = async (user: string, resumeId: string) => {
try {
const resume = await ResumeModel.findOne({
user: user,
_id: resumeId,
});
return resume;
} 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,
"Resume retrieval failed",
{ cause: err },
);
}
};

export const updateResume = async (
user: string,
resumeId: string,
resumesFields: resumeType,
) => {
try {
if (!resumeId) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Missing resume ID for update",
);
}

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

const updatedResume = await ResumeModel.findOneAndUpdate(
{ _id: resumeId, user: user }, // Query to match the document by _id and user
{ $set: resumesFields }, // Update operation
{ new: true, runValidators: true }, // Options: return the updated document and run schema validators
);
return updatedResume;
} 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,
"Resume update failed",
{ cause: err },
);
}
};

export const deleteResume = async (user: string, resumeId: string) => {
try {
const deletedResume = await ResumeModel.findOneAndDelete({
_id: resumeId,
user: user,
});
if (!deletedResume) {
throw new HttpError(
HttpStatus.NOT_FOUND,
"Resume not found or already deleted",
);
}
return { message: "Resume 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,
"Resume deletion failed",
{ cause: err },
);
}
};
7 changes: 7 additions & 0 deletions src/controllers/skills.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { SkillsModel, type SkillsType } from "../models/skills.model";
import { ResumeModel } from "../models/resume.model";
import mongoose from "mongoose";
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors";
import { checkDuplicateItemName } from "../utils/checkDuplicates";

Expand Down Expand Up @@ -113,6 +115,11 @@ export const updateSkill = async (

export const deleteSkill = async (user: string, skillId: string) => {
try {
await ResumeModel.updateMany(
{ itemIds: new mongoose.Types.ObjectId(skillId) },
{ $pull: { itemIds: new mongoose.Types.ObjectId(skillId) } }
);

const deletedSkill = await SkillsModel.findOneAndDelete({
_id: skillId,
user: user,
Expand Down
6 changes: 3 additions & 3 deletions src/models/resume.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const Schema = mongoose.Schema;
export interface resumeType extends mongoose.Document {
user: string;
itemName: string;
itemIds: mongoose.Schema.Types.ObjectId[];
templateId: mongoose.Schema.Types.ObjectId;
itemIds: mongoose.Types.ObjectId[];
templateId: mongoose.Types.ObjectId;
}

const Resume = new Schema<resumeType>({
user: { type: String, required: true },
itemName: { type: String, required: true },
itemName: { type: String, required: true, unique: true },
itemIds: { type: [Schema.Types.ObjectId], required: true },
templateId: { type: Schema.Types.ObjectId, required: true },
});
Expand Down
Loading

0 comments on commit b73a348

Please sign in to comment.