Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
seancrich11 committed Apr 1, 2024
2 parents 4fc0755 + 0414ee7 commit f0a4881
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 4 deletions.
141 changes: 141 additions & 0 deletions src/controllers/folder.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { FolderModel, type folderType } from "../models/folder.model";
import { HttpError, HttpStatus, checkMongooseErrors } from "../utils/errors";
import { checkDuplicateItemName } from "../utils/checkDuplicates";

export const createFolder = async (foldersFields: folderType) => {
try {
if(await checkDuplicateItemName(foldersFields.itemName)){

Check failure on line 7 in src/controllers/folder.controller.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected 2-3 arguments, but got 1.
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Duplicate folder name",
)
}

const newFolders = new FolderModel(foldersFields);
await newFolders.save();
return newFolders;
} catch (err: unknown) {
if (err instanceof HttpError) {
throw err;
}

checkMongooseErrors(err);

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

export const getAllFolders = async (user: string) => {
try {
const folders = await FolderModel.find({ user: user });
return folders;
} 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,
"Folders retrieval failed",
{ cause: err },
);
}
}

export const getFolderById = async (user: string, folderId: string) => {
try {
const folder = await FolderModel.findOne({
user: user,
_id: folderId,
});
return folder;
} 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,
"Folder retrieval failed",
{ cause: err },
);
}
};

export const updateFolder = async (
user: string,
folderId: string,
foldersFields: folderType,
) => {
try {
if (!folderId) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Missing folder ID for update",
);
}

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

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

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

export const createResume = async (resumesFields: resumeType) => {
try {
Expand Down Expand Up @@ -118,6 +121,10 @@ export const updateResume = async (

export const deleteResume = async (user: string, resumeId: string) => {
try {
await FolderModel.updateMany(
{ resumeIds: new mongoose.Types.ObjectId(resumeId) },
{ $pull: { resumeIds: new mongoose.Types.ObjectId(resumeId) } }
);
const deletedResume = await ResumeModel.findOneAndDelete({
_id: resumeId,
user: user,
Expand Down
8 changes: 4 additions & 4 deletions src/models/folder.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const Schema = mongoose.Schema;
//typescript type corresponding with the mongoose schema structure
export interface folderType extends mongoose.Document {
user: string;
name: string;
resumeIds: mongoose.Schema.Types.ObjectId[];
folderIds: mongoose.Schema.Types.ObjectId[];
itemName: string;
resumeIds: mongoose.Types.ObjectId[];
folderIds: mongoose.Types.ObjectId[];
}

const Folder = new Schema<folderType>({
user: { type: String, required: true },
name: { type: String, required: true },
itemName: { type: String, required: true },
resumeIds: { type: [Schema.Types.ObjectId], required: true, ref: 'ResumeModel' },
folderIds: { type: [Schema.Types.ObjectId], required: true, ref: 'FolderModel' },
});
Expand Down
118 changes: 118 additions & 0 deletions src/routers/folder.router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { Router, type Request, type Response } from "express";
import {
createFolder,
getAllFolders,
getFolderById,
updateFolder,
deleteFolder,
} from "../controllers/folder.controller";
import { HttpError, HttpStatus } from "../utils/errors";
import { type folderType } from "../models/folder.model";

export const folderRouter = Router();

//Add an folder
//Note that the user field (which is part of foldersType) in body is automatically populated by verifyToken middleware
folderRouter.post(
"/",
async (req: Request<any, any, folderType>, res: Response) => {
try {
const folder = await createFolder(req.body);
res.status(HttpStatus.OK).json(folder);
} catch (err: unknown) {
if (err instanceof HttpError) {
res.status(err.errorCode).json({ error: err.message });
} else {
res
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json({ error: "An unknown error occurred" });
}
}
},
);

//Get all folders
folderRouter.get(
"/",
async (req: Request<any, any, folderType>, res: Response) => {
try {
const folder = await getAllFolders(req.body.user);
res.status(HttpStatus.OK).json(folder);
} catch (err: unknown) {
if (err instanceof HttpError) {
res.status(err.errorCode).json({ error: err.message });
} else {
res
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json({ error: "An unknown error occurred" });
}
}
},
);

//Get a single folder by id
folderRouter.get(
"/:folderId",
async (req: Request<any, any, folderType>, res: Response) => {
try {
const folder = await getFolderById(
req.body.user,
req.params.folderId,
);
res.status(HttpStatus.OK).json(folder);
} catch (err: unknown) {
if (err instanceof HttpError) {
res.status(err.errorCode).json({ error: err.message });
} else {
res
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json({ error: "An unknown error occurred" });
}
}
},
);

//Update an folder
folderRouter.put(
"/:folderId",
async (req: Request<any, any, folderType>, res: Response) => {
try {
const folder = await updateFolder(
req.body.user,
req.params.folderId,
req.body,
);
res.status(HttpStatus.OK).json(folder);
} catch (err: unknown) {
if (err instanceof HttpError) {
res.status(err.errorCode).json({ error: err.message });
} else {
res
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json({ error: "An unknown error occurred" });
}
}
},
);

//Delete an folder
folderRouter.delete(
"/:folderId",
async (req: Request<any, any, folderType>, res: Response) => {
try {
const folder = await deleteFolder(
req.body.user,
req.params.folderId,
);
res.status(HttpStatus.OK).json(folder);
} catch (err: unknown) {
if (err instanceof HttpError) {
res.status(err.errorCode).json({ error: err.message });
} else {
res
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json({ error: "An unknown error occurred" });
}
}
},
);
7 changes: 7 additions & 0 deletions src/tests/controllers.tests/dummyData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ export const sectionHeadingDummyData1 = {
itemName: "sectionHeadingItem1",
title: "test section heading",
}

export const folderDummyData1 = {
user: "test",
itemName: "FolderItem1",
resumeIds: [new mongoose.Types.ObjectId("65e4f54db1e12e776e01cf31")],
folderIds: [new mongoose.Types.ObjectId("75e4f54db1e12e776e01cf31")],
}
Loading

0 comments on commit f0a4881

Please sign in to comment.