-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from SprintCV-S24/jg-backend-fixes
Backend Logic Fixes
- Loading branch information
Showing
11 changed files
with
443 additions
and
425 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); | ||
} | ||
}; |
Oops, something went wrong.